Jump to content

[Solved][1.7.10] Making an item with Gui and a slot


shmcrae

Recommended Posts

I'm trying to make when you have my item and you press a key it opens the Gui, but it doesn't work. I also don't know how to make an item have a slot and can't find tutorials on it.

 

Main:

 

@Mod(modid = RefStrings.MODID, name = RefStrings.NAME, version = RefStrings.VERSION)

 

public class Main

{

@SidedProxy(clientSide = RefStrings.CLIENT, serverSide = RefStrings.SERVER)

public static CommonProxy proxy;

 

@Mod.Instance(RefStrings.MODID)

public static Main instance;

 

public static final PacketPipeline packetPipeLine = new PacketPipeline();

 

 

@EventHandler

public void PreLoad(FMLPreInitializationEvent PreEvent)

{

MBlocks.mainRegistry();

MItems.mainRegistry();

MWorld.mainRegistry();

CraftingManager.mainRegistry();

NetworkRegistry.INSTANCE.registerGuiHandler(this, new GuiHandler());

packetPipeLine.initialize();

proxy.registerRenderInfo();

MinecraftForge.addGrassSeed(new ItemStack(MItems.tomatoSeed), 8);

}

 

@EventHandler

public static void Load(FMLInitializationEvent PreEvent)

{

 

}

 

@EventHandler

public static void PostLoad(FMLPostInitializationEvent PreEvent)

{

packetPipeLine.postInitialize();

}

}

 

 

KeyHandler:

 

public class KeyHandler

{

public static final int STAFF_KEY = 0;

 

private static final String[] keyDesc = {"key.elementstaff.desc"};

private static final int[] keyValues = {Keyboard.KEY_F};

private final KeyBinding[] keys;

 

public KeyHandler()

{

keys = new KeyBinding[keyDesc.length];

for(int i = 0; i < keyDesc.length; i++)

{

keys = new KeyBinding(keyDesc, keyValues, "key.tutmod.category");

ClientRegistry.registerKeyBinding(keys);

}

}

 

@SubscribeEvent

public void onKeyInput(InputEvent.KeyInputEvent event)

{

if(!FMLClientHandler.instance().isGUIOpen(GuiChat.class))

{

if(keys[sTAFF_KEY].isPressed())

{

Main.packetPipeLine.sendToServer(new OpenGuiPacket(RefStrings.GUI_STAFF));

}

}

}

}

 

 

PacketPipeline:

 

@ChannelHandler.Sharable

public class PacketPipeline extends MessageToMessageCodec<FMLProxyPacket, AbstractPacket>

{

private EnumMap<Side, FMLEmbeddedChannel> channels;

 

private LinkedList<Class<? extends AbstractPacket>> packets = new LinkedList<Class<? extends AbstractPacket>>();

 

private boolean isPostInitialized = false;

 

public boolean registerPacket(Class<? extends AbstractPacket> clazz)

{

if(this.packets.size() > 256)

{

System.err.println("Maximum ammount of packets reached!");

return false;

}

 

if(this.packets.contains(clazz))

{

System.err.println("This packet has already been registered!");

return false;

}

 

if(this.isPostInitialized)

{

System.err.println("Packet registered too late!");

return false;

}

 

this.packets.add(clazz);

return true;

}

 

public void initialize()

{

this.channels = NetworkRegistry.INSTANCE.newChannel(RefStrings.PACKET_CHANNEL, this);

registerPackets();

}

 

public void postInitialize()

{

if(isPostInitialized)

return;

 

isPostInitialized = true;

Collections.sort(this.packets, new Comparator<Class<? extends AbstractPacket>>()

{

@Override

public int compare(Class<? extends AbstractPacket> o1, Class<? extends AbstractPacket> o2)

{

int com = String.CASE_INSENSITIVE_ORDER.compare(o1.getCanonicalName(), o2.getCanonicalName());

if(com == 0)

com = o1.getCanonicalName().compareTo(o2.getCanonicalName());

return com;

}

});

}

 

public void registerPackets()

{

registerPacket(OpenGuiPacket.class);

}

 

@Override

protected void encode(ChannelHandlerContext ctx, AbstractPacket msg, List<Object> out) throws Exception

{

ByteBuf buffer = Unpooled.buffer();

 

Class<? extends AbstractPacket> clazz = msg.getClass();

 

if(!this.packets.contains(clazz))

throw new NullPointerException("This packet has never been registered: " + clazz.getCanonicalName());

 

byte discriminator = (byte) this.packets.indexOf(clazz);

buffer.writeByte(discriminator);

msg.encodeInto(ctx, buffer);

 

FMLProxyPacket proxyPacket = new FMLProxyPacket(buffer.copy(), ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get());

out.add(proxyPacket);

}

 

@SuppressWarnings("null")

@Override

protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception

{

ByteBuf payload = msg.payload();

byte discriminator = payload.readByte();

 

Class<? extends AbstractPacket> clazz = this.packets.get(discriminator);

 

if(clazz == null)

throw new NullPointerException("This packet has never been registered: " + clazz.getCanonicalName());

 

AbstractPacket abstractPacket = clazz.newInstance();

abstractPacket.decodeInto(ctx, payload.slice());

 

EntityPlayer player;

switch (FMLCommonHandler.instance().getEffectiveSide())

{

case CLIENT:

player = Minecraft.getMinecraft().thePlayer;

abstractPacket.handleClientSide(player);

break;

 

case SERVER:

INetHandler iNetHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get();

player = ((NetHandlerPlayServer) iNetHandler).playerEntity;

abstractPacket.handleServerSide(player);

break;

default:

 

}

 

out.add(abstractPacket);

}

 

public void sendToServer(AbstractPacket message)

{

this.channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER);

this.channels.get(Side.CLIENT).writeAndFlush(message);

}

 

}

 

 

AbstractPacket:

 

public abstract class AbstractPacket

{

public abstract void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer);

 

public abstract void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer);

 

public abstract void handleClientSide(EntityPlayer player);

 

public abstract void handleServerSide(EntityPlayer player);

}

 

 

OpenGuiPacket:

 

public class OpenGuiPacket extends AbstractPacket

{

private byte id;

 

public OpenGuiPacket()

{

}

 

public OpenGuiPacket(byte id)

{

this.id = id;

}

 

@Override

public void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {

buffer.writeByte(id);

}

 

@Override

public void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {

id = buffer.readByte();

}

 

@Override

public void handleClientSide(EntityPlayer player) {

 

}

 

@Override

public void handleServerSide(EntityPlayer player) {

System.out.println("You opened the staff on server");

FMLNetworkHandler.openGui(player, Main.instance, 0, player.worldObj, (int)player.posX, (int)player.posY, (int)player.posZ);

}

 

}

 

 

GuiHandler:

 

public class GuiHandler implements IGuiHandler

{

@Override

public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)

{

 

/*switch(ID)

{

case RefStrings.GUI_STAFF:

break;

}

 

return null;

*/

switch(ID)

{

default: return null;

case 0: return new StaffContainer(player);

}

}

 

@Override

public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)

{

/*switch(ID)

{

case RefStrings.GUI_STAFF:

break;

}

 

return null;

*/

switch(ID)

{

default: return null;

case 0: return new StaffGui(player);

}

}

 

}

 

 

StaffGui:

 

public class StaffGui extends GuiScreen

{

public static final int GUI_ID = 20;

 

ResourceLocation texture = new ResourceLocation(RefStrings.MODID, "textures/gui/staffGui.png");

 

public final int xSizeOfTexture = 175;

public final int ySizeOfTexture = 132;

 

public StaffGui(EntityPlayer player)

{

super();

}

 

@Override

public void drawScreen(int x, int y, float f)

{

drawDefaultBackground();

GL11.glColor4f(1F, 1F, 1F, 1F);

 

Minecraft.getMinecraft().getTextureManager().bindTexture(texture);

 

int posX = (this.width - xSizeOfTexture) / 2;

int posY = (this.height - ySizeOfTexture) / 2;

 

drawTexturedModalRect(posX, posY, 0, 0, xSizeOfTexture, ySizeOfTexture);

 

super.drawScreen(x, y, f);

}

 

@Override

public boolean doesGuiPauseGame()

{

return false;

}

}

 

 

 

StaffContainer:

 

public class StaffContainer extends Container

{

public StaffContainer(EntityPlayer player)

{

 

}

 

@Override

public boolean canInteractWith(EntityPlayer player)

{

return false;

}

 

}

 

 

 

ElementStaff:

 

public class ElementStaff extends Item

{

public ElementStaff()

{

super();

}

}

 

 

RefStrings:

 

public class RefStrings

{

 

public static final String MODID = "tutmod";

public static final String NAME = "tutMod";

public static final String VERSION = "1.0 Alpha";

 

public static final String CLIENT = "com.shmcrae.Main.ClientProxy";

public static final String SERVER = "com.shmcrae.Main.CommonProxy";

 

public static final String PACKET_CHANNEL = "shmcrae";

public static final byte GUI_STAFF = 0;

}

 

 

ClientProxy:

 

public class ClientProxy extends CommonProxy

{

public void registerRenderInfo()

{

KeyBindings.init();

FMLCommonHandler.instance().bus().register(new KeyHandler());

}

}

 

Link to comment
Share on other sites

How do i fix this

 

 

StaffGui:

 

public class StaffGui extends GuiContainer

{

private float xSizeTex;

private float ySizeTex;

 

//public final int xSizeTex = 175;

//public final int ySizeTex = 132;

 

ResourceLocation texture = new ResourceLocation(RefStrings.MODID, "textures/gui/staffGui.png");

 

private final StaffInventory inventory;

 

public StaffGui(StaffContainer containerItem)

{

super(containerItem);

this.inventory = containerItem.inventory;

}

 

public void drawScreen(int par1, int par2, float par3)

    {

        super.drawScreen(par1, par2, par3);

        this.xSizeTex = (float)par1;

        this.ySizeTex = (float)par2;

    }

 

    /**

    * Draw the foreground layer for the GuiContainer (everything in front of the items)

    */

    protected void drawGuiContainerForegroundLayer(int par1, int par2)

    {

        String s = this.inventory.hasCustomInventoryName() ? this.inventory.getInventoryName() : I18n.format(this.inventory.getInventoryName());

        this.fontRendererObj.drawString(s, this.xSize / 2 - this.fontRendererObj.getStringWidth(s) / 2, 4, 4210752);

        this.fontRendererObj.drawString(I18n.format("container.inventory"), 8, this.ySize - 129 + 1, 4210752);

    }

 

    /**

    * Draw the background layer for the GuiContainer (everything behind the items)

    */

    protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3)

    {

        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

        this.mc.getTextureManager().bindTexture(texture);

        int k = (this.width - this.xSize) / 2;

        int l = (this.height - this.ySize) / 2;

        this.drawTexturedModalRect(k, l, 1, 0, this.xSize, this.ySize);

        int i1;

    }

}

 

 

Link to comment
Share on other sites

I think the Gui is fine, it's just the slot positions are messed up and I followed a tutorial so I'm not sure how to fix it. I know it's probably in here but I'm not sure what numbers to use to fix it

 

StaffContainer:

 

public class StaffContainer extends Container

{

final StaffInventory inventory;

 

private static final int INV_START = StaffInventory.INV_SIZE, INV_END = INV_START+26, HOTBAR_START = INV_END+1, HOTBAR_END = HOTBAR_START+8;

 

public StaffContainer(EntityPlayer player, InventoryPlayer invPlayer, StaffInventory invItem)

{

this.inventory = invItem;

 

int i;

 

for(i = 0; i < StaffInventory.INV_SIZE; ++i)

{

this.addSlotToContainer(new SlotItemInv(this.inventory, i, 80 + (18 * (int)(i/4)), 8 + (18*(1%4))));

}

 

for(i = 0; i < 3; ++i)

{

for(int j = 0; j < 9; ++j)

{

this.addSlotToContainer(new Slot(invPlayer, j + i * 9 + 9, 8 + j * 18, 84 +i * 18));

}

}

 

for(i = 0; i < 9; ++i)

{

this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 142));

}

}

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.