Jump to content

[1.12] Properly creating a custom sign GUI (Without block)


Stenbergcsgo

Recommended Posts

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.

Link to comment
Share on other sites

41 minutes ago, 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.

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);
		}
		
	}
}

 

Link to comment
Share on other sites

53 minutes ago, 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.

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?

Link to comment
Share on other sites

8 minutes ago, 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.

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 =)

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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