MC百科社群

标题: 关于Forge1.20.1中InventoryMenu代码在Slot上的疑问 [打印本页]

作者: QQ酱76541    时间: 2024-3-13 12:17
标题: 关于Forge1.20.1中InventoryMenu代码在Slot上的疑问
以下是改其构造器全部代码:
[spoiler=InventoryMenu]public InventoryMenu(Inventory pPlayerInventory, boolean pActive, final Player pOwner) {
   super((MenuType<?>)null, 0);
   this.active = pActive;
   this.owner = pOwner;
   this.addSlot(new ResultSlot(pPlayerInventory.player, this.craftSlots, this.resultSlots, 0, 154, 28));

   for(int i = 0; i < 2; ++i) {
      for(int j = 0; j < 2; ++j) {
         this.addSlot(new Slot(this.craftSlots, j + i * 2, 98 + j * 18, 18 + i * 18));
      }
   }

   for(int k = 0; k < 4; ++k) {
      final EquipmentSlot equipmentslot = SLOT_IDS[k];
      this.addSlot(new Slot(pPlayerInventory, 39 - k, 8, 8 + k * 18) {
         public void setByPlayer(ItemStack p_270969_) {
            InventoryMenu.onEquipItem(pOwner, equipmentslot, p_270969_, this.getItem());
            super.setByPlayer(p_270969_);
         }

         /**
          * Returns the maximum stack size for a given slot (usually the same as getInventoryStackLimit(), but 1 in
          * the case of armor slots)
          */
         public int getMaxStackSize() {
            return 1;
         }

         /**
          * Check if the stack is allowed to be placed in this slot, used for armor slots as well as furnace fuel.
          */
         public boolean mayPlace(ItemStack p_39746_) {
            return p_39746_.canEquip(equipmentslot, owner);
         }

         /**
          * Return whether this slot's stack can be taken from this slot.
          */
         public boolean mayPickup(Player p_39744_) {
            ItemStack itemstack = this.getItem();
            return !itemstack.isEmpty() && !p_39744_.isCreative() && EnchantmentHelper.hasBindingCurse(itemstack) ? false : super.mayPickup(p_39744_);
         }

         public Pair<ResourceLocation, ResourceLocation> getNoItemIcon() {
            return Pair.of(InventoryMenu.BLOCK_ATLAS, InventoryMenu.TEXTURE_EMPTY_SLOTS[equipmentslot.getIndex()]);
         }
      });
   }

   for(int l = 0; l < 3; ++l) {
      for(int j1 = 0; j1 < 9; ++j1) {
         this.addSlot(new Slot(pPlayerInventory, j1 + (l + 1) * 9, 8 + j1 * 18, 84 + l * 18));
      }
   }

   for(int i1 = 0; i1 < 9; ++i1) {
      this.addSlot(new Slot(pPlayerInventory, i1, 8 + i1 * 18, 142));
   }

   this.addSlot(new Slot(pPlayerInventory, 40, 77, 62) {
      public void setByPlayer(ItemStack p_270479_) {
         InventoryMenu.onEquipItem(pOwner, EquipmentSlot.OFFHAND, p_270479_, this.getItem());
         super.setByPlayer(p_270479_);
      }

      public Pair<ResourceLocation, ResourceLocation> getNoItemIcon() {
         return Pair.of(InventoryMenu.BLOCK_ATLAS, InventoryMenu.EMPTY_ARMOR_SLOT_SHIELD);
      }
   });
}[/spoiler]
可以看出原版添加Item槽位id可以分为 显然SlotID出现重复,PlayerHotBar和2X2CraftSlot与ResultCraftSlot明显出现了0,1,2,3重复的Slotid


[spoiler=该类的字段]public static final int CONTAINER_ID = 0;
public static final int RESULT_SLOT = 0;
public static final int CRAFT_SLOT_START = 1;
public static final int CRAFT_SLOT_END = 5;
public static final int ARMOR_SLOT_START = 5;
public static final int ARMOR_SLOT_END = 9;
public static final int INV_SLOT_START = 9;
public static final int INV_SLOT_END = 36;
public static final int USE_ROW_SLOT_START = 36;
public static final int USE_ROW_SLOT_END = 45;
public static final int SHIELD_SLOT = 45;[/spoiler]

这是为什么





作者: Sonnenlicht    时间: 2024-3-17 01:34
在Slot类中有两个属性可以被视为“ID”:
一个是index属性,使用InventoryMenu的addSlot方法向Menu实例中的slots列表添加新的slot对象时,首先被添加的slot对象的index属性值被赋值为slots.size(),然后才会把slot对象添加进列表,这保证了每个添加到slots的slot对象的index在同一个InventoryMenu实例中是唯一的。
另一个是slot属性,是Slot类中container属性内的相对id,在同一个container实例中唯一,在不同container实例之间是可重复的,使用时一般通过 slotA.container == slotB.container && slotA.slot == slotB.slot 来判断两个slot是否相等。

”2X2CraftSlot“的四个槽位被添加到InventoryMenu的成员slots列表中,四个槽位的slot属性范围是1~4,对应这四个槽位在slots中的索引值;0~3是相对于container“craftSlots”的索引。
作者: Sonnenlicht    时间: 2024-3-17 01:35
补一下addSlot的代码:
  1.    protected Slot addSlot(Slot slot) {
  2.       slot.index = this.slots.size();
  3.       this.slots.add(slot);
  4.       this.lastSlots.add(ItemStack.EMPTY);
  5.       this.remoteSlots.add(ItemStack.EMPTY);
  6.       return slot;
  7.    }
复制代码





欢迎光临 MC百科社群 (https://bbs.mcmod.cn/) MC百科|最大的MineCraft中文模组百科