diff --git a/DuneTerrain/src/main/java/com/example/duneterrain/ArrakisEcology.java b/DuneTerrain/src/main/java/com/example/duneterrain/ArrakisEcology.java new file mode 100644 index 00000000..eeaac679 --- /dev/null +++ b/DuneTerrain/src/main/java/com/example/duneterrain/ArrakisEcology.java @@ -0,0 +1,87 @@ +package com.example.duneterrain; + +import java.lang.reflect.*; +import java.util.*; +import java.util.concurrent.ThreadLocalRandom; +import org.bukkit.*; +import org.bukkit.attribute.*; +import org.bukkit.entity.*; +import org.bukkit.event.*; +import org.bukkit.event.entity.*; +import org.bukkit.event.player.PlayerInteractEntityEvent; +import org.bukkit.event.block.BlockCookEvent; +import org.bukkit.event.server.PluginDisableEvent; +import org.bukkit.inventory.*; +import org.bukkit.persistence.PersistentDataType; +import org.bukkit.plugin.Plugin; +import org.bukkit.potion.*; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; + +/** Arrakis wildlife only. Native animal AI, growth and breeding remain the engine. */ +public final class ArrakisEcology implements Listener { + private static final Map INSTANCES=new WeakHashMap<>(); + private static final NamespacedKey PET=new NamespacedKey("dunerealms","tiny_worm_pet"),BOOST=new NamespacedKey("dunerealms","worm_water_until"),DAMAGE=new NamespacedKey("dunerealms","worm_water_damage"),HEALTH=new NamespacedKey("dunerealms","worm_water_health"),EI=new NamespacedKey("executableitems","ei-id"); + interface Items {ItemStack build(String id)throws ReflectiveOperationException;} + private final Plugin plugin;private final Items items;private final java.util.function.DoubleSupplier random; + private ArrakisEcology(Plugin plugin){this(plugin,ArrakisEcology::item,()->ThreadLocalRandom.current().nextDouble());} + ArrakisEcology(Plugin plugin,Items items,java.util.function.DoubleSupplier random){this.plugin=plugin;this.items=items;this.random=random;} + public static void start(Plugin plugin){if(INSTANCES.containsKey(plugin))return;var feature=new ArrakisEcology(plugin);INSTANCES.put(plugin,feature);Bukkit.getPluginManager().registerEvents(feature,plugin);Bukkit.getScheduler().runTaskTimer(plugin,feature::maintain,20,20);plugin.getLogger().info("Arrakis ecology registered; native item verification pending");Bukkit.getScheduler().runTaskLater(plugin,feature::verifyDefinitions,20);} + private void verifyDefinitions(){for(String id:List.of("jerboa_raw_meat","jerboa_cooked_meat","jerboa_stew","jerboa_hide","jerboa_water_vial","polluted_refined_spice","worm_memorial_bone"))try{var stack=items.build(id);if(stack==null||!id.equals(itemId(stack)))throw new ReflectiveOperationException("Native item identity mismatch");}catch(ReflectiveOperationException failure){plugin.getLogger().severe("Arrakis ecology item verification failed for "+id+": "+failure.getClass().getSimpleName());return;}plugin.getLogger().info("Arrakis ecology verified all seven native item definitions");} + private static boolean arrakis(Entity e){return e.getWorld().getName().equalsIgnoreCase("arrakis");} + private static boolean pet(Entity e){return e instanceof Wolf&&e.getPersistentDataContainer().has(PET,PersistentDataType.BYTE);} + private static String itemId(ItemStack stack){return stack==null||!stack.hasItemMeta()?null:stack.getItemMeta().getPersistentDataContainer().get(EI,PersistentDataType.STRING);} + private static boolean protectedMob(LivingEntity e){return pet(e)||e.customName()!=null||e.hasMetadata("NPC")||!e.getPersistentDataContainer().getKeys().isEmpty()||e instanceof Tameable t&&t.isTamed();} + @EventHandler(priority=EventPriority.HIGHEST,ignoreCancelled=true) public void spawning(CreatureSpawnEvent event){ + var e=event.getEntity();if(!arrakis(e))return; + if(e instanceof Rabbit rabbit){rabbit.customName(Component.text("Jerboa",NamedTextColor.YELLOW));rabbit.setRabbitType(Rabbit.Type.GOLD);return;} + if(event.getSpawnReason()!=CreatureSpawnEvent.SpawnReason.CUSTOM&&!protectedMob(e))event.setCancelled(true); + } + @EventHandler(priority=EventPriority.MONITOR) public void loaded(com.destroystokyo.paper.event.entity.EntityAddToWorldEvent event){ + if(!(event.getEntity() instanceof LivingEntity e)||e instanceof Player||!arrakis(e))return; + Bukkit.getScheduler().runTask(plugin,()->{if(!e.isValid())return;if(e instanceof Rabbit rabbit){rabbit.customName(Component.text("Jerboa",NamedTextColor.YELLOW));rabbit.setRabbitType(Rabbit.Type.GOLD);}else if(pet(e))restoreWater((Wolf)e);else if(!protectedMob(e))e.remove();}); + } + private void maintain(){for(var world:Bukkit.getWorlds())for(var e:world.getEntitiesByClass(Wolf.class))if(pet(e))restoreWater(e);} + /** Called at the existing bloom announcement; chunk PDC prevents duplicate pets. */ + public static void bloom(Plugin plugin,Location location){var instance=INSTANCES.get(plugin);if(instance==null||location==null||location.getWorld()==null||!location.getWorld().getName().equalsIgnoreCase("arrakis"))return;instance.spawnBloomPet(location);} + private void spawnBloomPet(Location location){ + var chunk=location.getChunk();var marker=new NamespacedKey(plugin,"worm_bloom_"+location.getBlockX()+"_"+location.getBlockY()+"_"+location.getBlockZ());if(chunk.getPersistentDataContainer().has(marker,PersistentDataType.BYTE))return; + for(int[] offset:new int[][]{{3,0},{-3,0},{0,3},{0,-3},{4,4}}){int x=location.getBlockX()+offset[0],z=location.getBlockZ()+offset[1];World world=location.getWorld();if(!world.isChunkLoaded(x>>4,z>>4))continue;int y=world.getHighestBlockYAt(x,z);var ground=world.getBlockAt(x,y,z);if(!ground.getType().isSolid()||Set.of(Material.MAGMA_BLOCK,Material.CACTUS,Material.CAMPFIRE,Material.SOUL_CAMPFIRE).contains(ground.getType())||!world.getBlockAt(x,y+1,z).isPassable()||!world.getBlockAt(x,y+2,z).isPassable())continue; + world.spawn(new Location(world,x+.5,y+1,z+.5),Wolf.class,w->{w.getPersistentDataContainer().set(PET,PersistentDataType.BYTE,(byte)1);w.setBaby();w.setRemoveWhenFarAway(false);w.customName(Component.text("Tiny worm",NamedTextColor.GOLD));});chunk.getPersistentDataContainer().set(marker,PersistentDataType.BYTE,(byte)1);return; + } + } + private static void consume(Player p,EquipmentSlot hand){if(p.getGameMode()==GameMode.CREATIVE)return;var inv=p.getInventory();var item=inv.getItem(hand);if(item!=null){item.setAmount(item.getAmount()-1);inv.setItem(hand,item);}} + private static void container(Player player,Material material){if(player.getGameMode()!=GameMode.CREATIVE)for(var rest:player.getInventory().addItem(new ItemStack(material)).values())player.getWorld().dropItemNaturally(player.getLocation(),rest);} + @EventHandler(priority=EventPriority.HIGHEST,ignoreCancelled=true) public void feed(PlayerInteractEntityEvent event){ + var player=event.getPlayer();var entity=event.getRightClicked();if(!arrakis(entity))return;var stack=player.getInventory().getItem(event.getHand());String id=itemId(stack); + if(entity instanceof Rabbit rabbit){event.setCancelled(true);if("refined_spice".equals(id)&&rabbit.isAdult()&&rabbit.canBreed()&&rabbit.getLoveModeTicks()==0){rabbit.setLoveModeTicks(600);consume(player,event.getHand());}return;} + if(!(entity instanceof Wolf wolf)||!pet(wolf))return; + if("jerboa_stew".equals(id)){ + event.setCancelled(true);if(!wolf.isTamed()){var tame=new EntityTameEvent(wolf,player);Bukkit.getPluginManager().callEvent(tame);if(tame.isCancelled())return;wolf.setOwner(player);wolf.setSitting(false);wolf.getWorld().spawnParticle(Particle.HEART,wolf.getLocation().add(0,.5,0),7,.3,.3,.3);} + else if(wolf.getHealth()0;modifier(wolf,Attribute.ATTACK_DAMAGE,DAMAGE,active);modifier(wolf,Attribute.MAX_HEALTH,HEALTH,active);var regen=wolf.getPotionEffect(PotionEffectType.REGENERATION);if(active&&(regen==null||regen.getAmplifier()==0))wolf.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION,(int)Math.min(1800,Math.max(1,(remaining+49)/50)),0),true);if(!active){if(until>0&®en!=null&®en.getAmplifier()==0)wolf.removePotionEffect(PotionEffectType.REGENERATION);wolf.getPersistentDataContainer().remove(BOOST);wolf.setHealth(Math.min(wolf.getHealth(),wolf.getAttribute(Attribute.MAX_HEALTH).getValue()));}} + @EventHandler(priority=EventPriority.HIGHEST) public void death(EntityDeathEvent event){ + var entity=event.getEntity();if(!arrakis(entity))return; + try{ + if(entity instanceof Rabbit){var raw=items.build(entity.getFireTicks()>0?"jerboa_cooked_meat":"jerboa_raw_meat");var hide=items.build("jerboa_hide");var drops=new ArrayList();drops.add(raw);drops.add(hide);if(random.getAsDouble()<.2)drops.add(items.build("jerboa_water_vial"));event.getDrops().clear();event.getDrops().addAll(drops);} + else if(pet(entity)){var bone=items.build("worm_memorial_bone");var meta=bone.getItemMeta();var name=entity.customName()==null?Component.text("Tiny worm"):entity.customName();meta.displayName(name.append(Component.text(" ยท Memorial bone",NamedTextColor.GOLD)));bone.setItemMeta(meta);var spice=new ItemStack(Material.ORANGE_DYE);var sm=spice.getItemMeta();sm.displayName(Component.text("Melange Spice",NamedTextColor.GOLD));sm.getPersistentDataContainer().set(new NamespacedKey("dunerealms","item"),PersistentDataType.STRING,"spice");spice.setItemMeta(sm);event.getDrops().clear();event.getDrops().add(spice);event.getDrops().add(bone);} + }catch(ReflectiveOperationException failure){plugin.getLogger().severe("Native Arrakis item definition unavailable: "+failure.getClass().getSimpleName());} + } + @EventHandler(priority=EventPriority.HIGHEST,ignoreCancelled=true) public void cook(BlockCookEvent event){if(!event.getBlock().getWorld().getName().equalsIgnoreCase("arrakis")||!"jerboa_raw_meat".equals(itemId(event.getSource())))return;try{event.setResult(items.build("jerboa_cooked_meat"));}catch(ReflectiveOperationException failure){event.setCancelled(true);plugin.getLogger().warning("Jerboa cooking definition unavailable; input retained");}} + private static Object call(Object target,String name,Object...args)throws ReflectiveOperationException{for(var method:target.getClass().getMethods())if(method.getName().equals(name)&&method.getParameterCount()==args.length){var types=method.getParameterTypes();boolean valid=true;for(int i=0;i)call(manager,"getExecutableItem",id);if(definition.isEmpty())throw new ReflectiveOperationException("Missing native item definition");return (ItemStack)call(definition.get(),"buildItem",1,Optional.empty(),Optional.empty());} + @EventHandler public void disable(PluginDisableEvent event){if(event.getPlugin()!=plugin)return;INSTANCES.remove(plugin);for(var world:Bukkit.getWorlds())for(var wolf:world.getEntitiesByClass(Wolf.class))if(pet(wolf)){modifier(wolf,Attribute.ATTACK_DAMAGE,DAMAGE,false);modifier(wolf,Attribute.MAX_HEALTH,HEALTH,false);wolf.setHealth(Math.min(wolf.getHealth(),wolf.getAttribute(Attribute.MAX_HEALTH).getValue()));}} +} diff --git a/DuneTerrain/src/main/java/com/example/duneterrain/DuneTerrain.java b/DuneTerrain/src/main/java/com/example/duneterrain/DuneTerrain.java index 05f920c8..8373b3d9 100644 --- a/DuneTerrain/src/main/java/com/example/duneterrain/DuneTerrain.java +++ b/DuneTerrain/src/main/java/com/example/duneterrain/DuneTerrain.java @@ -1,910 +1,912 @@ -package com.example.duneterrain; - -import org.bukkit.*; -import org.bukkit.block.Biome; -import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; -import org.bukkit.block.Chest; -import org.bukkit.block.BlockState; -import org.bukkit.block.data.type.Bed; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Arrow; -import org.bukkit.entity.Trident; - -import io.papermc.paper.event.entity.EntityMoveEvent; -import org.bukkit.event.block.Action; -import org.bukkit.event.entity.ProjectileHitEvent; -import org.bukkit.generator.*; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryCloseEvent; -import org.bukkit.event.inventory.InventoryDragEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerMoveEvent; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.persistence.PersistentDataType; -import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.server.ServerLoadEvent; -import org.bukkit.event.world.ChunkLoadEvent; -import java.io.File; -import java.util.*; - -public final class DuneTerrain extends JavaPlugin implements Listener { - private static final String[] PLANETS = {"arrakis", "giedi_prime", "kaitain", "caladan", "sigma_draconis_iv"}; - - private NamespacedKey sietchLootIdKey; - private final Map openSietchLoot = new HashMap<>(); - private record PersonalLoot(Inventory inventory, String id) {} - +package com.example.duneterrain; + +import org.bukkit.*; +import org.bukkit.block.Biome; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.Chest; +import org.bukkit.block.BlockState; +import org.bukkit.block.data.type.Bed; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Arrow; +import org.bukkit.entity.Trident; + +import io.papermc.paper.event.entity.EntityMoveEvent; +import org.bukkit.event.block.Action; +import org.bukkit.event.entity.ProjectileHitEvent; +import org.bukkit.generator.*; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.inventory.InventoryDragEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.persistence.PersistentDataType; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.server.ServerLoadEvent; +import org.bukkit.event.world.ChunkLoadEvent; +import java.io.File; +import java.util.*; + +public final class DuneTerrain extends JavaPlugin implements Listener { + private static final String[] PLANETS = {"arrakis", "giedi_prime", "kaitain", "caladan", "sigma_draconis_iv"}; + + private NamespacedKey sietchLootIdKey; + private final Map openSietchLoot = new HashMap<>(); + private record PersonalLoot(Inventory inventory, String id) {} + public void announceSpiceBloom(Location location) { - org.bukkit.plugin.Plugin core = Bukkit.getPluginManager().getPlugin("DuneCore"); - if (core == null || !core.isEnabled()) return; - try { - String id = "bloom:" + location.getWorld().getUID() + ":" + location.getBlockX() + ":" + location.getBlockY() + ":" + location.getBlockZ(); - core.getClass().getMethod("announceArrakisEvent", String.class, Location.class, String.class) - .invoke(core, "spicebloom", location, id); - } catch (ReflectiveOperationException ex) { getLogger().warning("Spice bloom announcement failed: " + ex.getMessage()); } - } - - @EventHandler public void onBloomChunkLoad(ChunkLoadEvent event) { - if (!event.getWorld().getName().equalsIgnoreCase("arrakis")) return; - Chunk chunk = event.getChunk(); - Bukkit.getScheduler().runTask(this, () -> inspectBlooms(chunk)); - } - - private void inspectBlooms(Chunk chunk) { - World world = chunk.getWorld(); - if (!chunk.isLoaded()) return; - for (int x = 0; x < 16; x++) for (int z = 0; z < 16; z++) { - int wx = chunk.getX() * 16 + x, wz = chunk.getZ() * 16 + z; - int y = world.getHighestBlockYAt(wx, wz); - // The raised marker sits above a 3x3 magma patch; no neighbouring chunk access. - if (y > world.getMinHeight() && chunk.getBlock(x, y, z).getType() == Material.MAGMA_BLOCK - && chunk.getBlock(x, y - 1, z).getType() == Material.MAGMA_BLOCK) - announceSpiceBloom(new Location(world, wx, y, wz)); - } - } - - @EventHandler public void onSpiceBlowProjectile(ProjectileHitEvent event) { - if (!(event.getEntity() instanceof Arrow) && !(event.getEntity() instanceof Trident)) return; - if (event.getHitBlock() != null) triggerSpiceBlowAt(event.getHitBlock().getLocation()); - else if (event.getHitEntity() != null) triggerSpiceBlowAt(event.getHitEntity().getLocation()); - } - - @EventHandler public void onSpiceBlowPlayerMove(PlayerMoveEvent event) { - if (event.getTo() == null || (event.getFrom().getBlockX() == event.getTo().getBlockX() - && event.getFrom().getBlockY() == event.getTo().getBlockY() - && event.getFrom().getBlockZ() == event.getTo().getBlockZ())) return; - triggerSpiceBlowAt(event.getTo()); - } - - @EventHandler public void onSpiceBlowEntityMove(EntityMoveEvent event) { - if (event.hasChangedBlock() && event.getTo() != null) triggerSpiceBlowAt(event.getTo()); - } - - private void triggerSpiceBlowAt(Location location) { - if (location == null || location.getWorld() == null || !location.getWorld().getName().equalsIgnoreCase("arrakis")) return; - int x = location.getBlockX(), y = location.getBlockY(), z = location.getBlockZ(); - for (int dy = -1; dy <= 0; dy++) for (int dx = -1; dx <= 1; dx++) for (int dz = -1; dz <= 1; dz++) { - Block block = location.getWorld().getBlockAt(x + dx, y + dy, z + dz); - if (block.getType() != Material.MAGMA_BLOCK) continue; - Block center = findSpiceBlowCenter(block); - if (center != null) detonateSpiceBlow(center); - } - } - - private Block findSpiceBlowCenter(Block touched) { - World world = touched.getWorld(); - for (int cx = touched.getX() - 1; cx <= touched.getX() + 1; cx++) - for (int cz = touched.getZ() - 1; cz <= touched.getZ() + 1; cz++) - for (int topY = touched.getY() - 1; topY <= touched.getY() + 1; topY++) { - if (world.getBlockAt(cx, topY, cz).getType() != Material.MAGMA_BLOCK - || world.getBlockAt(cx, topY - 1, cz).getType() != Material.MAGMA_BLOCK) continue; - boolean patch = true; - for (int dx = -1; dx <= 1 && patch; dx++) for (int dz = -1; dz <= 1; dz++) - if (world.getBlockAt(cx + dx, topY - 1, cz + dz).getType() != Material.MAGMA_BLOCK) { patch = false; break; } - if (patch) return world.getBlockAt(cx, topY, cz); - } - return null; - } - - private void detonateSpiceBlow(Block center) { - if (center.getType() != Material.MAGMA_BLOCK) return; - World world = center.getWorld(); - int x = center.getX(), z = center.getZ(), baseY = center.getY() - 1; - for (int dx = -1; dx <= 1; dx++) for (int dz = -1; dz <= 1; dz++) - world.getBlockAt(x + dx, baseY, z + dz).setType(Material.AIR, false); - center.setType(Material.GLOWSTONE, false); - world.playSound(center.getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 1.5f, 0.7f); - world.spawnParticle(Particle.EXPLOSION, center.getLocation().add(0.5, 0.5, 0.5), 1); - - String path = "spice-blow."; - int targetValue = Math.max(1, getConfig().getInt(path + "target-value", 15000)); - int unitPrice = Math.max(1, getConfig().getInt(path + "unit-price", 15)); - org.bukkit.plugin.Plugin economy = Bukkit.getPluginManager().getPlugin("DuneEss"); - if (economy instanceof JavaPlugin economyPlugin) - unitPrice = Math.max(1, economyPlugin.getConfig().getInt("currency.spice-price", unitPrice)); - int targetDeposits = (int)Math.ceil(targetValue / (double)unitPrice); - int radius = Math.max(12, getConfig().getInt(path + "radius", 24)); - List candidates = new ArrayList<>(); - for (int dx = -radius; dx <= radius; dx++) for (int dz = -radius; dz <= radius; dz++) { - if (dx * dx + dz * dz > radius * radius) continue; - int bx = x + dx, bz = z + dz; - int surfaceY = world.getHighestBlockYAt(bx, bz); - Material surface = world.getBlockAt(bx, surfaceY, bz).getType(); - if (surface == Material.SAND || surface == Material.RED_SAND) candidates.add(world.getBlockAt(bx, surfaceY, bz)); - } - candidates.sort(Comparator.comparingLong(block -> mix64((long)block.getX() * 0x9E3779B97F4A7C15L - ^ (long)block.getZ() * 0xC2B2AE3D27D4EB4FL ^ world.getSeed() ^ 0x5350494345424c4fL))); - int placed = 0; - for (Block block : candidates) { - if (placed >= targetDeposits) break; - block.setType(Material.SUSPICIOUS_SAND, false); - placed++; - } - announceSpiceBloom(center.getLocation()); - for (org.bukkit.entity.Player player : world.getPlayers()) player.sendMessage(ChatColor.GOLD + "A spice blow erupted at X " + x + ", Y " + center.getY() + ", Z " + z + " on Arrakis! " + placed + " deposits surfaced."); - getLogger().info("Spice blow at " + x + ", " + z + " deployed " + placed + " spice deposits (target value " + targetValue + " Solaris)."); - } - - @EventHandler public void onSietchChestOpen(PlayerInteractEvent event) { - if (event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getClickedBlock() == null) return; - if (event.getHand() != org.bukkit.inventory.EquipmentSlot.HAND) return; - BlockState state = event.getClickedBlock().getState(); - if (!(state instanceof Chest chest)) return; - String id = chest.getPersistentDataContainer().get(sietchLootIdKey, PersistentDataType.STRING); - if (id == null) return; - event.setCancelled(true); - org.bukkit.entity.Player player = event.getPlayer(); - NamespacedKey claimedKey = new NamespacedKey(this, "sietch_claim_" + id); - if (player.getPersistentDataContainer().has(claimedKey, PersistentDataType.BYTE)) { - player.sendMessage(ChatColor.YELLOW + "You have already claimed this sietch cache."); - return; - } - Inventory inventory = Bukkit.createInventory(null, 27, ChatColor.DARK_GRAY + "Sietch Cache"); - addLoot(inventory, spiceItem(64)); addLoot(inventory, spiceItem(64)); addLoot(inventory, spiceItem(64)); addLoot(inventory, spiceItem(58)); - addLoot(inventory, enchantedGold(Material.GOLDEN_HELMET)); - addLoot(inventory, enchantedGold(Material.GOLDEN_CHESTPLATE)); - addLoot(inventory, enchantedGold(Material.GOLDEN_LEGGINGS)); - addLoot(inventory, enchantedGold(Material.GOLDEN_BOOTS)); - addLoot(inventory, new ItemStack(Material.OAK_PLANKS, 32)); - addLoot(inventory, new ItemStack(Material.BREAD, 16)); - openSietchLoot.put(player.getUniqueId(), new PersonalLoot(inventory, id)); - player.openInventory(inventory); - player.sendMessage(ChatColor.GOLD + "This personal cache contains supplies for you."); - } - - @EventHandler public void onSietchLootClick(InventoryClickEvent event) { - if (!(event.getWhoClicked() instanceof org.bukkit.entity.Player player)) return; - PersonalLoot loot = openSietchLoot.get(player.getUniqueId()); - if (loot == null || event.getView().getTopInventory() != loot.inventory()) return; - if (event.getClickedInventory() != loot.inventory()) return; - event.setCancelled(true); - ItemStack item = event.getCurrentItem(); - if (item == null || item.getType().isAir()) return; - Map leftovers = player.getInventory().addItem(item.clone()); - if (leftovers.isEmpty()) loot.inventory().setItem(event.getSlot(), null); - else { - loot.inventory().setItem(event.getSlot(), leftovers.values().iterator().next()); - player.sendMessage(ChatColor.RED + "Your inventory only had room for part of that stack."); - } - } - - @EventHandler public void onSietchLootDrag(InventoryDragEvent event) { - if (!(event.getWhoClicked() instanceof org.bukkit.entity.Player player)) return; - PersonalLoot loot = openSietchLoot.get(player.getUniqueId()); - if (loot != null && event.getView().getTopInventory() == loot.inventory()) event.setCancelled(true); - } - - @EventHandler public void onSietchLootClose(InventoryCloseEvent event) { - if (!(event.getPlayer() instanceof org.bukkit.entity.Player player)) return; - PersonalLoot loot = openSietchLoot.get(player.getUniqueId()); - if (loot == null || event.getView().getTopInventory() != loot.inventory()) return; - openSietchLoot.remove(player.getUniqueId()); - for (ItemStack item : loot.inventory().getContents()) if (item != null && !item.getType().isAir()) { - Map leftovers = player.getInventory().addItem(item); - leftovers.values().forEach(left -> player.getWorld().dropItemNaturally(player.getLocation(), left)); - } - player.getPersistentDataContainer().set(new NamespacedKey(this, "sietch_claim_" + loot.id()), PersistentDataType.BYTE, (byte) 1); - } - - private static void addLoot(Inventory inventory, ItemStack item) { - for (int slot = 0; slot < inventory.getSize(); slot++) if (inventory.getItem(slot) == null) { - inventory.setItem(slot, item); - return; - } - } - - static ItemStack spiceItem(int amount) { - ItemStack item = new ItemStack(Material.ORANGE_DYE, amount); - ItemMeta meta = item.getItemMeta(); - meta.setDisplayName(ChatColor.GOLD + "Melange Spice"); - meta.getPersistentDataContainer().set(new NamespacedKey("dunerealms", "item"), PersistentDataType.STRING, "spice"); - item.setItemMeta(meta); - return item; - } - - private static ItemStack enchantedGold(Material material) { - ItemStack item = new ItemStack(material); - ItemMeta meta = item.getItemMeta(); - meta.addEnchant(Enchantment.PROTECTION, 2, true); - meta.addEnchant(Enchantment.UNBREAKING, 2, true); - item.setItemMeta(meta); - return item; - } - - @Override public ChunkGenerator getDefaultWorldGenerator(String worldName, String id) { - String planet = id == null || id.isBlank() - ? getConfig().getString("server.planet", "arrakis") - : id; - planet = planet.toLowerCase(Locale.ROOT); - if (!Arrays.asList(PLANETS).contains(planet)) { - getLogger().severe("Invalid generator planet '" + planet + "' for world '" + worldName + "'."); - return null; - } - if (!getConfig().getBoolean("planets." + planet + ".enabled", true)) { - getLogger().severe("Generator planet '" + planet + "' is disabled in config.yml."); - return null; - } - return new PlanetChunkGenerator(this, planet); - } - - @Override public BiomeProvider getDefaultBiomeProvider(String worldName, String id) { - String planet = id == null || id.isBlank() - ? getConfig().getString("server.planet", "arrakis") - : id; - planet = planet.toLowerCase(Locale.ROOT); - if (!Arrays.asList(PLANETS).contains(planet)) { - getLogger().severe("Invalid biome-provider planet '" + planet + "' for world '" + worldName + "'."); - return null; - } - return new PlanetBiomeProvider(this, planet); - } - + ArrakisEcology.bloom(this, location); + org.bukkit.plugin.Plugin core = Bukkit.getPluginManager().getPlugin("DuneCore"); + if (core == null || !core.isEnabled()) return; + try { + String id = "bloom:" + location.getWorld().getUID() + ":" + location.getBlockX() + ":" + location.getBlockY() + ":" + location.getBlockZ(); + core.getClass().getMethod("announceArrakisEvent", String.class, Location.class, String.class) + .invoke(core, "spicebloom", location, id); + } catch (ReflectiveOperationException ex) { getLogger().warning("Spice bloom announcement failed: " + ex.getMessage()); } + } + + @EventHandler public void onBloomChunkLoad(ChunkLoadEvent event) { + if (!event.getWorld().getName().equalsIgnoreCase("arrakis")) return; + Chunk chunk = event.getChunk(); + Bukkit.getScheduler().runTask(this, () -> inspectBlooms(chunk)); + } + + private void inspectBlooms(Chunk chunk) { + World world = chunk.getWorld(); + if (!chunk.isLoaded()) return; + for (int x = 0; x < 16; x++) for (int z = 0; z < 16; z++) { + int wx = chunk.getX() * 16 + x, wz = chunk.getZ() * 16 + z; + int y = world.getHighestBlockYAt(wx, wz); + // The raised marker sits above a 3x3 magma patch; no neighbouring chunk access. + if (y > world.getMinHeight() && chunk.getBlock(x, y, z).getType() == Material.MAGMA_BLOCK + && chunk.getBlock(x, y - 1, z).getType() == Material.MAGMA_BLOCK) + announceSpiceBloom(new Location(world, wx, y, wz)); + } + } + + @EventHandler public void onSpiceBlowProjectile(ProjectileHitEvent event) { + if (!(event.getEntity() instanceof Arrow) && !(event.getEntity() instanceof Trident)) return; + if (event.getHitBlock() != null) triggerSpiceBlowAt(event.getHitBlock().getLocation()); + else if (event.getHitEntity() != null) triggerSpiceBlowAt(event.getHitEntity().getLocation()); + } + + @EventHandler public void onSpiceBlowPlayerMove(PlayerMoveEvent event) { + if (event.getTo() == null || (event.getFrom().getBlockX() == event.getTo().getBlockX() + && event.getFrom().getBlockY() == event.getTo().getBlockY() + && event.getFrom().getBlockZ() == event.getTo().getBlockZ())) return; + triggerSpiceBlowAt(event.getTo()); + } + + @EventHandler public void onSpiceBlowEntityMove(EntityMoveEvent event) { + if (event.hasChangedBlock() && event.getTo() != null) triggerSpiceBlowAt(event.getTo()); + } + + private void triggerSpiceBlowAt(Location location) { + if (location == null || location.getWorld() == null || !location.getWorld().getName().equalsIgnoreCase("arrakis")) return; + int x = location.getBlockX(), y = location.getBlockY(), z = location.getBlockZ(); + for (int dy = -1; dy <= 0; dy++) for (int dx = -1; dx <= 1; dx++) for (int dz = -1; dz <= 1; dz++) { + Block block = location.getWorld().getBlockAt(x + dx, y + dy, z + dz); + if (block.getType() != Material.MAGMA_BLOCK) continue; + Block center = findSpiceBlowCenter(block); + if (center != null) detonateSpiceBlow(center); + } + } + + private Block findSpiceBlowCenter(Block touched) { + World world = touched.getWorld(); + for (int cx = touched.getX() - 1; cx <= touched.getX() + 1; cx++) + for (int cz = touched.getZ() - 1; cz <= touched.getZ() + 1; cz++) + for (int topY = touched.getY() - 1; topY <= touched.getY() + 1; topY++) { + if (world.getBlockAt(cx, topY, cz).getType() != Material.MAGMA_BLOCK + || world.getBlockAt(cx, topY - 1, cz).getType() != Material.MAGMA_BLOCK) continue; + boolean patch = true; + for (int dx = -1; dx <= 1 && patch; dx++) for (int dz = -1; dz <= 1; dz++) + if (world.getBlockAt(cx + dx, topY - 1, cz + dz).getType() != Material.MAGMA_BLOCK) { patch = false; break; } + if (patch) return world.getBlockAt(cx, topY, cz); + } + return null; + } + + private void detonateSpiceBlow(Block center) { + if (center.getType() != Material.MAGMA_BLOCK) return; + World world = center.getWorld(); + int x = center.getX(), z = center.getZ(), baseY = center.getY() - 1; + for (int dx = -1; dx <= 1; dx++) for (int dz = -1; dz <= 1; dz++) + world.getBlockAt(x + dx, baseY, z + dz).setType(Material.AIR, false); + center.setType(Material.GLOWSTONE, false); + world.playSound(center.getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 1.5f, 0.7f); + world.spawnParticle(Particle.EXPLOSION, center.getLocation().add(0.5, 0.5, 0.5), 1); + + String path = "spice-blow."; + int targetValue = Math.max(1, getConfig().getInt(path + "target-value", 15000)); + int unitPrice = Math.max(1, getConfig().getInt(path + "unit-price", 15)); + org.bukkit.plugin.Plugin economy = Bukkit.getPluginManager().getPlugin("DuneEss"); + if (economy instanceof JavaPlugin economyPlugin) + unitPrice = Math.max(1, economyPlugin.getConfig().getInt("currency.spice-price", unitPrice)); + int targetDeposits = (int)Math.ceil(targetValue / (double)unitPrice); + int radius = Math.max(12, getConfig().getInt(path + "radius", 24)); + List candidates = new ArrayList<>(); + for (int dx = -radius; dx <= radius; dx++) for (int dz = -radius; dz <= radius; dz++) { + if (dx * dx + dz * dz > radius * radius) continue; + int bx = x + dx, bz = z + dz; + int surfaceY = world.getHighestBlockYAt(bx, bz); + Material surface = world.getBlockAt(bx, surfaceY, bz).getType(); + if (surface == Material.SAND || surface == Material.RED_SAND) candidates.add(world.getBlockAt(bx, surfaceY, bz)); + } + candidates.sort(Comparator.comparingLong(block -> mix64((long)block.getX() * 0x9E3779B97F4A7C15L + ^ (long)block.getZ() * 0xC2B2AE3D27D4EB4FL ^ world.getSeed() ^ 0x5350494345424c4fL))); + int placed = 0; + for (Block block : candidates) { + if (placed >= targetDeposits) break; + block.setType(Material.SUSPICIOUS_SAND, false); + placed++; + } + announceSpiceBloom(center.getLocation()); + for (org.bukkit.entity.Player player : world.getPlayers()) player.sendMessage(ChatColor.GOLD + "A spice blow erupted at X " + x + ", Y " + center.getY() + ", Z " + z + " on Arrakis! " + placed + " deposits surfaced."); + getLogger().info("Spice blow at " + x + ", " + z + " deployed " + placed + " spice deposits (target value " + targetValue + " Solaris)."); + } + + @EventHandler public void onSietchChestOpen(PlayerInteractEvent event) { + if (event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getClickedBlock() == null) return; + if (event.getHand() != org.bukkit.inventory.EquipmentSlot.HAND) return; + BlockState state = event.getClickedBlock().getState(); + if (!(state instanceof Chest chest)) return; + String id = chest.getPersistentDataContainer().get(sietchLootIdKey, PersistentDataType.STRING); + if (id == null) return; + event.setCancelled(true); + org.bukkit.entity.Player player = event.getPlayer(); + NamespacedKey claimedKey = new NamespacedKey(this, "sietch_claim_" + id); + if (player.getPersistentDataContainer().has(claimedKey, PersistentDataType.BYTE)) { + player.sendMessage(ChatColor.YELLOW + "You have already claimed this sietch cache."); + return; + } + Inventory inventory = Bukkit.createInventory(null, 27, ChatColor.DARK_GRAY + "Sietch Cache"); + addLoot(inventory, spiceItem(64)); addLoot(inventory, spiceItem(64)); addLoot(inventory, spiceItem(64)); addLoot(inventory, spiceItem(58)); + addLoot(inventory, enchantedGold(Material.GOLDEN_HELMET)); + addLoot(inventory, enchantedGold(Material.GOLDEN_CHESTPLATE)); + addLoot(inventory, enchantedGold(Material.GOLDEN_LEGGINGS)); + addLoot(inventory, enchantedGold(Material.GOLDEN_BOOTS)); + addLoot(inventory, new ItemStack(Material.OAK_PLANKS, 32)); + addLoot(inventory, new ItemStack(Material.BREAD, 16)); + openSietchLoot.put(player.getUniqueId(), new PersonalLoot(inventory, id)); + player.openInventory(inventory); + player.sendMessage(ChatColor.GOLD + "This personal cache contains supplies for you."); + } + + @EventHandler public void onSietchLootClick(InventoryClickEvent event) { + if (!(event.getWhoClicked() instanceof org.bukkit.entity.Player player)) return; + PersonalLoot loot = openSietchLoot.get(player.getUniqueId()); + if (loot == null || event.getView().getTopInventory() != loot.inventory()) return; + if (event.getClickedInventory() != loot.inventory()) return; + event.setCancelled(true); + ItemStack item = event.getCurrentItem(); + if (item == null || item.getType().isAir()) return; + Map leftovers = player.getInventory().addItem(item.clone()); + if (leftovers.isEmpty()) loot.inventory().setItem(event.getSlot(), null); + else { + loot.inventory().setItem(event.getSlot(), leftovers.values().iterator().next()); + player.sendMessage(ChatColor.RED + "Your inventory only had room for part of that stack."); + } + } + + @EventHandler public void onSietchLootDrag(InventoryDragEvent event) { + if (!(event.getWhoClicked() instanceof org.bukkit.entity.Player player)) return; + PersonalLoot loot = openSietchLoot.get(player.getUniqueId()); + if (loot != null && event.getView().getTopInventory() == loot.inventory()) event.setCancelled(true); + } + + @EventHandler public void onSietchLootClose(InventoryCloseEvent event) { + if (!(event.getPlayer() instanceof org.bukkit.entity.Player player)) return; + PersonalLoot loot = openSietchLoot.get(player.getUniqueId()); + if (loot == null || event.getView().getTopInventory() != loot.inventory()) return; + openSietchLoot.remove(player.getUniqueId()); + for (ItemStack item : loot.inventory().getContents()) if (item != null && !item.getType().isAir()) { + Map leftovers = player.getInventory().addItem(item); + leftovers.values().forEach(left -> player.getWorld().dropItemNaturally(player.getLocation(), left)); + } + player.getPersistentDataContainer().set(new NamespacedKey(this, "sietch_claim_" + loot.id()), PersistentDataType.BYTE, (byte) 1); + } + + private static void addLoot(Inventory inventory, ItemStack item) { + for (int slot = 0; slot < inventory.getSize(); slot++) if (inventory.getItem(slot) == null) { + inventory.setItem(slot, item); + return; + } + } + + static ItemStack spiceItem(int amount) { + ItemStack item = new ItemStack(Material.ORANGE_DYE, amount); + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(ChatColor.GOLD + "Melange Spice"); + meta.getPersistentDataContainer().set(new NamespacedKey("dunerealms", "item"), PersistentDataType.STRING, "spice"); + item.setItemMeta(meta); + return item; + } + + private static ItemStack enchantedGold(Material material) { + ItemStack item = new ItemStack(material); + ItemMeta meta = item.getItemMeta(); + meta.addEnchant(Enchantment.PROTECTION, 2, true); + meta.addEnchant(Enchantment.UNBREAKING, 2, true); + item.setItemMeta(meta); + return item; + } + + @Override public ChunkGenerator getDefaultWorldGenerator(String worldName, String id) { + String planet = id == null || id.isBlank() + ? getConfig().getString("server.planet", "arrakis") + : id; + planet = planet.toLowerCase(Locale.ROOT); + if (!Arrays.asList(PLANETS).contains(planet)) { + getLogger().severe("Invalid generator planet '" + planet + "' for world '" + worldName + "'."); + return null; + } + if (!getConfig().getBoolean("planets." + planet + ".enabled", true)) { + getLogger().severe("Generator planet '" + planet + "' is disabled in config.yml."); + return null; + } + return new PlanetChunkGenerator(this, planet); + } + + @Override public BiomeProvider getDefaultBiomeProvider(String worldName, String id) { + String planet = id == null || id.isBlank() + ? getConfig().getString("server.planet", "arrakis") + : id; + planet = planet.toLowerCase(Locale.ROOT); + if (!Arrays.asList(PLANETS).contains(planet)) { + getLogger().severe("Invalid biome-provider planet '" + planet + "' for world '" + worldName + "'."); + return null; + } + return new PlanetBiomeProvider(this, planet); + } + @Override public void onEnable() { - saveDefaultConfig(); - String formations = "planets.arrakis.darkstone-islands."; - if (getConfig().getInt(formations + "min-radius") == 10 - && getConfig().getInt(formations + "max-radius") == 18) { - getConfig().set(formations + "min-radius", 45); - getConfig().set(formations + "max-radius", 55); - saveConfig(); - getLogger().info("Upgraded default Arrakis rock formations to about 100 blocks across in new chunks."); - } - getServer().getPluginManager().registerEvents(this, this); - getServer().getPluginManager().registerEvents(new DenseSpice(this), this); - getServer().getPluginManager().registerEvents(new HarkonnenFortress(this), this); - - sietchLootIdKey = new NamespacedKey(this, "sietch-loot-id"); - String planet = getConfig().getString("server.planet", "arrakis").toLowerCase(Locale.ROOT); - if (!Arrays.asList(PLANETS).contains(planet)) { - getLogger().severe("Invalid server.planet '" + planet + "'. Choose one of: " + String.join(", ", PLANETS)); - getServer().getPluginManager().disablePlugin(this); - return; - } - if (!getConfig().getBoolean("planets." + planet + ".enabled", true)) { - getLogger().severe("Planet '" + planet + "' is disabled in config.yml."); - getServer().getPluginManager().disablePlugin(this); - return; - } - getLogger().info("DuneTerrain registered the " + planet + " generator. World setup will run after Paper finishes loading worlds."); - } - - @EventHandler public void onServerLoad(ServerLoadEvent event) { - World arrakis = Bukkit.getWorld("arrakis"); - if (arrakis != null) for (Chunk chunk : arrakis.getLoadedChunks()) - Bukkit.getScheduler().runTask(this, () -> inspectBlooms(chunk)); - String planet = getConfig().getString("server.planet", "arrakis").toLowerCase(Locale.ROOT); - World world = Bukkit.getWorld(planet); - if (world == null) { - File worldFolder = new File(getServer().getWorldContainer(), planet); - boolean existingWorld = worldFolder.isDirectory(); - world = Bukkit.createWorld(new WorldCreator(planet) - .environment(World.Environment.NORMAL) - .generator(new PlanetChunkGenerator(this, planet))); - if (world == null) { - getLogger().severe("Could not create or load world '" + planet + "'."); - return; - } - if (existingWorld) getLogger().warning("Loaded existing world '" + planet + "'. Existing chunks remain unchanged; move the world folder after backing it up to regenerate terrain."); - else getLogger().info("Created world '" + planet + "' with DuneTerrain."); - } else if (!(world.getGenerator() instanceof PlanetChunkGenerator)) { - getLogger().severe("World '" + planet + "' was loaded without DuneTerrain's generator. Configure bukkit.yml worlds." + planet + ".generator: DuneTerrain:" + planet + " and worlds." + planet + ".biome-provider: DuneTerrain:" + planet + ". Existing chunks remain unchanged."); - } else { - getLogger().info("World '" + planet + "' was loaded with DuneTerrain's configured generator."); - } - world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, !planet.equals("arrakis")); - world.setTime(planet.equals("giedi_prime") ? 18000 : 6000); - setupPlanetSpawn(world, planet); - } - - private void setupPlanetSpawn(World world, String planet) { - String path = "planets." + planet + ".spawn."; - int x = getConfig().getInt(path + "x", 0), z = getConfig().getInt(path + "z", 0); - world.getChunkAt(x >> 4, z >> 4).load(true); - Location spawn = findSafeSpawn(world, x, z); - world.setSpawnLocation(spawn); - - } - - private Location findSafeSpawn(World world, int x, int z) { - for (int radius = 0; radius <= 12; radius++) { - for (int dx = -radius; dx <= radius; dx++) for (int dz = -radius; dz <= radius; dz++) { - if (radius > 0 && Math.abs(dx) != radius && Math.abs(dz) != radius) continue; - int bx = x + dx, bz = z + dz; - int y = world.getHighestBlockYAt(bx, bz); - Material ground = world.getBlockAt(bx, y, bz).getType(); - if (!ground.isSolid() || ground == Material.MAGMA_BLOCK || ground == Material.CACTUS || ground == Material.POWDER_SNOW) continue; - if (world.getBlockAt(bx, y + 1, bz).isLiquid() || world.getBlockAt(bx, y + 2, bz).isLiquid()) continue; - return new Location(world, bx + 0.5, y + 1, bz + 0.5); - } - } - int y = Math.max(world.getMinHeight() + 2, world.getHighestBlockYAt(x, z) + 1); - return new Location(world, x + 0.5, y, z + 0.5); - } - - private static class PlanetChunkGenerator extends ChunkGenerator { - private final DuneTerrain plugin; - private final String planet; - private record GroundKey(RockPlateaus.Shape shape, int baseHeight, double variation) {} - private final java.util.concurrent.ConcurrentHashMap formationGround = new java.util.concurrent.ConcurrentHashMap<>(); - PlanetChunkGenerator(DuneTerrain plugin, String planet) { this.plugin = plugin; this.planet = planet; } - @Override public List getDefaultPopulators(World world) { - return planet.equals("arrakis") && world.getName().equalsIgnoreCase("arrakis") - ? List.of(new SietchPopulator(plugin), new SpiceBlowPopulator(plugin), new DesertVillages(plugin), new HarkonnenFortress(plugin)) : List.of(); - } - @Override public void generateNoise(WorldInfo world, Random random, int chunkX, int chunkZ, ChunkData data) { - String path = "planets." + planet + "."; - int minY = world.getMinHeight(), maxY = world.getMaxHeight() - 1; - int baseHeight = plugin.getConfig().getInt(path + "terrain.base-height", 68); - double variation = plugin.getConfig().getDouble(path + "terrain.height-variation", 8); - double scale = Math.max(0.001, plugin.getConfig().getDouble(path + "terrain.noise-scale", 0.02)); - int waterLevel = plugin.getConfig().getInt(path + "water.level", 63); - Material surface = configuredMaterial(path + "surface", Material.GRASS_BLOCK); - Material filler = configuredMaterial(path + "filler", Material.DIRT); - Material core = planet.equals("giedi_prime") || planet.equals("sigma_draconis_iv") ? Material.DEEPSLATE : Material.STONE; - double flowers = Math.max(0, plugin.getConfig().getDouble(path + "vegetation.flowers-per-chunk", 0)); - double treeChance = Math.max(0, plugin.getConfig().getDouble(path + "vegetation.tree-chance", 0)); - int[][] heights = new int[16][16]; - for (int x = 0; x < 16; x++) for (int z = 0; z < 16; z++) { - int gx = chunkX * 16 + x, gz = chunkZ * 16 + z; - double n = Math.sin(gx * scale) * Math.cos(gz * scale * 1.13) - + Math.sin(gx * scale * 2.7 + gz * scale * 1.45) * 0.35; - int h = Math.max(minY + 8, Math.min(maxY - 8, baseHeight + (int) Math.round(n * variation))); - heights[x][z] = h; - data.setBlock(x, minY, z, Material.BEDROCK); - for (int y = minY + 1; y < h - 3; y++) data.setBlock(x, y, z, core); - for (int y = h - 3; y < h; y++) data.setBlock(x, y, z, filler); - data.setBlock(x, h, z, surface); - - if (plugin.getConfig().getBoolean(path + "water.swamp-pools", false) && h < waterLevel) { - double poolNoise = Math.sin(gx * 0.035) * Math.cos(gz * 0.031); - double coverage = Math.max(0, Math.min(1, plugin.getConfig().getDouble(path + "water.coverage", 0.18))); - if (poolNoise > 0.72 - coverage * 0.75) { - for (int y = h + 1; y <= Math.min(waterLevel, maxY); y++) data.setBlock(x, y, z, Material.WATER); - } - } - if (flowers > 0 && surface == Material.GRASS_BLOCK && random.nextDouble() < Math.min(1, flowers / 256.0)) { - Material[] flowerTypes = {Material.POPPY, Material.DANDELION, Material.CORNFLOWER, - Material.AZURE_BLUET, Material.ALLIUM, Material.OXEYE_DAISY}; - data.setBlock(x, h + 1, z, flowerTypes[random.nextInt(flowerTypes.length)]); - } - if (plugin.getConfig().getBoolean(path + "vegetation.trees", false) - && surface == Material.GRASS_BLOCK && random.nextDouble() < treeChance && x >= 3 && x <= 12 && z >= 3 && z <= 12) { - placeJungleTree(data, x, h + 1, z, random, maxY); - } - } - boolean arrakisWorld = planet.equals("arrakis") && world.getName().equalsIgnoreCase("arrakis"); - if (arrakisWorld) placeOasisTree(data, chunkX, chunkZ, heights, minY, maxY); - if (arrakisWorld) { - placeDarkstoneIsland(data, chunkX, chunkZ, heights, minY, maxY); - placeSpiceDeposits(data, heights, random, minY, maxY); - placeRandomSpicePatch(data, chunkX, chunkZ, heights); - if (plugin.getConfig().getBoolean("dense-spice.enabled", true)) { - int spacing = Math.max(128, plugin.getConfig().getInt("dense-spice.spacing", 512)); - for (int x=0;x<16;x++) for(int z=0;z<16;z++) - if(DenseSpice.contains(world.getSeed(),chunkX*16+x,chunkZ*16+z,spacing)) - data.setBlock(x,heights[x][z],z,Material.ORANGE_TERRACOTTA); - } - } - placeOres(data, random, path, minY, maxY); - } - @Override public BiomeProvider getDefaultBiomeProvider(WorldInfo worldInfo) { - return new PlanetBiomeProvider(plugin, planet); - } - private void placeOasisTree(ChunkData data, int chunkX, int chunkZ, int[][] heights, int minY, int maxY) { - String path = "planets.arrakis.oasis-trees."; - if (!plugin.getConfig().getBoolean(path + "enabled", true)) return; - int spacing = Math.max(64, plugin.getConfig().getInt(path + "spacing", 300)); - long[] anchor = gridAnchor(chunkX, chunkZ, spacing, 0x4f41534953545245L); - if (Math.floorDiv(anchor[0], 16) != chunkX || Math.floorDiv(anchor[1], 16) != chunkZ) return; - int x = Math.floorMod(anchor[0], 16), z = Math.floorMod(anchor[1], 16); - int y = heights[x][z] + 1; - int height = Math.max(4, plugin.getConfig().getInt(path + "trunk-height", 7)); - if (y + height + 3 >= maxY || data.getType(x, y - 1, z) != Material.SAND) return; - for (int dy = 0; dy < height; dy++) data.setBlock(x, y + dy, z, Material.OAK_LOG); - for (int dy = height - 3; dy <= height + 2; dy++) { - int radius = dy >= height + 1 ? 2 : 3; - for (int dx = -radius; dx <= radius; dx++) for (int dz = -radius; dz <= radius; dz++) { - if (Math.abs(dx) == radius && Math.abs(dz) == radius && Math.floorMod(dx * 31 + dz * 17, 3) == 0) continue; - int lx = x + dx, lz = z + dz, ly = y + dy; - if (lx < 0 || lx >= 16 || lz < 0 || lz >= 16 || ly >= maxY || !data.getType(lx, ly, lz).isAir()) continue; - org.bukkit.block.data.type.Leaves leaves = (org.bukkit.block.data.type.Leaves) Bukkit.createBlockData(Material.OAK_LEAVES); - leaves.setPersistent(true); - data.setBlock(lx, ly, lz, leaves); - } - } - } - private long[] gridAnchor(int chunkX, int chunkZ, int spacing, long salt) { - int centerX = chunkX * 16 + 8, centerZ = chunkZ * 16 + 8; - long cellX = Math.floorDiv(centerX, spacing), cellZ = Math.floorDiv(centerZ, spacing); - long h1 = mix64(cellX * 0x9E3779B97F4A7C15L ^ cellZ * 0xC2B2AE3D27D4EB4FL ^ salt); - long h2 = mix64(cellZ * 0x165667B19E3779F9L ^ cellX * 0x85EBCA77C2B2AE63L ^ ~salt); - long x = cellX * spacing + 8 + Math.floorMod(h1, spacing - 16); - long z = cellZ * spacing + 8 + Math.floorMod(h2, spacing - 16); - if (Math.floorDiv(x, spacing) != cellX || Math.floorDiv(z, spacing) != cellZ) return new long[]{Long.MIN_VALUE, Long.MIN_VALUE}; - return new long[]{x, z}; - } - private void placeDarkstoneIsland(ChunkData data, int chunkX, int chunkZ, int[][] heights, int minY, int maxY) { - String path = "planets.arrakis.darkstone-islands."; - if (!plugin.getConfig().getBoolean("planets.arrakis.highlands.enabled", true)) return; - int minimumGap = Math.max(192, plugin.getConfig().getInt("planets.arrakis.highlands.min-spacing", 300)); - int maximumGap = Math.max(minimumGap, plugin.getConfig().getInt("planets.arrakis.highlands.max-spacing", 500)); - int spacing = (minimumGap + maximumGap) / 2; - int minRadius = Math.max(6, plugin.getConfig().getInt(path + "min-radius", 45)); - int maxRadius = Math.max(minRadius, plugin.getConfig().getInt(path + "max-radius", 55)); - int maxRise = Math.max(1, plugin.getConfig().getInt(path + "max-height", 14)); - boolean highlands = plugin.getConfig().getBoolean("planets.arrakis.highlands.enabled", true); - int mountainRise = highlands ? Math.max(0, Math.min(96, plugin.getConfig().getInt("planets.arrakis.highlands.mountain-rise", 32))) : 0; - int baseHeight = plugin.getConfig().getInt("planets.arrakis.terrain.base-height", 70); - double variation = plugin.getConfig().getDouble("planets.arrakis.terrain.height-variation", 11); - double scale = Math.max(0.001, plugin.getConfig().getDouble("planets.arrakis.terrain.noise-scale", 0.02)); - int worldMinX = chunkX * 16, worldMinZ = chunkZ * 16; - int reach = (int)Math.ceil(maxRadius * 2.0) + 3; - int firstCellX = Math.floorDiv(worldMinX - reach, spacing), lastCellX = Math.floorDiv(worldMinX + 15 + reach, spacing); - int firstCellZ = Math.floorDiv(worldMinZ - reach, spacing), lastCellZ = Math.floorDiv(worldMinZ + 15 + reach, spacing); - for (int cellX = firstCellX; cellX <= lastCellX; cellX++) for (int cellZ = firstCellZ; cellZ <= lastCellZ; cellZ++) { - long seed = RockPlateaus.formationSeed(cellX, cellZ); - long centerX = RockPlateaus.centerX(cellX,minimumGap,maximumGap); - long centerZ = RockPlateaus.centerZ(cellZ,minimumGap,maximumGap); - int radius = RockPlateaus.radius(seed,minRadius,maxRadius); - RockPlateaus.Shape shape = RockPlateaus.shape(seed, centerX, centerZ, radius, scale); - if (formationGround.size() > 2048) formationGround.clear(); - int highestGround = formationGround.computeIfAbsent(new GroundKey(shape,baseHeight,variation), - key -> RockPlateaus.highestGround(key.shape(),key.baseHeight(),key.variation())); - int allowedPlateaus = Math.max(1, Math.min(3, Math.min(maxRise, plugin.getConfig().getInt(path + "max-plateaus", 3)))); - int plateauCount = highlands ? allowedPlateaus : RockPlateaus.count(seed, allowedPlateaus); - int interval = RockPlateaus.interval(plugin.getConfig().getInt(path + "plateau-interval", 5), maxRise, plateauCount); - int plateauY = highestGround + mountainRise + plateauCount * interval; - for (int lx = 0; lx < 16; lx++) for (int lz = 0; lz < 16; lz++) { - long gx = (long) worldMinX + lx, gz = (long) worldMinZ + lz; - long rough = mix64(gx * 0x9E3779B97F4A7C15L ^ gz * 0xC2B2AE3D27D4EB4FL ^ seed); - double depth = shape.depth(gx, gz); - if (depth < 0) continue; - int baseY = heights[lx][lz]; - if (plateauY + 2 >= maxY || baseY <= minY + 1) continue; - int level = RockPlateaus.level(highlands ? (depth - 0.40) / 0.60 : depth, plateauCount); - int lowerLevel = highlands ? RockPlateaus.lowerTerrace(depth) : 0; - boolean plateau = level > 0 || lowerLevel > 0; - int terraceY = lowerLevel > 0 ? highestGround + lowerLevel * interval : highestGround + mountainRise + level * interval; - double slope = Math.max(0.0, Math.min(1.0, depth / (highlands ? 0.46 : 0.10))); - slope = slope * slope * (3 - 2 * slope); - int sideTop = highlands ? baseY + (int)Math.floor(slope * (highestGround + mountainRise - baseY)) - : highestGround + (int)Math.floor(slope * interval); - Material[] darkRock = {Material.BLACKSTONE, Material.BASALT, Material.POLISHED_BLACKSTONE}; - if (plateau) { - for (int y = baseY + 1; y < terraceY; y++) { - int hashIndex = (int)Math.floorMod(mix64(rough + y * 0x9E3779B97F4A7C15L), darkRock.length); - data.setBlock(lx, y, lz, darkRock[hashIndex]); - } - data.setBlock(lx, terraceY, lz, Material.SAND); - heights[lx][lz] = terraceY; - } else { - int spire = highlands ? RockPlateaus.spireRise(seed,gx,gz,depth, - plugin.getConfig().getInt("planets.arrakis.highlands.spire-height",40)) : 0; - int sideSurface = Math.max(baseY, Math.min(maxY - 2, sideTop + spire)); - heights[lx][lz] = sideSurface; - for (int y = baseY; y <= sideSurface; y++) { - int hashIndex = (int)Math.floorMod(mix64(rough + y * 0x9E3779B97F4A7C15L), darkRock.length); - data.setBlock(lx, y, lz, darkRock[hashIndex]); - } - } - } - } - } - private Material configuredMaterial(String path, Material fallback) { - String name = plugin.getConfig().getString(path, fallback.name()); - Material material = Material.matchMaterial(name == null ? "" : name); - return material == null ? fallback : material; - } - private void placeSpiceDeposits(ChunkData data, int[][] heights, Random random, int minY, int maxY) { - int count = Math.max(0, plugin.getConfig().getInt("spice-deposits.per-chunk", 2)); - for (int i = 0; i < count; i++) { - int x = random.nextInt(16), z = random.nextInt(16), y = heights[x][z]; - Material surface = data.getType(x, y, z); - if (y + 1 < maxY && y > minY && (surface == Material.SAND || surface == Material.RED_SAND)) - data.setBlock(x, y, z, Material.SUSPICIOUS_SAND); - } - } - private void placeRandomSpicePatch(ChunkData data, int chunkX, int chunkZ, int[][] heights) { - String path = "spice-deposits.patches."; - if (!plugin.getConfig().getBoolean(path + "enabled", true)) return; - int spacing = Math.max(128, plugin.getConfig().getInt(path + "region-spacing", 600)); - int radius = Math.max(2, plugin.getConfig().getInt(path + "radius", 12)); - int targetValue = Math.max(1, plugin.getConfig().getInt(path + "target-value", 5000)); - int unitPrice = Math.max(1, plugin.getConfig().getInt(path + "unit-price", 15)); - double density = Math.min(1.0, Math.ceil(targetValue / (double) unitPrice) / (Math.PI * radius * radius)); - double chance = Math.max(0, Math.min(1, plugin.getConfig().getDouble(path + "chance-per-region", 0.35))); - int worldMinX = chunkX * 16, worldMinZ = chunkZ * 16; - int firstCellX = Math.floorDiv(worldMinX - radius, spacing), lastCellX = Math.floorDiv(worldMinX + 15 + radius, spacing); - int firstCellZ = Math.floorDiv(worldMinZ - radius, spacing), lastCellZ = Math.floorDiv(worldMinZ + 15 + radius, spacing); - long salt = 0x5350494345504154L; - for (int cellX = firstCellX; cellX <= lastCellX; cellX++) for (int cellZ = firstCellZ; cellZ <= lastCellZ; cellZ++) { - long cellSeed = mix64((long) cellX * 0x9E3779B97F4A7C15L ^ (long) cellZ * 0xC2B2AE3D27D4EB4FL ^ salt); - if (hashUnit(cellSeed) >= chance) continue; - long offsetX = Math.floorMod(mix64(cellSeed ^ 0x165667B19E3779F9L), spacing - radius * 2L); - long offsetZ = Math.floorMod(mix64(cellSeed ^ 0x85EBCA77C2B2AE63L), spacing - radius * 2L); - long centerX = (long) cellX * spacing + radius + offsetX; - long centerZ = (long) cellZ * spacing + radius + offsetZ; - for (int x = 0; x < 16; x++) for (int z = 0; z < 16; z++) { - long gx = (long) worldMinX + x, gz = (long) worldMinZ + z; - long dx = gx - centerX, dz = gz - centerZ; - if (dx * dx + dz * dz > (long) radius * radius) continue; - long blockSeed = mix64(gx * 0x9E3779B97F4A7C15L ^ gz * 0xC2B2AE3D27D4EB4FL ^ salt); - if (hashUnit(blockSeed) >= density) continue; - int y = heights[x][z]; - if (data.getType(x, y, z) == Material.SAND || data.getType(x, y, z) == Material.RED_SAND) - data.setBlock(x, y, z, Material.SUSPICIOUS_SAND); - } - } - } - private static double hashUnit(long hash) { return (hash >>> 11) * 0x1.0p-53; } - private void placeOres(ChunkData data, Random random, String path, int minWorldY, int maxWorldY) { - if (planet.equals("arrakis")) return; // Spice, never ores. - org.bukkit.configuration.ConfigurationSection ores = plugin.getConfig().getConfigurationSection(path + "ores"); - if (ores == null) return; - int minY = Math.max(minWorldY + 1, plugin.getConfig().getInt(path + "ores.min-y", -48)); - int maxY = Math.min(maxWorldY - 4, plugin.getConfig().getInt(path + "ores.max-y", 48)); - int veinSize = Math.max(1, Math.min(16, plugin.getConfig().getInt(path + "ores.vein-size", 5))); - if (maxY < minY) return; - for (String key : ores.getKeys(false)) { - Material ore = Material.matchMaterial(key); - if (ore == null || (!key.endsWith("_ORE") && !(planet.equals("giedi_prime") && ore == Material.ANCIENT_DEBRIS))) continue; - int attempts = Math.max(0, Math.min(64, ores.getInt(key, 0))); - int effectiveVeinSize = ore == Material.ANCIENT_DEBRIS - ? Math.max(1, Math.min(16, plugin.getConfig().getInt(path + "ancient-debris.vein-size", 5))) : veinSize; - int oreMinY = ore == Material.ANCIENT_DEBRIS ? Math.max(minWorldY + 1, plugin.getConfig().getInt(path + "ancient-debris.min-y", -59)) : minY; - int oreMaxY = ore == Material.ANCIENT_DEBRIS ? Math.min(maxWorldY - 4, plugin.getConfig().getInt(path + "ancient-debris.max-y", 16)) : maxY; - if (oreMaxY < oreMinY) continue; - for (int attempt = 0; attempt < attempts; attempt++) { - int x = random.nextInt(16), z = random.nextInt(16), y = oreMinY + random.nextInt(oreMaxY - oreMinY + 1); - for (int n = 0; n < effectiveVeinSize; n++) { - int vx = Math.max(0, Math.min(15, x + random.nextInt(3) - 1)); - int vz = Math.max(0, Math.min(15, z + random.nextInt(3) - 1)); - int vy = Math.max(oreMinY, Math.min(oreMaxY, y + random.nextInt(3) - 1)); - Material existing = data.getType(vx, vy, vz); - if (existing == Material.STONE || existing == Material.DEEPSLATE || existing == Material.TUFF) data.setBlock(vx, vy, vz, ore); - } - } - } - } - private void placeJungleTree(ChunkData data, int x, int y, int z, Random random, int maxY) { - int height = 4 + random.nextInt(4); - if (y + height + 2 >= maxY || data.getType(x, y - 1, z) != Material.GRASS_BLOCK) return; - for (int dy = 0; dy < height; dy++) data.setBlock(x, y + dy, z, Material.JUNGLE_LOG); - for (int dy = height - 2; dy <= height + 1; dy++) { - int radius = dy >= height ? 1 : 2; - for (int dx = -radius; dx <= radius; dx++) for (int dz = -radius; dz <= radius; dz++) { - if (Math.abs(dx) == radius && Math.abs(dz) == radius && random.nextBoolean()) continue; - int lx = x + dx, lz = z + dz, ly = y + dy; - if (lx >= 0 && lx < 16 && lz >= 0 && lz < 16 && data.getType(lx, ly, lz).isAir()) data.setBlock(lx, ly, lz, Material.JUNGLE_LEAVES); - } - } - } - } - private static final class PlanetBiomeProvider extends BiomeProvider { - private final DuneTerrain plugin; - private final String planet; - private final Biome biome, highlands, denseSpice; - PlanetBiomeProvider(DuneTerrain plugin, String planet) { - this.plugin = plugin; this.planet = planet; - String configured = plugin.getConfig().getString("planets." + planet + ".biome", "DESERT"); - NamespacedKey key = NamespacedKey.fromString(configured.toLowerCase(Locale.ROOT)); - Biome selected = key == null ? null : Registry.BIOME.get(key); - this.biome = selected == null ? Biome.DESERT : selected; - String custom = plugin.getConfig().getString("planets.arrakis.highlands.biome", "dunerealms:dune-highlands"); - NamespacedKey customKey = NamespacedKey.fromString(custom); - this.denseSpice = planet.equals("arrakis") && plugin.getConfig().getBoolean("dense-spice.enabled", true) - ? Registry.BIOME.get(new NamespacedKey("dunerealms", "dense-spice")) : null; - this.highlands = planet.equals("arrakis") && plugin.getConfig().getBoolean("planets.arrakis.highlands.enabled", true) - && customKey != null ? Registry.BIOME.get(customKey) : null; - } - @Override public Biome getBiome(WorldInfo info, int x, int y, int z) { - if (denseSpice != null && DenseSpice.contains(info.getSeed(),x,z,Math.max(128,plugin.getConfig().getInt("dense-spice.spacing",512)))) return denseSpice; - if (highlands == null) return biome; - String path = "planets.arrakis.darkstone-islands."; - int minimumGap = Math.max(192, plugin.getConfig().getInt("planets.arrakis.highlands.min-spacing", 300)); - int maximumGap = Math.max(minimumGap, plugin.getConfig().getInt("planets.arrakis.highlands.max-spacing", 500)); - int minimum = Math.max(6, plugin.getConfig().getInt(path + "min-radius", 45)); - int maximum = Math.max(minimum, plugin.getConfig().getInt(path + "max-radius", 55)); - double scale = Math.max(.001, plugin.getConfig().getDouble("planets.arrakis.terrain.noise-scale", .02)); - return RockPlateaus.regionDepth(x,z,minimumGap,maximumGap,minimum,maximum,scale) >= 0 ? highlands : biome; - } - @Override public List getBiomes(WorldInfo info) { - Set all = new LinkedHashSet<>(); all.add(biome); if(highlands!=null)all.add(highlands); if(denseSpice!=null)all.add(denseSpice); return List.copyOf(all); - } - } - - private static long mix64(long value) { - value = (value ^ (value >>> 30)) * 0xBF58476D1CE4E5B9L; - value = (value ^ (value >>> 27)) * 0x94D049BB133111EBL; - return value ^ (value >>> 31); - } - - private static final class SietchPopulator extends BlockPopulator { - private final DuneTerrain plugin; - private final java.util.concurrent.ConcurrentHashMap> highlandSites = new java.util.concurrent.ConcurrentHashMap<>(); - private SietchPopulator(DuneTerrain plugin) { this.plugin = plugin; } - @Override public void populate(WorldInfo world, Random random, int cx, int cz, LimitedRegion region) { - if (!world.getName().equalsIgnoreCase("arrakis")) return; - if (!plugin.getConfig().getBoolean("sietches.enabled", true)) return; - populateHighlands(world,cx,cz,region); - int spacing = Math.max(256, plugin.getConfig().getInt("sietches.spacing", 2000)); - int centerX = cx * 16 + 8, centerZ = cz * 16 + 8; - long cellX = Math.floorDiv(centerX, spacing), cellZ = Math.floorDiv(centerZ, spacing); - long h1 = mix64(cellX * 0x9E3779B97F4A7C15L ^ cellZ * 0xC2B2AE3D27D4EB4FL ^ 0x534945544348L); - long h2 = mix64(cellZ * 0x165667B19E3779F9L ^ cellX * 0x85EBCA77C2B2AE63L ^ ~0x534945544348L); - long anchorX = cellX * spacing + 8 + Math.floorMod(h1, spacing - 16); - long anchorZ = cellZ * spacing + 8 + Math.floorMod(h2, spacing - 16); - if (Math.floorDiv(anchorX, 16) != cx || Math.floorDiv(anchorZ, 16) != cz) return; - int x = (int) anchorX, z = (int) anchorZ; - int y = region.getHighestBlockYAt(x, z); - if (y < world.getMinHeight() + 40 || y > world.getMaxHeight() - 4 || y - 36 < world.getMinHeight()) return; - - // Enlarged hub, side chambers, cache chamber, and reservoir. The surface entrance stays compact. - int hy = y - 11; - carveRoom(region, x, hy, z, 10, 6, 10); - carveTunnelNetwork(region, new int[][]{ - {x - 8, hy - 3, z - 2}, {x - 12, hy - 4, z - 2}, {x - 16, hy - 5, z - 2} - }); - carveRoom(region, x - 18, hy - 6, z - 2, 8, 4, 8); - carveTunnelNetwork(region, new int[][]{ - {x + 8, hy - 3, z + 2}, {x + 12, hy - 4, z + 2}, {x + 16, hy - 5, z + 2} - }); - carveRoom(region, x + 18, hy - 6, z + 2, 8, 4, 8); - carveTunnelNetwork(region, new int[][]{ - {x, hy - 3, z - 3}, {x - 3, hy - 4, z - 5}, {x - 6, hy - 5, z - 8}, - {x - 4, hy - 6, z - 11}, {x, hy - 7, z - 12}, {x + 3, hy - 8, z - 11}, - {x, hy - 10, z - 10} - }); - carveRoom(region, x, hy - 12, z - 10, 8, 4, 8); - carveRoom(region, x, hy - 23, z + 8, 14, 10, 14); - - // A broad, rough cave mouth opens from the surface and slopes down into the hub. - carveCaveEntrance(region, x, z, hy); - - // Long switchbacks keep the descent gradual from the hub to the enlarged reservoir. - carveTunnelNetwork(region, new int[][]{ - {x, hy - 3, z + 3}, {x - 4, hy - 4, z + 5}, {x - 8, hy - 5, z + 8}, - {x - 10, hy - 6, z + 12}, {x - 8, hy - 7, z + 14}, {x - 4, hy - 8, z + 14}, - {x - 2, hy - 9, z + 12}, {x - 5, hy - 10, z + 9}, {x - 8, hy - 11, z + 7}, - {x - 10, hy - 12, z + 9}, {x - 8, hy - 13, z + 12}, {x - 5, hy - 14, z + 13}, - {x - 3, hy - 15, z + 10}, {x - 5, hy - 16, z + 7}, {x - 7, hy - 17, z + 8}, - {x - 5, hy - 18, z + 10}, {x - 4, hy - 19, z + 9}, {x - 5, hy - 20, z + 8} - }); - - // The waterfall runs beside the ramp and feeds a broad pool at the bottom. - for (int dy = -4; dy >= -25; dy--) { - set(region, x + 3, hy + dy, z + 5, Material.WATER); - set(region, x + 2, hy + dy, z + 5, Material.STONE); - set(region, x + 4, hy + dy, z + 5, Material.STONE); - set(region, x + 3, hy + dy, z + 4, Material.STONE); - set(region, x + 3, hy + dy, z + 6, Material.STONE); - } - for (int dx = -5; dx <= 5; dx++) for (int dz = -5; dz <= 5; dz++) { - if (dx * dx + dz * dz > 25) continue; - set(region, x + dx, hy - 29, z + 8 + dz, Material.STONE); - for (int waterY = hy - 28; waterY <= hy - 25; waterY++) - set(region, x + dx, waterY, z + 8 + dz, Material.WATER); - } - - furnishSleepingHall(region, x - 18, hy - 6, z - 2); - furnishKitchen(region, x + 18, hy - 6, z + 2); - placePersonalChest(region, x, hy - 14, z - 10, cellX, cellZ); - for (int[] lamp : new int[][]{{0,-1,0},{-20,-5,-2},{20,-5,2},{0,-11,-11}}) - set(region, x + lamp[0], hy + lamp[1], z + lamp[2], Material.TORCH); - } - - private void populateHighlands(WorldInfo world,int cx,int cz,LimitedRegion region) { - if(!plugin.getConfig().getBoolean("planets.arrakis.highlands.enabled",true) - ||!plugin.getConfig().getBoolean("sietches.highlands.enabled",true))return; - int minGap=Math.max(192,plugin.getConfig().getInt("planets.arrakis.highlands.min-spacing",300)); - int maxGap=Math.max(minGap,plugin.getConfig().getInt("planets.arrakis.highlands.max-spacing",500)); - int spacing=(minGap+maxGap)/2; - int minRadius=Math.max(6,plugin.getConfig().getInt("planets.arrakis.darkstone-islands.min-radius",45)); - int maxRadius=Math.max(minRadius,plugin.getConfig().getInt("planets.arrakis.darkstone-islands.max-radius",55)); - double scale=Math.max(.001,plugin.getConfig().getDouble("planets.arrakis.terrain.noise-scale",.02)); - int reach=(int)Math.ceil(maxRadius*2.0)+3; - if(highlandSites.size()>2048)highlandSites.clear(); - for(int gx=Math.floorDiv(cx*16-reach,spacing);gx<=Math.floorDiv(cx*16+15+reach,spacing);gx++) - for(int gz=Math.floorDiv(cz*16-reach,spacing);gz<=Math.floorDiv(cz*16+15+reach,spacing);gz++) { - long seed=RockPlateaus.formationSeed(gx,gz); - var shape=RockPlateaus.shape(seed,RockPlateaus.centerX(gx,minGap,maxGap),RockPlateaus.centerZ(gz,minGap,maxGap),RockPlateaus.radius(seed,minRadius,maxRadius),scale); - var sites=highlandSites.computeIfAbsent(shape,k->HighlandSietches.sites(k, - plugin.getConfig().getInt("sietches.highlands.frequency-multiplier",3),plugin.getConfig().getInt("sietches.highlands.minimum-per-region",3))); - for(var site:sites) { - int x=site.x(),z=site.z(); - if(Math.floorDiv(x,16)!=cx||Math.floorDiv(z,16)!=cz)continue; - int surface=region.getHighestBlockYAt(x,z),hy=surface-11; - if(hy-12<=world.getMinHeight()||surface+2>=world.getMaxHeight())continue; - // Compact multi-room caves fit three sites without the large desert template colliding. - carveRoom(region,x,hy,z,7,4,7); - carveRoom(region,x-5,hy-3,z+5,4,3,4); - carveRoom(region,x+5,hy-3,z+5,4,3,4); - carveTunnelNetwork(region,new int[][]{{x,hy-1,z},{x-3,hy-2,z+3},{x-5,hy-3,z+5}}); - carveTunnelNetwork(region,new int[][]{{x,hy-1,z},{x+3,hy-2,z+3},{x+5,hy-3,z+5}}); - carveCaveEntrance(region,x,z,hy); - furnishSleepingHall(region,x-5,hy-4,z+5); - furnishKitchen(region,x+5,hy-4,z+5); - carveRoom(region,x,hy-8,z+6,4,3,4); - carveTunnelNetwork(region,new int[][]{{x,hy-1,z+2},{x+2,hy-3,z+4},{x+2,hy-5,z+6},{x,hy-8,z+6}}); - for(int dx=-2;dx<=2;dx++)for(int dz=-2;dz<=2;dz++) { - set(region,x+dx,hy-11,z+6+dz,Material.STONE); - set(region,x+dx,hy-10,z+6+dz,Material.WATER); - } - placePersonalChest(region,x,hy-2,z-3,site.cacheId(),0x484947484c414e44L); - set(region,x,hy-3,z,Material.CHISELED_STONE_BRICKS); - set(region,x,hy-2,z,Material.LANTERN); - } - } - } - - private static void carveRoom(LimitedRegion region, int cx, int cy, int cz, int rx, int ry, int rz) { - for (int dx = -rx; dx <= rx; dx++) for (int dy = -ry; dy <= ry; dy++) for (int dz = -rz; dz <= rz; dz++) { - double outer = dx * dx / (double)(rx * rx) + dy * dy / (double)(ry * ry) + dz * dz / (double)(rz * rz); - if (outer > 1.15) continue; - int x = cx + dx, y = cy + dy, z = cz + dz; - if (outer >= 0.58) { - long variation = mix64((long)x * 341873128712L ^ (long)y * 132897987541L ^ z); - Material wall = switch ((int)Math.floorMod(variation, 10)) { - case 0, 1 -> Material.TUFF; - case 2 -> Material.ANDESITE; - case 3 -> Material.COBBLED_DEEPSLATE; - case 4 -> Material.COBBLESTONE; - case 5 -> Material.MOSSY_COBBLESTONE; - default -> Material.STONE; - }; - set(region, x, y, z, wall); - } else set(region, x, y, z, Material.AIR); - } - } - - private static void carveCaveEntrance(LimitedRegion region, int x, int z, int hubY) { - int entryX = x + 6, entryZ = z - 7; - int outsideX = x + 11; - int surfaceY = region.getHighestBlockYAt(outsideX, entryZ); - int mouthY = Math.max(hubY + 4, surfaceY - 3); - - // Hollow a rough chamber just behind the stone arch, then connect it to the hub. - carveRoom(region, entryX, mouthY, entryZ, 3, 2, 3); - carveCaveTunnel(region, outsideX, surfaceY - 1, entryZ, x + 6, mouthY, entryZ); - carveTunnelNetwork(region, new int[][]{ - {x + 6, mouthY, entryZ}, {x + 3, mouthY - 1, z - 9}, {x, mouthY - 2, z - 11}, - {x - 4, mouthY - 3, z - 10}, {x - 6, mouthY - 4, z - 7}, - {x - 5, mouthY - 5, z - 3}, {x - 2, mouthY - 6, z - 1}, {x, hubY + 1, z} - }); - - // Irregular stone and tuff form a visible cave arch around the entrance. - int faceX = x + 7; - for (int dy = -3; dy <= 3; dy++) for (int dz = -4; dz <= 4; dz++) { - double arch = dy * dy / 16.0 + dz * dz / 25.0; - if (arch > 1.35) continue; - if (arch < 0.72) { - set(region, faceX, mouthY + dy, entryZ + dz, Material.AIR); - continue; - } - long variation = mix64((long)(faceX + dz) * 341873128712L ^ (long)(mouthY + dy) * 132897987541L ^ 0x4341564541524348L); - Material rock = switch ((int)Math.floorMod(variation, 5)) { - case 0 -> Material.TUFF; - case 1 -> Material.ANDESITE; - case 2 -> Material.COBBLED_DEEPSLATE; - default -> Material.STONE; - }; - set(region, faceX, mouthY + dy, entryZ + dz, rock); - } - } - - private static void carveTunnel(LimitedRegion region, double ax, double ay, double az, double bx, double by, double bz) { - int steps = (int)Math.ceil(Math.sqrt((bx-ax)*(bx-ax) + (by-ay)*(by-ay) + (bz-az)*(bz-az)) * 1.5); - for (int i = 0; i <= steps; i++) { - double t = i / (double)Math.max(1, steps); - int cx = (int)Math.round(ax + (bx-ax)*t), cy = (int)Math.round(ay + (by-ay)*t), cz = (int)Math.round(az + (bz-az)*t); - for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) for (int dz = -1; dz <= 1; dz++) - if (dx*dx + dy*dy + dz*dz <= 2) set(region, cx+dx, cy+dy, cz+dz, Material.AIR); - } - } - - private static void carveTunnelNetwork(LimitedRegion region, int[][] points) { - for (int i = 1; i < points.length; i++) { - int[] from = points[i - 1], to = points[i]; - carveTunnel(region, from[0], from[1], from[2], to[0], to[1], to[2]); - } - } - - private static void carveCaveTunnel(LimitedRegion region, double ax, double ay, double az, double bx, double by, double bz) { - int steps = (int)Math.ceil(Math.sqrt((bx-ax)*(bx-ax) + (by-ay)*(by-ay) + (bz-az)*(bz-az)) * 1.5); - for (int i = 0; i <= steps; i++) { - double t = i / (double)Math.max(1, steps); - int cx = (int)Math.round(ax + (bx-ax)*t), cy = (int)Math.round(ay + (by-ay)*t), cz = (int)Math.round(az + (bz-az)*t); - for (int dx = -2; dx <= 2; dx++) for (int dy = -2; dy <= 2; dy++) for (int dz = -2; dz <= 2; dz++) - if (dx*dx + dy*dy + dz*dz <= 4) set(region, cx+dx, cy+dy, cz+dz, Material.AIR); - } - } - - private static void furnishSleepingHall(LimitedRegion r, int x, int y, int z) { - for (int i = -1; i <= 1; i++) { - set(r, x+i, y-1, z, Material.POLISHED_ANDESITE); - set(r, x+i, y, z, Material.RED_CARPET); - set(r, x+i, y+1, z, Material.AIR); - } - set(r, x-1, y, z, Material.WHITE_CARPET); - set(r, x+1, y, z, Material.WHITE_CARPET); - set(r, x-2, y-1, z+2, Material.BARREL); - } - - private static void furnishKitchen(LimitedRegion r, int x, int y, int z) { - set(r, x-2, y-1, z, Material.STONE_BRICKS); - set(r, x-2, y, z, Material.FURNACE); - set(r, x-1, y-1, z, Material.STONE_BRICKS); - set(r, x-1, y, z, Material.SMOKER); - set(r, x, y-1, z, Material.STONE_BRICKS); - set(r, x, y, z, Material.CRAFTING_TABLE); - set(r, x+2, y-1, z+1, Material.BARREL); - set(r, x+2, y, z+1, Material.CAULDRON); - } - - private void placePersonalChest(LimitedRegion r, int x, int y, int z, long cellX, long cellZ) { - set(r, x, y-1, z, Material.CHISELED_STONE_BRICKS); - set(r, x, y, z, Material.CHEST); - BlockState state = r.getBlockState(x, y, z); - if (state instanceof Chest chest) { - String id = Long.toUnsignedString(mix64(cellX * 0x9E3779B97F4A7C15L ^ cellZ * 0xC2B2AE3D27D4EB4FL ^ 0x534945544348L), 16); - chest.setCustomName("Personal Sietch Cache"); - chest.getPersistentDataContainer().set(plugin.sietchLootIdKey, PersistentDataType.STRING, id); - chest.update(true, false); - } - } - - private static void set(LimitedRegion region, int x, int y, int z, Material material) { - if (region.isInRegion(x, y, z)) region.setType(x, y, z, material); - } - } - - private static final class SpiceBlowPopulator extends BlockPopulator { - private static final long SALT = 0x5350494345424c4fL; - private final DuneTerrain plugin; - private SpiceBlowPopulator(DuneTerrain plugin) { this.plugin = plugin; } - - @Override public void populate(WorldInfo world, Random random, int chunkX, int chunkZ, LimitedRegion region) { - if (!world.getName().equalsIgnoreCase("arrakis") || !plugin.getConfig().getBoolean("spice-blow.enabled", true)) return; - int spacing = Math.max(256, plugin.getConfig().getInt("spice-blow.spacing", 2000)); - long centerX = chunkX * 16L + 8, centerZ = chunkZ * 16L + 8; - long cellX = Math.floorDiv(centerX, spacing), cellZ = Math.floorDiv(centerZ, spacing); - long h1 = mix64(cellX * 0x9E3779B97F4A7C15L ^ cellZ * 0xC2B2AE3D27D4EB4FL ^ SALT); - long h2 = mix64(cellZ * 0x165667B19E3779F9L ^ cellX * 0x85EBCA77C2B2AE63L ^ ~SALT); - long anchorX = cellX * spacing + 8 + Math.floorMod(h1, spacing - 16); - long anchorZ = cellZ * spacing + 8 + Math.floorMod(h2, spacing - 16); - if (Math.floorDiv(anchorX, 16) != chunkX || Math.floorDiv(anchorZ, 16) != chunkZ) return; - int x = (int)anchorX, z = (int)anchorZ, y = region.getHighestBlockYAt(x, z); - if (y < world.getMinHeight() + 2 || y + 2 >= world.getMaxHeight()) return; - if (!region.isInRegion(x, y, z) || region.getType(x, y, z) != Material.SAND) return; - for (int dx = -1; dx <= 1; dx++) for (int dz = -1; dz <= 1; dz++) - if (region.isInRegion(x + dx, y, z + dz)) region.setType(x + dx, y, z + dz, Material.MAGMA_BLOCK); - if (region.isInRegion(x, y + 1, z)) region.setType(x, y + 1, z, Material.MAGMA_BLOCK); - } - } -} + ArrakisEcology.start(this); + saveDefaultConfig(); + String formations = "planets.arrakis.darkstone-islands."; + if (getConfig().getInt(formations + "min-radius") == 10 + && getConfig().getInt(formations + "max-radius") == 18) { + getConfig().set(formations + "min-radius", 45); + getConfig().set(formations + "max-radius", 55); + saveConfig(); + getLogger().info("Upgraded default Arrakis rock formations to about 100 blocks across in new chunks."); + } + getServer().getPluginManager().registerEvents(this, this); + getServer().getPluginManager().registerEvents(new DenseSpice(this), this); + getServer().getPluginManager().registerEvents(new HarkonnenFortress(this), this); + + sietchLootIdKey = new NamespacedKey(this, "sietch-loot-id"); + String planet = getConfig().getString("server.planet", "arrakis").toLowerCase(Locale.ROOT); + if (!Arrays.asList(PLANETS).contains(planet)) { + getLogger().severe("Invalid server.planet '" + planet + "'. Choose one of: " + String.join(", ", PLANETS)); + getServer().getPluginManager().disablePlugin(this); + return; + } + if (!getConfig().getBoolean("planets." + planet + ".enabled", true)) { + getLogger().severe("Planet '" + planet + "' is disabled in config.yml."); + getServer().getPluginManager().disablePlugin(this); + return; + } + getLogger().info("DuneTerrain registered the " + planet + " generator. World setup will run after Paper finishes loading worlds."); + } + + @EventHandler public void onServerLoad(ServerLoadEvent event) { + World arrakis = Bukkit.getWorld("arrakis"); + if (arrakis != null) for (Chunk chunk : arrakis.getLoadedChunks()) + Bukkit.getScheduler().runTask(this, () -> inspectBlooms(chunk)); + String planet = getConfig().getString("server.planet", "arrakis").toLowerCase(Locale.ROOT); + World world = Bukkit.getWorld(planet); + if (world == null) { + File worldFolder = new File(getServer().getWorldContainer(), planet); + boolean existingWorld = worldFolder.isDirectory(); + world = Bukkit.createWorld(new WorldCreator(planet) + .environment(World.Environment.NORMAL) + .generator(new PlanetChunkGenerator(this, planet))); + if (world == null) { + getLogger().severe("Could not create or load world '" + planet + "'."); + return; + } + if (existingWorld) getLogger().warning("Loaded existing world '" + planet + "'. Existing chunks remain unchanged; move the world folder after backing it up to regenerate terrain."); + else getLogger().info("Created world '" + planet + "' with DuneTerrain."); + } else if (!(world.getGenerator() instanceof PlanetChunkGenerator)) { + getLogger().severe("World '" + planet + "' was loaded without DuneTerrain's generator. Configure bukkit.yml worlds." + planet + ".generator: DuneTerrain:" + planet + " and worlds." + planet + ".biome-provider: DuneTerrain:" + planet + ". Existing chunks remain unchanged."); + } else { + getLogger().info("World '" + planet + "' was loaded with DuneTerrain's configured generator."); + } + world.setGameRule(GameRule.DO_DAYLIGHT_CYCLE, !planet.equals("arrakis")); + world.setTime(planet.equals("giedi_prime") ? 18000 : 6000); + setupPlanetSpawn(world, planet); + } + + private void setupPlanetSpawn(World world, String planet) { + String path = "planets." + planet + ".spawn."; + int x = getConfig().getInt(path + "x", 0), z = getConfig().getInt(path + "z", 0); + world.getChunkAt(x >> 4, z >> 4).load(true); + Location spawn = findSafeSpawn(world, x, z); + world.setSpawnLocation(spawn); + + } + + private Location findSafeSpawn(World world, int x, int z) { + for (int radius = 0; radius <= 12; radius++) { + for (int dx = -radius; dx <= radius; dx++) for (int dz = -radius; dz <= radius; dz++) { + if (radius > 0 && Math.abs(dx) != radius && Math.abs(dz) != radius) continue; + int bx = x + dx, bz = z + dz; + int y = world.getHighestBlockYAt(bx, bz); + Material ground = world.getBlockAt(bx, y, bz).getType(); + if (!ground.isSolid() || ground == Material.MAGMA_BLOCK || ground == Material.CACTUS || ground == Material.POWDER_SNOW) continue; + if (world.getBlockAt(bx, y + 1, bz).isLiquid() || world.getBlockAt(bx, y + 2, bz).isLiquid()) continue; + return new Location(world, bx + 0.5, y + 1, bz + 0.5); + } + } + int y = Math.max(world.getMinHeight() + 2, world.getHighestBlockYAt(x, z) + 1); + return new Location(world, x + 0.5, y, z + 0.5); + } + + private static class PlanetChunkGenerator extends ChunkGenerator { + private final DuneTerrain plugin; + private final String planet; + private record GroundKey(RockPlateaus.Shape shape, int baseHeight, double variation) {} + private final java.util.concurrent.ConcurrentHashMap formationGround = new java.util.concurrent.ConcurrentHashMap<>(); + PlanetChunkGenerator(DuneTerrain plugin, String planet) { this.plugin = plugin; this.planet = planet; } + @Override public List getDefaultPopulators(World world) { + return planet.equals("arrakis") && world.getName().equalsIgnoreCase("arrakis") + ? List.of(new SietchPopulator(plugin), new SpiceBlowPopulator(plugin), new DesertVillages(plugin), new HarkonnenFortress(plugin)) : List.of(); + } + @Override public void generateNoise(WorldInfo world, Random random, int chunkX, int chunkZ, ChunkData data) { + String path = "planets." + planet + "."; + int minY = world.getMinHeight(), maxY = world.getMaxHeight() - 1; + int baseHeight = plugin.getConfig().getInt(path + "terrain.base-height", 68); + double variation = plugin.getConfig().getDouble(path + "terrain.height-variation", 8); + double scale = Math.max(0.001, plugin.getConfig().getDouble(path + "terrain.noise-scale", 0.02)); + int waterLevel = plugin.getConfig().getInt(path + "water.level", 63); + Material surface = configuredMaterial(path + "surface", Material.GRASS_BLOCK); + Material filler = configuredMaterial(path + "filler", Material.DIRT); + Material core = planet.equals("giedi_prime") || planet.equals("sigma_draconis_iv") ? Material.DEEPSLATE : Material.STONE; + double flowers = Math.max(0, plugin.getConfig().getDouble(path + "vegetation.flowers-per-chunk", 0)); + double treeChance = Math.max(0, plugin.getConfig().getDouble(path + "vegetation.tree-chance", 0)); + int[][] heights = new int[16][16]; + for (int x = 0; x < 16; x++) for (int z = 0; z < 16; z++) { + int gx = chunkX * 16 + x, gz = chunkZ * 16 + z; + double n = Math.sin(gx * scale) * Math.cos(gz * scale * 1.13) + + Math.sin(gx * scale * 2.7 + gz * scale * 1.45) * 0.35; + int h = Math.max(minY + 8, Math.min(maxY - 8, baseHeight + (int) Math.round(n * variation))); + heights[x][z] = h; + data.setBlock(x, minY, z, Material.BEDROCK); + for (int y = minY + 1; y < h - 3; y++) data.setBlock(x, y, z, core); + for (int y = h - 3; y < h; y++) data.setBlock(x, y, z, filler); + data.setBlock(x, h, z, surface); + + if (plugin.getConfig().getBoolean(path + "water.swamp-pools", false) && h < waterLevel) { + double poolNoise = Math.sin(gx * 0.035) * Math.cos(gz * 0.031); + double coverage = Math.max(0, Math.min(1, plugin.getConfig().getDouble(path + "water.coverage", 0.18))); + if (poolNoise > 0.72 - coverage * 0.75) { + for (int y = h + 1; y <= Math.min(waterLevel, maxY); y++) data.setBlock(x, y, z, Material.WATER); + } + } + if (flowers > 0 && surface == Material.GRASS_BLOCK && random.nextDouble() < Math.min(1, flowers / 256.0)) { + Material[] flowerTypes = {Material.POPPY, Material.DANDELION, Material.CORNFLOWER, + Material.AZURE_BLUET, Material.ALLIUM, Material.OXEYE_DAISY}; + data.setBlock(x, h + 1, z, flowerTypes[random.nextInt(flowerTypes.length)]); + } + if (plugin.getConfig().getBoolean(path + "vegetation.trees", false) + && surface == Material.GRASS_BLOCK && random.nextDouble() < treeChance && x >= 3 && x <= 12 && z >= 3 && z <= 12) { + placeJungleTree(data, x, h + 1, z, random, maxY); + } + } + boolean arrakisWorld = planet.equals("arrakis") && world.getName().equalsIgnoreCase("arrakis"); + if (arrakisWorld) placeOasisTree(data, chunkX, chunkZ, heights, minY, maxY); + if (arrakisWorld) { + placeDarkstoneIsland(data, chunkX, chunkZ, heights, minY, maxY); + placeSpiceDeposits(data, heights, random, minY, maxY); + placeRandomSpicePatch(data, chunkX, chunkZ, heights); + if (plugin.getConfig().getBoolean("dense-spice.enabled", true)) { + int spacing = Math.max(128, plugin.getConfig().getInt("dense-spice.spacing", 512)); + for (int x=0;x<16;x++) for(int z=0;z<16;z++) + if(DenseSpice.contains(world.getSeed(),chunkX*16+x,chunkZ*16+z,spacing)) + data.setBlock(x,heights[x][z],z,Material.ORANGE_TERRACOTTA); + } + } + placeOres(data, random, path, minY, maxY); + } + @Override public BiomeProvider getDefaultBiomeProvider(WorldInfo worldInfo) { + return new PlanetBiomeProvider(plugin, planet); + } + private void placeOasisTree(ChunkData data, int chunkX, int chunkZ, int[][] heights, int minY, int maxY) { + String path = "planets.arrakis.oasis-trees."; + if (!plugin.getConfig().getBoolean(path + "enabled", true)) return; + int spacing = Math.max(64, plugin.getConfig().getInt(path + "spacing", 300)); + long[] anchor = gridAnchor(chunkX, chunkZ, spacing, 0x4f41534953545245L); + if (Math.floorDiv(anchor[0], 16) != chunkX || Math.floorDiv(anchor[1], 16) != chunkZ) return; + int x = Math.floorMod(anchor[0], 16), z = Math.floorMod(anchor[1], 16); + int y = heights[x][z] + 1; + int height = Math.max(4, plugin.getConfig().getInt(path + "trunk-height", 7)); + if (y + height + 3 >= maxY || data.getType(x, y - 1, z) != Material.SAND) return; + for (int dy = 0; dy < height; dy++) data.setBlock(x, y + dy, z, Material.OAK_LOG); + for (int dy = height - 3; dy <= height + 2; dy++) { + int radius = dy >= height + 1 ? 2 : 3; + for (int dx = -radius; dx <= radius; dx++) for (int dz = -radius; dz <= radius; dz++) { + if (Math.abs(dx) == radius && Math.abs(dz) == radius && Math.floorMod(dx * 31 + dz * 17, 3) == 0) continue; + int lx = x + dx, lz = z + dz, ly = y + dy; + if (lx < 0 || lx >= 16 || lz < 0 || lz >= 16 || ly >= maxY || !data.getType(lx, ly, lz).isAir()) continue; + org.bukkit.block.data.type.Leaves leaves = (org.bukkit.block.data.type.Leaves) Bukkit.createBlockData(Material.OAK_LEAVES); + leaves.setPersistent(true); + data.setBlock(lx, ly, lz, leaves); + } + } + } + private long[] gridAnchor(int chunkX, int chunkZ, int spacing, long salt) { + int centerX = chunkX * 16 + 8, centerZ = chunkZ * 16 + 8; + long cellX = Math.floorDiv(centerX, spacing), cellZ = Math.floorDiv(centerZ, spacing); + long h1 = mix64(cellX * 0x9E3779B97F4A7C15L ^ cellZ * 0xC2B2AE3D27D4EB4FL ^ salt); + long h2 = mix64(cellZ * 0x165667B19E3779F9L ^ cellX * 0x85EBCA77C2B2AE63L ^ ~salt); + long x = cellX * spacing + 8 + Math.floorMod(h1, spacing - 16); + long z = cellZ * spacing + 8 + Math.floorMod(h2, spacing - 16); + if (Math.floorDiv(x, spacing) != cellX || Math.floorDiv(z, spacing) != cellZ) return new long[]{Long.MIN_VALUE, Long.MIN_VALUE}; + return new long[]{x, z}; + } + private void placeDarkstoneIsland(ChunkData data, int chunkX, int chunkZ, int[][] heights, int minY, int maxY) { + String path = "planets.arrakis.darkstone-islands."; + if (!plugin.getConfig().getBoolean("planets.arrakis.highlands.enabled", true)) return; + int minimumGap = Math.max(192, plugin.getConfig().getInt("planets.arrakis.highlands.min-spacing", 300)); + int maximumGap = Math.max(minimumGap, plugin.getConfig().getInt("planets.arrakis.highlands.max-spacing", 500)); + int spacing = (minimumGap + maximumGap) / 2; + int minRadius = Math.max(6, plugin.getConfig().getInt(path + "min-radius", 45)); + int maxRadius = Math.max(minRadius, plugin.getConfig().getInt(path + "max-radius", 55)); + int maxRise = Math.max(1, plugin.getConfig().getInt(path + "max-height", 14)); + boolean highlands = plugin.getConfig().getBoolean("planets.arrakis.highlands.enabled", true); + int mountainRise = highlands ? Math.max(0, Math.min(96, plugin.getConfig().getInt("planets.arrakis.highlands.mountain-rise", 32))) : 0; + int baseHeight = plugin.getConfig().getInt("planets.arrakis.terrain.base-height", 70); + double variation = plugin.getConfig().getDouble("planets.arrakis.terrain.height-variation", 11); + double scale = Math.max(0.001, plugin.getConfig().getDouble("planets.arrakis.terrain.noise-scale", 0.02)); + int worldMinX = chunkX * 16, worldMinZ = chunkZ * 16; + int reach = (int)Math.ceil(maxRadius * 2.0) + 3; + int firstCellX = Math.floorDiv(worldMinX - reach, spacing), lastCellX = Math.floorDiv(worldMinX + 15 + reach, spacing); + int firstCellZ = Math.floorDiv(worldMinZ - reach, spacing), lastCellZ = Math.floorDiv(worldMinZ + 15 + reach, spacing); + for (int cellX = firstCellX; cellX <= lastCellX; cellX++) for (int cellZ = firstCellZ; cellZ <= lastCellZ; cellZ++) { + long seed = RockPlateaus.formationSeed(cellX, cellZ); + long centerX = RockPlateaus.centerX(cellX,minimumGap,maximumGap); + long centerZ = RockPlateaus.centerZ(cellZ,minimumGap,maximumGap); + int radius = RockPlateaus.radius(seed,minRadius,maxRadius); + RockPlateaus.Shape shape = RockPlateaus.shape(seed, centerX, centerZ, radius, scale); + if (formationGround.size() > 2048) formationGround.clear(); + int highestGround = formationGround.computeIfAbsent(new GroundKey(shape,baseHeight,variation), + key -> RockPlateaus.highestGround(key.shape(),key.baseHeight(),key.variation())); + int allowedPlateaus = Math.max(1, Math.min(3, Math.min(maxRise, plugin.getConfig().getInt(path + "max-plateaus", 3)))); + int plateauCount = highlands ? allowedPlateaus : RockPlateaus.count(seed, allowedPlateaus); + int interval = RockPlateaus.interval(plugin.getConfig().getInt(path + "plateau-interval", 5), maxRise, plateauCount); + int plateauY = highestGround + mountainRise + plateauCount * interval; + for (int lx = 0; lx < 16; lx++) for (int lz = 0; lz < 16; lz++) { + long gx = (long) worldMinX + lx, gz = (long) worldMinZ + lz; + long rough = mix64(gx * 0x9E3779B97F4A7C15L ^ gz * 0xC2B2AE3D27D4EB4FL ^ seed); + double depth = shape.depth(gx, gz); + if (depth < 0) continue; + int baseY = heights[lx][lz]; + if (plateauY + 2 >= maxY || baseY <= minY + 1) continue; + int level = RockPlateaus.level(highlands ? (depth - 0.40) / 0.60 : depth, plateauCount); + int lowerLevel = highlands ? RockPlateaus.lowerTerrace(depth) : 0; + boolean plateau = level > 0 || lowerLevel > 0; + int terraceY = lowerLevel > 0 ? highestGround + lowerLevel * interval : highestGround + mountainRise + level * interval; + double slope = Math.max(0.0, Math.min(1.0, depth / (highlands ? 0.46 : 0.10))); + slope = slope * slope * (3 - 2 * slope); + int sideTop = highlands ? baseY + (int)Math.floor(slope * (highestGround + mountainRise - baseY)) + : highestGround + (int)Math.floor(slope * interval); + Material[] darkRock = {Material.BLACKSTONE, Material.BASALT, Material.POLISHED_BLACKSTONE}; + if (plateau) { + for (int y = baseY + 1; y < terraceY; y++) { + int hashIndex = (int)Math.floorMod(mix64(rough + y * 0x9E3779B97F4A7C15L), darkRock.length); + data.setBlock(lx, y, lz, darkRock[hashIndex]); + } + data.setBlock(lx, terraceY, lz, Material.SAND); + heights[lx][lz] = terraceY; + } else { + int spire = highlands ? RockPlateaus.spireRise(seed,gx,gz,depth, + plugin.getConfig().getInt("planets.arrakis.highlands.spire-height",40)) : 0; + int sideSurface = Math.max(baseY, Math.min(maxY - 2, sideTop + spire)); + heights[lx][lz] = sideSurface; + for (int y = baseY; y <= sideSurface; y++) { + int hashIndex = (int)Math.floorMod(mix64(rough + y * 0x9E3779B97F4A7C15L), darkRock.length); + data.setBlock(lx, y, lz, darkRock[hashIndex]); + } + } + } + } + } + private Material configuredMaterial(String path, Material fallback) { + String name = plugin.getConfig().getString(path, fallback.name()); + Material material = Material.matchMaterial(name == null ? "" : name); + return material == null ? fallback : material; + } + private void placeSpiceDeposits(ChunkData data, int[][] heights, Random random, int minY, int maxY) { + int count = Math.max(0, plugin.getConfig().getInt("spice-deposits.per-chunk", 2)); + for (int i = 0; i < count; i++) { + int x = random.nextInt(16), z = random.nextInt(16), y = heights[x][z]; + Material surface = data.getType(x, y, z); + if (y + 1 < maxY && y > minY && (surface == Material.SAND || surface == Material.RED_SAND)) + data.setBlock(x, y, z, Material.SUSPICIOUS_SAND); + } + } + private void placeRandomSpicePatch(ChunkData data, int chunkX, int chunkZ, int[][] heights) { + String path = "spice-deposits.patches."; + if (!plugin.getConfig().getBoolean(path + "enabled", true)) return; + int spacing = Math.max(128, plugin.getConfig().getInt(path + "region-spacing", 600)); + int radius = Math.max(2, plugin.getConfig().getInt(path + "radius", 12)); + int targetValue = Math.max(1, plugin.getConfig().getInt(path + "target-value", 5000)); + int unitPrice = Math.max(1, plugin.getConfig().getInt(path + "unit-price", 15)); + double density = Math.min(1.0, Math.ceil(targetValue / (double) unitPrice) / (Math.PI * radius * radius)); + double chance = Math.max(0, Math.min(1, plugin.getConfig().getDouble(path + "chance-per-region", 0.35))); + int worldMinX = chunkX * 16, worldMinZ = chunkZ * 16; + int firstCellX = Math.floorDiv(worldMinX - radius, spacing), lastCellX = Math.floorDiv(worldMinX + 15 + radius, spacing); + int firstCellZ = Math.floorDiv(worldMinZ - radius, spacing), lastCellZ = Math.floorDiv(worldMinZ + 15 + radius, spacing); + long salt = 0x5350494345504154L; + for (int cellX = firstCellX; cellX <= lastCellX; cellX++) for (int cellZ = firstCellZ; cellZ <= lastCellZ; cellZ++) { + long cellSeed = mix64((long) cellX * 0x9E3779B97F4A7C15L ^ (long) cellZ * 0xC2B2AE3D27D4EB4FL ^ salt); + if (hashUnit(cellSeed) >= chance) continue; + long offsetX = Math.floorMod(mix64(cellSeed ^ 0x165667B19E3779F9L), spacing - radius * 2L); + long offsetZ = Math.floorMod(mix64(cellSeed ^ 0x85EBCA77C2B2AE63L), spacing - radius * 2L); + long centerX = (long) cellX * spacing + radius + offsetX; + long centerZ = (long) cellZ * spacing + radius + offsetZ; + for (int x = 0; x < 16; x++) for (int z = 0; z < 16; z++) { + long gx = (long) worldMinX + x, gz = (long) worldMinZ + z; + long dx = gx - centerX, dz = gz - centerZ; + if (dx * dx + dz * dz > (long) radius * radius) continue; + long blockSeed = mix64(gx * 0x9E3779B97F4A7C15L ^ gz * 0xC2B2AE3D27D4EB4FL ^ salt); + if (hashUnit(blockSeed) >= density) continue; + int y = heights[x][z]; + if (data.getType(x, y, z) == Material.SAND || data.getType(x, y, z) == Material.RED_SAND) + data.setBlock(x, y, z, Material.SUSPICIOUS_SAND); + } + } + } + private static double hashUnit(long hash) { return (hash >>> 11) * 0x1.0p-53; } + private void placeOres(ChunkData data, Random random, String path, int minWorldY, int maxWorldY) { + if (planet.equals("arrakis")) return; // Spice, never ores. + org.bukkit.configuration.ConfigurationSection ores = plugin.getConfig().getConfigurationSection(path + "ores"); + if (ores == null) return; + int minY = Math.max(minWorldY + 1, plugin.getConfig().getInt(path + "ores.min-y", -48)); + int maxY = Math.min(maxWorldY - 4, plugin.getConfig().getInt(path + "ores.max-y", 48)); + int veinSize = Math.max(1, Math.min(16, plugin.getConfig().getInt(path + "ores.vein-size", 5))); + if (maxY < minY) return; + for (String key : ores.getKeys(false)) { + Material ore = Material.matchMaterial(key); + if (ore == null || (!key.endsWith("_ORE") && !(planet.equals("giedi_prime") && ore == Material.ANCIENT_DEBRIS))) continue; + int attempts = Math.max(0, Math.min(64, ores.getInt(key, 0))); + int effectiveVeinSize = ore == Material.ANCIENT_DEBRIS + ? Math.max(1, Math.min(16, plugin.getConfig().getInt(path + "ancient-debris.vein-size", 5))) : veinSize; + int oreMinY = ore == Material.ANCIENT_DEBRIS ? Math.max(minWorldY + 1, plugin.getConfig().getInt(path + "ancient-debris.min-y", -59)) : minY; + int oreMaxY = ore == Material.ANCIENT_DEBRIS ? Math.min(maxWorldY - 4, plugin.getConfig().getInt(path + "ancient-debris.max-y", 16)) : maxY; + if (oreMaxY < oreMinY) continue; + for (int attempt = 0; attempt < attempts; attempt++) { + int x = random.nextInt(16), z = random.nextInt(16), y = oreMinY + random.nextInt(oreMaxY - oreMinY + 1); + for (int n = 0; n < effectiveVeinSize; n++) { + int vx = Math.max(0, Math.min(15, x + random.nextInt(3) - 1)); + int vz = Math.max(0, Math.min(15, z + random.nextInt(3) - 1)); + int vy = Math.max(oreMinY, Math.min(oreMaxY, y + random.nextInt(3) - 1)); + Material existing = data.getType(vx, vy, vz); + if (existing == Material.STONE || existing == Material.DEEPSLATE || existing == Material.TUFF) data.setBlock(vx, vy, vz, ore); + } + } + } + } + private void placeJungleTree(ChunkData data, int x, int y, int z, Random random, int maxY) { + int height = 4 + random.nextInt(4); + if (y + height + 2 >= maxY || data.getType(x, y - 1, z) != Material.GRASS_BLOCK) return; + for (int dy = 0; dy < height; dy++) data.setBlock(x, y + dy, z, Material.JUNGLE_LOG); + for (int dy = height - 2; dy <= height + 1; dy++) { + int radius = dy >= height ? 1 : 2; + for (int dx = -radius; dx <= radius; dx++) for (int dz = -radius; dz <= radius; dz++) { + if (Math.abs(dx) == radius && Math.abs(dz) == radius && random.nextBoolean()) continue; + int lx = x + dx, lz = z + dz, ly = y + dy; + if (lx >= 0 && lx < 16 && lz >= 0 && lz < 16 && data.getType(lx, ly, lz).isAir()) data.setBlock(lx, ly, lz, Material.JUNGLE_LEAVES); + } + } + } + } + private static final class PlanetBiomeProvider extends BiomeProvider { + private final DuneTerrain plugin; + private final String planet; + private final Biome biome, highlands, denseSpice; + PlanetBiomeProvider(DuneTerrain plugin, String planet) { + this.plugin = plugin; this.planet = planet; + String configured = plugin.getConfig().getString("planets." + planet + ".biome", "DESERT"); + NamespacedKey key = NamespacedKey.fromString(configured.toLowerCase(Locale.ROOT)); + Biome selected = key == null ? null : Registry.BIOME.get(key); + this.biome = selected == null ? Biome.DESERT : selected; + String custom = plugin.getConfig().getString("planets.arrakis.highlands.biome", "dunerealms:dune-highlands"); + NamespacedKey customKey = NamespacedKey.fromString(custom); + this.denseSpice = planet.equals("arrakis") && plugin.getConfig().getBoolean("dense-spice.enabled", true) + ? Registry.BIOME.get(new NamespacedKey("dunerealms", "dense-spice")) : null; + this.highlands = planet.equals("arrakis") && plugin.getConfig().getBoolean("planets.arrakis.highlands.enabled", true) + && customKey != null ? Registry.BIOME.get(customKey) : null; + } + @Override public Biome getBiome(WorldInfo info, int x, int y, int z) { + if (denseSpice != null && DenseSpice.contains(info.getSeed(),x,z,Math.max(128,plugin.getConfig().getInt("dense-spice.spacing",512)))) return denseSpice; + if (highlands == null) return biome; + String path = "planets.arrakis.darkstone-islands."; + int minimumGap = Math.max(192, plugin.getConfig().getInt("planets.arrakis.highlands.min-spacing", 300)); + int maximumGap = Math.max(minimumGap, plugin.getConfig().getInt("planets.arrakis.highlands.max-spacing", 500)); + int minimum = Math.max(6, plugin.getConfig().getInt(path + "min-radius", 45)); + int maximum = Math.max(minimum, plugin.getConfig().getInt(path + "max-radius", 55)); + double scale = Math.max(.001, plugin.getConfig().getDouble("planets.arrakis.terrain.noise-scale", .02)); + return RockPlateaus.regionDepth(x,z,minimumGap,maximumGap,minimum,maximum,scale) >= 0 ? highlands : biome; + } + @Override public List getBiomes(WorldInfo info) { + Set all = new LinkedHashSet<>(); all.add(biome); if(highlands!=null)all.add(highlands); if(denseSpice!=null)all.add(denseSpice); return List.copyOf(all); + } + } + + private static long mix64(long value) { + value = (value ^ (value >>> 30)) * 0xBF58476D1CE4E5B9L; + value = (value ^ (value >>> 27)) * 0x94D049BB133111EBL; + return value ^ (value >>> 31); + } + + private static final class SietchPopulator extends BlockPopulator { + private final DuneTerrain plugin; + private final java.util.concurrent.ConcurrentHashMap> highlandSites = new java.util.concurrent.ConcurrentHashMap<>(); + private SietchPopulator(DuneTerrain plugin) { this.plugin = plugin; } + @Override public void populate(WorldInfo world, Random random, int cx, int cz, LimitedRegion region) { + if (!world.getName().equalsIgnoreCase("arrakis")) return; + if (!plugin.getConfig().getBoolean("sietches.enabled", true)) return; + populateHighlands(world,cx,cz,region); + int spacing = Math.max(256, plugin.getConfig().getInt("sietches.spacing", 2000)); + int centerX = cx * 16 + 8, centerZ = cz * 16 + 8; + long cellX = Math.floorDiv(centerX, spacing), cellZ = Math.floorDiv(centerZ, spacing); + long h1 = mix64(cellX * 0x9E3779B97F4A7C15L ^ cellZ * 0xC2B2AE3D27D4EB4FL ^ 0x534945544348L); + long h2 = mix64(cellZ * 0x165667B19E3779F9L ^ cellX * 0x85EBCA77C2B2AE63L ^ ~0x534945544348L); + long anchorX = cellX * spacing + 8 + Math.floorMod(h1, spacing - 16); + long anchorZ = cellZ * spacing + 8 + Math.floorMod(h2, spacing - 16); + if (Math.floorDiv(anchorX, 16) != cx || Math.floorDiv(anchorZ, 16) != cz) return; + int x = (int) anchorX, z = (int) anchorZ; + int y = region.getHighestBlockYAt(x, z); + if (y < world.getMinHeight() + 40 || y > world.getMaxHeight() - 4 || y - 36 < world.getMinHeight()) return; + + // Enlarged hub, side chambers, cache chamber, and reservoir. The surface entrance stays compact. + int hy = y - 11; + carveRoom(region, x, hy, z, 10, 6, 10); + carveTunnelNetwork(region, new int[][]{ + {x - 8, hy - 3, z - 2}, {x - 12, hy - 4, z - 2}, {x - 16, hy - 5, z - 2} + }); + carveRoom(region, x - 18, hy - 6, z - 2, 8, 4, 8); + carveTunnelNetwork(region, new int[][]{ + {x + 8, hy - 3, z + 2}, {x + 12, hy - 4, z + 2}, {x + 16, hy - 5, z + 2} + }); + carveRoom(region, x + 18, hy - 6, z + 2, 8, 4, 8); + carveTunnelNetwork(region, new int[][]{ + {x, hy - 3, z - 3}, {x - 3, hy - 4, z - 5}, {x - 6, hy - 5, z - 8}, + {x - 4, hy - 6, z - 11}, {x, hy - 7, z - 12}, {x + 3, hy - 8, z - 11}, + {x, hy - 10, z - 10} + }); + carveRoom(region, x, hy - 12, z - 10, 8, 4, 8); + carveRoom(region, x, hy - 23, z + 8, 14, 10, 14); + + // A broad, rough cave mouth opens from the surface and slopes down into the hub. + carveCaveEntrance(region, x, z, hy); + + // Long switchbacks keep the descent gradual from the hub to the enlarged reservoir. + carveTunnelNetwork(region, new int[][]{ + {x, hy - 3, z + 3}, {x - 4, hy - 4, z + 5}, {x - 8, hy - 5, z + 8}, + {x - 10, hy - 6, z + 12}, {x - 8, hy - 7, z + 14}, {x - 4, hy - 8, z + 14}, + {x - 2, hy - 9, z + 12}, {x - 5, hy - 10, z + 9}, {x - 8, hy - 11, z + 7}, + {x - 10, hy - 12, z + 9}, {x - 8, hy - 13, z + 12}, {x - 5, hy - 14, z + 13}, + {x - 3, hy - 15, z + 10}, {x - 5, hy - 16, z + 7}, {x - 7, hy - 17, z + 8}, + {x - 5, hy - 18, z + 10}, {x - 4, hy - 19, z + 9}, {x - 5, hy - 20, z + 8} + }); + + // The waterfall runs beside the ramp and feeds a broad pool at the bottom. + for (int dy = -4; dy >= -25; dy--) { + set(region, x + 3, hy + dy, z + 5, Material.WATER); + set(region, x + 2, hy + dy, z + 5, Material.STONE); + set(region, x + 4, hy + dy, z + 5, Material.STONE); + set(region, x + 3, hy + dy, z + 4, Material.STONE); + set(region, x + 3, hy + dy, z + 6, Material.STONE); + } + for (int dx = -5; dx <= 5; dx++) for (int dz = -5; dz <= 5; dz++) { + if (dx * dx + dz * dz > 25) continue; + set(region, x + dx, hy - 29, z + 8 + dz, Material.STONE); + for (int waterY = hy - 28; waterY <= hy - 25; waterY++) + set(region, x + dx, waterY, z + 8 + dz, Material.WATER); + } + + furnishSleepingHall(region, x - 18, hy - 6, z - 2); + furnishKitchen(region, x + 18, hy - 6, z + 2); + placePersonalChest(region, x, hy - 14, z - 10, cellX, cellZ); + for (int[] lamp : new int[][]{{0,-1,0},{-20,-5,-2},{20,-5,2},{0,-11,-11}}) + set(region, x + lamp[0], hy + lamp[1], z + lamp[2], Material.TORCH); + } + + private void populateHighlands(WorldInfo world,int cx,int cz,LimitedRegion region) { + if(!plugin.getConfig().getBoolean("planets.arrakis.highlands.enabled",true) + ||!plugin.getConfig().getBoolean("sietches.highlands.enabled",true))return; + int minGap=Math.max(192,plugin.getConfig().getInt("planets.arrakis.highlands.min-spacing",300)); + int maxGap=Math.max(minGap,plugin.getConfig().getInt("planets.arrakis.highlands.max-spacing",500)); + int spacing=(minGap+maxGap)/2; + int minRadius=Math.max(6,plugin.getConfig().getInt("planets.arrakis.darkstone-islands.min-radius",45)); + int maxRadius=Math.max(minRadius,plugin.getConfig().getInt("planets.arrakis.darkstone-islands.max-radius",55)); + double scale=Math.max(.001,plugin.getConfig().getDouble("planets.arrakis.terrain.noise-scale",.02)); + int reach=(int)Math.ceil(maxRadius*2.0)+3; + if(highlandSites.size()>2048)highlandSites.clear(); + for(int gx=Math.floorDiv(cx*16-reach,spacing);gx<=Math.floorDiv(cx*16+15+reach,spacing);gx++) + for(int gz=Math.floorDiv(cz*16-reach,spacing);gz<=Math.floorDiv(cz*16+15+reach,spacing);gz++) { + long seed=RockPlateaus.formationSeed(gx,gz); + var shape=RockPlateaus.shape(seed,RockPlateaus.centerX(gx,minGap,maxGap),RockPlateaus.centerZ(gz,minGap,maxGap),RockPlateaus.radius(seed,minRadius,maxRadius),scale); + var sites=highlandSites.computeIfAbsent(shape,k->HighlandSietches.sites(k, + plugin.getConfig().getInt("sietches.highlands.frequency-multiplier",3),plugin.getConfig().getInt("sietches.highlands.minimum-per-region",3))); + for(var site:sites) { + int x=site.x(),z=site.z(); + if(Math.floorDiv(x,16)!=cx||Math.floorDiv(z,16)!=cz)continue; + int surface=region.getHighestBlockYAt(x,z),hy=surface-11; + if(hy-12<=world.getMinHeight()||surface+2>=world.getMaxHeight())continue; + // Compact multi-room caves fit three sites without the large desert template colliding. + carveRoom(region,x,hy,z,7,4,7); + carveRoom(region,x-5,hy-3,z+5,4,3,4); + carveRoom(region,x+5,hy-3,z+5,4,3,4); + carveTunnelNetwork(region,new int[][]{{x,hy-1,z},{x-3,hy-2,z+3},{x-5,hy-3,z+5}}); + carveTunnelNetwork(region,new int[][]{{x,hy-1,z},{x+3,hy-2,z+3},{x+5,hy-3,z+5}}); + carveCaveEntrance(region,x,z,hy); + furnishSleepingHall(region,x-5,hy-4,z+5); + furnishKitchen(region,x+5,hy-4,z+5); + carveRoom(region,x,hy-8,z+6,4,3,4); + carveTunnelNetwork(region,new int[][]{{x,hy-1,z+2},{x+2,hy-3,z+4},{x+2,hy-5,z+6},{x,hy-8,z+6}}); + for(int dx=-2;dx<=2;dx++)for(int dz=-2;dz<=2;dz++) { + set(region,x+dx,hy-11,z+6+dz,Material.STONE); + set(region,x+dx,hy-10,z+6+dz,Material.WATER); + } + placePersonalChest(region,x,hy-2,z-3,site.cacheId(),0x484947484c414e44L); + set(region,x,hy-3,z,Material.CHISELED_STONE_BRICKS); + set(region,x,hy-2,z,Material.LANTERN); + } + } + } + + private static void carveRoom(LimitedRegion region, int cx, int cy, int cz, int rx, int ry, int rz) { + for (int dx = -rx; dx <= rx; dx++) for (int dy = -ry; dy <= ry; dy++) for (int dz = -rz; dz <= rz; dz++) { + double outer = dx * dx / (double)(rx * rx) + dy * dy / (double)(ry * ry) + dz * dz / (double)(rz * rz); + if (outer > 1.15) continue; + int x = cx + dx, y = cy + dy, z = cz + dz; + if (outer >= 0.58) { + long variation = mix64((long)x * 341873128712L ^ (long)y * 132897987541L ^ z); + Material wall = switch ((int)Math.floorMod(variation, 10)) { + case 0, 1 -> Material.TUFF; + case 2 -> Material.ANDESITE; + case 3 -> Material.COBBLED_DEEPSLATE; + case 4 -> Material.COBBLESTONE; + case 5 -> Material.MOSSY_COBBLESTONE; + default -> Material.STONE; + }; + set(region, x, y, z, wall); + } else set(region, x, y, z, Material.AIR); + } + } + + private static void carveCaveEntrance(LimitedRegion region, int x, int z, int hubY) { + int entryX = x + 6, entryZ = z - 7; + int outsideX = x + 11; + int surfaceY = region.getHighestBlockYAt(outsideX, entryZ); + int mouthY = Math.max(hubY + 4, surfaceY - 3); + + // Hollow a rough chamber just behind the stone arch, then connect it to the hub. + carveRoom(region, entryX, mouthY, entryZ, 3, 2, 3); + carveCaveTunnel(region, outsideX, surfaceY - 1, entryZ, x + 6, mouthY, entryZ); + carveTunnelNetwork(region, new int[][]{ + {x + 6, mouthY, entryZ}, {x + 3, mouthY - 1, z - 9}, {x, mouthY - 2, z - 11}, + {x - 4, mouthY - 3, z - 10}, {x - 6, mouthY - 4, z - 7}, + {x - 5, mouthY - 5, z - 3}, {x - 2, mouthY - 6, z - 1}, {x, hubY + 1, z} + }); + + // Irregular stone and tuff form a visible cave arch around the entrance. + int faceX = x + 7; + for (int dy = -3; dy <= 3; dy++) for (int dz = -4; dz <= 4; dz++) { + double arch = dy * dy / 16.0 + dz * dz / 25.0; + if (arch > 1.35) continue; + if (arch < 0.72) { + set(region, faceX, mouthY + dy, entryZ + dz, Material.AIR); + continue; + } + long variation = mix64((long)(faceX + dz) * 341873128712L ^ (long)(mouthY + dy) * 132897987541L ^ 0x4341564541524348L); + Material rock = switch ((int)Math.floorMod(variation, 5)) { + case 0 -> Material.TUFF; + case 1 -> Material.ANDESITE; + case 2 -> Material.COBBLED_DEEPSLATE; + default -> Material.STONE; + }; + set(region, faceX, mouthY + dy, entryZ + dz, rock); + } + } + + private static void carveTunnel(LimitedRegion region, double ax, double ay, double az, double bx, double by, double bz) { + int steps = (int)Math.ceil(Math.sqrt((bx-ax)*(bx-ax) + (by-ay)*(by-ay) + (bz-az)*(bz-az)) * 1.5); + for (int i = 0; i <= steps; i++) { + double t = i / (double)Math.max(1, steps); + int cx = (int)Math.round(ax + (bx-ax)*t), cy = (int)Math.round(ay + (by-ay)*t), cz = (int)Math.round(az + (bz-az)*t); + for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) for (int dz = -1; dz <= 1; dz++) + if (dx*dx + dy*dy + dz*dz <= 2) set(region, cx+dx, cy+dy, cz+dz, Material.AIR); + } + } + + private static void carveTunnelNetwork(LimitedRegion region, int[][] points) { + for (int i = 1; i < points.length; i++) { + int[] from = points[i - 1], to = points[i]; + carveTunnel(region, from[0], from[1], from[2], to[0], to[1], to[2]); + } + } + + private static void carveCaveTunnel(LimitedRegion region, double ax, double ay, double az, double bx, double by, double bz) { + int steps = (int)Math.ceil(Math.sqrt((bx-ax)*(bx-ax) + (by-ay)*(by-ay) + (bz-az)*(bz-az)) * 1.5); + for (int i = 0; i <= steps; i++) { + double t = i / (double)Math.max(1, steps); + int cx = (int)Math.round(ax + (bx-ax)*t), cy = (int)Math.round(ay + (by-ay)*t), cz = (int)Math.round(az + (bz-az)*t); + for (int dx = -2; dx <= 2; dx++) for (int dy = -2; dy <= 2; dy++) for (int dz = -2; dz <= 2; dz++) + if (dx*dx + dy*dy + dz*dz <= 4) set(region, cx+dx, cy+dy, cz+dz, Material.AIR); + } + } + + private static void furnishSleepingHall(LimitedRegion r, int x, int y, int z) { + for (int i = -1; i <= 1; i++) { + set(r, x+i, y-1, z, Material.POLISHED_ANDESITE); + set(r, x+i, y, z, Material.RED_CARPET); + set(r, x+i, y+1, z, Material.AIR); + } + set(r, x-1, y, z, Material.WHITE_CARPET); + set(r, x+1, y, z, Material.WHITE_CARPET); + set(r, x-2, y-1, z+2, Material.BARREL); + } + + private static void furnishKitchen(LimitedRegion r, int x, int y, int z) { + set(r, x-2, y-1, z, Material.STONE_BRICKS); + set(r, x-2, y, z, Material.FURNACE); + set(r, x-1, y-1, z, Material.STONE_BRICKS); + set(r, x-1, y, z, Material.SMOKER); + set(r, x, y-1, z, Material.STONE_BRICKS); + set(r, x, y, z, Material.CRAFTING_TABLE); + set(r, x+2, y-1, z+1, Material.BARREL); + set(r, x+2, y, z+1, Material.CAULDRON); + } + + private void placePersonalChest(LimitedRegion r, int x, int y, int z, long cellX, long cellZ) { + set(r, x, y-1, z, Material.CHISELED_STONE_BRICKS); + set(r, x, y, z, Material.CHEST); + BlockState state = r.getBlockState(x, y, z); + if (state instanceof Chest chest) { + String id = Long.toUnsignedString(mix64(cellX * 0x9E3779B97F4A7C15L ^ cellZ * 0xC2B2AE3D27D4EB4FL ^ 0x534945544348L), 16); + chest.setCustomName("Personal Sietch Cache"); + chest.getPersistentDataContainer().set(plugin.sietchLootIdKey, PersistentDataType.STRING, id); + chest.update(true, false); + } + } + + private static void set(LimitedRegion region, int x, int y, int z, Material material) { + if (region.isInRegion(x, y, z)) region.setType(x, y, z, material); + } + } + + private static final class SpiceBlowPopulator extends BlockPopulator { + private static final long SALT = 0x5350494345424c4fL; + private final DuneTerrain plugin; + private SpiceBlowPopulator(DuneTerrain plugin) { this.plugin = plugin; } + + @Override public void populate(WorldInfo world, Random random, int chunkX, int chunkZ, LimitedRegion region) { + if (!world.getName().equalsIgnoreCase("arrakis") || !plugin.getConfig().getBoolean("spice-blow.enabled", true)) return; + int spacing = Math.max(256, plugin.getConfig().getInt("spice-blow.spacing", 2000)); + long centerX = chunkX * 16L + 8, centerZ = chunkZ * 16L + 8; + long cellX = Math.floorDiv(centerX, spacing), cellZ = Math.floorDiv(centerZ, spacing); + long h1 = mix64(cellX * 0x9E3779B97F4A7C15L ^ cellZ * 0xC2B2AE3D27D4EB4FL ^ SALT); + long h2 = mix64(cellZ * 0x165667B19E3779F9L ^ cellX * 0x85EBCA77C2B2AE63L ^ ~SALT); + long anchorX = cellX * spacing + 8 + Math.floorMod(h1, spacing - 16); + long anchorZ = cellZ * spacing + 8 + Math.floorMod(h2, spacing - 16); + if (Math.floorDiv(anchorX, 16) != chunkX || Math.floorDiv(anchorZ, 16) != chunkZ) return; + int x = (int)anchorX, z = (int)anchorZ, y = region.getHighestBlockYAt(x, z); + if (y < world.getMinHeight() + 2 || y + 2 >= world.getMaxHeight()) return; + if (!region.isInRegion(x, y, z) || region.getType(x, y, z) != Material.SAND) return; + for (int dx = -1; dx <= 1; dx++) for (int dz = -1; dz <= 1; dz++) + if (region.isInRegion(x + dx, y, z + dz)) region.setType(x + dx, y, z + dz, Material.MAGMA_BLOCK); + if (region.isInRegion(x, y + 1, z)) region.setType(x, y + 1, z, Material.MAGMA_BLOCK); + } + } +} diff --git a/DuneTerrain/src/test/java/com/example/duneterrain/ArrakisEcologyTest.java b/DuneTerrain/src/test/java/com/example/duneterrain/ArrakisEcologyTest.java new file mode 100644 index 00000000..0ca0fb0f --- /dev/null +++ b/DuneTerrain/src/test/java/com/example/duneterrain/ArrakisEcologyTest.java @@ -0,0 +1,20 @@ +package com.example.duneterrain; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; +import java.util.*; +import org.bukkit.*; +import org.bukkit.entity.*; +import org.bukkit.event.entity.*; +import org.bukkit.inventory.ItemStack; +import org.bukkit.persistence.*; +import org.bukkit.plugin.Plugin; + +class ArrakisEcologyTest { + private T entity(Class kind,String worldName){T e=mock(kind);World world=mock(World.class);when(world.getName()).thenReturn(worldName);when(e.getWorld()).thenReturn(world);var pdc=mock(PersistentDataContainer.class);when(e.getPersistentDataContainer()).thenReturn(pdc);when(pdc.getKeys()).thenReturn(Set.of());return e;} + @Test void ordinaryNaturalMobsAreBlockedOnlyOnArrakis(){var feature=new ArrakisEcology(mock(Plugin.class),id->{throw new AssertionError();},()->0);var desert=new CreatureSpawnEvent(entity(Zombie.class,"arrakis"),CreatureSpawnEvent.SpawnReason.NATURAL);feature.spawning(desert);assertTrue(desert.isCancelled());var other=new CreatureSpawnEvent(entity(Zombie.class,"caladan"),CreatureSpawnEvent.SpawnReason.NATURAL);feature.spawning(other);assertFalse(other.isCancelled());} + @Test void customSpawnedRaidersAndTaggedCreaturesSurvive(){var feature=new ArrakisEcology(mock(Plugin.class),id->{throw new AssertionError();},()->0);var custom=new CreatureSpawnEvent(entity(Zombie.class,"arrakis"),CreatureSpawnEvent.SpawnReason.CUSTOM);feature.spawning(custom);assertFalse(custom.isCancelled());var tagged=entity(Zombie.class,"arrakis");when(tagged.getPersistentDataContainer().getKeys()).thenReturn(Set.of(new NamespacedKey("dunerealms","raider")));var event=new CreatureSpawnEvent(tagged,CreatureSpawnEvent.SpawnReason.NATURAL);feature.spawning(event);assertFalse(event.isCancelled());} + @Test void fireDeathsUseCookedDefinitionAndWaterRollIsExactlyTwentyPercent(){for(boolean burning:new boolean[]{false,true})for(double roll:new double[]{.19999,.2}){var animal=entity(Rabbit.class,"arrakis");when(animal.getFireTicks()).thenReturn(burning?20:0);var meat=mock(ItemStack.class);var hide=mock(ItemStack.class);var vial=mock(ItemStack.class);var requested=new ArrayList();var feature=new ArrakisEcology(mock(Plugin.class),id->{requested.add(id);return id.equals("jerboa_hide")?hide:id.equals("jerboa_water_vial")?vial:meat;},()->roll);var event=new EntityDeathEvent(animal,mock(org.bukkit.damage.DamageSource.class),new ArrayList<>());feature.death(event);assertEquals(burning?"jerboa_cooked_meat":"jerboa_raw_meat",requested.getFirst());assertEquals(roll<.2?List.of(meat,hide,vial):List.of(meat,hide),event.getDrops());}} + @Test void suffocationImmunityDoesNotDependOnWater(){var wolf=entity(Wolf.class,"arrakis");when(wolf.getPersistentDataContainer().has(new NamespacedKey("dunerealms","tiny_worm_pet"),PersistentDataType.BYTE)).thenReturn(true);var feature=new ArrakisEcology(mock(Plugin.class),id->{throw new AssertionError();},()->0);var event=mock(EntityDamageEvent.class);when(event.getEntity()).thenReturn(wolf);when(event.getCause()).thenReturn(EntityDamageEvent.DamageCause.SUFFOCATION);feature.suffocation(event);verify(event).setCancelled(true);var attack=mock(EntityDamageEvent.class);when(attack.getEntity()).thenReturn(wolf);when(attack.getCause()).thenReturn(EntityDamageEvent.DamageCause.ENTITY_ATTACK);feature.suffocation(attack);verify(attack,never()).setCancelled(anyBoolean());} +} + diff --git a/network/ARRAKIS-CREATURES.md b/network/ARRAKIS-CREATURES.md index 6cc6d4ac..b66ec93f 100644 --- a/network/ARRAKIS-CREATURES.md +++ b/network/ARRAKIS-CREATURES.md @@ -17,3 +17,13 @@ and preserve the live 1.0.12-arrakeen1 terrain build when recovering its source. Reapplication refreshes the duration rather than stacking modifiers. - Pet death: spice and one bone, both guaranteed. Bone bears the pet's name, Legendary MK XIV; memorial item, no extra combat power. + +## Ecology implementation and rollout + +`ArrakisEcology` supplies the Arrakis-only spawn filter, Jerboa breeding and custom drops, fire cooking, furnace/smoker/campfire result hook, and native wolf pet flow. Bloom coordinates are recorded in the chunk PDC so revisiting a bloom does not issue another pet. Water applies one named +25% attack/max-health modifier for 90 seconds, plus Regeneration I. Refreshes do not stack modifiers; absolute expiry is checked after entity load and on a one-second maintenance tick. Native wolf AI, ownership, growth and mating remain in use. + +`tools/PatchArrakisEcology.java` adds only this class and its item-factory interface to the exact SHA-256-pinned live `1.0.12-arrakeen1` JAR. It injects two calls into the existing startup and bloom methods. All unrelated existing methods and JAR entries are checked byte for byte. Do not deploy the complete older terrain build from this checkout. + +Four new behavioural tests cover planet isolation, natural vs custom/tagged mobs, raw vs fire-cooked death drops and the exact 20% vial threshold, and suffocation immunity without a water buff. The terrain suite has 19 passing tests. + +Still pending: custom Jerboa/worm textures and the flesh/hide artwork, pet/water runtime verification, and actual rollout. Gold rabbits and native wolves remain visual placeholders until the world-scoped artwork is ready. No texture or real player interaction is claimed as verified. diff --git a/tools/PatchArrakisEcology.java b/tools/PatchArrakisEcology.java new file mode 100644 index 00000000..6625f797 --- /dev/null +++ b/tools/PatchArrakisEcology.java @@ -0,0 +1,35 @@ +import java.io.*; +import java.nio.file.*; +import java.security.*; +import java.util.*; +import java.util.zip.*; +import org.objectweb.asm.*; +import org.objectweb.asm.tree.*; + +/** Extend the exact live terrain build; never deploy a rebuild of stale terrain sources. */ +public final class PatchArrakisEcology { + static final String MAIN="com/example/duneterrain/DuneTerrain.class",ADDON="com/example/duneterrain/ArrakisEcology"; + static final String ORIGINAL="f6f8547049bfd022bc68b57f37c5a259f268c2ad54a2aa08790ac3f011920bcf"; + static byte[] methodBytes(MethodNode method){var out=new ClassWriter(0);out.visit(65,Opcodes.ACC_PUBLIC,"MethodCheck",null,"java/lang/Object",null);method.accept(out.visitMethod(method.access,method.name,method.desc,method.signature,method.exceptions.toArray(String[]::new)));out.visitEnd();return out.toByteArray();} + static byte[] patch(byte[] original){ + var node=new ClassNode();new ClassReader(original).accept(node,0);var before=new HashMap();var touched=new HashSet(); + for(var method:node.methods){ + for(var instruction:method.instructions)if(instruction instanceof MethodInsnNode call&&call.owner.equals(ADDON))throw new IllegalStateException("Ecology is already installed"); + String key=method.name+method.desc;before.put(key,methodBytes(method)); + boolean start=key.equals("onEnable()V"),bloom=key.equals("announceSpiceBloom(Lorg/bukkit/Location;)V");if(!start&&!bloom)continue; + var hook=new InsnList();hook.add(new VarInsnNode(Opcodes.ALOAD,0));if(bloom)hook.add(new VarInsnNode(Opcodes.ALOAD,1));hook.add(new MethodInsnNode(Opcodes.INVOKESTATIC,ADDON,start?"start":"bloom",start?"(Lorg/bukkit/plugin/Plugin;)V":"(Lorg/bukkit/plugin/Plugin;Lorg/bukkit/Location;)V",false));method.instructions.insert(hook);method.maxStack=Math.max(method.maxStack,bloom?2:1);touched.add(key); + } + if(!touched.equals(Set.of("onEnable()V","announceSpiceBloom(Lorg/bukkit/Location;)V")))throw new IllegalStateException("Live terrain hooks changed"); + var writer=new ClassWriter(0);node.accept(writer);byte[] result=writer.toByteArray();var check=new ClassNode();new ClassReader(result).accept(check,0);for(var method:check.methods){String key=method.name+method.desc;if(!touched.contains(key)&&!Arrays.equals(before.get(key),methodBytes(method)))throw new IllegalStateException("Unrelated terrain method changed: "+key);}return result; + } + public static void main(String[] args)throws Exception{ + if(args.length!=3)throw new IllegalArgumentException("original.jar classes-folder output.jar");var source=Path.of(args[0]);var classes=Path.of(args[1]);var target=Path.of(args[2]);if(Files.exists(target)||source.toRealPath().equals(target.toAbsolutePath()))throw new IllegalStateException("Use a new output file");if(!HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(source))).equals(ORIGINAL))throw new IllegalStateException("Live terrain identity changed"); + var entries=new LinkedHashMap();try(var zip=new ZipInputStream(Files.newInputStream(source))){for(ZipEntry entry;(entry=zip.getNextEntry())!=null;){if(entries.put(entry.getName(),zip.readAllBytes())!=null)throw new IllegalStateException("Duplicate JAR entry");}} + var baseline=new LinkedHashMap<>(entries);entries.put(MAIN,patch(Objects.requireNonNull(entries.get(MAIN))));String descriptor=new String(entries.get("plugin.yml"),java.nio.charset.StandardCharsets.UTF_8);if(!descriptor.contains("version: 1.0.12-arrakeen1"))throw new IllegalStateException("Unexpected live version");entries.put("plugin.yml",descriptor.replace("version: 1.0.12-arrakeen1","version: 1.0.12-arrakeen1-ecology1").getBytes(java.nio.charset.StandardCharsets.UTF_8)); + var additions=new ArrayList();try(var paths=Files.list(classes.resolve("com/example/duneterrain"))){for(var p:paths.filter(p->p.getFileName().toString().matches("ArrakisEcology(?:\\$[A-Za-z0-9]+)?\\.class")).sorted().toList()){String name="com/example/duneterrain/"+p.getFileName();if(entries.containsKey(name))throw new IllegalStateException("Duplicate ecology class");entries.put(name,Files.readAllBytes(p));additions.add(name);}} + if(!additions.contains(ADDON+".class")||!additions.contains(ADDON+"$Items.class"))throw new IllegalStateException("Compiled ecology classes missing"); + for(var e:baseline.entrySet())if(!Set.of(MAIN,"plugin.yml").contains(e.getKey())&&!Arrays.equals(e.getValue(),entries.get(e.getKey())))throw new IllegalStateException("Unrelated JAR entry changed"); + Files.createDirectories(target.toAbsolutePath().getParent());try(var out=new ZipOutputStream(Files.newOutputStream(target,StandardOpenOption.CREATE_NEW))){for(var e:entries.entrySet()){var entry=new ZipEntry(e.getKey());entry.setTime(0);out.putNextEntry(entry);out.write(e.getValue());out.closeEntry();}} + System.out.println("Preserved "+(baseline.size()-2)+" existing JAR entries; changed only terrain hooks and version; added "+additions.size()+" ecology classes.");System.out.println("SHA256 "+HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(target)))); + } +}