Jump to content

Recommended Posts

Posted

Hi, i'm updating my mod and i have some problems with networking.

When i use my packet in a command to give points to the players it only changes in the client

 

 

public class Handler {
	private static final String PROTOCOL_VERSION = Integer.toString(1);
	public static SimpleChannel INSTANCE;
	
	public static void register()
	{
		INSTANCE = NetworkRegistry.ChannelBuilder
				.named(new ResourceLocation(Main.MODID, "main"))
				.clientAcceptedVersions(PROTOCOL_VERSION::equals)
				.serverAcceptedVersions(PROTOCOL_VERSION::equals)
				.networkProtocolVersion(() -> PROTOCOL_VERSION)
				.simpleChannel();
		
		int id = 0;
		
		INSTANCE.messageBuilder(PacketPoints.class, id++)
		.decoder(PacketPoints::new)
		.encoder(PacketPoints::encode)
		.consumer(PacketPoints::handle)
		.add();
	
		}
}

PacketPoints message:

public class PacketPoints {
	
	private int quantity;
	private UUID playerUuid;

	public PacketPoints(){}
	public PacketPoints(PacketBuffer buf) {
	    this.quantity = buf.readInt();
	    this.player = buf.readUniqueId();
	}
 	 
	public PacketPoints(int pQuantity, UUID pPlayerUuid) {
	    this.quantity = pQuantity;
	    this.playerUuid = pPlayerUuid;

	}

    public void encode(PacketBuffer buf) {
        buf.writeInt(quantity);
        buf.writeUniqueId(playerUuid);
    }

    public void handle(Supplier<NetworkEvent.Context> context) {
    	context.get().enqueueWork(() -> {
	        PlayerEntity player = Minecraft.getInstance().player.world.getPlayerByUuid(playerUuid);
	        if(player != null) {
	        	IPlayerData cap = PlayerData.getPlayerData(player);
	        	cap.setPoints(quantity);
	        }
	    });
    	context.get().setPacketHandled(true);
    }
}

Main class:

    private void setup(final FMLCommonSetupEvent event)
    {
        registerCapabilities();
        Handler.register();
    }

 

Posted

Show where you are sending your packet (the command).

If you send the packet to the client, it will be handled on the client, so the server won't get updated.

Posted (edited)
27 minutes ago, Alpvax said:

Show where you are sending your packet (the command).

If you send the packet to the client, it will be handled on the client, so the server won't get updated.

public class PointsCommand {
	
	public static ArgumentBuilder<CommandSource, ?> register(CommandDispatcher<CommandSource> dispatcher){
		return Commands.literal("points")
					   .requires(cs -> cs.hasPermissionLevel(3))
					   .executes(source -> { return run(null, 0);})
					   .then(Commands.argument("target", EntityArgument.player())
					   .executes(source -> { return run(EntityArgument.getPlayer(source, "target"), 0);})
					   .then(Commands.argument("quantity", IntegerArgumentType.integer())
					   .executes(source -> { return run(EntityArgument.getPlayer(source, "target"), IntegerArgumentType.getInteger(source, "quantity")); }))));
	}
	

	public static int run(PlayerEntity pPlayer, int pQuantity) {
		if(pPlayer != null) {
			if(Integer.class.isInstance(pQuantity)) {
				Handler.INSTANCE.sendToServer(new PacketPoints(pQuantity, pPlayer.getUniqueID()));
				return 0;
			} else {
				Minecraft.getInstance().player.sendMessage(new TranslationTextComponent("gui.error.1"));
				return 0;
			}
		} else {
			Minecraft.getInstance().player.sendMessage(new TranslationTextComponent("gui.error.2"));
			return 0;
		}
	}

}

 

Edited by Narlecks
Posted

Oh dear, I can see multiple issues with your code:

What on earth are you trying to achieve with this?

2 minutes ago, Narlecks said:

if(Integer.class.isInstance(pQuantity))

 

This will never work. If you are on the server (i.e. trying to send a message to the client), you CANNOT use Minecraft.getInstance().

2 minutes ago, Narlecks said:

Minecraft.getInstance().player.sendMessage(...);

 

Also, I'm pretty sure most (if not all, I haven't looked in a while) commands run on the server, so you shouldn't need to send packets (unless you need to update the client(s)).

Please show where PointsCommand::register is called (just to check that it is being registered on the server).

Posted
6 minutes ago, Alpvax said:

Oh dear, I can see multiple issues with your code:

What on earth are you trying to achieve with this?

 

This will never work. If you are on the server (i.e. trying to send a message to the client), you CANNOT use Minecraft.getInstance().

 

Also, I'm pretty sure most (if not all, I haven't looked in a while) commands run on the server, so you shouldn't need to send packets (unless you need to update the client(s)).

Please show where PointsCommand::register is called (just to check that it is being registered on the server).

Ok i see, i use a commandhandler bc i want to put different subcommands in one.

CommandHandler:

public class CommandHandler {

	public static void register(CommandDispatcher<CommandSource> dispatcher) {
		LiteralCommandNode<CommandSource> command = dispatcher.register(
				Commands.literal(Main.MODID)
						.then(PointsCommand.register(dispatcher))
		);
		dispatcher.register(Commands.literal(Main.MODID).redirect(command));
	}
}

and i call it on my main class

    @SubscribeEvent
    public void onServerStarting(FMLServerStartingEvent event) {
    	CommandHandler.register(event.getCommandDispatcher());

    }

 

Posted
12 minutes ago, Alpvax said:

Oh dear, I can see multiple issues with your code:

What on earth are you trying to achieve with this?

 

This will never work. If you are on the server (i.e. trying to send a message to the client), you CANNOT use Minecraft.getInstance().

 

Also, I'm pretty sure most (if not all, I haven't looked in a while) commands run on the server, so you shouldn't need to send packets (unless you need to update the client(s)).

Please show where PointsCommand::register is called (just to check that it is being registered on the server).

If u want a full view of my main class:

@Mod("dpz")
public class Main
{
    public static final String MODID = "dpz";
    public static final String NAME = "Update test";
    public static final String VERSION = "1.0";

    public Main() {

        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
        FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);
        MinecraftForge.EVENT_BUS.register(this);
    }

    private void setup(final FMLCommonSetupEvent event)
    {
        registerCapabilities();
        Handler.register();
        MinecraftForge.EVENT_BUS.register(new ServerEvents());
    }

    private void doClientStuff(final FMLClientSetupEvent event) {

		MinecraftForge.EVENT_BUS.register(new Hud(null));
    }


    @SubscribeEvent
    public void onServerStarting(FMLServerStartingEvent event) {
    	CommandHandler.register(event.getCommandDispatcher());

    }

	public void registerCapabilities() {
		CapabilityManager.INSTANCE.register(IPlayerData.class, new Storage<IPlayerData>(), PlayerData::new);
	    MinecraftForge.EVENT_BUS.register(new Capabilities());
	}
}

 

Posted
3 minutes ago, Narlecks said:

LiteralCommandNode<CommandSource> command = dispatcher.register( Commands.literal(Main.MODID) .then(PointsCommand.register(dispatcher)) ); dispatcher.register(Commands.literal(Main.MODID).redirect(command));

Is this what you actually meant to do? Just register the command you have created, instead of redirecting it to itself.

 

5 minutes ago, Narlecks said:

public void onServerStarting(FMLServerStartingEvent event) { CommandHandler.register(event.getCommandDispatcher());

Yup, you are registering it on the server, so any packets you send need to be from the server to the client (HINT: you have the correct client to send to passed into your 'run' function).

 

Perform your logic directly in the 'run' function of your command, and if you need it on the client, send a packet to just update the client with the new value (don't do any calculation on the client, do it on the server and send the result).

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.