Jump to content

Recommended Posts

Posted

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.

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.