diff --git a/src/main/java/weather2/block/DeflectorBlock.java b/src/main/java/weather2/block/DeflectorBlock.java index 507b249..9f06046 100644 --- a/src/main/java/weather2/block/DeflectorBlock.java +++ b/src/main/java/weather2/block/DeflectorBlock.java @@ -35,4 +35,13 @@ public RenderShape getRenderShape(BlockState p_49232_) { public BlockEntityTicker getTicker(Level p_153212_, BlockState p_153213_, BlockEntityType p_153214_) { return createTickerHelper(p_153214_, WeatherBlocks.BLOCK_ENTITY_DEFLECTOR.get(), DeflectorBlockEntity::tickHelper); } + + @Override + public void onRemove(BlockState p_60515_, Level level, BlockPos pos, BlockState p_60518_, boolean p_60519_) { + System.out.println("removed deflector block"); + if (level.getBlockEntity(pos) instanceof DeflectorBlockEntity deflectorBlockEntity) { + deflectorBlockEntity.blockBroken(); + } + super.onRemove(p_60515_, level, pos, p_60518_, p_60519_); + } } diff --git a/src/main/java/weather2/blockentity/DeflectorBlockEntity.java b/src/main/java/weather2/blockentity/DeflectorBlockEntity.java index c509c59..387f18a 100644 --- a/src/main/java/weather2/blockentity/DeflectorBlockEntity.java +++ b/src/main/java/weather2/blockentity/DeflectorBlockEntity.java @@ -31,15 +31,21 @@ public void tick() { public void init() { if (!level.isClientSide()) { - CULog.dbg("adding weather deflector poi at " + getBlockPos()); ServerTickHandler.getWeatherManagerFor(level).registerDeflector(getBlockPos()); } } + public void blockBroken() { + WeatherManagerServer weatherManagerServer = ServerTickHandler.getWeatherManagerFor(level); + if (weatherManagerServer != null) { + //CULog.dbg("removing weather deflector poi at " + getBlockPos()); + weatherManagerServer.removeDeflector(getBlockPos()); + } + } + @Override public void setRemoved() { super.setRemoved(); - WeatherManagerServer weatherManagerServer = ServerTickHandler.getWeatherManagerFor(level); - if (weatherManagerServer != null) weatherManagerServer.removeDeflector(getBlockPos()); + } } diff --git a/src/main/java/weather2/weathersystem/WeatherManager.java b/src/main/java/weather2/weathersystem/WeatherManager.java index 699215b..fe67907 100644 --- a/src/main/java/weather2/weathersystem/WeatherManager.java +++ b/src/main/java/weather2/weathersystem/WeatherManager.java @@ -40,7 +40,7 @@ public abstract class WeatherManager implements IWorldData { //going to vary the amount randomly over time like wind, for aesthetic only mode public float vanillaRainAmountOnServer = 0; - private HashMap listWeatherBlockDamageDeflector = new HashMap<>(); + private HashMap lookupWeatherBlockDamageDeflector = new HashMap<>(); public WeatherManager(ResourceKey dimension) { this.dimension = dimension; @@ -383,6 +383,14 @@ public CompoundTag save(CompoundTag data) { listStormsNBT.put("storm_" + obj.ID, obj.getNbtCache().getNewNBT()); } data.put("stormData", listStormsNBT); + CompoundTag listDeflectorsNBT = new CompoundTag(); + int i = 0; + for (Map.Entry entry : lookupWeatherBlockDamageDeflector.entrySet()) { + CULog.dbg("writing out deflector to disk: " + entry.getKey()); + listDeflectorsNBT.putLong("deflector_" + i, entry.getKey()); + i++; + } + data.put("deflectorData", listDeflectorsNBT); data.putLong("lastUsedIDStorm", WeatherObject.lastUsedStormID); data.putLong("lastStormFormed", lastStormFormed); @@ -455,6 +463,21 @@ public void read() { }*/ } + CompoundTag nbtDeflectors = data.getCompound("deflectorData"); + + Iterator it2 = nbtDeflectors.getAllKeys().iterator(); + + while (it2.hasNext()) { + String tagName = (String) it2.next(); + long hash = nbtDeflectors.getLong(tagName); + if (!BlockPos.of(hash).equals(new BlockPos(0, 0, 0))) { + CULog.dbg("adding deflector from disk: " + BlockPos.of(hash)); + registerDeflector(BlockPos.of(hash)); + } else { + CULog.dbg("????????"); + } + } + CULog.dbg("reloaded weather objects: " + listStormObjects.size()); } @@ -462,20 +485,22 @@ public WindManager getWindManager() { return this.wind; } - public HashMap getListWeatherBlockDamageDeflector() { - return listWeatherBlockDamageDeflector; + public HashMap getLookupWeatherBlockDamageDeflector() { + return lookupWeatherBlockDamageDeflector; } public void registerDeflector(BlockPos pos) { long hash = BlockPos.asLong(pos.getX(), pos.getY(), pos.getZ()); - if (!this.listWeatherBlockDamageDeflector.containsKey(hash)) { - this.listWeatherBlockDamageDeflector.put(hash, pos); + if (!this.lookupWeatherBlockDamageDeflector.containsKey(hash)) { + CULog.dbg("adding weather deflector poi at " + pos); + this.lookupWeatherBlockDamageDeflector.put(hash, pos); } } public void removeDeflector(BlockPos pos) { long hash = BlockPos.asLong(pos.getX(), pos.getY(), pos.getZ()); - this.listWeatherBlockDamageDeflector.remove(hash); + CULog.dbg("removing weather deflector poi at " + pos); + this.lookupWeatherBlockDamageDeflector.remove(hash); } } diff --git a/src/main/java/weather2/weathersystem/WeatherManagerServer.java b/src/main/java/weather2/weathersystem/WeatherManagerServer.java index b73bbad..b70424f 100644 --- a/src/main/java/weather2/weathersystem/WeatherManagerServer.java +++ b/src/main/java/weather2/weathersystem/WeatherManagerServer.java @@ -9,13 +9,9 @@ import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.util.Mth; -import net.minecraft.world.effect.MobEffectInstance; -import net.minecraft.world.effect.MobEffects; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntitySelector; import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.entity.ai.village.poi.PoiManager; -import net.minecraft.world.entity.ai.village.poi.PoiType; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; import net.minecraft.world.level.biome.Biome; @@ -23,14 +19,12 @@ import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.chunk.LevelChunk; -import net.minecraft.world.level.levelgen.Heightmap; import net.minecraft.world.phys.Vec3; import net.minecraftforge.network.NetworkDirection; import net.minecraftforge.network.PacketDistributor; import weather2.*; import weather2.config.*; import weather2.datatypes.StormState; -import weather2.player.PlayerData; import weather2.util.CachedNBTTagCompound; import weather2.util.WeatherUtilBlock; import weather2.weathersystem.storm.StormObject; @@ -229,7 +223,7 @@ public void tick() { public Optional findWeatherDeflector(BlockPos pos, int range) { double closestDist = Float.MAX_VALUE; BlockPos closestPos = null; - for (Map.Entry entrySet : getListWeatherBlockDamageDeflector().entrySet()) { + for (Map.Entry entrySet : getLookupWeatherBlockDamageDeflector().entrySet()) { double dist = pos.distSqr(entrySet.getValue()); if (dist < range * range) { if (dist < closestDist) { diff --git a/src/main/java/weather2/weathersystem/storm/TornadoHelper.java b/src/main/java/weather2/weathersystem/storm/TornadoHelper.java index 97592bb..2abf69a 100644 --- a/src/main/java/weather2/weathersystem/storm/TornadoHelper.java +++ b/src/main/java/weather2/weathersystem/storm/TornadoHelper.java @@ -1,11 +1,9 @@ package weather2.weathersystem.storm; -import com.corosus.coroutil.util.CULog; import com.corosus.coroutil.util.CoroUtilBlock; import com.mojang.authlib.GameProfile; import net.minecraft.client.Minecraft; import net.minecraft.core.BlockPos; -import net.minecraft.core.particles.ParticleTypes; import net.minecraft.resources.ResourceKey; import net.minecraft.server.level.ServerLevel; import net.minecraft.sounds.SoundEvents; @@ -837,7 +835,7 @@ public boolean isBlockGrabbingBlocked(Level world, BlockState state, BlockPos po isBlockGrabbingBlockedCached = false; - for (Long hash : storm.manager.getListWeatherBlockDamageDeflector().keySet()) { + for (Long hash : storm.manager.getLookupWeatherBlockDamageDeflector().keySet()) { BlockPos posDeflect = BlockPos.of(hash); if (pos.distSqr(posDeflect) < ConfigStorm.Storm_Deflector_RadiusOfStormRemoval * ConfigStorm.Storm_Deflector_RadiusOfStormRemoval) {