Melt snow and ice around heat sources on Sigma
This commit is contained in:
parent
3b6199d781
commit
f7af3271aa
2 changed files with 355 additions and 2 deletions
|
|
@ -16,7 +16,7 @@ public final class PlanetSoil {
|
|||
private final Plugin owner;
|
||||
private int round;
|
||||
private PlanetSoil(Plugin owner){this.owner=owner;}
|
||||
public static void start(Plugin owner){if(!STARTED.add(owner))return;var feature=new PlanetSoil(owner);Bukkit.getScheduler().runTaskTimer(owner,feature::tick,100,20);owner.getLogger().info("Planet soil weathering active; loaded chunks only, roofs and nearby water preserved");}
|
||||
public static void start(Plugin owner){if(!STARTED.add(owner))return;SigmaHeat.start(owner);var feature=new PlanetSoil(owner);Bukkit.getScheduler().runTaskTimer(owner,feature::tick,100,20);owner.getLogger().info("Planet soil weathering active; loaded chunks only, roofs and nearby water preserved");}
|
||||
static boolean source(Material material){return material==Material.GRASS_BLOCK||material==Material.DIRT;}
|
||||
static boolean exposed(Block soil){return soil.getWorld().getHighestBlockYAt(soil.getX(),soil.getZ(),HeightMap.WORLD_SURFACE)==soil.getY();}
|
||||
/** Unknown adjacent chunks retain soil. Waterlogged blocks hydrate like water. */
|
||||
|
|
@ -57,7 +57,7 @@ public final class PlanetSoil {
|
|||
// Native snowy grass plus a thin snow layer is the frozen-ground representation.
|
||||
if(!block.getRelative(0,1,0).getType().isAir())return;
|
||||
((Snowable)next).setSnowy(true);
|
||||
Block cover=block.getRelative(0,1,0);BlockData air=cover.getBlockData();cover.setType(Material.SNOW,false);log(cover,air,cover.getBlockData());
|
||||
Block cover=block.getRelative(0,1,0);if(SigmaHeat.heated(cover))return;BlockData air=cover.getBlockData();cover.setType(Material.SNOW,false);log(cover,air,cover.getBlockData());
|
||||
}
|
||||
block.setBlockData(next,true);log(block,before,next);
|
||||
}
|
||||
|
|
|
|||
262
DuneTerrain/src/main/java/com/example/duneterrain/SigmaHeat.java
Normal file
262
DuneTerrain/src/main/java/com/example/duneterrain/SigmaHeat.java
Normal file
|
|
@ -0,0 +1,262 @@
|
|||
package com.example.duneterrain;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.ChunkSnapshot;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockFadeEvent;
|
||||
import org.bukkit.event.block.BlockFormEvent;
|
||||
import org.bukkit.event.block.BlockFromToEvent;
|
||||
import org.bukkit.event.block.BlockIgniteEvent;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
import org.bukkit.event.block.BlockRedstoneEvent;
|
||||
import org.bukkit.event.inventory.FurnaceBurnEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
final class SigmaHeat implements Listener {
|
||||
private static final String WORLD = "sigma_draconis_iv";
|
||||
private static final int RADIUS = 8;
|
||||
private static final List<Point> OFFSETS = offsets();
|
||||
private final Plugin plugin;
|
||||
private List<Point> sources = List.of();
|
||||
private int chunkIndex, sourceIndex, offsetIndex;
|
||||
private boolean scanning;
|
||||
private final LinkedHashMap<Point,Integer> urgent = new LinkedHashMap<>();
|
||||
private final LinkedHashSet<ChunkPosition> nearby = new LinkedHashSet<>();
|
||||
private final LinkedHashMap<Long,Integer> chunkCursors = new LinkedHashMap<>();
|
||||
|
||||
|
||||
private record ChunkPosition(int x, int z) {}
|
||||
private record Point(int x, int y, int z) {}
|
||||
|
||||
private SigmaHeat(Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
static void start(Plugin plugin) {
|
||||
var heat = new SigmaHeat(plugin);
|
||||
Bukkit.getPluginManager().registerEvents(heat, plugin);
|
||||
Bukkit.getScheduler().runTaskTimer(plugin, heat::tick, 20, 1);
|
||||
plugin.getLogger().info("Sigma heat melting active; radius 8, loaded chunks only");
|
||||
}
|
||||
|
||||
static boolean applies(World world) {
|
||||
return world.getEnvironment() == World.Environment.NORMAL && WORLD.equals(world.getName());
|
||||
}
|
||||
|
||||
private static List<Point> offsets() {
|
||||
var points = new ArrayList<Point>();
|
||||
for (int x = -RADIUS; x <= RADIUS; x++) {
|
||||
for (int y = -RADIUS; y <= RADIUS; y++) {
|
||||
for (int z = -RADIUS; z <= RADIUS; z++) {
|
||||
if (x*x + y*y + z*z <= RADIUS*RADIUS) points.add(new Point(x,y,z));
|
||||
}
|
||||
}
|
||||
}
|
||||
return List.copyOf(points);
|
||||
}
|
||||
|
||||
static boolean hot(BlockData data) {
|
||||
Material material = data.getMaterial();
|
||||
return material == Material.MAGMA_BLOCK || material == Material.LAVA || data.getLightEmission() > 0;
|
||||
}
|
||||
|
||||
private static Block loadedBlock(World world, int x, int y, int z) {
|
||||
if (y < world.getMinHeight() || y >= world.getMaxHeight() || !world.isChunkLoaded(x >> 4,z >> 4)) return null;
|
||||
return world.getBlockAt(x,y,z);
|
||||
}
|
||||
|
||||
static boolean heated(Block block) {
|
||||
World world = block.getWorld();
|
||||
if (!applies(world)) return false;
|
||||
int x = block.getX(), y = block.getY(), z = block.getZ();
|
||||
for (Point delta : OFFSETS) {
|
||||
Block candidate = loadedBlock(world, x+delta.x,y+delta.y,z+delta.z);
|
||||
if (candidate != null && hot(candidate.getBlockData())) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void preventFreezing(BlockFormEvent event) {
|
||||
Material next = event.getNewState().getType();
|
||||
if ((next == Material.SNOW || next == Material.ICE) && heated(event.getBlock())) event.setCancelled(true);
|
||||
}
|
||||
|
||||
private void expedite(Block block) {
|
||||
if (!applies(block.getWorld())) return;
|
||||
Point point = new Point(block.getX(), block.getY(), block.getZ());
|
||||
expedite(point);
|
||||
}
|
||||
|
||||
private void expedite(Point point) {
|
||||
if (urgent.containsKey(point)) return;
|
||||
if (urgent.size() >= 128) urgent.pollFirstEntry();
|
||||
urgent.putLast(point,0);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void placed(BlockPlaceEvent event) {
|
||||
if (hot(event.getBlockPlaced().getBlockData())) expedite(event.getBlockPlaced());
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void ignite(BlockIgniteEvent event) { expedite(event.getBlock()); }
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void lavaFlows(BlockFromToEvent event) {
|
||||
if (event.getBlock().getType() == Material.LAVA) expedite(event.getToBlock());
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void redstone(BlockRedstoneEvent event) {
|
||||
if (event.getOldCurrent() != event.getNewCurrent()) expedite(event.getBlock());
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void furnace(FurnaceBurnEvent event) { expedite(event.getBlock()); }
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void light(PlayerInteractEvent event) {
|
||||
Block block = event.getClickedBlock();
|
||||
if (block != null && event.getAction() == org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK
|
||||
&& event.getItem() != null
|
||||
&& (event.getItem().getType() == Material.FLINT_AND_STEEL || event.getItem().getType() == Material.FIRE_CHARGE)) expedite(block);
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void joined(PlayerJoinEvent event) { prioritize(event.getPlayer().getLocation()); }
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void enteredChunk(PlayerMoveEvent event) {
|
||||
Location from = event.getFrom(), to = event.getTo();
|
||||
if (to == null) return;
|
||||
if (from.getWorld() != to.getWorld() || (from.getBlockX() >> 4) != (to.getBlockX() >> 4)
|
||||
|| (from.getBlockZ() >> 4) != (to.getBlockZ() >> 4)) prioritize(to);
|
||||
}
|
||||
|
||||
private void prioritize(Location location) {
|
||||
if (!applies(location.getWorld())) return;
|
||||
int x = location.getBlockX() >> 4, z = location.getBlockZ() >> 4;
|
||||
prioritize(new ChunkPosition(x,z));
|
||||
for (int dx = -1; dx <= 1; dx++) for (int dz = -1; dz <= 1; dz++) {
|
||||
if (dx != 0 || dz != 0) prioritize(new ChunkPosition(x+dx,z+dz));
|
||||
}
|
||||
}
|
||||
|
||||
private void prioritize(ChunkPosition chunk) {
|
||||
if (nearby.size() >= 128) nearby.removeLast();
|
||||
nearby.add(chunk);
|
||||
}
|
||||
|
||||
private boolean applyOffset(World world, Point source, int index) {
|
||||
Block origin = loadedBlock(world,source.x,source.y,source.z);
|
||||
if (origin == null || !hot(origin.getBlockData())) return false;
|
||||
Point delta = OFFSETS.get(index);
|
||||
Block block = loadedBlock(world,source.x+delta.x,source.y+delta.y,source.z+delta.z);
|
||||
if (block != null) melt(block);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void tick() {
|
||||
World world = Bukkit.getWorld(WORLD);
|
||||
if (world == null || !applies(world)) return;
|
||||
int budget = 512;
|
||||
for (int priority = 0; priority < 384 && !urgent.isEmpty(); priority++, budget--) {
|
||||
var next = urgent.firstEntry();
|
||||
int offset = next.getValue();
|
||||
if (!applyOffset(world,next.getKey(),offset) || offset+1 == OFFSETS.size()) urgent.pollFirstEntry();
|
||||
else urgent.put(next.getKey(),offset+1);
|
||||
}
|
||||
while (budget-- > 0 && sourceIndex < sources.size()) {
|
||||
if (!applyOffset(world,sources.get(sourceIndex),offsetIndex) || ++offsetIndex == OFFSETS.size()) {
|
||||
sourceIndex++;
|
||||
offsetIndex = 0;
|
||||
}
|
||||
}
|
||||
if (scanning) return;
|
||||
while (!nearby.isEmpty() && urgent.size() <= 96) {
|
||||
ChunkPosition next = nearby.removeFirst();
|
||||
if (world.isChunkLoaded(next.x,next.z)) {
|
||||
discover(world.getChunkAt(next.x,next.z),true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (sourceIndex == sources.size()) {
|
||||
Chunk[] chunks = world.getLoadedChunks();
|
||||
if (chunks.length > 0) discover(chunks[Math.floorMod(chunkIndex++,chunks.length)],false);
|
||||
}
|
||||
}
|
||||
|
||||
private void discover(Chunk chunk, boolean priority) {
|
||||
ChunkSnapshot snapshot = chunk.getChunkSnapshot(false,false,false);
|
||||
int minY = chunk.getWorld().getMinHeight(), maxY = chunk.getWorld().getMaxHeight();
|
||||
long chunkKey = chunk.getChunkKey();
|
||||
scanning = true;
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
List<Point> found = scan(snapshot,minY,maxY);
|
||||
if (!plugin.isEnabled()) return;
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
if (priority) {
|
||||
for (int index = 0; index < Math.min(found.size(),12); index++) expedite(found.get(index));
|
||||
} else {
|
||||
int next = found.isEmpty() ? 0 : Math.floorMod(chunkCursors.getOrDefault(chunkKey,0),found.size());
|
||||
int end = Math.min(next+12,found.size());
|
||||
sources = found.subList(next,end);
|
||||
sourceIndex = 0;
|
||||
if (end == found.size()) chunkCursors.remove(chunkKey);
|
||||
else {
|
||||
if (chunkCursors.size() >= 8192) chunkCursors.pollFirstEntry();
|
||||
chunkCursors.putLast(chunkKey,end);
|
||||
}
|
||||
}
|
||||
scanning = false;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private static List<Point> scan(ChunkSnapshot snapshot, int minY, int maxY) {
|
||||
var found = new ArrayList<Point>();
|
||||
var geothermal = new ArrayList<Point>();
|
||||
for (int y = minY; y < maxY; y++) {
|
||||
if (snapshot.isSectionEmpty((y-minY) >> 4)) {
|
||||
y += 15;
|
||||
continue;
|
||||
}
|
||||
for (int x = 0; x < 16; x++) {
|
||||
for (int z = 0; z < 16; z++) {
|
||||
Material type = snapshot.getBlockType(x,y,z);
|
||||
if (!type.isAir() && hot(snapshot.getBlockData(x,y,z))) {
|
||||
(type == Material.LAVA || type == Material.MAGMA_BLOCK ? geothermal : found).add(new Point((snapshot.getX()<<4)+x,y,(snapshot.getZ()<<4)+z));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
found.addAll(geothermal);
|
||||
return found;
|
||||
}
|
||||
|
||||
private static void melt(Block block) {
|
||||
Material type = block.getType();
|
||||
if (type != Material.SNOW && type != Material.SNOW_BLOCK && type != Material.ICE) return;
|
||||
var replacement = block.getState();
|
||||
replacement.setType(type == Material.ICE ? Material.WATER : Material.AIR);
|
||||
var fade = new BlockFadeEvent(block,replacement);
|
||||
Bukkit.getPluginManager().callEvent(fade);
|
||||
if (!fade.isCancelled() && block.getType() == type) replacement.update(true,true);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue

Staff