Fixed thrown jars despawning

This commit is contained in:
Sollace 2023-09-04 11:56:08 +01:00
parent 8214342520
commit 5927734943
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
2 changed files with 15 additions and 6 deletions

View file

@ -45,7 +45,7 @@ abstract class ProjectileItem extends Item {
player.incrementStat(Stats.USED.getOrCreateStat(this)); player.incrementStat(Stats.USED.getOrCreateStat(this));
if (!player.getAbilities().creativeMode) { if (!player.isCreative()) {
stack.decrement(1); stack.decrement(1);
} }
@ -56,6 +56,7 @@ abstract class ProjectileItem extends Item {
MagicProjectileEntity projectile = player == null ? new MagicProjectileEntity(world) : new MagicProjectileEntity(world, player); MagicProjectileEntity projectile = player == null ? new MagicProjectileEntity(world) : new MagicProjectileEntity(world, player);
projectile.setItem(stack); projectile.setItem(stack);
projectile.setThrowDamage(getProjectileDamage(stack)); projectile.setThrowDamage(getProjectileDamage(stack));
projectile.setMaxAge(-1);
if (player != null) { if (player != null) {
projectile.setVelocity(player, player.getPitch(), player.getYaw(), 0, 1.5F, 1); projectile.setVelocity(player, player.getPitch(), player.getYaw(), 0, 1.5F, 1);
} }

View file

@ -39,6 +39,7 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.Items; import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.Packet;
import net.minecraft.network.listener.ClientPlayPacketListener; import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.particle.ItemStackParticleEffect; import net.minecraft.particle.ItemStackParticleEffect;
@ -71,6 +72,8 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster<Ma
private final EntityReference<Entity> homingTarget = new EntityReference<>(); private final EntityReference<Entity> homingTarget = new EntityReference<>();
private EntityReference<LivingEntity> owner; private EntityReference<LivingEntity> owner;
private int maxAge = 90;
public MagicProjectileEntity(EntityType<MagicProjectileEntity> type, World world) { public MagicProjectileEntity(EntityType<MagicProjectileEntity> type, World world) {
super(type, world); super(type, world);
} }
@ -125,7 +128,6 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster<Ma
return getMaster(); return getMaster();
} }
@Override @Override
public EntityReference<LivingEntity> getMasterReference() { public EntityReference<LivingEntity> getMasterReference() {
if (owner == null) { if (owner == null) {
@ -188,13 +190,15 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster<Ma
return getDataTracker().get(HYDROPHOBIC); return getDataTracker().get(HYDROPHOBIC);
} }
public void setMaxAge(int maxAge) {
this.maxAge = maxAge;
}
@Override @Override
public void tick() { public void tick() {
if (!getWorld().isClient() && homingTarget.getOrEmpty(asWorld()).isEmpty()) { if (maxAge > 0 && !getWorld().isClient() && homingTarget.getOrEmpty(asWorld()).isEmpty() && (getVelocity().length() < 0.1 || age > maxAge)) {
if (getVelocity().length() < 0.1 || age > 90) {
discard(); discard();
} }
}
super.tick(); super.tick();
@ -263,6 +267,9 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster<Ma
if (compound.contains("effect")) { if (compound.contains("effect")) {
getSpellSlot().put(Spell.readNbt(compound.getCompound("effect"))); getSpellSlot().put(Spell.readNbt(compound.getCompound("effect")));
} }
if (compound.contains("maxAge", NbtElement.INT_TYPE)) {
maxAge = compound.getInt("maxAge");
}
} }
@Override @Override
@ -271,6 +278,7 @@ public class MagicProjectileEntity extends ThrownItemEntity implements Caster<Ma
physics.toNBT(compound); physics.toNBT(compound);
compound.put("homingTarget", homingTarget.toNBT()); compound.put("homingTarget", homingTarget.toNBT());
compound.put("owner", getMasterReference().toNBT()); compound.put("owner", getMasterReference().toNBT());
compound.putInt("maxAge", maxAge);
getSpellSlot().get(true).ifPresent(effect -> { getSpellSlot().get(true).ifPresent(effect -> {
compound.put("effect", Spell.writeNbt(effect)); compound.put("effect", Spell.writeNbt(effect));
}); });