Minecraft_HT
如题,我按照官方的教程在做一个方块实体时打算尝试制作一个容器,但是出现一个问题:
这个是代码:

  1. package net.Star_Era.BlockEntity;

  2. import net.Star_Era.Inventories.ImplementedInventory;
  3. import net.Star_Era.api.Era_BlockEntity;
  4. import net.minecraft.block.BlockState;
  5. import net.minecraft.block.entity.BlockEntity;
  6. import net.minecraft.entity.player.PlayerEntity;
  7. import net.minecraft.entity.player.PlayerInventory;
  8. import net.minecraft.inventory.Inventories;
  9. import net.minecraft.item.ItemStack;
  10. import net.minecraft.nbt.NbtCompound;
  11. import net.minecraft.screen.NamedScreenHandlerFactory;
  12. import net.minecraft.screen.ScreenHandler;
  13. import net.minecraft.text.Text;
  14. import net.minecraft.text.TranslatableText;
  15. import net.minecraft.util.collection.DefaultedList;
  16. import net.minecraft.util.math.BlockPos;

  17. public class Storage_BinEntity extends BlockEntity implements NamedScreenHandlerFactory, ImplementedInventory {
  18.     private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(27, ItemStack.EMPTY);

  19.     public Storage_BinEntity(BlockPos pos, BlockState state) {
  20.         super(Era_BlockEntity.STORAGE_BIN_ENTITY, pos, state);
  21.     }

  22.     @Override
  23.     public DefaultedList<ItemStack> getItems() {
  24.         return inventory;

  25.     }

  26.     //These Methods are from the NamedScreenHandlerFactory Interface
  27.     //createMenu creates the ScreenHandler itself
  28.     //getDisplayName will Provide its name which is normally shown at the top

  29.     @Override
  30.     public ScreenHandler createMenu(int syncId, PlayerInventory playerInventory, PlayerEntity player) {
  31.         //We provide *this* to the screenHandler as our class Implements Inventory
  32.         //Only the Server has the Inventory at the start, this will be synced to the client in the ScreenHandler
  33.         return new Storage_BinScreenHandler(syncId, playerInventory, this);
  34.     }

  35.     @Override
  36.     public Text getDisplayName() {
  37.         return new TranslatableText(getCachedState().getBlock().getTranslationKey());
  38.     }

  39.     @Override
  40.     public void readNbt(NbtCompound nbt) {
  41.         super.readNbt(nbt);
  42.         Inventories.readNbt(nbt, this.inventory);
  43.     }

  44.     @Override
  45.     public <u><font color="Black">NbtCompound</font></u> writeNbt(NbtCompound nbt) {
  46.         super.writeNbt(nbt);
  47.         Inventories.writeNbt(nbt, this.inventory);
  48.         return nbt;
  49.     }
  50. }
复制代码
出现的问题是:




请问怎么解决呢



Sunbread
我去翻了下yarn,这个父类方法的签名是void writeNbt(NbtCompound nbt)
你如果要重写父类方法,应该将重写方法的签名也改成void writeNbt(NbtCompound nbt)
如果要重载方法,应该让重载方法的形参与被重载方法有区别

Minecraft_HT
Sunbread 发表于 2022-2-2 14:57
我去翻了下yarn,这个父类方法的签名是void writeNbt(NbtCompound nbt)
你如果要重写父类方法,应该将重写 ...


呃,主要是这个方法我在yarm那看过了,按照IDEA的修改方案改的,然后就是他不能返回值,导致如果我把return nbt去掉的话,容器在minecraft里就不能运行




15614877085
356448356448356448356448356448

15614877085
https://fxg.jinritemai.com/login

Sunbread
Minecraft_HT 发表于 2022-2-3 10:41
呃,主要是这个方法我在yarm那看过了,按照IDEA的修改方案改的,然后就是他不能返回值,导致如果我把ret ...

我不太明白你写这段的目的

第一页 上一页 下一页 最后一页