Jump to content

Set client and server value of a tile_entity using packets[1.12.2]


Zeher_Monkey

Recommended Posts

So I have a method for setting information inside a tile_entity. When i set this information (String and ArrayList<String>) it sets it on the isRemote = false side so the server side. The gui needs to see the isRemote = true side to show the information. I have been trying to get the information from the server to the client but dont fully understand sending a server > client packet with this information. I have also tried to use IContainerListener as well but this cannot work with a String and certainly not with a ArrayList<String>. I have also tried to change where the method to set these values is called with no luck.

 

Gui Class:

[GuiPocketAllowedPlayers]

Spoiler

@Override
    protected void drawGuiContainerForegroundLayer(int x, int y) {
        int[] screen_coords = new int[] { (this.width - this.xSize) / 2, (this.height - this.ySize) / 2 };

        this.fontRenderer.drawString("Allowed Players", screen_coords[0] + 9, screen_coords[1] + 4, ModGuiUtil.DEFAULT_COLOUR_BACKGROUND);

        this.buttonList.clear();
        this.plus_button = this.addButton(new GuiIconButton(0, screen_coords[0] + 148, screen_coords[1] + 13, 18, 18, 1, true));
        this.minus_button = this.addButton(new GuiIconButton(1, screen_coords[0] + 168, screen_coords[1] + 13, 18, 18, 2, true));
        this.text_clear_button = this.addButton(new GuiIconButton(2, screen_coords[0] + 128, screen_coords[1] + 13, 18, 18, 3, !(this.text_field.getText().isEmpty())));
        
        this.text_field.drawTextBox();
        
        ModGuiUtil.FONT_LIST.DRAW.drawList(this, fontRenderer, screen_coords, 8, 59, tile.getPocket().getAllowedPlayers());
        ModGuiUtil.FONT.DRAW.drawStringUnformatted(fontRenderer, screen_coords[0] + 9, screen_coords[1] + 32, tile.getPocket().getCreator());
        
    }

 

 

The specific lines getting the information are:

tile.getPocket().getAllowedPlayers()

This returns an ArrayList<String>.

 

tile.getPocket().getCreator()

-- This returns a String.

 

This is where im actually setting the information:

[TileEntityDimensionalPocket]

Spoiler

@Override
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
        if (playerIn == null) {
            return true;
        } else if (!(playerIn.isSneaking()) && !ModUtil.isHoldingHammer(playerIn)) {
            if (!(worldIn.isRemote)) {
                String creator = this.getPocket().getCreator();
                String locked_comp;

                if (this.locked) {
                    locked_comp = TextUtil.RED + TextUtil.BOLD + "[Locked]";
                } else {
                    locked_comp = TextUtil.GREEN + TextUtil.BOLD + "[Unlocked]";
                }

                TextComponentString comp = new TextComponentString(TextUtil.TEAL + TextUtil.BOLD + "Pocket is owned by: [" + creator + "] " + locked_comp);
                playerIn.sendMessage(comp);
            }
            return true;

        } else if (!(playerIn.dimension == 1)) {
            if (playerIn.isSneaking() && !ModUtil.isHoldingHammer(playerIn)) {
                playerIn.swingArm(hand);

                if (this.getPocket().isGenerated()) {
                    if (this.getPocket().getCreator() == null) {
                        
                    }
                } else {
                    this.getPocket().setCreator(playerIn.getName());
                    NetworkHandler.sendPocketSetCreator(playerIn.getName(), pos);
                }

 

These lines set the information.

this.getPocket().setCreator(playerIn.getName());

NetworkHandler.sendPocketSetCreator(playerIn.getName(), pos);

 

Here is where the information is ultimately set:

[Pocket]

Spoiler

public void setCreator(String name) {
        this.creator = name;
        this.getNBT().setString(NBT_GENERATED_KEY, name);
        
        this.addAllowedPlayer(name);
    }

 

I want the information to be displayed so i can see a list of players and be able to add / remove them. The adding and removal is sorted, its just the display of them, especially after exiting and returning.

 

Basically i need to send a packet back to the client to update that information both server and client side. I have the server side done, but im lost on how to send that information to the client side of the tile entity.

Edited by Zeher_Monkey
Link to comment
Share on other sites

So that nbt string is setting the creator_name when saving to NBT.

this.getNBT().setString(NBT_GENERATED_KEY, name);

 

NBT_GENERATED_KEY is just an identifier to use on both sides of writing to and reading from NBT.

 

Basically i need to see data from the tile_entity in the gui. I have a container, but the methods inside container are not suitable for updating a String, and certainly not an ArrayList<String>. Those methods, such as updateProgressBar() etc are only suited to sending integer values. When i call the method to set the data, is called on the server side. I have tried various methods to set it on the client & server side to no avail. I have also tried a custom packet for the client side, but it does not set the client-side value. Unless the container can somehow update the values i need im stumped.

Link to comment
Share on other sites

Ah I see. I have removed this line. Here is my client packet:

Spoiler

package com.zeher.dimensionalpockets.network.core.packet;

import com.zeher.dimensionalpockets.core.util.DimLogger;
import com.zeher.dimensionalpockets.pocket.client.tileentity.TileEntityDimensionalPocket;

import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class PacketCreatorClient implements IMessage {

    private static String name;
    private static BlockPos pos;

    public PacketCreatorClient() { }

    public PacketCreatorClient(String name, BlockPos pos) {
        this.name = name;
        this.pos = pos;
    }

    @Override
    public void fromBytes(ByteBuf buf) { }

    @Override
    public void toBytes(ByteBuf buf) { }

    public static class Handler implements IMessageHandler<PacketCreatorClient, IMessage> {
        
        @SideOnly(Side.CLIENT)
        @Override
        public IMessage onMessage(final PacketCreatorClient message, final MessageContext ctx) {
            DimLogger.info("Client_Creator Packet sent. ID: [" + name + "] [" + ctx.side + "]");
            
            //NBTTagCompound tag = new NBTTagCompound();
            //tag.setString("name", name);
            //SPacketUpdateTileEntity packet = new SPacketUpdateTileEntity(pos, 0, tag);
            
            //ctx.getClientHandler().handleUpdateTileEntity(packet);
            
            World world = Minecraft.getMinecraft().world;
            TileEntity world_tile = world.getTileEntity(pos);
            
            if (world.isRemote) {
                ((TileEntityDimensionalPocket) world_tile).getPocket().setCreator(name);
            }
            if (world_tile != null) {
                if (world_tile instanceof TileEntityDimensionalPocket) {
                    if (!(name.isEmpty())) {
                        ((TileEntityDimensionalPocket) world_tile).getPocket().setCreator(name);
                    }
                }
            }
            return null;
        }
    }
}

And my server packet:

Spoiler

package com.zeher.dimensionalpockets.network.core.packet;

import com.zeher.dimensionalpockets.core.handler.NetworkHandler;
import com.zeher.dimensionalpockets.core.util.DimLogger;
import com.zeher.dimensionalpockets.pocket.client.tileentity.TileEntityDimensionalPocket;

import io.netty.buffer.ByteBuf;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class PacketCreatorServer implements IMessage {

    private static String name;
    private static BlockPos pos;

    public PacketCreatorServer() { }

    public PacketCreatorServer(String name, BlockPos pos) {
        this.name = name;
        this.pos = pos;
    }

    @Override
    public void fromBytes(ByteBuf buf) { }

    @Override
    public void toBytes(ByteBuf buf) { }

    public static class Handler implements IMessageHandler<PacketCreatorServer, IMessage> {

        @Override
        public IMessage onMessage(final PacketCreatorServer message, final MessageContext ctx) {
            TileEntity tile_offset = ctx.getServerHandler().player.world.getTileEntity(pos);

            DimLogger.info("Server_Creator Packet sent. ID: [" + name + "]");
            
            if (!(tile_offset == null)) {
                if (tile_offset instanceof TileEntityDimensionalPocket) {
                    if (name != null) {
                        ((TileEntityDimensionalPocket) tile_offset).getPocket().setCreator(name);
                        //NetworkHandler.sendCreatorPacketToClient(name, pos, tile);
                    }
                }
            }
            return null;
        }
    }
}

 

Link to comment
Share on other sites

8 minutes ago, Zeher_Monkey said:

Ah I see. I have removed this line. Here is my client packet:

That's not how packets work... you only need one packet. The server is the only one sending data the client just needs to react to that data accordingly. So your main problem is you don't do anything in the toBytes and fromBytes method. You need to actually write that data you want to send into bytes using the parameter you are given. And then read those bytes back into the data and store it in your instance fields.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Ok i have seen this in some tutorials. I'm again not sure how to use these functions. I cant see a way to send  a string or ArrayList<String> with these functions.

 

And once sent, how to i get the information from the fromBytes() into my tile_entity fields?

Edited by Zeher_Monkey
Link to comment
Share on other sites

9 minutes ago, Zeher_Monkey said:

 And once sent, how to i get the information from the fromBytes() into my tile_entity fields?

You don't you put it in your Packets instance fields in that method. Then in the handle method you use those instance fields in the packet to update your TileEntity fields.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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.