Skip to content

Commit

Permalink
Fix weather deflector not functioning, it could get chunk unloaded, n…
Browse files Browse the repository at this point in the history
…ow as long as it ticked once after it was initially placed, it should be loaded until the block itself is broken.
  • Loading branch information
Corosauce committed Apr 4, 2024
1 parent 5e5c8b4 commit 0659d42
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
9 changes: 9 additions & 0 deletions src/main/java/weather2/block/DeflectorBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@ public RenderShape getRenderShape(BlockState p_49232_) {
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level p_153212_, BlockState p_153213_, BlockEntityType<T> 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_);
}
}
12 changes: 9 additions & 3 deletions src/main/java/weather2/blockentity/DeflectorBlockEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}
}
37 changes: 31 additions & 6 deletions src/main/java/weather2/weathersystem/WeatherManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Long, BlockPos> listWeatherBlockDamageDeflector = new HashMap<>();
private HashMap<Long, BlockPos> lookupWeatherBlockDamageDeflector = new HashMap<>();

public WeatherManager(ResourceKey<Level> dimension) {
this.dimension = dimension;
Expand Down Expand Up @@ -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<Long, BlockPos> 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);
Expand Down Expand Up @@ -455,27 +463,44 @@ 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());
}

public WindManager getWindManager() {
return this.wind;
}

public HashMap<Long, BlockPos> getListWeatherBlockDamageDeflector() {
return listWeatherBlockDamageDeflector;
public HashMap<Long, BlockPos> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,22 @@
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;
import net.minecraft.world.level.block.Block;
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;
Expand Down Expand Up @@ -229,7 +223,7 @@ public void tick() {
public Optional<BlockPos> findWeatherDeflector(BlockPos pos, int range) {
double closestDist = Float.MAX_VALUE;
BlockPos closestPos = null;
for (Map.Entry<Long, BlockPos> entrySet : getListWeatherBlockDamageDeflector().entrySet()) {
for (Map.Entry<Long, BlockPos> entrySet : getLookupWeatherBlockDamageDeflector().entrySet()) {
double dist = pos.distSqr(entrySet.getValue());
if (dist < range * range) {
if (dist < closestDist) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 0659d42

Please sign in to comment.