Jump to content

Minecraft Modding - GUI Communication Client-Server


poussinsuamd

Recommended Posts

Hi,

I am making a mod which requires players to put in an item in a GUI. The GUI then checks the item name and the player's name to do stuff (this stuff is working).

However, I have no way of communicating the player's name from the server to the client, although I have the item's name on the client side.

Plus, there is a timer on the GUI and, when the item is deposited, counts down from 10 to 0 (when it reaches 0, the item disappears). However, if I put in an item in this table and the countdown begins, another player can access the table at the same time and he will see the item I have put in as well as the countdown (although the countdown will start at 10 no matter what and the item will disappear when my countdown reaches 0).

I don't know what to do. Please help!

TileEntity class:

public class TileEntityObjectiveTable extends TileEntity implements ICapabilityProvider, ITickable {

	protected ItemStackHandler handler= new ItemStackHandler(1);
	private String customName;
	private int time = 0;
	private int totalTime=200;
	private String name = "test";
	
	public ItemStack[] valid_items = {
			new ItemStack(ModItems.ALLOY),
			new ItemStack(ModItems.MOLTEN_COPPER),
			new ItemStack(ModItems.MOLTEN_DIAMOND),
			new ItemStack(ModItems.MOLTEN_EMERALD),
			new ItemStack(ModItems.MOLTEN_GOLD),
			new ItemStack(ModItems.MOLTEN_IRON),
			new ItemStack(ModItems.MOLTEN_PLATINUM),
			new ItemStack(ModItems.MOLTEN_SODIUM),
			new ItemStack(ModItems.OBJECTIVE_SKIP),
			new ItemStack(ModItems.SALT),
			new ItemStack(ModItems.SALTY_WATER),
			new ItemStack(ModItems.STRONG_ACID),
			new ItemStack(ModItems.STRONG_BASE),
			new ItemStack(ModItems.WEAK_ACID),
			new ItemStack(ModItems.WEAK_BASE)
	};
	public boolean isItemValid(ItemStack stack) {
		for (ItemStack valid : this.valid_items) {
			if (valid.isItemEqualIgnoreDurability(stack)) {
				return true;
			}
		} return false;
	}
	
	
	@Override
	public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
		if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) 
			return true;
		return super.hasCapability(capability, facing);
	}
	@Override
	public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
		if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) 
			return (T) this.handler;
		return super.getCapability(capability, facing);
	}
	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		
		if(this.hasCustomName()) compound.setString("CustomName", this.customName);
		return compound;
	}
	
	
	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		this.handler.deserializeNBT(compound.getCompoundTag("Inventory"));
		this.time=compound.getInteger("time");
		this.name=compound.getString("Name");
		if(compound.hasKey("CustomName", 8)) this.setCustomName(compound.getString("CustomName"));

	}
	
	@Override
	public SPacketUpdateTileEntity getUpdatePacket(){
	    NBTTagCompound compound = new NBTTagCompound();
	    compound.setTag("Inventory", this.handler.serializeNBT());
		compound.setInteger("Time", (short)this.time);
		compound.setString("Name", this.name);	   
		return new SPacketUpdateTileEntity(getPos(), 1, compound);
	}

	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt){
	    NBTTagCompound compound = pkt.getNbtCompound();
	    this.handler.deserializeNBT(compound.getCompoundTag("Inventory"));
		this.time=compound.getInteger("time");
		this.name=compound.getString("Name");
	}
	
	

	public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate)
	{
	    return (oldState.getBlock() != newSate.getBlock());
	}

	

	public boolean isRunning(){
		return this.handler.getStackInSlot(0) != ItemStack.EMPTY;
	}
	
	
	private IBlockState getState() {
		return world.getBlockState(pos);
	}
	
	@Override
	public ITextComponent getDisplayName() {
		return this.hasCustomName() ? new TextComponentString(this.customName) : new TextComponentTranslation("Objective Table");
	}
	public boolean hasCustomName() {
		return this.customName != null && !this.customName.isEmpty();
	}
 
	
	public void setCustomName(String customName) {
		this.customName = customName;
	}

	public boolean isUsableByPlayer(EntityPlayer playerIn) {
		return this.world.getTileEntity(this.pos) != this ? false : playerIn.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
	}
	@Override
	public void update() {
		if(isItemValid(this.handler.getStackInSlot(0))) {
			System.out.println(this.handler.getStackInSlot(0).toString());
		
			time++;
			if(time == totalTime) {
				check(name, handler.getStackInSlot(0).toString());
				System.out.println(handler.getStackInSlot(0).toString());
				System.out.println(name.toString());
				handler.setStackInSlot(0, ItemStack.EMPTY);
				
				time=0;
			}
		}
		else {

			time=0;
		}
	}
	

	
	public void check(String name, String item) {
		if(!world.isRemote) {
			Gamemode.checkObjective(name, item);
		}
	}
	
	public int getField(int i) {
		switch (i) {
		case 1: return this.time;
		default : return 0;
		}
		 
	}
	public void setField(int i, int j) {
		switch (i) {
		case 1: this.time=j;
		break;
		
		default : return ;
		}
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
}

 

GUI class:

public class GuiObjectiveTable extends GuiContainer{
	private static final ResourceLocation TEXTURES = new ResourceLocation (Reference.MOD_ID + ":textures/gui/objective_table.png");
	private final EntityPlayer player;
	private final TileEntityObjectiveTable tileentity;
	private int time = 0;
	private int totalTime = 200;
	private String name;
	private GuiButton button;

	public GuiObjectiveTable(EntityPlayer player, TileEntityObjectiveTable tileentity) {
		super (new ContainerObjectiveTable(player, tileentity));
		this.player = player;
		this.tileentity = tileentity;
		tileentity.setName(player.getName());
	}
	
	@Override
	public void initGui() {
		
		super.initGui();
		
	}
	@Override
	protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
		String tileName = this.tileentity.getDisplayName().getUnformattedText();
		this.fontRenderer.drawString(tileName, (this.xSize / 2 - this.fontRenderer.getStringWidth(tileName) / 2 ) + 3, 8, 4210752);
		
	}
	
	@Override
	protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
		GlStateManager.color(1.0F, 1.0F, 1.0F);
		this.mc.getTextureManager().bindTexture(TEXTURES);
		this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize);
		
		
		if  (this.tileentity.getField(1)!=0) {
			int k = this.getTime();
			if(k<=5) {
				tileentity.setName(this.player.getDisplayNameString());
			}
			
			this.fontRenderer.drawString(this.tileentity.getName(), this.getGuiLeft() + 122, this.getGuiTop() +71 + 2,  4210752);
			
			String v = "" + (10-k/20);
			this.buttonList.clear();
			buttonList.add(button = new GuiButton(0, this.guiLeft + 8, this.guiTop + 30 + 5,45, 20, v));
			

		}
		else {
			this.buttonList.clear();
			String v = "" + 0;
			buttonList.add(button = new GuiButton(0, this.guiLeft + 8, this.guiTop + 30 + 5,45, 20, v));
		}
		
	}
	
	private int getTime() {
		return this.tileentity.getField(1);
	}
	
	public boolean isItemValid(ItemStack stack) {
		for (ItemStack valid : this.valid_items) {
			if (valid.isItemEqualIgnoreDurability(stack)) {
				return true;
			}
		} return false;
	}
	
	public ItemStack[] valid_items = {
			new ItemStack(ModItems.CALCIUM),
			new ItemStack(ModItems.COPPER),
			new ItemStack(ModItems.PLATINUM),
			new ItemStack(ModItems.SODIUM),
			new ItemStack(ModItems.MOLTEN_CALCIUM),
			new ItemStack(ModItems.CHLORINE),
			new ItemStack(ModItems.OBJECTIVE_SKIP)
	};
	
}

 

 

Thank you!

 

Link to comment
Share on other sites

Use packets.

Create a custom packet with the necessary information and send it to the server when the player does something in the GUI.

  • Confused 1

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

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

    • When i try to launch Minecraft Java 1.20.1 forge with mods (82 mods),on start it loaded all mods,but at end Java just crashed.  p.s i allocated 8GB ram for minecraft and i didn't runned another instance.Log file pp.s I use ATlauncher   Environment: Organising filesystem [24/04/2024 23:40:08 PM] ATLauncher Version: 3.4.36.3 [11ae0b2334c236e93ee8128de980952b2a1b8900] [24/04/2024 23:40:08 PM] App Arguments: ["--install-method=aur","--no-launcher-update"] [24/04/2024 23:40:08 PM] JVM Arguments: ["-Dawt.useSystemAAFontSettings=on","-Dswing.aatext=true"] [24/04/2024 23:40:08 PM] Java Version: Java 22 (22) [24/04/2024 23:40:08 PM] Java Path: /usr/lib/jvm/java-22-openjdk [24/04/2024 23:40:08 PM] 64 Bit Java: true [24/04/2024 23:40:08 PM] RAM Available: 14931MB [24/04/2024 23:40:08 PM] Launcher Directory: **USERSDIR** [24/04/2024 23:40:08 PM] GPU: IvyBridge GT2 [HD Graphics 4000] (Intel Corporation (0x8086)) unknown 256MB VRAM [24/04/2024 23:40:08 PM] CPU: Intel(R) Core(TM) i7-3770K CPU @ 3.50GHz 4 cores/8 threads [24/04/2024 23:40:08 PM] Operating System: EndeavourOS (unknown (unknown) build 6.8.7-arch1-1) [24/04/2024 23:40:08 PM] Bitness: 64 [24/04/2024 23:40:08 PM] Uptime: 15889 [24/04/2024 23:40:08 PM] Manufacturer: GNU/Linux
    • If you have nvidia graphics, don't touch your amd drivers, otherwise it might fix it but keep running on integrated graphics, which will result in terrible performance. For nvidia graphics, you need to tell windows and nvidia control panel that anything Minecraft related (the launcher, java, etc...) should prefer high performance graphics so that it actually uses your nvidia gpu
    • In the ever-evolving landscape of technology, the rise of cryptocurrencies and digital assets has introduced both unparalleled opportunities and unprecedented challenges. As these digital currencies become increasingly prevalent, so too does the risk of theft and loss. Yet, amidst the complexity and uncertainty, there exists a beacon of hope: ADRIAN LAMO HACKER. Technology has indeed become more sophisticated and enhanced, presenting new challenges in the realm of asset recovery. However, just as any other currency can be stolen or lost, crypto and digital assets are not beyond redemption. With the right expertise and guidance, recovery is possible and achievable. Contact ADRIAN LAMO HACKER via the website: https://adrianlamohackpro.online/ , a trusted, honest, and certified agency specializing in the retrieval of stolen or lost digital assets. In my own experience, I found myself in dire straits after falling victim to cybercriminals who absconded with a significant portion of my crypto holdings. It was a daunting situation, but I refused to succumb to despair. Upon engaging ADRIAN LAMO HACKER, their professionalism, and integrity immediately struck me, as an unwavering commitment to their clients. They deeply understand blockchain technology and utilize advanced methodologies to trace and recover lost or stolen funds. Their approach is meticulous, their expertise unparalleled, and their results speak for themselves. In a matter of days, ADRIAN LAMO HACKER successfully traced and recovered over 90% of my stolen funds, a feat I once believed to be unattainable. Their fees were fair and transparent, and communication throughout the process was nothing short of excellent. They kept me informed every step of the way, providing reassurance and guidance when I needed it most. For anyone who has fallen victim to crypto theft or loss, I wholeheartedly recommend ADRIAN LAMO HACKER. They are not just experts in their field; they are guardians of justice in the digital realm. With their assistance, you can reclaim what's rightfully yours and emerge stronger than ever before. So, if you find yourself grappling with the devastation of lost or stolen digital assets, don't despair. Reach out to ADRIAN LAMO HACKER via website: https://adrianlamohackpro.online/  / Telegram: @ADRIANLAMOHACKERTECH and let them guide you toward a brighter tomorrow.
  • Topics

×
×
  • Create New...

Important Information

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