Add TE with inv gui
This commit is contained in:
parent
1631f5538a
commit
7379940c4f
41
src/main/java/com/pjht/ssspcore/ModGuiHandler.java
Normal file
41
src/main/java/com/pjht/ssspcore/ModGuiHandler.java
Normal file
@ -0,0 +1,41 @@
|
||||
package com.pjht.ssspcore;
|
||||
|
||||
import com.pjht.ssspcore.block.te.pedestal.ContainerPedestal;
|
||||
import com.pjht.ssspcore.block.te.pedestal.GuiPedestal;
|
||||
import com.pjht.ssspcore.block.te.pedestal.TileEntityPedestal;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.fml.common.network.IGuiHandler;
|
||||
import net.minecraftforge.fml.common.network.NetworkRegistry;
|
||||
|
||||
public class ModGuiHandler implements IGuiHandler {
|
||||
public static final int PEDESTAL = 0;
|
||||
|
||||
public static void init() {
|
||||
NetworkRegistry.INSTANCE.registerGuiHandler(SSSPCore.instance, new ModGuiHandler());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Container getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
|
||||
switch (ID) {
|
||||
case PEDESTAL:
|
||||
return new ContainerPedestal(player.inventory, (TileEntityPedestal)world.getTileEntity(new BlockPos(x, y, z)));
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
|
||||
switch (ID) {
|
||||
case PEDESTAL:
|
||||
return new GuiPedestal(getServerGuiElement(ID, player, world, x, y, z), player.inventory);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -15,6 +15,7 @@ import net.minecraftforge.fml.common.event.FMLInitializationEvent;
|
||||
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
|
||||
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.network.NetworkRegistry;
|
||||
|
||||
@Mod(modid = SSSPCore.modId, name = SSSPCore.name, version = SSSPCore.version)
|
||||
public class SSSPCore {
|
||||
@ -35,6 +36,7 @@ public class SSSPCore {
|
||||
System.out.println(name + " is loading!");
|
||||
ModItems.init();
|
||||
ModBlocks.init();
|
||||
ModGuiHandler.init();
|
||||
}
|
||||
|
||||
|
||||
|
@ -2,6 +2,7 @@ package com.pjht.ssspcore.block.te.pedestal;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.pjht.ssspcore.ModGuiHandler;
|
||||
import com.pjht.ssspcore.SSSPCore;
|
||||
import com.pjht.ssspcore.block.te.BlockTileEntity;
|
||||
|
||||
@ -13,7 +14,6 @@ import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.items.CapabilityItemHandler;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
@ -60,13 +60,7 @@ public class BlockPedestal extends BlockTileEntity<TileEntityPedestal>{
|
||||
}
|
||||
tile.markDirty();
|
||||
} else {
|
||||
ItemStack stack = itemHandler.getStackInSlot(0);
|
||||
if (!stack.isEmpty()) {
|
||||
String localized = SSSPCore.proxy.localize(stack.getUnlocalizedName() + ".name");
|
||||
player.sendMessage(new TextComponentString(stack.getCount() + "x " + localized));
|
||||
} else {
|
||||
player.sendMessage(new TextComponentString("Empty"));
|
||||
}
|
||||
player.openGui(SSSPCore.instance, ModGuiHandler.PEDESTAL, world, pos.getX(), pos.getY(), pos.getZ());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
@ -0,0 +1,74 @@
|
||||
package com.pjht.ssspcore.block.te.pedestal;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.InventoryPlayer;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.inventory.Slot;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.items.CapabilityItemHandler;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import net.minecraftforge.items.SlotItemHandler;
|
||||
|
||||
public class ContainerPedestal extends Container {
|
||||
|
||||
public ContainerPedestal(InventoryPlayer playerInv, final TileEntityPedestal pedestal) {
|
||||
IItemHandler inventory = pedestal.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH);
|
||||
addSlotToContainer(new SlotItemHandler(inventory, 0, 80, 35) {
|
||||
@Override
|
||||
public void onSlotChanged() {
|
||||
pedestal.markDirty();
|
||||
}
|
||||
});
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int j = 0; j < 9; j++) {
|
||||
addSlotToContainer(new Slot(playerInv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 0; k < 9; k++) {
|
||||
addSlotToContainer(new Slot(playerInv, k, 8 + k * 18, 142));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canInteractWith(EntityPlayer playerIn) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack transferStackInSlot(EntityPlayer player, int index) {
|
||||
ItemStack itemstack = ItemStack.EMPTY;
|
||||
Slot slot = inventorySlots.get(index);
|
||||
|
||||
if (slot != null && slot.getHasStack()) {
|
||||
ItemStack itemstack1 = slot.getStack();
|
||||
itemstack = itemstack1.copy();
|
||||
|
||||
int containerSlots = inventorySlots.size() - player.inventory.mainInventory.size();
|
||||
|
||||
if (index < containerSlots) {
|
||||
if (!this.mergeItemStack(itemstack1, containerSlots, inventorySlots.size(), true)) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
} else if (!this.mergeItemStack(itemstack1, 0, containerSlots, false)) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
if (itemstack1.getCount() == 0) {
|
||||
slot.putStack(ItemStack.EMPTY);
|
||||
} else {
|
||||
slot.onSlotChanged();
|
||||
}
|
||||
|
||||
if (itemstack1.getCount() == itemstack.getCount()) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
slot.onTake(player, itemstack1);
|
||||
}
|
||||
|
||||
return itemstack;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.pjht.ssspcore.block.te.pedestal;
|
||||
|
||||
import com.pjht.ssspcore.SSSPCore;
|
||||
import com.pjht.ssspcore.block.ModBlocks;
|
||||
|
||||
import net.minecraft.client.gui.inventory.GuiContainer;
|
||||
import net.minecraft.client.renderer.GlStateManager;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import net.minecraft.entity.player.InventoryPlayer;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
public class GuiPedestal extends GuiContainer {
|
||||
private static final ResourceLocation BG_TEXTURE = new ResourceLocation(SSSPCore.modId, "textures/gui/pedestal.png");
|
||||
private InventoryPlayer _playerInv;
|
||||
|
||||
|
||||
public GuiPedestal(Container container, InventoryPlayer playerInv) {
|
||||
super(container);
|
||||
_playerInv = playerInv;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
|
||||
GlStateManager.color(1, 1, 1, 1);
|
||||
mc.getTextureManager().bindTexture(BG_TEXTURE);
|
||||
int x = (width - xSize) / 2;
|
||||
int y = (height - ySize) / 2;
|
||||
drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
|
||||
String name = I18n.format(ModBlocks.blocks.get("pedestal").getUnlocalizedName() + ".name");
|
||||
fontRenderer.drawString(name, xSize / 2 - fontRenderer.getStringWidth(name) / 2, 6, 0x404040);
|
||||
fontRenderer.drawString(_playerInv.getDisplayName().getUnformattedText(), 8, ySize - 94, 0x404040);
|
||||
}
|
||||
|
||||
}
|
BIN
src/main/resources/assets/ssspcore/textures/gui/pedestal.png
Normal file
BIN
src/main/resources/assets/ssspcore/textures/gui/pedestal.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.2 KiB |
Loading…
x
Reference in New Issue
Block a user