Stenbergcsgo Posted April 6, 2018 Posted April 6, 2018 Hello Minecraft Forge, I'm trying to create a GUI which does the following: - Player rightclicks with custom item - GUI similar to the sign GUI appears, where the player can input a string, which is then stored (Client side only) - Clicking done closes the GUI So I've tried creating this, but ran into some issues. The GUI doesn't load at all. signInputGui: package com.mta.utzonmod.client.gui; import java.io.IOException; import org.lwjgl.input.Keyboard; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.resources.I18n; import net.minecraft.util.ChatAllowedCharacters; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TextComponentString; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @SideOnly(Side.CLIENT) public class signInputGui extends GuiScreen { private GuiButton doneBtn; private int updateCounter; private int editLine; public String playerInput; public void initGui() { this.buttonList.clear(); Keyboard.enableRepeatEvents(true); this.doneBtn = this.addButton(new GuiButton(0, this.width / 2 - 100, this.height / 4 + 120, I18n.format("gui.done"))); } public void onGuiClosed() { Keyboard.enableRepeatEvents(false); } public void updateScreen() { ++this.updateCounter; } protected void actionPerformed(GuiButton button) throws IOException { if(button.enabled) { if(button.id == 0) { this.mc.displayGuiScreen((GuiScreen)null); } } } public final ITextComponent[] signText = new ITextComponent[] {new TextComponentString(""), new TextComponentString(""), new TextComponentString(""), new TextComponentString("")}; protected void keyTyped(char typedChar, int keyCode) throws IOException { if(keyCode == 200) { this.editLine = this.editLine - 1 & 3; } if (keyCode == 208 || keyCode == 28 || keyCode == 156) { this.editLine = this.editLine + 1 & 3; } for (int i = 0; i < 4; ++i) { playerInput = ITextComponent.Serializer.componentToJson(this.signText[i]); } if (keyCode == 14 && !playerInput.isEmpty()) { playerInput = playerInput.substring(0, playerInput.length() - 1); } if (ChatAllowedCharacters.isAllowedCharacter(typedChar) && this.fontRenderer.getStringWidth(playerInput + typedChar) <= 90) { playerInput = playerInput + typedChar; } if (keyCode == 1) { this.actionPerformed(this.doneBtn); } } public void drawScreen(int mouseX, int mouseY, float partialTicks) { this.drawDefaultBackground(); this.drawCenteredString(this.fontRenderer, I18n.format("sign.edit"),this.width / 2, 40, 16777215); GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); GlStateManager.pushMatrix(); GlStateManager.translate((float)(this.width / 2), 0.0F, 50.0F); GlStateManager.scale(-93.75F, -93.75F, -93.75F); GlStateManager.rotate(180.0F, 0.0F, 1.0F, 0.0F); GlStateManager.popMatrix(); super.drawScreen(mouseX, mouseY, partialTicks); } } SchematicSign: package com.mta.utzonmod.items; import com.mta.utzonmod.client.gui.signInputGui; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.util.ActionResult; import net.minecraft.util.EnumHand; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TextComponentString; import net.minecraft.world.World; public class SchematicSign extends ItemBase { public SchematicSign(String name) { super(name); } @Override public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer player, EnumHand handIn) { ITextComponent message = new TextComponentString("Hello"); player.sendMessage(message); //player.openGui(Main.instance, 0, worldIn, 1, 1, 1); signInputGui inputGui = new signInputGui(); inputGui.initGui(); return super.onItemRightClick(worldIn, player, handIn); } } ItemBase: package com.mta.utzonmod.items; import com.mta.utzonmod.Main; import com.mta.utzonmod.init.ModItems; import com.mta.utzonmod.util.IHasModel; import net.minecraft.item.Item; import net.minecraft.nbt.NBTTagCompound; public class ItemBase extends Item implements IHasModel{ public ItemBase(String name) { setUnlocalizedName(name); setRegistryName(name); setCreativeTab(Main.utzontab); ModItems.ITEMS.add(this); } @Override public void registerModels() { Main.proxy.registerItemRenderer(this, 0, "inventory"); } } So instead of messing too much around, I'd hope some of you guys might be able to chip in where I went right and where I went wrong. As mentioned the GUI doesn't even appear at the moment, which I assume is related to the "drawScreen()" not getting called. Quote
Stenbergcsgo Posted April 6, 2018 Author Posted April 6, 2018 On 4/6/2018 at 9:33 AM, diesieben07 said: That's not how you open a GuiScreen. Never call initGui yourself. To open a screen, call Minecraft::displayGuiScreen. You cannot access client-only classes (such as your GuiScreen) from common code (your item). You must use your @SidedProxy. You must perform a check for the logical side in onItemRightClick. Expand So would I modify my ClientProxy to?: package com.mta.utzonmod.proxy; import com.mta.utzonmod.client.gui.signInputGui; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.block.model.ModelResourceLocation; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.world.World; import net.minecraftforge.client.model.ModelLoader; import net.minecraftforge.fml.common.network.IGuiHandler; public class ClientProxy extends CommonProxy { public void registerItemRenderer(Item item, int meta, String id) { ModelLoader.setCustomModelResourceLocation(item, meta, new ModelResourceLocation(item.getRegistryName(), id)); } public void init() { Minecraft mc = Minecraft.getMinecraft(); mc.displayGuiScreen(new signInputGui()); } } And also added the check to my item: package com.mta.utzonmod.items; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.util.ActionResult; import net.minecraft.util.EnumHand; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TextComponentString; import net.minecraft.world.World; public class SchematicSign extends ItemBase { public SchematicSign(String name) { super(name); } @Override public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer player, EnumHand handIn) { if(!worldIn.isRemote) { ITextComponent message = new TextComponentString("Hello"); player.sendMessage(message); //player.openGui(Main.instance, 0, worldIn, 1, 1, 1); return super.onItemRightClick(worldIn, player, handIn); } else { return super.onItemRightClick(worldIn, player, handIn); } } } Quote
Stenbergcsgo Posted April 6, 2018 Author Posted April 6, 2018 On 4/6/2018 at 11:21 AM, diesieben07 said: You are now not opening the GUI at all from your item. You probably want to return a different result than EnumActionResult.PASS. Expand Just a really stupid question, so how should i properly open it from my item? Move Minecraft mc = Minecraft.getMinecraft(); mc.displayGuiScreen(new signInputGui()); to the item class? Quote
Stenbergcsgo Posted April 6, 2018 Author Posted April 6, 2018 On 4/6/2018 at 12:36 PM, diesieben07 said: Do you even understand how @SidedProxy works? You now made a method in your proxy, but you don't call it from anywhere. What do you think this does? Exactly: nothing. Expand Yea my bad guess I'm starting to oversee some stuff after messing around with it for so long. Called the init from my item, and it works now. Thanks =) Quote
Recommended Posts
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.