Kaynağa Gözat

一个至少不会崩的加 NBT 读取版本

LTCatt 6 yıl önce
ebeveyn
işleme
e16fc4d6e4

+ 23 - 0
src/main/java/com/hexdragon/core/item/CraftInputInventory.java

@@ -0,0 +1,23 @@
+package com.hexdragon.core.item;
+
+import net.minecraft.inventory.Inventory;
+import net.minecraft.inventory.container.Container;
+import net.minecraft.item.ItemStack;
+
+// 在触发 markDirty 时触发 onCraftMatrixChanged 的 Inventory
+public class CraftInputInventory extends Inventory {
+
+    public CraftInputInventory(int numSlots) {
+        super(numSlots);
+    }
+    public CraftInputInventory(ItemStack... stacksIn) {
+        super(stacksIn);
+    }
+
+    public Container container = null;
+    public void markDirty() {
+        super.markDirty();
+        if (container != null) container.onCraftMatrixChanged(this);
+    }
+
+}

+ 2 - 4
src/main/java/com/hexdragon/enchrebirth/EnchRebirth.java

@@ -5,13 +5,11 @@ import net.minecraftforge.eventbus.api.IEventBus;
 import net.minecraftforge.fml.common.Mod;
 import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
 
-@Mod(EnchRebirth.MODID)
+@Mod(Main.MODID)
 public class EnchRebirth {
-    public static final String MODID = "enchrebirth";
-
     public EnchRebirth() {
         IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
         RegMain.CONTAINERS.register(modEventBus);
         RegMain.TILE_ENTITIES.register(modEventBus);
     }
-}
+}

+ 10 - 0
src/main/java/com/hexdragon/enchrebirth/Main.java

@@ -0,0 +1,10 @@
+package com.hexdragon.enchrebirth;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+public class Main {
+    public static final String MODID = "enchrebirth";
+    public static final Logger LOGGER = LogManager.getLogger();
+
+}

+ 23 - 22
src/main/java/com/hexdragon/enchrebirth/block/AnvilContainerRe.java

@@ -1,7 +1,9 @@
 package com.hexdragon.enchrebirth.block;
 
+import com.hexdragon.core.item.CraftInputInventory;
 import com.hexdragon.core.item.EnchantmentHelperRe;
 import com.hexdragon.core.item.ItemHelperRe;
+import com.hexdragon.enchrebirth.Main;
 import com.hexdragon.enchrebirth.registry.RegMain;
 import net.minecraft.block.AnvilBlock;
 import net.minecraft.block.BlockState;
@@ -11,7 +13,6 @@ import net.minecraft.entity.player.PlayerEntity;
 import net.minecraft.entity.player.PlayerInventory;
 import net.minecraft.inventory.CraftResultInventory;
 import net.minecraft.inventory.IInventory;
-import net.minecraft.inventory.Inventory;
 import net.minecraft.inventory.container.Container;
 import net.minecraft.inventory.container.Slot;
 import net.minecraft.item.ItemStack;
@@ -25,17 +26,31 @@ import java.util.Map;
 
 public class AnvilContainerRe extends Container {
 
+    // 构造函数
+    private final IWorldPosCallable worldPosCallable;
+    private final PlayerEntity player;
+    public AnvilContainerRe(int id, PlayerInventory playerInventory) {
+        this(id, playerInventory, IWorldPosCallable.DUMMY);
+        this.inputInventory.container = this;
+        Main.LOGGER.warn("NEW2");
+    }
+    public AnvilContainerRe(int id, PlayerInventory playerInventory, IWorldPosCallable worldPosCallable) {
+        super(RegMain.containerAnvil.get(), id);
+        Main.LOGGER.warn("NEW1");
+        this.worldPosCallable = worldPosCallable;
+        this.player = playerInventory.player;
+        worldPosCallable.consume((world, blockPos) -> this.inputInventory = ((AnvilTileEntity) world.getTileEntity(blockPos)).getInventory());
+        if (this.inputInventory == null) this.inputInventory = new CraftInputInventory(2); // TODO
+        Constuct(playerInventory);
+    }
+
     // 输入与输出物品槽
-    private final Inventory inputInventory = new Inventory(2) {
-        public void markDirty() {
-            super.markDirty();
-            AnvilContainerRe.this.onCraftMatrixChanged(this);
-        }
-    };
-    private final CraftResultInventory outputInventory = new CraftResultInventory();
+    public CraftInputInventory inputInventory;
+    public final CraftResultInventory outputInventory = new CraftResultInventory();
 
     // 构造页面槽位
     private void Constuct(PlayerInventory playerInventory) {
+        Main.LOGGER.warn("CONSTUCT");
         this.addSlot(new Slot(this.inputInventory, 0, 62, 28));
         this.addSlot(new Slot(this.inputInventory, 1, 80, 28));
         this.addSlot(new Slot(this.outputInventory, 2, 138, 28) {
@@ -185,20 +200,6 @@ public class AnvilContainerRe extends Container {
      * --------------------------------------------------------------
      */
 
-    // 构造函数
-    private final IWorldPosCallable worldPosCallable;
-    private final PlayerEntity player;
-    public AnvilContainerRe(int id, PlayerInventory playerInventory) {
-        this(id, playerInventory, IWorldPosCallable.DUMMY);
-    }
-    public AnvilContainerRe(int id, PlayerInventory playerInventory, IWorldPosCallable worldPosCallable) {
-        super(RegMain.containerAnvil.get(), id);
-        this.worldPosCallable = worldPosCallable;
-        this.player = playerInventory.player;
-        // worldPosCallable.consume((world,  blockPos) -> this.inputInventory = ((AnvilTileEntity) world.getTileEntity(blockPos)).getInventory());
-        Constuct(playerInventory);
-    }
-
     // 接口: 当输入物品改变时触发更新
     public void onCraftMatrixChanged(IInventory inventoryIn) {
         super.onCraftMatrixChanged(inventoryIn);

+ 2 - 2
src/main/java/com/hexdragon/enchrebirth/block/AnvilScreenRe.java

@@ -1,6 +1,6 @@
 package com.hexdragon.enchrebirth.block;
 
-import com.hexdragon.enchrebirth.EnchRebirth;
+import com.hexdragon.enchrebirth.Main;
 import com.mojang.blaze3d.matrix.MatrixStack;
 import com.mojang.blaze3d.systems.RenderSystem;
 import net.minecraft.client.Minecraft;
@@ -14,7 +14,7 @@ import org.lwjgl.opengl.GL11;
 
 @OnlyIn(Dist.CLIENT)
 public class AnvilScreenRe extends ContainerScreen<AnvilContainerRe> {
-    private static final ResourceLocation ANVIL_RESOURCE = new ResourceLocation(EnchRebirth.MODID, "textures/gui/container/anvil.png");
+    private static final ResourceLocation ANVIL_RESOURCE = new ResourceLocation(Main.MODID, "textures/gui/container/anvil.png");
 
     public AnvilScreenRe(AnvilContainerRe container, PlayerInventory playerInventory, ITextComponent title) {
         super(container, playerInventory, title);

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

@@ -1,50 +1,56 @@
 package com.hexdragon.enchrebirth.block;
 
+import com.hexdragon.core.item.CraftInputInventory;
+import com.hexdragon.enchrebirth.Main;
 import com.hexdragon.enchrebirth.registry.RegMain;
 import com.sun.istack.internal.Nullable;
+import net.minecraft.block.BlockState;
 import net.minecraft.entity.player.PlayerEntity;
 import net.minecraft.entity.player.PlayerInventory;
 import net.minecraft.inventory.container.Container;
 import net.minecraft.inventory.container.INamedContainerProvider;
+import net.minecraft.item.ItemStack;
+import net.minecraft.nbt.CompoundNBT;
 import net.minecraft.tileentity.TileEntity;
 import net.minecraft.util.IWorldPosCallable;
 import net.minecraft.util.text.ITextComponent;
-import net.minecraft.util.text.StringTextComponent;
+import net.minecraft.util.text.TranslationTextComponent;
 
 public class AnvilTileEntity extends TileEntity implements INamedContainerProvider {
     public AnvilTileEntity() {
         super(RegMain.tileEntityAnvil.get());
     }
+    @Override public ITextComponent getDisplayName() {
+        return new TranslationTextComponent("block.minecraft.anvil");
+    }
+
+    public final CraftInputInventory inputInventory = new CraftInputInventory(2);
 
-    @Override
-    public ITextComponent getDisplayName() {
-        return new StringTextComponent("First Container");
+    @Nullable @Override public Container createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
+        Main.LOGGER.warn("CREATE MENU");
+        AnvilContainerRe container = new AnvilContainerRe(sycID, inventory, IWorldPosCallable.of(this.world, this.pos));
+        inputInventory.container = container;
+        return container;
     }
 
-    @Nullable
-    @Override
-    public Container createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
-        return new AnvilContainerRe(sycID, inventory, IWorldPosCallable.of(this.world, this.pos));
+    @Override public void read(BlockState state, CompoundNBT compound) {
+        Main.LOGGER.warn("READ");
+        inputInventory.addItem(ItemStack.read(compound.getCompound("item1")));
+        inputInventory.addItem(ItemStack.read(compound.getCompound("item2")));
+        super.read(null, compound);
     }
 
-//    @Override
-//    public void read(BlockState state, CompoundNBT compound) {
-//        this.inventory.addItem(ItemStack.read(compound.getCompound("item1")));
-//        this.inventory.addItem(ItemStack.read(compound.getCompound("item2")));
-//        super.read(null, compound);
-//    }
-//
-//    @Override
-//    public CompoundNBT write(CompoundNBT compound) {
-//        ItemStack itemStack1 = this.inventory.getStackInSlot(0).copy();
-//        compound.put("item1", itemStack1.serializeNBT());
-//        ItemStack itemStack2 = this.inventory.getStackInSlot(1).copy();
-//        compound.put("item2", itemStack2.serializeNBT());
-//        return super.write(compound);
-//    }
-//
-//    public Inventory getInventory() {
-//        return inventory;
-//    }
+    @Override public CompoundNBT write(CompoundNBT compound) {
+        Main.LOGGER.warn("WRITE");
+        ItemStack itemStack1 = inputInventory.getStackInSlot(0).copy();
+        compound.put("item1", itemStack1.serializeNBT());
+        ItemStack itemStack2 = inputInventory.getStackInSlot(1).copy();
+        compound.put("item2", itemStack2.serializeNBT());
+        return super.write(compound);
+    }
+
+    public CraftInputInventory getInventory() {
+        return inputInventory;
+    }
 
 }

+ 2 - 2
src/main/java/com/hexdragon/enchrebirth/block/GrindstoneScreenRe.java

@@ -1,6 +1,6 @@
 package com.hexdragon.enchrebirth.block;
 
-import com.hexdragon.enchrebirth.EnchRebirth;
+import com.hexdragon.enchrebirth.Main;
 import com.mojang.blaze3d.matrix.MatrixStack;
 import net.minecraft.client.gui.screen.inventory.ContainerScreen;
 import net.minecraft.entity.player.PlayerInventory;
@@ -12,7 +12,7 @@ import org.lwjgl.opengl.GL11;
 
 @OnlyIn(Dist.CLIENT)
 public class GrindstoneScreenRe extends ContainerScreen<GrindstoneContainerRe> {
-    private static final ResourceLocation GRINDSTONE_GUI_TEXTURES = new ResourceLocation(EnchRebirth.MODID, "textures/gui/container/grindstone.png");
+    private static final ResourceLocation GRINDSTONE_GUI_TEXTURES = new ResourceLocation(Main.MODID, "textures/gui/container/grindstone.png");
 
     public GrindstoneScreenRe(GrindstoneContainerRe container, PlayerInventory playerInventory, ITextComponent textComponent) {
         super(container, playerInventory, textComponent);

+ 27 - 0
src/main/java/com/hexdragon/enchrebirth/mixin/YggdrasilMixin.java

@@ -0,0 +1,27 @@
+package com.hexdragon.enchrebirth.mixin;
+
+import com.hexdragon.enchrebirth.Main;
+import com.mojang.authlib.GameProfile;
+import com.mojang.authlib.HttpAuthenticationService;
+import com.mojang.authlib.minecraft.HttpMinecraftSessionService;
+import com.mojang.authlib.yggdrasil.YggdrasilMinecraftSessionService;
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.injection.At;
+import org.spongepowered.asm.mixin.injection.Inject;
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
+import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
+
+@Mixin(YggdrasilMinecraftSessionService.class)
+public abstract class YggdrasilMixin extends HttpMinecraftSessionService {
+    protected YggdrasilMixin(HttpAuthenticationService authenticationService) { super(authenticationService); }
+
+    // 强制结束 MC 的玩家档案获取,以避免进入存档时卡死
+    @Inject(method = "fillGameProfile", at = @At(value = "HEAD"), cancellable = true, locals = LocalCapture.CAPTURE_FAILEXCEPTION)
+    private void fillGameProfile(GameProfile profile, boolean requireSecure, CallbackInfoReturnable<GameProfile> cir) {
+        Main.LOGGER.warn("Canceled game profile filling.");
+        cir.setReturnValue(profile);
+        cir.cancel();
+    }
+
+}
+

+ 2 - 2
src/main/java/com/hexdragon/enchrebirth/registry/RegClient.java

@@ -1,6 +1,6 @@
 package com.hexdragon.enchrebirth.registry;
 
-import com.hexdragon.enchrebirth.EnchRebirth;
+import com.hexdragon.enchrebirth.Main;
 import com.hexdragon.enchrebirth.block.AnvilRenderer;
 import com.hexdragon.enchrebirth.block.AnvilScreenRe;
 import com.hexdragon.enchrebirth.block.GrindstoneScreenRe;
@@ -12,7 +12,7 @@ import net.minecraftforge.fml.client.registry.ClientRegistry;
 import net.minecraftforge.fml.common.Mod;
 import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
 
[email protected](modid = EnchRebirth.MODID, bus = Mod.EventBusSubscriber.Bus.MOD) @OnlyIn(Dist.CLIENT)
[email protected](modid = Main.MODID, bus = Mod.EventBusSubscriber.Bus.MOD) @OnlyIn(Dist.CLIENT)
 public class RegClient {
 
     @SubscribeEvent

+ 3 - 3
src/main/java/com/hexdragon/enchrebirth/registry/RegMain.java

@@ -1,6 +1,6 @@
 package com.hexdragon.enchrebirth.registry;
 
-import com.hexdragon.enchrebirth.EnchRebirth;
+import com.hexdragon.enchrebirth.Main;
 import com.hexdragon.enchrebirth.block.AnvilContainerRe;
 import com.hexdragon.enchrebirth.block.AnvilTileEntity;
 import com.hexdragon.enchrebirth.block.GrindstoneContainerRe;
@@ -17,12 +17,12 @@ import net.minecraftforge.registries.ForgeRegistries;
 public class RegMain {
 
     // 容器
-    public static final DeferredRegister<ContainerType<?>> CONTAINERS = DeferredRegister.create(ForgeRegistries.CONTAINERS, EnchRebirth.MODID);
+    public static final DeferredRegister<ContainerType<?>> CONTAINERS = DeferredRegister.create(ForgeRegistries.CONTAINERS, Main.MODID);
     public static final RegistryObject<ContainerType<GrindstoneContainerRe>> containerGrindstone = CONTAINERS.register("grindstone", () -> IForgeContainerType.create((int windowId, PlayerInventory inv, PacketBuffer data) -> new GrindstoneContainerRe(windowId, inv)));
     public static final RegistryObject<ContainerType<AnvilContainerRe>> containerAnvil = CONTAINERS.register("anvil", () -> IForgeContainerType.create((int windowId, PlayerInventory inv, PacketBuffer data) -> new AnvilContainerRe(windowId, inv)));
 
     // TileEntity
-    public static final DeferredRegister<TileEntityType<?>> TILE_ENTITIES = DeferredRegister.create(ForgeRegistries.TILE_ENTITIES, EnchRebirth.MODID);
+    public static final DeferredRegister<TileEntityType<?>> TILE_ENTITIES = DeferredRegister.create(ForgeRegistries.TILE_ENTITIES, Main.MODID);
     public static RegistryObject<TileEntityType<AnvilTileEntity>> tileEntityAnvil = TILE_ENTITIES.register("obsidian_ter_tileentity", () -> TileEntityType.Builder.create(AnvilTileEntity::new, Blocks.ANVIL).build(null));
 
 }

+ 2 - 1
src/main/resources/mixins.enchrebirth.json

@@ -5,6 +5,7 @@
   "mixins": [
     "RepairItemRecipeMixin",
     "block/GrindstoneBlockMixin",
-    "block/AnvilBlockMixin"
+    "block/AnvilBlockMixin",
+    "YggdrasilMixin"
   ]
 }