尝试添加区块卸载功能

This commit is contained in:
17146 2025-01-17 21:05:49 +08:00
parent 7ef216f443
commit 0429c59831

View file

@ -10,6 +10,7 @@ import com.atsuishio.superbwarfare.tools.ProjectileTool;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes; import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener; import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
@ -18,6 +19,7 @@ import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.projectile.ThrowableItemProjectile; import net.minecraft.world.entity.projectile.ThrowableItemProjectile;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BellBlock; import net.minecraft.world.level.block.BellBlock;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
@ -31,12 +33,16 @@ import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache
import software.bernie.geckolib.core.animation.AnimatableManager; import software.bernie.geckolib.core.animation.AnimatableManager;
import software.bernie.geckolib.util.GeckoLibUtil; import software.bernie.geckolib.util.GeckoLibUtil;
import java.util.HashSet;
import java.util.Set;
public class MortarShellEntity extends ThrowableItemProjectile implements GeoEntity { public class MortarShellEntity extends ThrowableItemProjectile implements GeoEntity {
private float damage = ExplosionConfig.MORTAR_SHELL_EXPLOSION_DAMAGE.get(); private float damage = ExplosionConfig.MORTAR_SHELL_EXPLOSION_DAMAGE.get();
private int life = 600; private int life = 600;
private float radius = ExplosionConfig.MORTAR_SHELL_EXPLOSION_RADIUS.get(); private float radius = ExplosionConfig.MORTAR_SHELL_EXPLOSION_RADIUS.get();
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
public Set<Long> loadedChunks = new HashSet<>();
public MortarShellEntity(EntityType<? extends MortarShellEntity> type, Level world) { public MortarShellEntity(EntityType<? extends MortarShellEntity> type, Level world) {
super(type, world); super(type, world);
@ -62,6 +68,14 @@ public class MortarShellEntity extends ThrowableItemProjectile implements GeoEnt
pCompound.putFloat("Damage", this.damage); pCompound.putFloat("Damage", this.damage);
pCompound.putInt("Life", this.life); pCompound.putInt("Life", this.life);
pCompound.putFloat("Radius", this.radius); pCompound.putFloat("Radius", this.radius);
ListTag listTag = new ListTag();
for (long chunkPos : this.loadedChunks) {
CompoundTag tag = new CompoundTag();
tag.putLong("Pos", chunkPos);
listTag.add(tag);
}
pCompound.put("Chunks", listTag);
} }
@Override @Override
@ -84,6 +98,14 @@ public class MortarShellEntity extends ThrowableItemProjectile implements GeoEnt
} else { } else {
this.radius = ExplosionConfig.MORTAR_SHELL_EXPLOSION_RADIUS.get(); this.radius = ExplosionConfig.MORTAR_SHELL_EXPLOSION_RADIUS.get();
} }
if (pCompound.contains("Chunks")) {
ListTag listTag = pCompound.getList("Chunks", 10);
for (int i = 0; i < listTag.size(); i++) {
CompoundTag tag = listTag.getCompound(i);
this.loadedChunks.add(tag.getLong("Pos"));
}
}
} }
@Override @Override
@ -146,6 +168,11 @@ public class MortarShellEntity extends ThrowableItemProjectile implements GeoEnt
ForgeChunkManager.forceChunk(serverLevel, ModUtils.MODID, this, currentX, nextZ, true, false); ForgeChunkManager.forceChunk(serverLevel, ModUtils.MODID, this, currentX, nextZ, true, false);
ForgeChunkManager.forceChunk(serverLevel, ModUtils.MODID, this, nextX, currentZ, true, false); ForgeChunkManager.forceChunk(serverLevel, ModUtils.MODID, this, nextX, currentZ, true, false);
ForgeChunkManager.forceChunk(serverLevel, ModUtils.MODID, this, nextX, nextZ, true, false); ForgeChunkManager.forceChunk(serverLevel, ModUtils.MODID, this, nextX, nextZ, true, false);
this.loadedChunks.add(ChunkPos.asLong(currentX, currentZ));
this.loadedChunks.add(ChunkPos.asLong(currentX, nextZ));
this.loadedChunks.add(ChunkPos.asLong(nextX, currentZ));
this.loadedChunks.add(ChunkPos.asLong(nextX, nextZ));
} }
if (this.tickCount > this.life || this.isInWater()) { if (this.tickCount > this.life || this.isInWater()) {
if (this.level() instanceof ServerLevel) { if (this.level() instanceof ServerLevel) {
@ -164,9 +191,18 @@ public class MortarShellEntity extends ThrowableItemProjectile implements GeoEnt
return this.cache; return this.cache;
} }
@Override @Override
protected float getGravity() { protected float getGravity() {
return 0.05F; return 0.05F;
} }
@Override
public void onRemovedFromWorld() {
if (this.level() instanceof ServerLevel serverLevel) {
for (long chunkPos : this.loadedChunks) {
ForgeChunkManager.forceChunk(serverLevel, ModUtils.MODID, this, ChunkPos.getX(chunkPos), ChunkPos.getZ(chunkPos), false, false);
}
}
super.onRemovedFromWorld();
}
} }