Jump to content

Recommended Posts

Posted

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!

 

Posted

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:

  Reveal hidden contents

 

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

    • Yes that’s the full log, I managed to get it working last night, the anvil fix mod is what was causing it to crash
    • Hey guys, i'm currently developping a mod with forge 1.12.2 2860 and i'm using optifine and gradle 4.9. The thing is i'm trying to figure out how to show the player's body in first person. So far everything's going well since i've try to use a shader. The player's body started to blink dark when using a shader. I've try a lot of shader like chocapic, zeus etc etc but still the same issue. So my question is : How should i apply the current shader to the body ? At the same time i'm also drawing a HUD so maybe it could be the problem?   Here is the issue :    And here is the code where i'm trying to display the body :    private static void renderFirstPersonBody(EntityPlayerSP player, float partialTicks) { Minecraft mc = Minecraft.getMinecraft(); GlStateManager.pushMatrix(); GlStateManager.pushAttrib(); try { // Préparation OpenGL GlStateManager.enableDepth(); GlStateManager.depthMask(true); GlStateManager.enableAlpha(); GlStateManager.alphaFunc(GL11.GL_GREATER, 0.1F); GlStateManager.enableBlend(); GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); // Éclairage correct pour shaders GlStateManager.enableLighting(); RenderHelper.enableStandardItemLighting(); GlStateManager.enableRescaleNormal(); // Active la lightmap pour les shaders mc.entityRenderer.enableLightmap(); // Position de rendu interpolée double px = player.lastTickPosX + (player.posX - player.lastTickPosX) * partialTicks; double py = player.lastTickPosY + (player.posY - player.lastTickPosY) * partialTicks; double pz = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * partialTicks; GlStateManager.translate( px - mc.getRenderManager().viewerPosX, py - mc.getRenderManager().viewerPosY, pz - mc.getRenderManager().viewerPosZ ); // Rendu du joueur sans la tête Render<?> render = mc.getRenderManager().getEntityRenderObject(player); if (render instanceof RenderPlayer) { RenderPlayer renderPlayer = (RenderPlayer) render; boolean oldHeadHidden = renderPlayer.getMainModel().bipedHead.isHidden; boolean oldHeadwearHidden = renderPlayer.getMainModel().bipedHeadwear.isHidden; renderPlayer.getMainModel().bipedHead.isHidden = true; renderPlayer.getMainModel().bipedHeadwear.isHidden = true; setArmorHeadVisibility(renderPlayer, false); renderPlayer.doRender(player, 0, 0, 0, player.rotationYaw, partialTicks); renderPlayer.getMainModel().bipedHead.isHidden = oldHeadHidden; renderPlayer.getMainModel().bipedHeadwear.isHidden = oldHeadwearHidden; setArmorHeadVisibility(renderPlayer, !oldHeadwearHidden); } // Nettoyage post rendu mc.entityRenderer.disableLightmap(); GlStateManager.disableRescaleNormal(); } catch (Exception e) { // silent fail } finally { GlStateManager.popAttrib(); GlStateManager.popMatrix(); } }   Ty for your help. 
    • Item successfully registered, but there was a problem with the texture of the item, it did not insert and has just the wrong texture.     
    • Keep on using the original Launcher Run Vanilla 1.12.2 once and close the game Download Optifine and run optifine as installer (click on the optifine jar) Start the launcher and make sure the Optifine profile is selected - then test it again  
    • Hi everyone, I’m hoping to revisit an old version of Minecraft — specifically around Beta 1.7.3 — for nostalgia’s sake. I’ve heard you can do this through the official Minecraft Launcher, but I’m unsure how to do it safely without affecting my current installation or save files. Are there any compatibility issues I should watch out for when switching between versions? Would really appreciate any tips or advice from anyone who’s done this before! – Adam
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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