Jump to content

[SOLVED] [1.12.2] Every 100 packets there's a memory leak, relogging doesn't sync player capabilities


FlashHUN

Recommended Posts

I'm working on a mod where I have to sync player capabilities from the server to the client and vice versa quite often, but every 100th packet sent causes a memory leak causing the client to not get the data from the server. Also when I exit a singleplayer world and enter it again (haven't tested on servers yet, it's probably the same though) the data from the server doesn't get synced with the client. What could be causing this?

Leak message:

Quote

[16:24:29] [Netty Local Client IO #1/FATAL] [FML]: Detected ongoing potential memory leak. 100 packets have leaked. Top offenders
[16:24:29] [Netty Local Client IO #1/FATAL] [FML]:      fnm : 100

PacketMana (all packets are set up this way):

import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraftforge.fml.common.FMLCommonHandler;
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 PacketMana implements IMessage {
	EntityPlayerMP player;
    private int data;

    @Override
    public void fromBytes(ByteBuf buf) {
        IMana mana = Minecraft.getMinecraft().player.getCapability(CapabilityMana.MANA_CAPABILITY, null);
        data = mana.setMana(buf.readInt());
    }

    @Override
    public void toBytes(ByteBuf buf) {
        IMana mana = Minecraft.getMinecraft().player.getCapability(CapabilityMana.MANA_CAPABILITY, null);
        buf.writeInt(mana.getMana());
    }

    public PacketMana() {
        IMana mana = Minecraft.getMinecraft().player.getCapability(CapabilityMana.MANA_CAPABILITY, null);
        data = mana.getMana();
    }
    
    public PacketMana(EntityPlayerMP player) {
    	this.player = player;
        IMana mana = player.getCapability(CapabilityMana.MANA_CAPABILITY, null);
        data = mana.getMana();
    }

    public static class Handler implements IMessageHandler<PacketMana, IMessage> {
        @Override
        public IMessage onMessage(PacketMana message, MessageContext ctx) {
            FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx));
            return null;
        }

        private void handle(PacketMana message, MessageContext ctx) {
            EntityPlayerMP playerEntity = ctx.getServerHandler().player;
            playerEntity.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(message.data);
        }
    }
}

 

Events for syncing data (sync(player) is a function that sends all packets to the player):

	@SubscribeEvent
    public static void loginEvent(final PlayerLoggedInEvent event) {
    	EntityPlayerMP player = (EntityPlayerMP)event.player;
    	Minecraft.getMinecraft().addScheduledTask(() -> sync(player));
    	System.out.println("Synced");
    }
    
    @SubscribeEvent
    public static void changeDimesionEvent(final PlayerChangedDimensionEvent event) {
    	EntityPlayerMP player = (EntityPlayerMP)event.player;
    	Minecraft.getMinecraft().addScheduledTask(() -> sync(player));
    	System.out.println("Synced");
    }
    
    @SubscribeEvent
    public static void respawnEvent(final PlayerRespawnEvent event) {
    	EntityPlayerMP player = (EntityPlayerMP)event.player;
    	Minecraft.getMinecraft().addScheduledTask(() -> sync(player));
    	System.out.println("Synced");
    }
    }

 

Player Clone Event:

	@SubscribeEvent
    public static void playerClone(final PlayerEvent.Clone event) {
        final IMana oldMana = getMana(event.getOriginal());
        final IMana newMana = getMana(event.getEntityPlayer());

        if (newMana != null && oldMana != null) {
            newMana.setMaxMana(oldMana.getMaxMana());
            newMana.setManaTimer(oldMana.getManaTimer());
            newMana.setMana(oldMana.getMana());
            
            newMana.setClan(oldMana.getClan());
            newMana.setLand(oldMana.getLand());
            newMana.setAffiliation(oldMana.getAffiliation());

            newMana.setGenjutsu(oldMana.getGenjutsu());
            newMana.setTaijutsu(oldMana.getTaijutsu());
            newMana.setKenjutsu(oldMana.getKenjutsu());

            newMana.setSharingan(oldMana.getSharingan());
            newMana.setSharinganSize(oldMana.getSharinganSize());
            newMana.setSharinganPattern(oldMana.getSharinganPattern());
            newMana.setSharinganActive(oldMana.getSharinganActive());
            newMana.setByakugan(oldMana.getByakugan());
            newMana.setByakuganSize(oldMana.getByakuganSize());
            newMana.setByakuganActive(oldMana.getByakuganActive());
            newMana.setKetsuryuugan(oldMana.getKetsuryuugan());
            newMana.setKetsuryuuganActive(oldMana.getKetsuryuuganActive());
            
            newMana.setCurseMark(oldMana.getCurseMark());
            newMana.setCurseMarkSize(oldMana.getCurseMarkSize());
            newMana.setCurseMarkType(oldMana.getCurseMarkType());
            newMana.setCurseMarkActive(oldMana.getCurseMarkActive());
            
            newMana.setFireRelease(oldMana.getFireRelease());
            newMana.setWindRelease(oldMana.getWindRelease());
            newMana.setWaterRelease(oldMana.getWaterRelease());
            newMana.setLightningRelease(oldMana.getLightningRelease());
            newMana.setEarthRelease(oldMana.getEarthRelease());
            newMana.setWoodRelease(oldMana.getWoodRelease());
            newMana.setYinRelease(oldMana.getYinRelease());
            newMana.setYangRelease(oldMana.getYangRelease());
            newMana.setIceRelease(oldMana.getIceRelease());

            newMana.setShikotsumyaku(oldMana.getShikotsumyaku());

        	Minecraft.getMinecraft().addScheduledTask(() -> sync((EntityPlayerMP)event.getEntityPlayer()));
        }
        System.out.println("Player Clone Event Successful");
    }

 

Edited by FlashHUN
marked as solved
Link to comment
Share on other sites

1 hour ago, diesieben07 said:

Your code shows a complete lack of understanding about sides. Read and understand the documentation.

  • Your packet needs to be sent from server to client. Not client to server.
  • Accessing the capability on the client player every time in the packet makes no sense. You need to grab the data from the server side player and send it to the client.
  • Do not send packets in PlayerEvent.Clone. 

What if I need to edit player capability data through a GUI? How would I get the server side player in a GUI and edit their server side capabilities through the GUI? Also, when I don't send packets in the PlayerEvent.Clone the capabilities don't sync between the client and server.

Link to comment
Share on other sites

6 hours ago, FlashHUN said:

What if I need to edit player capability data through a GUI? How would I get the server side player in a GUI and edit their server side capabilities through the GUI?

You don't. When the data changes on in the GUI, you send a packet from the client to the server containing the new information. The server will receive that packet and pass it to the packet handler together with the player, where you can verify if the data is correct (to prevent cheating) and set the capability data.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

11 hours ago, larsgerrits said:

You don't. When the data changes on in the GUI, you send a packet from the client to the server containing the new information. The server will receive that packet and pass it to the packet handler together with the player, where you can verify if the data is correct (to prevent cheating) and set the capability data.

Alright, I was just confused because diesieben said

19 hours ago, diesieben07 said:

Your packet needs to be sent from server to client. Not client to server

 

Anyways, I still don't know why the memory leak is happening and why the player data doesn't sync when the player relogs.

Link to comment
Share on other sites

I haven't updated anything because I don't know what to update and how. Like I said, if I don't send packets in PlayerEvent.Clone, then the stuff doesn't get synced from the server to the client when the event happens. I need to be able to access data from the client side player to be able to send it to the server from the GUI. What should I do?

Link to comment
Share on other sites

I understand what I should be doing now, I just don't know how and can't really find anything on it. Sorry for making you waste your time on me like this.

How can I get the data only from the server side player in the events?

How can I apply the data to the client player in the client proxy? Just simply send packets to them?

If I don't access the client side player in the IMessage, should I do this in the packets, or am I just a completely lost cause: (Sorry for not putting it in code blocks but it wouldn't load)

Spoiler

public class PacketMana implements IMessage {
    @SidedProxy(clientSide = References.client_proxy_class, serverSide = References.common_proxy_class)
    static CommonProxy proxy;
    static MessageContext ctx;
    static EntityPlayer player = proxy.getPlayerEntityFromContext(ctx);
    IMana mana = player.getCapability(CapabilityMana.MANA_CAPABILITY, null);
    private int data;

 

    @Override
    public void fromBytes(ByteBuf buf) {
        data = mana.setMana(buf.readInt());
    }

 

    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeInt(mana.getMana());
    }


    public PacketMana(EntityPlayerMP player) {
        this.player = player;
        data = mana.getMana();
    }

 

    public static class Handler implements IMessageHandler<PacketMana, IMessage> {
        @Override
        public IMessage onMessage(PacketMana message, MessageContext ctx) {
            FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx));
            return null;
        }

 

        private void handle(PacketMana message, MessageContext ctx) {
            EntityPlayerMP playerEntity = (EntityPlayerMP)player;
            playerEntity.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(message.data);
        }
    }
}

 

CommonProxy:

Spoiler

public class CommonProxy {

    public void registerItemRenderer(Item item, int meta, String id) {}
    
    public EntityPlayer getPlayerEntityFromContext(MessageContext ctx)
    {
        return ctx.getServerHandler().player;
    }
    
    public void handleData() {}
}

 

ClientProxy:

Spoiler

public class ClientProxy extends CommonProxy {

    public void registerItemRenderer(Item item, int meta, String id) {
        ModelLoader.setCustomModelResourceLocation(item,  meta,  new ModelResourceLocation(item.getRegistryName(), id));
    }
    
    public EntityPlayer getPlayerEntity(MessageContext ctx)
    {
        return (ctx.side.isClient() ? Minecraft.getMinecraft().player : ctx.getServerHandler().player);
    }
    
    public void handleData() {
        
    }
}

 

Link to comment
Share on other sites

Should the packet look something like this then? (I don't really know how else to do it without the @SidedProxy)

public class PacketMana implements IMessage {
	@SidedProxy(clientSide = References.client_proxy_class, serverSide = References.common_proxy_class)
	static CommonProxy proxy;
    MessageContext ctx;
    private int data;

    @Override
    public void fromBytes(ByteBuf buf) {
    	EntityPlayer player = proxy.getPlayerEntityFromContext(ctx);
        data = player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(buf.readInt());
    }

    @Override
    public void toBytes(ByteBuf buf) {
    	EntityPlayer player = proxy.getPlayerEntityFromContext(ctx);
        buf.writeInt(player.getCapability(CapabilityMana.MANA_CAPABILITY, null).getMana());
    }

    public PacketMana() {
    	EntityPlayer player = proxy.getPlayerEntityFromContext(ctx);
        data = player.getCapability(CapabilityMana.MANA_CAPABILITY, null).getMana();
    }
    
    public PacketMana(EntityPlayer player) {
        data = player.getCapability(CapabilityMana.MANA_CAPABILITY, null).getMana();
    }

    public static class Handler implements IMessageHandler<PacketMana, IMessage> {
        @Override
        public IMessage onMessage(PacketMana message, MessageContext ctx) {
            FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx));
            return null;
        }

        private void handle(PacketMana message, MessageContext ctx) {
            EntityPlayerMP playerEntity = ctx.getServerHandler().player;
            playerEntity.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(message.data);
        }
    }
}

 

I need all packets in my mod to be bi-directional.

 

Events for syncing:

    private static void sync(EntityPlayer player) {
    	player.getCapability(MANA_CAPABILITY, null);
    	PacketHandler.INSTANCE.sendTo(new PacketMana(player), (EntityPlayerMP)player);
    }

    @SubscribeEvent
    public static void loginEvent(final PlayerLoggedInEvent event) {
    	EntityPlayer player = event.player;
    	Minecraft.getMinecraft().addScheduledTask(() -> sync(player));
    	System.out.println("Synced");
    }
    
    @SubscribeEvent
    public static void changeDimesionEvent(final PlayerChangedDimensionEvent event) {
    	EntityPlayer player = event.player;
    	Minecraft.getMinecraft().addScheduledTask(() -> sync(player));
    	System.out.println("Synced");
    }
    
    @SubscribeEvent
    public static void respawnEvent(final PlayerRespawnEvent event) {
    	EntityPlayer player = event.player;
    	Minecraft.getMinecraft().addScheduledTask(() -> sync(player));
    	System.out.println("Synced");
    }
    }

 

Should the handle method in my ClientProxy look like this? Or does casting EntityPlayerMP to EntityPlayer in the ClientProxy cause any problems?

    public void handleData(MessageContext ctx) {
		EntityPlayer player = Minecraft.getMinecraft().player;
		player.getCapability(CapabilityMana.MANA_CAPABILITY, null);
		
		PacketHandler.INSTANCE.sendTo(new PacketMana(player), (EntityPlayerMP)player);
	}

 

How do I pass the data from the received data from PacketHandler to the proxy?

Link to comment
Share on other sites

So, should it look something like this? (At least am I getting closer to how it should look like?)

public class PacketMana implements IMessage {
    private int data;

    public PacketMana() {}
    
    public PacketMana(EntityPlayer player) {
        data = player.getCapability(CapabilityMana.MANA_CAPABILITY, null).getMana();
    }
    
    @Override
    public void fromBytes(ByteBuf buf) {
    	data = buf.readInt();
    }

    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeInt(data);
    }

    public static class Handler implements IMessageHandler<PacketMana, IMessage> {
        @Override
        public IMessage onMessage(PacketMana message, MessageContext ctx) {
            FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx));
            return null;
        }

        private void handle(PacketMana message, MessageContext ctx) {
            EntityPlayer playerEntity = ctx.getServerHandler().player;
            playerEntity.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(message.data);
        }
    }
}

Should the

EntityPlayer playerEntity = ctx.getServerHandler().player;

be something else?

 

If I do

    @SubscribeEvent
    public static void loginEvent(final PlayerLoggedInEvent event) {
    	EntityPlayer player = event.player;
    	PacketHandler.INSTANCE.sendTo(new PacketMana(player), (EntityPlayerMP)player);
    	System.out.println("Synced");
    }
    
    @SubscribeEvent
    public static void changeDimesionEvent(final PlayerChangedDimensionEvent event) {
    	EntityPlayer player = event.player;
    	PacketHandler.INSTANCE.sendTo(new PacketMana(player), (EntityPlayerMP)player);
    	System.out.println("Synced");
    }
    
    @SubscribeEvent
    public static void respawnEvent(final PlayerRespawnEvent event) {
    	EntityPlayer player = event.player;
    	PacketHandler.INSTANCE.sendTo(new PacketMana(player), (EntityPlayerMP)player);
    	System.out.println("Synced");
    }

the EntityPlayer hasn't constructed yet in the PlayerLoggedInEvent since it fires a tick before that happens.

 

Should I just do

    public void handleData(MessageContext ctx) {
		EntityPlayer player = Minecraft.getMinecraft().player;
		player.getCapability(CapabilityMana.MANA_CAPABILITY, null);
	}

in the ClientProxy?

Link to comment
Share on other sites

1 hour ago, diesieben07 said:

Yes, except you are still using the server player in the handle method. In that method you need to actually call the method in your proxy, which needs to take the data as an argument and then apply the data to the client-side player (like you do now in handle with the server-side player).

So, then the packet should look like this?

public class PacketMana implements IMessage {
    static CommonProxy proxy;
    private int mana;

    public PacketMana() {}
    
    public PacketMana(EntityPlayer player) {
        mana = player.getCapability(CapabilityMana.MANA_CAPABILITY, null).getMana();
    }
    
    @Override
    public void fromBytes(ByteBuf buf) {
    	mana = buf.readInt();
    }

    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeInt(mana);
    }

    public static class Handler implements IMessageHandler<PacketMana, IMessage> {
        @Override
        public IMessage onMessage(PacketMana message, MessageContext ctx) {
            FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx));
            return null;
        }

        private void handle(PacketMana message, MessageContext ctx) {
        	proxy.handleMana(mana);
        }
    }
}

 

The problem then is that the

proxy.handleMana(mana);

method asks for the

mana

int to be static. Is that fine?

 

handleMana() in CommonProxy:

public void handleMana(int mana) {}

 

handleMana() in ClientProxy:

    public void handleMana(int mana) {
    	EntityPlayer player = Minecraft.getMinecraft().player;
    	player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(mana);
    }

 

 

1 hour ago, diesieben07 said:
1 hour ago, FlashHUN said:

the EntityPlayer hasn't constructed yet in the PlayerLoggedInEvent since it fires a tick before that happens.

This is not true. 

Odd. When I did it that way the data didn't sync unless I did it through the scheduled task way.

Link to comment
Share on other sites

Alright, this is how it looks now:

 

PacketHandler:

public class PacketHandler 
{
	
	 private static int packetId = 0;

	    public static SimpleNetworkWrapper INSTANCE = null;

	    public PacketHandler() {
	    }

	    public static int nextID() {
	        return packetId++;
	    }

	    public static void registerMessages(String channelName) {
	        INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(channelName);
	        registerMessages();	
	    }

	    public static void registerMessages() {
	        INSTANCE.registerMessage(PacketMana.Handler.class, PacketMana.class, nextID(), Side.SERVER);
	    }
}

 

PacketMana:

public class PacketMana implements IMessage {
	static CommonProxy proxy;
    private int mana;

    public PacketMana() {}
    
    public PacketMana(EntityPlayer player) {
        mana = player.getCapability(CapabilityMana.MANA_CAPABILITY, null).getMana();
    }
    
    @Override
    public void fromBytes(ByteBuf buf) {
    	mana = buf.readInt();
    }

    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeInt(mana);
    }

    public static class Handler implements IMessageHandler<PacketMana, IMessage> {
        @Override
        public IMessage onMessage(PacketMana message, MessageContext ctx) {
            FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx));
            return null;
        }

        private void handle(PacketMana message, MessageContext ctx) {
        	proxy.handleMana(message.mana);
        }
    }
}

 

ClientProxy:

public class ClientProxy extends CommonProxy {

	public void registerItemRenderer(Item item, int meta, String id) {
		ModelLoader.setCustomModelResourceLocation(item,  meta,  new ModelResourceLocation(item.getRegistryName(), id));
	}
	
	public EntityPlayer getPlayerEntityFromContext(MessageContext ctx)
	{
		return Minecraft.getMinecraft().player;
	}
	
	public void handleMana(int mana) {
		EntityPlayer player = Minecraft.getMinecraft().player;
		player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(mana);
	}
}

 

CommonProxy:

public class CommonProxy {

    public void registerItemRenderer(Item item, int meta, String id) {}
	
    public EntityPlayer getPlayerEntityFromContext(MessageContext ctx)
    {
        return ctx.getServerHandler().player;
    }
	
	public void handleMana(int mana) {}
}

 

CapabilityMana:

public class CapabilityMana {
	
    @CapabilityInject(IMana.class)
    public static final Capability<IMana> MANA_CAPABILITY = null;

    public static final EnumFacing DEFAULT_FACING = null;

    public static final ResourceLocation ID = new ResourceLocation(References.mod_id, "Mana");

    public static void register(){
        CapabilityManager.INSTANCE.register(IMana.class, new Capability.IStorage<IMana>() {
            @Override
            public NBTBase writeNBT(Capability<IMana> capability, IMana instance, EnumFacing side) {
                NBTTagCompound nbt = new NBTTagCompound();
                nbt.setInteger("mana", instance.getMana());
                
                System.out.println("Writing");
                return nbt;
            }

            @Override
            public void readNBT(Capability<IMana> capability, IMana instance, EnumFacing side, NBTBase nbt) {
                instance.setMana(((NBTTagCompound) nbt).getInteger("mana"));
                
                System.out.println("Reading");
            }
        }, () -> new Mana());
    }

    @Nullable
    public static IMana getMana(final EntityLivingBase entity){
        return CapabilityUtils.getCapability(entity, MANA_CAPABILITY, DEFAULT_FACING);

    }

    public static ICapabilityProvider createProvider(final IMana mana){
        return new CapabilityProviderSerializable<>(MANA_CAPABILITY, DEFAULT_FACING, mana);
    }	

    @Mod.EventBusSubscriber(modid = References.mod_id)
    private static class EventHandler {@SubscribeEvent
    public static void attachCapabilities(final AttachCapabilitiesEvent<Entity> event) {
        if (event.getObject() instanceof EntityPlayer) {
            final Mana mana = new Mana();
            event.addCapability(ID, createProvider(mana));
        }
    }

    @SubscribeEvent
    public static void playerClone(final PlayerEvent.Clone event) {
        final IMana oldMana = getMana(event.getOriginal());
        final IMana newMana = getMana(event.getEntityPlayer());

        if (newMana != null && oldMana != null) {
            newMana.setMana(oldMana.getMana());
            System.out.println("Player Clone Event Successful");
        }
    }
    
    @SubscribeEvent
    public static void onUpdate(final PlayerTickEvent event) {
    	final IMana mana = getMana(event.player);
    	if(event.player instanceof EntityPlayerMP){
    		mana.fillMana(1);
    		PacketHandler.INSTANCE.sendTo(new PacketMana(event.player), (EntityPlayerMP)event.player);
    	}
    }
    
    @SubscribeEvent
    public static void loginEvent(final PlayerLoggedInEvent event) {
    	EntityPlayer player = event.player;
    	PacketHandler.INSTANCE.sendTo(new PacketMana(player), (EntityPlayerMP)player);
    	System.out.println("Synced");
    }
    
    @SubscribeEvent
    public static void changeDimesionEvent(final PlayerChangedDimensionEvent event) {
    	EntityPlayer player = event.player;
    	PacketHandler.INSTANCE.sendTo(new PacketMana(player), (EntityPlayerMP)player);
    	System.out.println("Synced");
    }
    
    @SubscribeEvent
    public static void respawnEvent(final PlayerRespawnEvent event) {
    	EntityPlayer player = event.player;
    	PacketHandler.INSTANCE.sendTo(new PacketMana(player), (EntityPlayerMP)player);
    	System.out.println("Synced");
    }
    }
}

 

Is there anything else I should be doing? Since the memory leak still happens.

Link to comment
Share on other sites

54 minutes ago, diesieben07 said:
1 hour ago, FlashHUN said:

INSTANCE.registerMessage(PacketMana.Handler.class, PacketMana.class, nextID(), Side.SERVER);

No. This packet is sent to the client.

So, should I change the Side.SERVER to Side.CLIENT?

55 minutes ago, diesieben07 said:
1 hour ago, FlashHUN said:

static CommonProxy proxy;

This will always be null.

How can I make that not be null? Use @SidedProxy?

55 minutes ago, diesieben07 said:
1 hour ago, FlashHUN said:

public EntityPlayer getPlayerEntityFromContext(MessageContext ctx) { return Minecraft.getMinecraft().player; }

This is not correct on the client. You have to check if you are on the integrated server or not.

That is done with world.isRemote, right?

Link to comment
Share on other sites

19 minutes ago, diesieben07 said:

Yes.

 

You could do that. Or just remove this field and use the one you likely already have in your main mod class.

 

Usually yes, but in this case you do not have a World available, just the MessageContext. The MessageContext has a field side though, which you can check.

Thank you so much for all the help! I really appreciate it. Now I finally don't have memory leaks. I have two hopefully last questions though:

 

  • Can I make the ClientProxy method handle multiple capabilities?

For example instead of

    public void handleMana(int mana) {
    	EntityPlayer player = Minecraft.getMinecraft().player;
    	player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(mana);
    }

could I make it take a String too, and depending on what that is make it handle other capabilities? Something like this:

    public void handleMana(int data, String type) {
    	EntityPlayer player = Minecraft.getMinecraft().player;
    	if (type.equals("Mana") {
    		player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(data);
    	}
    	else if (type.equals("MaxMana") {
    		player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMaxMana(data);
    	}
    }

and then in PacketMana do:

Main.proxy.handleMana(message.mana, "Mana");

and in PacketMaxMana do:

Main.proxy.handleMana(message.mana, "MaxMana");

 

 

  • If I have to send one of these packets to the server, do I have to change anything? Like I said before, most of these packets need to be bi-directional since most of them could be edited through a GUI. Do I have to just copy the
handleMana()

in the ClientProxy to the CommonProxy so the data changes on both sides, or do I have to change something in the PacketHandler aswell, or even create a new packet that handles specifically receiving packets on the server?

Link to comment
Share on other sites

1 hour ago, diesieben07 said:

Yes, you could use a string as an identifier. But that is just... terrible.

Why is it terrible? I can see through it quite clearly that way. What would be a better way of doing it?

 

1 hour ago, diesieben07 said:

Also, if the values belong to the same capability, why not just send them in one packet?

I would have A LOT of different values. How could I make it so the packet differentiates which one I want changed?

 

1 hour ago, diesieben07 said:

The client-to-server packets mean "the player just entered this value on the GUI, do with that what you want". And then the server decides what to do with that information. Do not blindly trust the client. Assume the client is always lying. 

How could I do that? I would not like if players cheated, but how can I make the server decide what to do with said information?

Link to comment
Share on other sites

1 minute ago, diesieben07 said:

Do not let the client just send over the new value. Instead (for example) send over "player pressed button x". Then perform the action solely on the server.

If there are other restrictions, such as "must only be done every x seconds" enforce those on the server, not just the client.

So, for example, I have a GUI where a player can spend their acquired points in skills. If the player wants to do that, then when they click the button in the GUI to do so, a packet is sent where the data is that the button was pressed. Then how can I get that data from the packet, and in what type of method should I enforce the restrictions on the server?

Link to comment
Share on other sites

1 minute ago, diesieben07 said:

What data? You said it was just a button, so the fact that the packet arrives at all on the server is enough to signify that the button was pressed. If you have a text field that lets you choose how much to spend, that is what you would send in the packet. In the same way you send the mana value now.

Then on the server, in your packet handler, you can check that the player actually has enough points and if so, perform the "spend points" operation.

Then the data in the packet would equal something like GUISkillMenu.pointstospend in the constructor? How can I detect if a packet arrives on a server? Sorry for all of these stupid questions, but these anti cheating things are new to me.

Link to comment
Share on other sites

Just now, diesieben07 said:

Please, actually try and understand what these things are. You are just stabbing in the dark.

I'm trying to understand, but all of this was a bit too much information to handle at once after a tiring day for my brain.

Link to comment
Share on other sites

So, the packet that the client sends to the server should look something like this, right?

public class ClientMana implements IMessage {
    private int data;

    public ClientMana(int data) {
    	this.data = data;
    }
    
    @Override
    public void fromBytes(ByteBuf buf) {
    	data = buf.readInt();
    }

    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeInt(data);
    }

    public static class Handler implements IMessageHandler<ClientMana, IMessage> {
        @Override
        public IMessage onMessage(ClientMana message, MessageContext ctx) {
            FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx));
            return null;
        }

        private void handle(ClientMana message, MessageContext ctx) {
        	Main.proxy.handleServerMana(message.data, 1, ctx);
        }
    }
}

 

And the data would then get handled in the CommonProxy where this happens:

public class CommonProxy {

	public void registerItemRenderer(Item item, int meta, String id) {}
	
    public EntityPlayer getPlayerEntityFromContext(MessageContext ctx)
    {
        return ctx.getServerHandler().player;
    }

	public void handleMana(int mana, int packetType) {}

	public void handleManaB(boolean mana, int packetType) {}
	
	public void handleManaD(double mana, int packetType) {}
	
	public void handleServerMana(int mana, int packetType, MessageContext ctx) {
		EntityPlayer player = ctx.getServerHandler().player;
		switch (packetType) {
			case 1: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(mana); break;
			case 2: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMaxMana(mana); break;
		}
	}
}

 

And the ClientProxy looks like this:

public class ClientProxy extends CommonProxy {

	public void registerItemRenderer(Item item, int meta, String id) {
		ModelLoader.setCustomModelResourceLocation(item,  meta,  new ModelResourceLocation(item.getRegistryName(), id));
	}
	
	public EntityPlayer getPlayerEntityFromContext(MessageContext ctx)
	{
		return ctx.side.isClient() ? Minecraft.getMinecraft().player : ctx.getServerHandler().player;
	}
	
	public void handleMana(int mana, int packetType) {
		EntityPlayer player = Minecraft.getMinecraft().player;
		switch (packetType) {
			case 1: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMana(mana); break;
			case 2: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setMaxMana(mana); break;
			case 3: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setByakugan(mana); break;
			case 5: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setByakuganSize(mana); break;
			case 6: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setKetsuryuugan(mana); break;
			case 8: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setSharingan(mana); break;
			case 10: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setSharinganPattern(mana); break;
			case 11: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setSharinganSize(mana); break;
			case 12: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setCurseMark(mana); break;
			case 13: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setCurseMarkSize(mana); break;
			case 14: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setCurseMarkType(mana); break;
			case 15: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setShikotsumyaku(mana); break;
			case 16: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setEarthRelease(mana); break;
			case 17: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setFireRelease(mana); break;
			case 18: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setIceRelease(mana); break;
			case 19: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setLightningRelease(mana); break;
			case 20: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setWaterRelease(mana); break;
			case 21: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setWindRelease(mana); break;
			case 22: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setWoodRelease(mana); break;
			case 23: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setYangRelease(mana); break;
			case 24: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setYinRelease(mana); break;
			case 25: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setAffiliation(mana); break;
			case 26: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setClan(mana); break;
			case 27: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setLand(mana); break;
			case 28: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setGenjutsu(mana); break;
		}
	}

	public void handleManaB(boolean mana, int packetType) {
		EntityPlayer player = Minecraft.getMinecraft().player;
		switch (packetType) {
			case 4: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setByakuganActive(mana); break;
			case 7: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setKetsuryuuganActive(mana); break;
			case 9: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setSharinganActive(mana); break;
			case 13: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setCurseMarkActive(mana); break;
		}
		
	}
	
	public void handleManaD(double mana, int packetType) {
		EntityPlayer player = Minecraft.getMinecraft().player;
		switch (packetType) {
			case 29: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setKenjutsu(mana); break;
			case 30: player.getCapability(CapabilityMana.MANA_CAPABILITY, null).setTaijutsu(mana); break;
		}
	}
}

 

Edited by FlashHUN
Link to comment
Share on other sites

1 hour ago, diesieben07 said:

This packet is the exact thing you must not do prevent cheating. A malicious client can just send over "hey, please 1,000,000 mana!" and the server will go "okay!" and apply it.

Then how else should I do it? This is a packet that would be sent when the player first starts out and picks their base stats (they can pick 2 things through a GUI and those 2 things determine what stats you get at the start), so there aren't really any restrictions. For example, depending on the clan they choose at the start they might get more mana than you'd normally start out with.

Link to comment
Share on other sites

Just now, diesieben07 said:

Send over exactly what they select in the GUI. Do they select the amount of mana? No, they select their clan. So, send over the clan and then let the server decide how much mana that clan implies.

Otherwise the client decides, which means effectively every clan gets as much mana as they want. Which is not what you want.

Ah, thank you for clearing that up.

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.