Jump to content

Recommended Posts

Posted

I'm trying to check a player's armor inventory to see if my custom Armor currently has a capability attached to it. If it does in another event I give the player an effect. This works fine in SInglePlayer. However, when I run it in a Dedicated Server, the events are out of sync so the Client Player can see the armor and get the capabilities but the Server Player just returns 0. Is there a better way to get the Player's Armor as an ItemStack? I've tried packets but there's no simple way to return them and putting the information in the Player's Persistent Data causes worse desync problems. I know that player.inventory.armorItemInSlot() can only be run on Client but when I use player.inventory.armorInventory.get() or player.inventory.getStackInSlot() instead it doesn't work on Server.

 

 public double getPlayerGemStrength(PlayerEntity player, String attribute) {
        double fullStrength = 0;
        for (int i = 0; i < 4; i++) {
            ItemStack armor;
            armor = player.inventory.armorItemInSlot(3 - i);
            LazyOptional<ISlottedArmor> armorCapability = armor.getCapability(SlottedArmorProvider.ARMOR_CAPABILITY);
            ISlottedArmor armorInstance = armorCapability.orElse(new SlottedArmorInstance());
            ItemStack gem = armorInstance.getGem();
            if (gem != null) {
                LazyOptional<IGem> gemCapability = gem.getCapability(GemProvider.GEM_CAPABILITY);
                IGem gemInstance = gemCapability.orElse(new GemInstance());
                String gemAttribute = gemInstance.getAttribute();
                double strength = gemInstance.getStrength();
                if (gemAttribute != null && !gemAttribute.equals("")) {
                    if (gemAttribute.equals(attribute)) {
                        fullStrength += strength;
                    }
                }
            }
        }
        return fullStrength;
    }

Here is an example effect:
 

 @SubscribeEvent
    public void giveEffects(PlayerEvent event) {
        if (event.getPlayer() != null) {
            PlayerEntity player = event.getPlayer();
            double fullStrength = getPlayerGemStrength(player, "HP Up");
            if (fullStrength > 0) {
                MaxHealthHandler.updateHealthModifier(player, fullStrength);
            } else {
                MaxHealthHandler.updateHealthModifier(player, 0);
            }
        }
    }

 

  • Like 1
Posted (edited)

Use orElseThrow instead of orElse otherwise you won't notice if there's any error. Also use tick event not PlayerEvent, or it will be called whenever a sub event of it is triggered.

 

armorItemInSlot(int slotIn)

is client side only.

Edited by poopoodice
Posted

Try doing some like this:

YourInterface data = armour.getCapability(SlottedArmorProvider.ARMOR_CAPABILITY).orElseThrow(IllegalStateException::new);

Posted

I've added the orElseThrow and it throws an exception when player.inventory.getStackInSlot() is called instead of player.inventory.armorItemInSlot(). What are the armor slotIds when accessing player.inventory? I've tried both 5-8 and 100-103 but both return Air ItemStacks.

Posted

The ItemStack is always an Air ItemStack unless I access it with armorItemInStack() instead of getStackInSlot() from the player inventory. If the capability is empty or not there it doesn't get added. I just need to know a way to grab the ItemStacks in the Armor Slots from the Player's Inventory that is not a Client Only method.

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.