- Версия(и) Minecraft
- 1.7.10
В этом ресурсе Вы узнаете:
Скачать мод
Что нам нужно:
Немного кода:
Регистрируем нужные ресурсы
Отменяем открытие ванильного инвентаря и открываем свой
Рендерим GUI инвентаря
Добавляем свои слоты
Апдейтим кастомные слоты для других игроков на примере наплечников
Апдейтим кастомные слоты для onArmorTick на примере наплечников
Результат:
- Как создать свой инвентарь с кастомными слотами.
Скачать мод
Что нам нужно:
Текстура формата *.png
Немного кода:
Регистрируем нужные ресурсы
Resources.java:
public class Resources {
public static final ResourceLocation customArmorTexture = new ResourceLocation(Info.modid, "textures/models/items/customArmorTexture.png");
public static final ResourceLocation inventoryTexture = new ResourceLocation(Info.modid, "textures/gui/inventory.png");
}
Отменяем открытие ванильного инвентаря и открываем свой
OpenInventory.java::
@SubscribeEvent
public void onOpenGui(GuiOpenEvent event) {
if(event.gui instanceof GuiInventory) {
EntityPlayer player = FMLClientHandler.instance().getClient().thePlayer;
if (!player.capabilities.isCreativeMode) {
event.setCanceled(true);
PacketDispatcher.sendToServer(new OpenGuiMessage(Core.GUI_CUSTOM_INV));
}
}
}
Рендерим GUI инвентаря
GuiCustomPlayerInventory.java::
private int xSize_lo;
private int ySize_lo;
private final InventoryCustomPlayer inventory;
public GuiCustomPlayerInventory(EntityPlayer player, InventoryPlayer inventoryPlayer, InventoryCustomPlayer inventoryCustom) {
super(new ContainerCustomPlayer(player, inventoryPlayer, inventoryCustom));
this.inventory = inventoryCustom;
}
@Override
public void drawScreen(int mouseX, int mouseY, float f) {
super.drawScreen(mouseX, mouseY, f);
xSize_lo = mouseX;
ySize_lo = mouseY;
}
@Override
protected void drawGuiContainerBackgroundLayer(float f, int mouseX, int mouseY) {
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
mc.getTextureManager().bindTexture(Resources.inventoryTexture);
drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
//рендер кастомных слотов когда пустые
for (int i = 0; i < 4; i++) {
int noEquip = 169;
if(inventory.getStackInSlot(i) != null) {
noEquip = 0;
}
drawTexturedModalRect(guiLeft + 7, guiTop + 7 + 18 * i, 7 + noEquip, 7 + 18 * i, 18, 18);
}
for (int i = 0; i < 4; i++) {
int noEquip = 205;
if(inventory.getStackInSlot(i + 4) != null) {
noEquip = 0;
}
drawTexturedModalRect(guiLeft + 43, guiTop + 7 + 18 * i, 7 + noEquip, 7 + 18 * i, 18, 18);
}
//рендер игрока в инвентаре
GuiInventory.func_147046_a(guiLeft - 70, guiTop + 165, 85, guiLeft + 51 - xSize_lo, guiTop + 25 - ySize_lo, mc.thePlayer);
}
Добавляем свои слоты
ContainerCustomPlayer.java::
public ContainerCustomPlayer(EntityPlayer player, InventoryPlayer inventoryPlayer, InventoryCustomPlayer inventoryCustom) {
this.player = player;
int i;
int j;
//кастомные слоты
addSlotToContainer(new SlotShoulders(inventoryCustom, CustomSlots.SHOULDERS.ordinal(), 8, 8)); //наплечники
addSlotToContainer(new SlotBracers(inventoryCustom, CustomSlots.BRACERS.ordinal(), 8, 26)); //наручи
addSlotToContainer(new SlotGloves(inventoryCustom, CustomSlots.GLOVES.ordinal(), 8, 44)); //перчатки
addSlotToContainer(new SlotBelt(inventoryCustom, CustomSlots.BELT.ordinal(), 8, 62)); //пояс
addSlotToContainer(new SlotNecklace(inventoryCustom, CustomSlots.NECKLACE.ordinal(), 44, 8)); //ожерелье
addSlotToContainer(new SlotRing(inventoryCustom, CustomSlots.RING1.ordinal(), 44, 26)); //кольцо
addSlotToContainer(new SlotRing(inventoryCustom, CustomSlots.RING2.ordinal(), 44, 44)); //кольцо
addSlotToContainer(new SlotArtifact(inventoryCustom, CustomSlots.ARTIFACT.ordinal(), 44, 62)); //артефакт
//сюда добавлять новые кастомные слоты
//потом изменить - enum в CustomSlots
//броня
for (i = 0; i < 4; ++i) {
addSlotToContainer(new SlotArmor(player, inventoryPlayer, inventoryPlayer.getSizeInventory() - 1 - i, 26, 8 + i * 18, i));
}
//сетка крафта
for(i = 0; i < 2; ++i) {
for(j = 0; j < 2; ++j) {
this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 2, 80 + j * 18, 26 + i * 18));
}
}
//результат крафта
this.addSlotToContainer(new SlotCrafting(player, this.craftMatrix, this.craftResult, slotCraftResult, 134, 35));
//инвентарь все 3 ряда
for (i = 0; i < 3; ++i) {
for (j = 0; j < 9; ++j) {
addSlotToContainer(new Slot(inventoryPlayer, j + 9 + i * 9, 8 + j * 18, 84 + i * 18));
}
}
//хотбар
for (i = 0; i < 9; ++i) {
addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
}
}
Апдейтим кастомные слоты для других игроков на примере наплечников
PlayerUpdateEquipShoulders.java::
private static HashMap<Integer, Integer> equip0Map = new HashMap<Integer, Integer>();
@SubscribeEvent
public void onPlayerUpdateEvent(LivingUpdateEvent event) {
if(event.entity.worldObj.isRemote || !(event.entityLiving instanceof EntityPlayer)) {
return;
}
EntityPlayer player = (EntityPlayer) event.entityLiving;
int prevEquip = equip0Map.containsKey(player.getEntityId()) ? equip0Map.get(player.getEntityId()) : -1;
int currentEquip = 0;
if(prevEquip == -1) {
for(Map.Entry<Integer, Integer> entry0 : equip0Map.entrySet()) {
Entity target = event.entity.worldObj.getEntityByID(entry0.getKey());
if(target != null && target.dimension == event.entity.dimension) {
PacketDispatcher.sendTo(new SyncEquipShouldersMessage(entry0.getKey(), entry0.getValue()), (EntityPlayerMP) player);
}
}
}
if(MinecraftServer.getServer().getTickCounter() % 20 == 0) {
ItemStack itemStack = ExtendedPlayer.get(player).inventory.getStackInSlot(ContainerCustomPlayer.slotShoulders);
if(itemStack != null) {
Item equip = itemStack.getItem();
if (equip == LoadItemArmor.shoulders) currentEquip = 1;
}
if(prevEquip != currentEquip) {
equip0Map.put(player.getEntityId(), currentEquip);
if(prevEquip + currentEquip >= 0) {
PacketDispatcher.sendToDimension(new SyncEquipShouldersMessage(player.getEntityId(), currentEquip), player.dimension);
}
}
if(MinecraftServer.getServer().getTickCounter() % 12000 == 0) {
equip0Map.clear();
}
}
}
Апдейтим кастомные слоты для onArmorTick на примере наплечников
CustomArmorTick.java:
private static ItemStack shoulders = (ItemStack)null;
private static ItemStack bracers = (ItemStack)null;
private static ItemStack gloves = (ItemStack)null;
private static ItemStack belt = (ItemStack)null;
private static ItemStack necklace = (ItemStack)null;
private static ItemStack ring1 = (ItemStack)null;
private static ItemStack ring2 = (ItemStack)null;
private static ItemStack artifact = (ItemStack)null;
@SubscribeEvent
public void onPlayerUpdateEvent(LivingUpdateEvent event) {
if(event.entity.worldObj.isRemote || !(event.entityLiving instanceof EntityPlayer)) {
return;
}
EntityPlayer player = (EntityPlayer) event.entityLiving;
if(ExtendedPlayer.get(player).inventory.getStackInSlot(CustomSlots.SHOULDERS.ordinal()) != null) {
shoulders = ExtendedPlayer.get(player).inventory.getStackInSlot(CustomSlots.SHOULDERS.ordinal());
CustomItemArmor.onCustomArmorTick(player.worldObj, player, shoulders);
} else {
shoulders = (ItemStack)null;
}
}
Результат: