Always store owner of a network node

This commit is contained in:
raoulvdberge
2017-09-01 09:51:12 +02:00
parent f9d4798750
commit 591f139f06
7 changed files with 51 additions and 56 deletions

View File

@@ -22,14 +22,19 @@ import net.minecraftforge.items.IItemHandler;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.UUID;
public abstract class NetworkNode implements INetworkNode, INetworkNodeVisitor, IWrenchable {
private static final String NBT_OWNER = "Owner";
@Nullable
protected INetwork network;
protected World world;
protected BlockPos pos;
protected int ticks;
protected RedstoneMode redstoneMode = RedstoneMode.IGNORE;
@Nullable
protected UUID owner;
private EnumFacing direction;
@@ -131,6 +136,10 @@ public abstract class NetworkNode implements INetworkNode, INetworkNodeVisitor,
@Override
public NBTTagCompound write(NBTTagCompound tag) {
if (owner != null) {
tag.setUniqueId(NBT_OWNER, owner);
}
writeConfiguration(tag);
return tag;
@@ -144,6 +153,10 @@ public abstract class NetworkNode implements INetworkNode, INetworkNodeVisitor,
}
public void read(NBTTagCompound tag) {
if (tag.hasKey(NBT_OWNER)) {
owner = tag.getUniqueId(NBT_OWNER);
}
readConfiguration(tag);
}
@@ -219,6 +232,17 @@ public abstract class NetworkNode implements INetworkNode, INetworkNodeVisitor,
this.active = active;
}
public void setOwner(@Nullable UUID owner) {
this.owner = owner;
markDirty();
}
@Nullable
public UUID getOwner() {
return owner;
}
@Override
public boolean equals(Object o) {
return API.instance().isNetworkNodeEqual(this, o);

View File

@@ -19,7 +19,6 @@ import net.minecraft.world.World;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.wrapper.CombinedInvWrapper;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@@ -27,8 +26,6 @@ import java.util.UUID;
public class NetworkNodeSecurityManager extends NetworkNode implements ISecurityCardContainer {
public static final String ID = "security_manager";
private static final String NBT_OWNER = "Owner";
private List<ISecurityCard> actualCards = new ArrayList<>();
private ItemHandlerBase cards = new ItemHandlerBase(9 * 2, new ItemHandlerListenerNetworkNode(this), new ItemValidatorBasic(RSItems.SECURITY_CARD)) {
@@ -47,9 +44,6 @@ public class NetworkNodeSecurityManager extends NetworkNode implements ISecurity
};
private ItemHandlerBase editCard = new ItemHandlerBase(1, new ItemHandlerListenerNetworkNode(this), new ItemValidatorBasic(RSItems.SECURITY_CARD));
@Nullable
private UUID owner;
public NetworkNodeSecurityManager(World world, BlockPos pos) {
super(world, pos);
}
@@ -76,17 +70,6 @@ public class NetworkNodeSecurityManager extends NetworkNode implements ISecurity
}
}
public void setOwner(@Nullable UUID owner) {
this.owner = owner;
markDirty();
}
@Nullable
public UUID getOwner() {
return owner;
}
private void rebuildCards() {
actualCards.clear();
@@ -115,10 +98,6 @@ public class NetworkNodeSecurityManager extends NetworkNode implements ISecurity
public void read(NBTTagCompound tag) {
super.read(tag);
if (tag.hasKey(NBT_OWNER)) {
owner = UUID.fromString(tag.getString(NBT_OWNER));
}
StackUtils.readItems(cards, 0, tag);
StackUtils.readItems(editCard, 1, tag);
}
@@ -132,10 +111,6 @@ public class NetworkNodeSecurityManager extends NetworkNode implements ISecurity
public NBTTagCompound write(NBTTagCompound tag) {
super.write(tag);
if (owner != null) {
tag.setString(NBT_OWNER, owner.toString());
}
StackUtils.writeItems(cards, 0, tag);
StackUtils.writeItems(editCard, 1, tag);

View File

@@ -10,6 +10,7 @@ import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
@@ -35,13 +36,17 @@ public abstract class BlockNode extends BlockBase {
super.onBlockPlacedBy(world, pos, state, placer, stack);
if (!world.isRemote) {
if (stack.hasTagCompound() && stack.getTagCompound().hasKey(NBT_REFINED_STORAGE_DATA)) {
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileNode) {
if (stack.hasTagCompound() && stack.getTagCompound().hasKey(NBT_REFINED_STORAGE_DATA)) {
((TileNode) tile).getNode().readConfiguration(stack.getTagCompound().getCompoundTag(NBT_REFINED_STORAGE_DATA));
((TileNode) tile).getNode().markDirty();
}
if (placer instanceof EntityPlayer) {
((TileNode) tile).getNode().setOwner(((EntityPlayer) placer).getGameProfile().getId());
}
}
API.instance().discoverNode(world, pos);

View File

@@ -5,9 +5,7 @@ import com.raoulvdberge.refinedstorage.RSGui;
import com.raoulvdberge.refinedstorage.api.network.security.Permission;
import com.raoulvdberge.refinedstorage.tile.TileSecurityManager;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
@@ -24,15 +22,6 @@ public class BlockSecurityManager extends BlockNode {
return new TileSecurityManager();
}
@Override
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
super.onBlockPlacedBy(world, pos, state, placer, stack);
if (!world.isRemote && placer instanceof EntityPlayer) {
((TileSecurityManager) world.getTileEntity(pos)).getNode().setOwner(((EntityPlayer) placer).getGameProfile().getId());
}
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
if (!world.isRemote) {

View File

@@ -5,22 +5,24 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
public abstract class ItemBase extends Item {
private final String domain;
private final String name;
public ItemBase(String name) {
this.name = name;
setRegistryName(getDomain(), name);
setCreativeTab(RS.INSTANCE.tab);
this(RS.ID, name);
}
protected String getDomain() {
return RS.ID;
public ItemBase(String domain, String name) {
this.domain = domain;
this.name = name;
setRegistryName(domain, name);
setCreativeTab(RS.INSTANCE.tab);
}
@Override
public String getUnlocalizedName() {
return "item." + getDomain() + ":" + name;
return "item." + domain + ":" + name;
}
@Override

View File

@@ -68,37 +68,37 @@ public abstract class TileBase extends TileEntity {
}
@Override
public NBTTagCompound getUpdateTag() {
public final NBTTagCompound getUpdateTag() {
return writeUpdate(super.getUpdateTag());
}
@Nullable
@Override
public SPacketUpdateTileEntity getUpdatePacket() {
public final SPacketUpdateTileEntity getUpdatePacket() {
return new SPacketUpdateTileEntity(pos, 1, getUpdateTag());
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
public final void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
readUpdate(packet.getNbtCompound());
}
@Override
public void handleUpdateTag(NBTTagCompound tag) {
public final void handleUpdateTag(NBTTagCompound tag) {
super.readFromNBT(tag);
readUpdate(tag);
}
@Override
public void readFromNBT(NBTTagCompound tag) {
public final void readFromNBT(NBTTagCompound tag) {
super.readFromNBT(tag);
read(tag);
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
public final NBTTagCompound writeToNBT(NBTTagCompound tag) {
return write(super.writeToNBT(tag));
}

View File

@@ -570,8 +570,8 @@ public class TileController extends TileBase implements ITickable, INetwork, IRe
}
@Override
public void readFromNBT(NBTTagCompound tag) {
super.readFromNBT(tag);
public void read(NBTTagCompound tag) {
super.read(tag);
if (tag.hasKey(NBT_ENERGY)) {
energy.setEnergyStored(tag.getInteger(NBT_ENERGY));
@@ -599,8 +599,8 @@ public class TileController extends TileBase implements ITickable, INetwork, IRe
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
super.writeToNBT(tag);
public NBTTagCompound write(NBTTagCompound tag) {
super.write(tag);
tag.setInteger(NBT_ENERGY, energy.getEnergyStored());