Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16] Synchronize player capability to all player


Recommended Posts

I think this is a question that has been asked many times, but it is difficult to understand, so I will ask it.

I want to update player A's capability when A presses a specific key. In addition, all players A, B, and C get the capability of each player and render them on the screen.

The keystrokes, capabilities, and rendering code are all implemented. Then use packets,
· Change the capability of A on the server when A pressed key
· When the A's capability of the server changes, change the A capability on the A, B, C clients
· Gets and renders the capability of all players on the server when rendered on the client.

I don't know how to do these, so please help me.

Link to post
Share on other sites
32 minutes ago, Zemelua said:

Change the capability of A on the server when A pressed key

Send the packet to the server, get the sending player. Check if it's okay to perform this action right now, then change the capability.

32 minutes ago, Zemelua said:

When the A's capability of the server changes, change the A capability on the A, B, C clients

Your packet needs to contain firstly the entityID for the player for which the data is. Then:

  • In PlayerLoggedInEvent, PlayerRespawnEvent and PlayerChangedDimensionEvent send the player their own capability data.
  • In PlayerEvent.StartTracking check if PlayerEvent.StartTracking#getTarget is a player. If it is, send the data of PlayerEvent.StartTracking#getTarget to PlayerEvent#getPlayer.
  • Whenever the data changes send it to the player and all tracking players using PacketDistributor.TRACKING_ENTITY_AND_SELF.

For general info about the packet system check the documentation.

Link to post
Share on other sites
2 hours ago, diesieben07 said:

Whenever the data changes send it to the player and all tracking players using PacketDistributor.TRACKING_ENTITY_AND_SELF.

Does it mean to send a packet(server-> tracking and self) within the handle of packet(player A-> server)? Then how do I get player A in the handle of the packet(server-> Tracking and self) ?

Edited by Zemelua
Link to post
Share on other sites
1 minute ago, Zemelua said:

Does it mean to send a packet(server-> tracking and self) within the handle of packet(player A-> server)?

No, your capability should do this automatically whenever you update it. Some pseudocode (in your capability):

class YourCap {
    private final PlayerEntity player;
    
    YourCap(PlayerEntity player) {
        this.player = player;
    }

    void setValue(foo) {
        this.foo = foo;
    }

    void syncChanges() {
        // send to tracking and self here
    }
}

 

Link to post
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.

Guest
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.



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • I'm creating a gui (container-less) and for the sake of visual simplicity, I'd like background screen components like the healthbar and hotbar to go away while this screen is open, like how pressing alt+f1 declutters the screen.  Anyone know how I can do this? 
    • Hi forum, I have a problem with forge 1.17.1  Every time i try to start version forge 1.17.1 it says: Game crasht Can Someone help me please Kind regards, Sven
    • I don't even have Optifine installed and the game doesn't start, it crashes right away
    • public enum ModArmorMaterial implements IArmorMaterial { ARGENTITE(Alcamod.ModID + ":argentite", 40, new int[] { 3, 6, 8, 3 }, 12, SoundEvents.ITEM_ARMOR_EQUIP_GENERIC, 3.5F, 0.15F, () -> { return Ingredient.fromItems(ModItems.ARGENTITE_INGOT.get()); }), AZURITE(Alcamod.ModID + ":azurite", 45, new int[] { 3, 6, 8, 3 }, 10, SoundEvents.ITEM_ARMOR_EQUIP_GENERIC, 3.7F, 0.16F, () -> { return Ingredient.fromItems(ModItems.AZURITE_INGOT.get()); }), EMATITE(Alcamod.ModID + ":ematite", 50, new int[] { 3, 6, 8, 3 }, 8, SoundEvents.ITEM_ARMOR_EQUIP_GENERIC, 4.0F, 0.17F, () -> { return Ingredient.fromItems(ModItems.EMATITE_INGOT.get()); }), BARITE(Alcamod.ModID + ":barite", 60, new int[] { 3, 6, 8, 3 }, 6, SoundEvents.ITEM_ARMOR_EQUIP_GENERIC, 4.2F, 0.19F, () -> { return Ingredient.fromItems(ModItems.BARITE_INGOT.get()); }), UNPOWERED_NETHER_STAR(Alcamod.ModID + ":unpowered_nether_star", 70, new int[] { 3, 6, 8, 3 }, 6, SoundEvents.ITEM_ARMOR_EQUIP_GENERIC, 4.0F, 0.21F, () -> { return Ingredient.fromItems(Items.NETHER_STAR.getItem()); }), NETHER_STAR(Alcamod.ModID + ":nether_star", 80, new int[] { 3, 6, 8, 3 }, 2, SoundEvents.ITEM_ARMOR_EQUIP_GENERIC, 5.0F, 0.35F, () -> { return Ingredient.fromItems(ModItems.POWERED_NETHER_STAR.get()); }), INVERTED_NETHER_STAR(Alcamod.ModID + ":inverted_nether_star", 100, new int[] { 3, 6, 8, 3 }, 2, SoundEvents.ITEM_ARMOR_EQUIP_GENERIC, 6.0F, 0.60F, () -> { return Ingredient.fromItems(ModItems.INVERTED_NETHER_STAR.get()); }); private static final int[] MAX_DAMAGE_ARRAY = new int[] { 11, 16, 15, 13 }; private final String name; private final int maxDamageFactor; private final int[] damageReductionAmountArray; private final int enchantability; private final SoundEvent soundEvent; private final float toughness; private final float knockBackResistance; private final Supplier<Ingredient> repairMaterial; ModArmorMaterial(String name, int maxDamageFactor, int[] damageReductionAmountArray, int enchantability, SoundEvent soundEvent, float toughness, float knockbackResistance, Supplier<Ingredient> repairMaterial) { this.name = name; this.maxDamageFactor = maxDamageFactor; this.damageReductionAmountArray = damageReductionAmountArray; this.enchantability = enchantability; this.soundEvent = soundEvent; this.toughness = toughness; this.repairMaterial = repairMaterial; this.knockBackResistance = knockbackResistance; } @Override public int getDurability(EquipmentSlotType slotIn) { return MAX_DAMAGE_ARRAY[slotIn.getIndex()] * this.maxDamageFactor; } @Override public int getDamageReductionAmount(EquipmentSlotType slotIn) { return this.damageReductionAmountArray[slotIn.getIndex()]; } @Override public int getEnchantability() { return this.enchantability; } @Override public SoundEvent getSoundEvent() { return this.soundEvent; } @Override public Ingredient getRepairMaterial() { return this.repairMaterial.get(); } @Override public String getName() { return this.name; } @OnlyIn( Dist.CLIENT ) @Override public float getToughness() { return this.toughness; } public float getKnockbackResistance() { return this.knockBackResistance; } }  
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.