Jump to content

read/get Share Tag is not working [1.16.5]


Yurim64

Recommended Posts

Hi!

I was adding some NBT data to some custom items, but no data is saved inside.

Doing a bit of debugging I found that the readShareTag and getShareTag methods were not being invoked.

This is the Item Class:

public abstract class AbstractGunItem extends Item {

    public AbstractGunItem() {
        super(new Item.Properties().tab(ItemGroup.TAB_COMBAT));
    }

    @Override
    public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
        if (world instanceof ServerWorld) {
            ItemStack stack = player.inventory.getSelected();
            if (stack.getItem() instanceof AbstractGunItem) {
                int ammo = GunProperties.getAmmo(stack);
                if (ammo > 0 || player.isCreative()) {
                    GunProperties.removeBullet(stack);
                    player.getCooldowns().addCooldown(this, GunProperties.getFireRate(stack));

                    Vector3d vector3d = player.getViewVector(1.0f);
                    BulletEntity bullet = new BulletEntity(world);
                    bullet.setOwner(player);
                    bullet.xPower = vector3d.x;
                    bullet.yPower = vector3d.y;
                    bullet.zPower = vector3d.z;
                    bullet.setDamage(GunProperties.getDamage(stack));
                    bullet.setPos(player.getX() + vector3d.x, player.getY() + player.getEyeHeight(), player.getZ() + vector3d.z);
                    bullet.setDeltaMovement(vector3d);
                    world.addFreshEntity(bullet);
                    if (GunProperties.getAmmo(stack) <= 0 && GunProperties.canAutoRefill(stack)) {
                        this.refill(player, stack);
                    }
                    return ActionResult.consume(player.inventory.getSelected());
                }
            }
        }
        return super.use(world, player, hand);
    }

    @Override
    public UseAction getUseAnimation(ItemStack stack) {
        return UseAction.SPEAR;
    }

    @Nullable
    @Override
    public CompoundNBT getShareTag(ItemStack stack) {
        CompoundNBT shareTag = super.getShareTag(stack);
        if (shareTag == null) {
            shareTag = new CompoundNBT();
        }
        GunProperties properties = getGunProperties(stack);
        GunProperties.addProperties(shareTag, properties);
        return shareTag;
    }

    @Override
    public void readShareTag(ItemStack stack, @Nullable CompoundNBT nbt) {
        super.readShareTag(stack, nbt);
        if (nbt == null) {
            System.out.println("Invalid NBT for abstract gun item " + stack.getItem());
        }
    }

    @Nonnull
    public abstract GunProperties defaultGunProperties();

    private GunProperties getGunProperties(ItemStack stack) {
        System.out.println("Getting Properties from " + stack);
        GunProperties properties = GunProperties.getProperties(stack);
        GunProperties defaultProperties = defaultGunProperties();
        if (properties.getMaxAmmo() == 0)
            properties = properties.maxAmmo(defaultProperties.getMaxAmmo());
        if (properties.getFireRate() == 0)
            properties = properties.fireRate(defaultProperties.getFireRate());
        if (properties.getDamage() == 0)
            properties = properties.damage(defaultProperties.getDamage());
        System.out.println(properties);
        return properties;
    }

    @Override
    public boolean showDurabilityBar(ItemStack stack) {
        return true;
    }

    @Override
    public double getDurabilityForDisplay(ItemStack stack) {
        double d = 1.0;
        double r = GunProperties.getAmmo(stack) / (double) GunProperties.getMaxAmmo(stack);
        return d - r;
    }

    public void refill(PlayerEntity player, ItemStack stack) {
        boolean flag = false;
        PlayerInventory inventory = player.inventory;
        int size = inventory.getContainerSize();
        for (int i = 0; i < size; i++) {
            ItemStack item = inventory.getItem(i);
            if (item.getItem() instanceof Bullet) {
                flag = true;
                int count = item.getCount();
                int remain = GunProperties.refill(stack, count);
                item.setCount(remain);
            }
            if (GunProperties.getAmmo(stack) == GunProperties.getMaxAmmo(stack)) {
                System.out.println(GunProperties.getAmmo(stack) + "/" + GunProperties.getMaxAmmo(stack));
                if (flag)
                    player.getCooldowns().addCooldown(this, 20);
                return;
            }
        }
        if (flag)
            player.getCooldowns().addCooldown(this, 20);
    }

    @Override
    public int getItemStackLimit(ItemStack stack) {
        return 1;
    }
}

And this is the Properties Class:

public class GunProperties {

    private int maxAmmo;
    private int ammo;
    private float damage;
    private int fireRate;

    public GunProperties() {
        this.maxAmmo = 0;
        this.ammo = 0;
        this.damage = 0;
        this.fireRate = 0;
    }

    public static boolean canAutoRefill(ItemStack stack) {
        return stack.getItem() instanceof SniperItem;
    }

    public GunProperties maxAmmo(int maxAmmo) {
        this.maxAmmo = maxAmmo;
        return this;
    }

    public GunProperties damage(float damage) {
        this.damage = damage;
        return this;
    }

    public GunProperties fireRate(int fireRate) {
        this.fireRate = fireRate;
        return this;
    }

    public int getMaxAmmo() {
        return maxAmmo;
    }

    public float getDamage() {
        return damage;
    }

    public int getFireRate() {
        return fireRate;
    }

    @Override
    public String toString() {
        return "GunProperties{" +
                "maxAmmo=" + maxAmmo +
                ", ammo=" + ammo +
                ", damage=" + damage +
                ", fireRate=" + fireRate +
                '}';
    }

    public static final String MAX_AMMO = "maxAmmo";
    public static final String AMMO = "ammo";
    public static final String DAMAGE = "damage";
    public static final String FIRE_RATE = "fireRate";

    public static void addProperties(ItemStack stack, GunProperties properties) {
        CompoundNBT tag = stack.getOrCreateTag();
        addProperties(tag, properties);
    }

    public static void addProperties(CompoundNBT tag, GunProperties properties) {
        tag.putInt(MAX_AMMO, properties.maxAmmo);
        tag.putInt(AMMO, properties.ammo);
        tag.putFloat(DAMAGE, properties.damage);
        tag.putInt(FIRE_RATE, properties.fireRate);
    }

    public static GunProperties getProperties(ItemStack stack) {
        CompoundNBT tag = stack.getOrCreateTag();
        GunProperties properties = new GunProperties();
        if (tag.contains(MAX_AMMO)) {
            properties.maxAmmo = tag.getInt(MAX_AMMO);
        }
        if (tag.contains(AMMO)) {
            properties.ammo = tag.getInt(AMMO);
        }
        if (tag.contains(DAMAGE)) {
            properties.damage = tag.getFloat(DAMAGE);
        }
        if (tag.contains(FIRE_RATE)) {
            properties.fireRate = tag.getInt(FIRE_RATE);
        }
        return properties;
    }

    public static int getMaxAmmo(ItemStack stack) {
        CompoundNBT tag = stack.getOrCreateTag();
        if (tag.contains(MAX_AMMO)) {
            return tag.getInt(MAX_AMMO);
        }
        return 0;
    }

    public static int getAmmo(ItemStack stack) {
        CompoundNBT tag = stack.getOrCreateTag();
        if (tag.contains(AMMO)) {
            return tag.getInt(AMMO);
        }
        return 0;
    }

    public static float getDamage(ItemStack stack) {
        CompoundNBT tag = stack.getOrCreateTag();
        if (tag.contains(DAMAGE)) {
            return tag.getFloat(DAMAGE);
        }
        return 0;
    }

    public static int getFireRate(ItemStack stack) {
        CompoundNBT tag = stack.getOrCreateTag();
        if (tag.contains(FIRE_RATE)) {
            return tag.getInt(FIRE_RATE);
        }
        return 0;
    }

    public static int refill(ItemStack stack, int amount) {
        CompoundNBT tag = stack.getOrCreateTag();
        System.out.println(tag);
        int rest = 0;
        if (tag.contains(MAX_AMMO) && tag.contains(AMMO)) {
            System.out.println(tag.getInt(AMMO));
            int maxAmmo = tag.getInt(MAX_AMMO);
            int ammo = tag.getInt(AMMO);
            ammo += amount;
            if (ammo > maxAmmo) {
                rest = ammo - maxAmmo;
                ammo = maxAmmo;
            }
            tag.putInt(AMMO, ammo);
        }
        return rest;
    }

    public static void removeBullet(ItemStack stack) {
        removeBullet(stack, 1);
    }

    public static void removeBullet(ItemStack stack, int amount) {
        CompoundNBT tag = stack.getOrCreateTag();
        if (tag.contains(AMMO)) {
            int ammo = tag.getInt(AMMO);
            ammo -= amount;
            if (ammo < 0)
                ammo = 0;
            tag.putInt(AMMO, ammo);
        }
    }

}


Can someone help me? Thanks.

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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello all! I'm currently trying to make the Biomes o' Plenty mod (1.12.2 version) compatible with my shaderpack (BSL v8.5). Even after I've already inserted a "shaders" folder with a correctly-formatted blocks.PROPERTIES file within the assets folder of the .jar, it still does not wave the textures from the mod. I definitely thought this was going to work since I noticed the 1.19.2 version of the mod laid it out exactly like that and it worked perfectly when my PC was able to run 1.19.2 Forge... ^^;;; Could there be some missing link I'm not getting at here?
    • After much experimentation I finally realised why this doesn't work and how to fix it. The critical component to understand here, is that ItemCraftedEvent is called before the crafting ingredients are purged from the crafting slots; this all happens in ResultSlot.java's onTake method: We can see here the first line fires checkTakeAchievements, this is what fires the ItemCraftedEvent (after a few method calls). Then we can see the purge behaviour. I worked around this by creating a CarverItem class, which uses hasCraftingRemainingItem and getCraftingRemainingItem to hook into ResultSlot.onTake: Now, I reconstruct my ItemCraftedEvent handler, and using some smart tags (which I omit for brevity), I handle two cases: 1. When crafting a carved item, I damage the tool used in the recipe 2. When crafting a carving tool from a repair recipe, I manually purge the input tools as with the new CarverItem class, as otherwise items would be duplicated   This has now greatly improved implementation, beyond my expectations, for the standard crafting table and the user's inventory crafting grid. A user can now click or shift-click 'carve' items, with no duplication or issues that I have spotted so far.  
    • Hi, I'm having issues launching my Forge 1.20.1 installation (47.2.0). I keep getting error code 1, and all the logs abruptly pause with the game attempting to open GL 4.6. Latest Crash: https://pastebin.com/zsP5Wipk Debug (No idea what the real name is) Log: https://pastebin.com/CzvqhNq0   Thanks for the help!
    • In the most up to date version of Allthemods 6 (v1.9.2) the stoned bee is not able to be interacted with apart from putting it into an empty bee jar. I can't breed the bee or put it into my aviary breeder. When I try to put it into the breeder it says import unsuccessful. The stoned bee is one of the most important bees for advancing in the mod so it is rather unfortunate that it is not working as intended.
    • Everytime I try to run the Forge 1.20.1 - 47.1.0 Installer, only appears an a black window and then closes. I don't appear the installing menu anymore.
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.