Jump to content

[1.6.4] Sync Items In GUI Slot


XVicarious

Recommended Posts

Basically my problem is this:

The server doesn't know that there is a new item in the custom GUI slot.  If I try to take it, it disappears.  I don't know why, or how to fix it.  I've spend countless hours googling and everything and can't figure this out.  Help would be greatly appreciated.

 

GUI http://pastebin.com/QNEEK9HA

GUIHandler http://pastebin.com/LduXKXsv

TileEntity http://pastebin.com/UcCYqp66

 

Basically this:

Click Button -> New Item Appears -> Try to take item -> Item Disappears! -> :(

Link to comment
Share on other sites

I figured that was the problem. I read the article on the wiki about sending packets, but still don't quite get it. Mind maybe pointing me in the right direction on how to do that?  I get how to send like integers and bytes.  But it doesn't really explain like how do I send an ItemStack and how does it know.  Or how do I send the experience level and how do I tell it what that is?  Custom packets?  I don't know I feel in over my head with this one.

Link to comment
Share on other sites

But it doesn't really explain like how do I send an ItemStack and how does it know.

 

ItemStacks are saved to NBT data, which is convertable to packet data.

 

The cool thing about TileEntities is that this is all handled automagically.  You just need to override two functions thusly:

 

        public Packet getDescriptionPacket() {
                NBTTagCompound nbtTag = new NBTTagCompound();
                this.writeToNBT(nbtTag);
                return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, nbtTag);
        }

        public void onDataPacket(INetworkManager net, Packet132TileEntityData packet) {
                readFromNBT(packet.data);
        }

 

Provided that your readFromNBT/writeToNBT functions are likewise up to date, this will work.

 

Here's the thing though: this is server to client only.  You need to implement a network handler and send a Packet250CustomData (sp?) when the user clicks the GUI button and have the server generate the item that goes in that slot.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

So I send a packet to the server, and then in the PacketHandler I have it call my tileentity.addItemStacktoSlot()? 

 

So the handler would be like:

If you get an itementity in a packet, do this

If you get an integer in a packet, do this

 

 

EDIT:

I've been looking around again and found this example:

https://github.com/dvd604/Xperium/blob/master/net/dvd/xperium/server/XperiumPacketHandler.java

 

So I have two channels, if it comes in on say "rpglevel_lus" do the stuff for the TileEntity and if it comes in on "rpglevel_xpl" do the stuff to the player.  So I'd send two packets.  One being a TileEntityData packet and the other being a CustomPayload and have it parse the integer data from there and apply it to the player?

 

Link to comment
Share on other sites

So I send a packet to the server, and then in the PacketHandler I have it call my tileentity.addItemStacktoSlot()? 

 

So the handler would be like:

If you get an itementity in a packet, do this

If you get an integer in a packet, do this

 

No no.  All you need to do is send one integer.  That integer says "I will be performing this set of actions" (see: my packet handler, the integer being discussed is the one that goes into the switch statement).

 

In your case, that's all you need: an integer expressing which button was pressed.  If you had additional information that needed to be passed, you'd also have to write and read those values (for example, my code handles a lot of potion effects, so I need the potion ID, power, and duration).

 

And actually, you do have additional information to send:

The location of the tile entity.

This will require 4 more integers: dimension, X, Y, and Z.

 

You can then use DimensionManager.getDimension(dimID).getBlockTileEntity(X, Y, Z) in order to get a reference to the TE and tell it to do something.

 

(For sending, you can get the dim ID by world.provider.dimensionID)

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Yep.  Just build it one piece at a time, printing out data as you go until you're ready to start using it.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

That's twenty levels I think.  Try player.experience

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Let me see your code.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

package us.xvicario.soultether.handler;

import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.Player;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.DimensionManager;
import us.xvicario.soultether.RPGLevel;
import us.xvicario.soultether.TileEntityLevelUpStation;

public class PacketHandler
  implements IPacketHandler
{
  private EntityPlayer player;
  
  public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player)
  {
    this.player = ((EntityPlayer)player);
    if (packet.channel.equals("RPGLEVEL")) {
      handlePacket(packet, player);
    }
  }
  
  private void handlePacket(Packet250CustomPayload packet, Player player)
  {
    World world = this.player.worldObj;
    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(packet.data));
    try
    {
      int bookId = dis.readInt();
      int dimension = dis.readInt();
      int x = dis.readInt();
      int y = dis.readInt();
      int z = dis.readInt();
      
      TileEntityLevelUpStation te = (TileEntityLevelUpStation)DimensionManager.getWorld(dimension).getBlockTileEntity(x, y, z);
      ItemStack levelUpBook = new ItemStack(RPGLevel.itemLevelUpBook, 1, 0);
      te.setInventorySlotContents(0, levelUpBook);
      
      this.player.addExperienceLevel(-20);
    }
    catch (IOException e)
    {
      System.out.println("Failed to Read Packet");
    }
  }
}

Link to comment
Share on other sites

Add an output statement where that happens.  E.g.

 

System.out.println("Subtracting 20 levels");
this.player.addExperienceLevel(-20);

 

Check to see that it's running only once.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

It is running many many times.  ???

 

There's you're problem, then! :D

 

Now your challenge is to find a way to add a sanity check so it runs once.

(Hint: flags)

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.