From 8d6a2e8882a1ea45067db1bbf429aedde1711f7f Mon Sep 17 00:00:00 2001 From: CocoTheOwner Date: Sat, 17 Jul 2021 21:41:30 +0200 Subject: [PATCH] Basic sapling manager logic. Needs polishing --- .../com/volmit/iris/core/SaplingManager.java | 109 +++++++++++++++--- .../iris/engine/object/IrisSapling.java | 3 +- 2 files changed, 95 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/volmit/iris/core/SaplingManager.java b/src/main/java/com/volmit/iris/core/SaplingManager.java index 46ffa7891..6a45944d4 100644 --- a/src/main/java/com/volmit/iris/core/SaplingManager.java +++ b/src/main/java/com/volmit/iris/core/SaplingManager.java @@ -5,8 +5,9 @@ import com.volmit.iris.engine.IrisEngine; import com.volmit.iris.engine.IrisWorldManager; import com.volmit.iris.engine.IrisWorlds; import com.volmit.iris.engine.framework.IrisAccess; -import com.volmit.iris.engine.object.IrisBiome; -import com.volmit.iris.engine.object.IrisDimension; +import com.volmit.iris.engine.object.*; +import com.volmit.iris.util.collection.KList; +import com.volmit.iris.util.math.RNG; import org.bukkit.TreeType; import org.bukkit.block.data.type.Sapling; import org.bukkit.event.EventHandler; @@ -20,29 +21,107 @@ public class SaplingManager implements Listener { public SaplingManager() { Iris.instance.registerListener(this); + Iris.info("Loading Sapling Manager"); } + /** + * This function does the following: + * 1. Is the sapling growing in an Iris world? No -> exit + * 2. Is the sapling overwriting setting on in that dimension? No -> exit + * 3. Check biome for overrides for that sapling type -> Found -> use + * 4. Check region ... + * 5. Check dimension ... + * 6. Exit if none are found + * @param event Checks the given event for sapling overrides + */ @EventHandler public void onStructureGrowEvent(StructureGrowEvent event) { - // Must be iris world - if (!IrisWorlds.isIrisWorld(event.getWorld())) return; + // TODO: Remove this line + Iris.info("Sapling grew @ " + event.getLocation() + " for " + event.getSpecies().name() + " bonemealed is " + event.isFromBonemeal()); - IrisAccess worldAccess; - try { - worldAccess = Objects.requireNonNull(IrisWorlds.access(event.getWorld())); - } catch (Throwable e){ - Iris.reportError(e); - return; + + // TODO: Remove if statement here once Iris worlds are creatable again + boolean debug = true; + + if (!debug) { + // Must be iris world + if (!IrisWorlds.isIrisWorld(event.getWorld())) return; + + IrisAccess worldAccess; + try { + worldAccess = Objects.requireNonNull(IrisWorlds.access(event.getWorld())); + } catch (Throwable e) { + Iris.reportError(e); + return; + } + + IrisDimension dim = worldAccess.getCompound().getRootDimension(); } - IrisDimension dim = worldAccess.getCompound().getDefaultEngine().getDimension(); - - // Must have override enabled - if (!dim.isOverrideSaplings()) return; // TODO: Remove this line - Iris.info("Sapling grew @ " + event.getLocation() + " for " + event.getSpecies().name() + " bonemealed is " + event.isFromBonemeal() + " by player " + Objects.requireNonNull(event.getPlayer()).getName()); + IrisDimension dimension = IrisDataManager.loadAnyDimension("overworld"); + + // Must have override enabled + if (!dimension.isOverrideSaplings()) return; + + // TODO: Remove this line Iris.info("Should replace sapling now!"); + + IrisAccess worldAccess = IrisWorlds.access(event.getWorld()); + assert worldAccess != null; + KList replace = null; + + // Check biome + IrisBiome biome = worldAccess.getBiome(event.getLocation().getBlockX(), event.getLocation().getBlockZ()); + for (IrisSapling sapling : biome.getSaplings()){ + for (TreeType type : sapling.getTypes()){ + if (type == event.getSpecies()){ + replace = sapling.getReplace(); + // If we decide to do some sort of addition (biome + region + dim for options) we can do that here + } + } + } + + // Check region + if (replace == null) { + IrisRegion region = worldAccess.getCompound().getDefaultEngine().getRegion(event.getLocation().getBlockX(), event.getLocation().getBlockZ()); + for (IrisSapling sapling : region.getSaplings()) { + for (TreeType type : sapling.getTypes()) { + if (type == event.getSpecies()) { + replace = sapling.getReplace(); + // If we decide to do some sort of addition (biome + region + dim for options) we can do that here + } + } + } + } + + // Check dimension + if (replace == null) { + for (IrisSapling sapling : dimension.getSaplings()) { + for (TreeType type : sapling.getTypes()) { + if (type == event.getSpecies()) { + replace = sapling.getReplace(); + // If we decide to do some sort of addition (biome + region + dim for options) we can do that here + } + } + } + } + + // Check to make sure something was found + if (replace == null || replace.size() == 0) return; + + // Pick a random object from the list of objects found + String object = replace.get(RNG.r.i(0, replace.size() - 1)); + + // Cancel vanilla event + event.setCancelled(true); + + // Retrieve & place the object + // TODO: Make this specific for this pack + Iris.info("Placing tree object instead of vanilla tree: " + object); + IrisObject obj = IrisDataManager.loadAnyObject(object); + obj.place(event.getLocation()); } } diff --git a/src/main/java/com/volmit/iris/engine/object/IrisSapling.java b/src/main/java/com/volmit/iris/engine/object/IrisSapling.java index cccea648e..cc80a38ba 100644 --- a/src/main/java/com/volmit/iris/engine/object/IrisSapling.java +++ b/src/main/java/com/volmit/iris/engine/object/IrisSapling.java @@ -30,5 +30,4 @@ public class IrisSapling { @MinNumber(1) @MaxNumber(4) private int size = 1; - -} +} \ No newline at end of file