Jump to content

Client/Server Containers


zenmogwai

Recommended Posts

I'm learning how to code for Minecraft so I'm following tutorials and trying to understand every method. The code below is from http://modwiki.temporal-reality.com/mw/index.php/Main_Page

 

I noticed that the container is created twice in the methods, once server side and once client side. Is it supposed to be created twice or is it preferable to create it only once?  Line 19 creates a new TestContainer server side. Then line 30 creates another new TestContainer client side.

 

The author says you need both containers - one client and one server.  Why?  It is possible for me to pass the TestContainer created from the server method down to the client method and then only create one container.  Then the client gets the GUI and the server gets the container.

 

Both approaches (one or two containers) don't appear to cause any errors. Which is preferable? Is there a reason why you would want to create only one or to specifically create two containers?  I'm just trying to understand how the code works.

 

12 public class GuiProxy implements IGuiHandler { 
13 
14     @Override 
15     public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { 
16         BlockPos pos = new BlockPos(x, y, z); 
17         TileEntity te = world.getTileEntity(pos); 
18         if (te instanceof TestContainerTileEntity) { 
19             return new TestContainer(player.inventory, (TestContainerTileEntity) te); 
20         } 
21         return null; 
22     } 
23 
24     @Override 
25     public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { 
26         BlockPos pos = new BlockPos(x, y, z); 
27         TileEntity te = world.getTileEntity(pos); 
28         if (te instanceof TestContainerTileEntity) { 
29             TestContainerTileEntity containerTileEntity = (TestContainerTileEntity) te; 
30             return new TestContainerGui(containerTileEntity, new TestContainer(player.inventory, containerTileEntity)); 
31         } 
32         return null; 
33     }

 

Link to comment
Share on other sites

The method's do not create the "same" thing.

 

First and formost:

The server only handles the Container. The container, is in essence, simply a list of boxes, and what they contain.

The client handles the GUI. This is where the boxes from the Container are drawn.  The client never touches the container directly. Anything you do inside a GUI, is sent to the server through packets. Think of it as a computer & screen. You interact with the screen, to change something in the computer. You don't manually open the harddrive and somehow magically set the correct state in the harddrive.

 

In the code you showed:

Line 19:

return new [b]TestContainer[/b](player.inventory, (TestContainerTileEntity) te); 

Line 30:

return new [b]TestContainerGui[/b](containerTileEntity, new TestContainer(player.inventory, containerTileEntity)); 

They return different things, if quite badly named, for the appropriate side.

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Link to comment
Share on other sites

Thanks, that's kind of my understanding as well - which is kind of why I ask the question.

 

Maybe I totally misunderstand the concepts but the TestContainerGui creates a "new" TestContainer as an argument.  So even though it only needs to return a GUI, it still goes through all the code to create an additional container.

 

The server code creates and returns a new TestContainer.  Then the Client code creates another new TestContainer as an argument that then gets passed to the TestContainerGui.  So all the code to create a TestContainer gets called twice by both methods.

 

Would it be better to output the TestContainer created by the server code and use it as the argument for the TestContainerGui rather than create a new container inside the TestContainerGui?  Or does it just not even matter?

 

I only noticed this because I put some tracers in the methods and I can see that all the TestContainer code gets called twice every time a new container is created - once by the server and then again by the client.

Link to comment
Share on other sites

Here is what I mean.  I create a field to hold the TestContainer created by the server and pass it to the client to create the GUI.  This results in the TestContainer code only being called once.

 

Again this may not matter but I just want to make sure I understand what is happening and why.

 

public static TestContainer serverContainer;

 

    @Override

    public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {

        BlockPos pos = new BlockPos(x, y, z);

        TileEntity te = world.getTileEntity(pos);

        if (te instanceof TestContainerTileEntity) {

        serverContainer = new TestContainer(player.inventory, (TestContainerTileEntity) te, world);

            return serverContainer;

        }

        return null;

    }

   

    @Override

    public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {

    BlockPos pos = new BlockPos(x, y, z);

        TileEntity te = world.getTileEntity(pos);

        if (te instanceof TestContainerTileEntity) {

            TestContainerTileEntity containerTileEntity = (TestContainerTileEntity) te;

            return new TestContainerGui((TestContainerTileEntity)te, serverContainer);

        }

        return null;

    }

Link to comment
Share on other sites

Yes, because the GUI portion still needs to know what the slots are which is defined in the Container (the "server" half).

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.



×
×
  • Create New...

Important Information

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