Explorar o código

整理 AnvilTileEntity

LTCatt %!s(int64=6) %!d(string=hai) anos
pai
achega
fbc8abb440

+ 27 - 53
src/main/java/com/hexdragon/enchrebirth/block/anvil/AnvilTileEntity.java

@@ -21,6 +21,11 @@ import javax.annotation.Nullable;
 
 // 铁砧 TileEntity,用于引导自定义模型与存储物品 NBT
 public class AnvilTileEntity extends LockableLootTileEntity {
+    protected ITextComponent getDefaultName() {
+        return new TranslationTextComponent("block.minecraft.anvil");
+    }
+
+    // 构造函数,为了支持不同损坏度的铁砧复读了三次
     public static class PerfectAnvilTileEntity extends AnvilTileEntity {
         public PerfectAnvilTileEntity() { super(RegMain.tileEntityPerfectAnvil.get()); }
     }
@@ -34,75 +39,44 @@ public class AnvilTileEntity extends LockableLootTileEntity {
     }
     public AnvilTileEntity(TileEntityType<?> typeIn) { super(typeIn); }
 
-    private Container container = null;
-    public void markDirty() {
-        super.markDirty();
-        if (container != null) container.onCraftMatrixChanged(this);
-        world.notifyBlockUpdate(pos, getBlockState(), getBlockState(), Constants.BlockFlags.BLOCK_UPDATE);
+    // 物品栏
+    public NonNullList<ItemStack> inventory = NonNullList.withSize(2, ItemStack.EMPTY);
+    public int getSizeInventory() {return 2;}
+    public NonNullList<ItemStack> getItems() {
+        return this.inventory;
+    }
+    public void setItems(NonNullList<ItemStack> itemsIn) {
+        this.inventory = itemsIn;
     }
 
-    public NonNullList<ItemStack> contents = NonNullList.withSize(2, ItemStack.EMPTY);
-
+    // NBT 交互
     public CompoundNBT write(CompoundNBT compound) {
         super.write(compound);
-        if (!this.checkLootAndWrite(compound)) {
-            ItemStackHelper.saveAllItems(compound, this.contents);
-        }
-
+        if (!this.checkLootAndWrite(compound)) ItemStackHelper.saveAllItems(compound, this.inventory);
         return compound;
     }
-
     public void read(BlockState state, CompoundNBT nbt) {
         super.read(state, nbt);
-        this.contents = NonNullList.withSize(this.getSizeInventory(), ItemStack.EMPTY);
-        if (!this.checkLootAndRead(nbt)) {
-            ItemStackHelper.loadAllItems(nbt, this.contents);
-        }
-
-    }
-
-    /**
-     * Returns the number of slots in the inventory.
-     */
-    public int getSizeInventory() {
-        return 2;
+        this.inventory = NonNullList.withSize(this.getSizeInventory(), ItemStack.EMPTY);
+        if (!this.checkLootAndRead(nbt)) ItemStackHelper.loadAllItems(nbt, this.inventory);
     }
 
-    public NonNullList<ItemStack> getItems() {
-        return this.contents;
-    }
-
-    public void setItems(NonNullList<ItemStack> itemsIn) {
-        this.contents = itemsIn;
-    }
+    // 支持从服务器接受数据,以在物品改变时同步显示
+    @Nullable public SUpdateTileEntityPacket getUpdatePacket() {return new SUpdateTileEntityPacket(pos, 0, getUpdateTag());}
+    @Override public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {handleUpdateTag(null, pkt.getNbtCompound());}
+    public CompoundNBT getUpdateTag() {return write(new CompoundNBT());}
 
-    protected ITextComponent getDefaultName() {
-        return new TranslationTextComponent("block.minecraft.anvil");
+    // 物品更新触发的事件
+    private Container container = null;
+    public void markDirty() {
+        super.markDirty();
+        if (container != null) container.onCraftMatrixChanged(this); // 通知 Container 改变输出内容
+        world.notifyBlockUpdate(pos, getBlockState(), getBlockState(), Constants.BlockFlags.BLOCK_UPDATE); // 通知客户端更新物品模型渲染
     }
-
     protected Container createMenu(int id, PlayerInventory player) {
         AnvilContainerRe container = new AnvilContainerRe(id, player, IWorldPosCallable.of(this.world, this.pos));
         this.container = container;
         return container;
     }
 
-    /**
-     * Retrieves packet to send to the client whenever this Tile Entity is resynced via World.notifyBlockUpdate. For
-     * modded TE's, this packet comes back to you clientside in {@link #onDataPacket}
-     */
-    @Nullable
-    public SUpdateTileEntityPacket getUpdatePacket() {
-        return new SUpdateTileEntityPacket(pos, 0, getUpdateTag());
-    }
-    @Override
-    public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
-        handleUpdateTag(null, pkt.getNbtCompound());
-    }
-    public CompoundNBT getUpdateTag() {
-        return write(new CompoundNBT());
-    }
-
-    // TODO : 检查红石信号输出
-    // TODO : 需要重构
-
 }

+ 1 - 9
src/main/java/com/hexdragon/enchrebirth/mixin/block/AnvilBlockMixin.java

@@ -43,20 +43,12 @@ public abstract class AnvilBlockMixin extends FallingBlock implements IForgeBloc
         }
     }
 
-    // 将同步事件传递到客户端
-    @Override public boolean eventReceived(BlockState state, World worldIn, BlockPos pos, int id, int param) {
-        super.eventReceived(state, worldIn, pos, id, param);
-        TileEntity tileentity = worldIn.getTileEntity(pos);
-        return tileentity != null && tileentity.receiveClientEvent(id, param);
-    }
-
     // 当破坏时掉落内容物
     @Override public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
         if (!state.isIn(newState.getBlock())) {
             TileEntity tileentity = worldIn.getTileEntity(pos);
-            if (tileentity instanceof AnvilTileEntity) {
+            if (tileentity instanceof AnvilTileEntity)
                 InventoryHelper.dropItems(worldIn, pos, ((AnvilTileEntity) tileentity).getItems());
-            }
             super.onReplaced(state, worldIn, pos, newState, isMoving);
         }
     }