Эх сурвалжийг харах

铁砧可以显示物品了,但是不能更新

LTCatt 6 жил өмнө
parent
commit
3586bc111f

+ 14 - 9
src/main/java/com/hexdragon/enchrebirth/block/anvil/AnvilRenderer.java

@@ -9,33 +9,38 @@ import net.minecraft.client.renderer.model.ItemCameraTransforms;
 import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
 import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
 import net.minecraft.item.ItemStack;
-import net.minecraft.util.math.vector.Quaternion;
+import net.minecraft.util.math.vector.Vector3f;
+import net.minecraftforge.api.distmarker.Dist;
+import net.minecraftforge.api.distmarker.OnlyIn;
 
 // 渲染铁砧中存储的物品
-public class AnvilRenderer extends TileEntityRenderer<AnvilTileEntity> {
+@OnlyIn(Dist.CLIENT) public class AnvilRenderer extends TileEntityRenderer<AnvilTileEntity> {
     public AnvilRenderer(TileEntityRendererDispatcher rendererDispatcherIn) { super(rendererDispatcherIn); }
 
     @Override public void render(AnvilTileEntity tileEntityIn, float partialTicks, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int combinedLightIn, int combinedOverlayIn) {
-        AnvilContainerRe container = (AnvilContainerRe) tileEntityIn.container;
-        if (container == null) return;
         ItemRenderer itemRenderer = Minecraft.getInstance().getItemRenderer();
 
         // 渲染左侧的物品
-        ItemStack stackLeft = container.inputInventory.getStackInSlot(0);
+        ItemStack stackLeft = tileEntityIn.getStackInSlot(0);
         if (!stackLeft.isEmpty()) {
             matrixStackIn.push();
-            matrixStackIn.translate(2, 1.01, 0);
+            matrixStackIn.translate(0.7, 1.01, 0.45);
+            matrixStackIn.rotate(Vector3f.XP.rotationDegrees(90.0F));
+            matrixStackIn.rotate(Vector3f.ZN.rotationDegrees(5.0F));
+            matrixStackIn.scale(0.375F, 0.375F, 0.375F);
             IBakedModel ibakedmodel = itemRenderer.getItemModelWithOverrides(stackLeft, tileEntityIn.getWorld(), null);
             itemRenderer.renderItem(stackLeft, ItemCameraTransforms.TransformType.FIXED, true, matrixStackIn, bufferIn, combinedLightIn, combinedOverlayIn, ibakedmodel);
             matrixStackIn.pop();
         }
 
         // 渲染右侧的物品
-        ItemStack stackRight = container.inputInventory.getStackInSlot(1);
+        ItemStack stackRight = tileEntityIn.getStackInSlot(1);
         if (!stackRight.isEmpty()) {
             matrixStackIn.push();
-            matrixStackIn.translate(0, 1.01, 0);
-            matrixStackIn.rotate(new Quaternion(0, 1, 1, (float) Math.PI));
+            matrixStackIn.translate(0.27, 1.01, 0.51);
+            matrixStackIn.rotate(Vector3f.XP.rotationDegrees(90.0F));
+            matrixStackIn.rotate(Vector3f.ZP.rotationDegrees(15.0F));
+            matrixStackIn.scale(0.375F, 0.375F, 0.375F);
             IBakedModel ibakedmodel = itemRenderer.getItemModelWithOverrides(stackRight, tileEntityIn.getWorld(), null);
             itemRenderer.renderItem(stackRight, ItemCameraTransforms.TransformType.FIXED, true, matrixStackIn, bufferIn, combinedLightIn, combinedOverlayIn, ibakedmodel);
             matrixStackIn.pop();

+ 19 - 29
src/main/java/com/hexdragon/enchrebirth/block/anvil/AnvilTileEntity.java

@@ -7,22 +7,26 @@ import net.minecraft.inventory.ItemStackHelper;
 import net.minecraft.inventory.container.Container;
 import net.minecraft.item.ItemStack;
 import net.minecraft.nbt.CompoundNBT;
+import net.minecraft.network.play.server.SUpdateTileEntityPacket;
 import net.minecraft.tileentity.LockableLootTileEntity;
 import net.minecraft.util.IWorldPosCallable;
 import net.minecraft.util.NonNullList;
 import net.minecraft.util.text.ITextComponent;
 import net.minecraft.util.text.TranslationTextComponent;
 
+import javax.annotation.Nullable;
+
 // 铁砧 TileEntity,用于引导自定义模型与存储物品 NBT
 public class AnvilTileEntity extends LockableLootTileEntity {
     public AnvilTileEntity() {
         super(RegMain.tileEntityAnvil.get());
     }
 
-    public Container container = null;
+    private Container container = null;
     public void markDirty() {
         super.markDirty();
         if (container != null) container.onCraftMatrixChanged(this);
+        this.getWorld().notifyBlockUpdate(this.getPos(), this.getBlockState(), this.getBlockState(), 3);
     }
 
     public NonNullList<ItemStack> contents = NonNullList.withSize(2, ItemStack.EMPTY);
@@ -52,11 +56,11 @@ public class AnvilTileEntity extends LockableLootTileEntity {
         return 2;
     }
 
-    protected NonNullList<ItemStack> getItems() {
+    public NonNullList<ItemStack> getItems() {
         return this.contents;
     }
 
-    protected void setItems(NonNullList<ItemStack> itemsIn) {
+    public void setItems(NonNullList<ItemStack> itemsIn) {
         this.contents = itemsIn;
     }
 
@@ -71,33 +75,19 @@ public class AnvilTileEntity extends LockableLootTileEntity {
         // return ChestContainer.createGeneric9X3(id, player, this);
     }
 
+    /**
+     * 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(this.pos, 0, this.getUpdateTag());
+    }
 
-    // TODO : 检查红石信号输出
-
+    public CompoundNBT getUpdateTag() {
+        return this.write(new CompoundNBT());
+    }
 
-//    @Override public ITextComponent getDisplayName() {
-//        return new TranslationTextComponent("block.minecraft.anvil");
-//    }
-//
-//    // 内建物品栏
-//    public final CraftInputInventory inventory = new CraftInputInventory(2);
-//    @Nullable @Override public Container createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
-//        // 与 Container 同步物品栏
-//        AnvilContainerRe container = new AnvilContainerRe(sycID, inventory, IWorldPosCallable.of(this.world, this.pos));
-//        this.inventory.container = container;
-//        return container;
-//    }
-//
-//    // 将物品栏 NBT 化
-//    @Override public void read(BlockState state, CompoundNBT compound) {
-//        inventory.setInventorySlotContents(0, ItemStack.read(compound.getCompound("ItemLeft")));
-//        inventory.setInventorySlotContents(1, ItemStack.read(compound.getCompound("ItemRight")));
-//        super.read(null, compound);
-//    }
-//    @Override public CompoundNBT write(CompoundNBT compound) {
-//        compound.put("ItemLeft", inventory.getStackInSlot(0).serializeNBT());
-//        compound.put("ItemRight", inventory.getStackInSlot(1).serializeNBT());
-//        return super.write(compound);
-//    }
+    // TODO : 检查红石信号输出
 
 }

+ 17 - 0
src/main/java/com/hexdragon/enchrebirth/mixin/block/AnvilBlockMixin.java

@@ -4,6 +4,7 @@ import com.hexdragon.enchrebirth.block.anvil.AnvilTileEntity;
 import net.minecraft.block.AnvilBlock;
 import net.minecraft.block.BlockState;
 import net.minecraft.block.FallingBlock;
+import net.minecraft.inventory.InventoryHelper;
 import net.minecraft.inventory.container.INamedContainerProvider;
 import net.minecraft.tileentity.TileEntity;
 import net.minecraft.util.math.BlockPos;
@@ -35,4 +36,20 @@ public abstract class AnvilBlockMixin extends FallingBlock implements IForgeBloc
         return new AnvilTileEntity();
     }
 
+    @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) {
+                InventoryHelper.dropItems(worldIn, pos, ((AnvilTileEntity) tileentity).getItems());
+            }
+
+            super.onReplaced(state, worldIn, pos, newState, isMoving);
+        }
+    }
 }