From 944ef838054ad1d370377294057ad31af76e78e3 Mon Sep 17 00:00:00 2001 From: CocoTheOwner Date: Sun, 18 Jul 2021 17:21:29 +0200 Subject: [PATCH] everything reworked --- .../com/volmit/iris/core/SaplingManager.java | 173 ++++++++++-------- .../volmit/iris/core/edit/DustRevealer.java | 1 + .../volmit/iris/engine/object/IrisBiome.java | 4 +- .../iris/engine/object/IrisDimension.java | 6 +- .../volmit/iris/engine/object/IrisRegion.java | 4 +- .../{IrisSapling.java => IrisTree.java} | 16 +- .../iris/engine/object/IrisTreeModes.java | 12 ++ .../iris/engine/object/IrisTreeSettings.java | 29 +++ .../iris/engine/object/IrisTreeType.java | 75 ++++++++ 9 files changed, 227 insertions(+), 93 deletions(-) rename src/main/java/com/volmit/iris/engine/object/{IrisSapling.java => IrisTree.java} (62%) create mode 100644 src/main/java/com/volmit/iris/engine/object/IrisTreeModes.java create mode 100644 src/main/java/com/volmit/iris/engine/object/IrisTreeSettings.java create mode 100644 src/main/java/com/volmit/iris/engine/object/IrisTreeType.java diff --git a/src/main/java/com/volmit/iris/core/SaplingManager.java b/src/main/java/com/volmit/iris/core/SaplingManager.java index 6a45944d4..85927584a 100644 --- a/src/main/java/com/volmit/iris/core/SaplingManager.java +++ b/src/main/java/com/volmit/iris/core/SaplingManager.java @@ -1,127 +1,148 @@ package com.volmit.iris.core; import com.volmit.iris.Iris; -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.*; import com.volmit.iris.util.collection.KList; import com.volmit.iris.util.math.RNG; +import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.TreeType; -import org.bukkit.block.data.type.Sapling; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.world.StructureGrowEvent; +import org.jetbrains.annotations.NotNull; import java.util.Objects; public class SaplingManager implements Listener { + private static final boolean debugMe = false; + 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 + /**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) { - // TODO: Remove this line - Iris.info("Sapling grew @ " + event.getLocation() + " for " + event.getSpecies().name() + " bonemealed is " + event.isFromBonemeal()); + if (debugMe) + Iris.info("Sapling grew @ " + event.getLocation() + " for " + event.getSpecies().name() + " usedBoneMeal is " + event.isFromBonemeal()); + // Must be iris world + if (!IrisWorlds.isIrisWorld(event.getWorld())) 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(); + IrisAccess worldAccess; + try { + worldAccess = Objects.requireNonNull(IrisWorlds.access(event.getWorld())); + } catch (Throwable e) { + Iris.reportError(e); + return; } + IrisTreeSettings settings = worldAccess.getCompound().getRootDimension().getSaplingSettings(); - // TODO: Remove this line - IrisDimension dimension = IrisDataManager.loadAnyDimension("overworld"); - + if (debugMe) Iris.info("Custom saplings are enabled: " + (settings.isEnabled() ? "Yes" : "No")); // Must have override enabled - if (!dimension.isOverrideSaplings()) return; + if (!settings.isEnabled()) return; - // TODO: Remove this line - Iris.info("Should replace sapling now!"); + KList treeObjects = new KList<>(); - IrisAccess worldAccess = IrisWorlds.access(event.getWorld()); - assert worldAccess != null; - KList replace = null; + // Get biome and region + IrisBiome biome = worldAccess.getBiome(event.getLocation().getBlockX(), event.getLocation().getBlockY(), event.getLocation().getBlockZ()); + IrisRegion region = worldAccess.getCompound().getDefaultEngine().getRegion(event.getLocation().getBlockX(), event.getLocation().getBlockZ()); - // 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 - } - } - } + if (debugMe) + Iris.info("Biome name: " + biome.getName() + " | List of saplings: " + biome.getSaplings().toString()); + if (debugMe) + Iris.info("Region name: " + region.getName() + " | List of saplings: " + region.getSaplings().toString()); + if (debugMe) + Iris.info("Dimension saplings: " + settings.getSaplings().toString()); + + int saplingSize = getSaplingSize(event.getLocation()); + + // Check biome, region and dimension + treeObjects.addAll(getSaplingsFrom(biome.getSaplings(), event.getSpecies(), saplingSize)); // 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 - } - } - } - } + if (settings.getMode() == IrisTreeModes.ALL || treeObjects.size() == 0) + treeObjects.addAll(getSaplingsFrom(region.getSaplings(), event.getSpecies(), saplingSize)); // 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 - } - } - } - } + if (settings.getMode() == IrisTreeModes.ALL || treeObjects.size() == 0) + treeObjects.addAll(getSaplingsFrom(settings.getSaplings(), event.getSpecies(), saplingSize)); + + if (debugMe) Iris.info("List of saplings (together): " + treeObjects); // Check to make sure something was found - if (replace == null || replace.size() == 0) return; + if (treeObjects.size() == 0) return; // Pick a random object from the list of objects found - String object = replace.get(RNG.r.i(0, replace.size() - 1)); + String pickedObjectString = treeObjects.get(RNG.r.i(0, treeObjects.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()); + if (debugMe) Iris.info("Placing tree object instead of vanilla tree: " + pickedObjectString); + IrisObject pickedObject = IrisDataManager.loadAnyObject(pickedObjectString); + + // Delete the saplings (some objects may not have blocks where the sapling is) + // TODO: Rewrite this to delete the saplings that matter + event.getBlocks().forEach(b -> b.setType(Material.AIR)); + + // Rotate and place the object + pickedObject.rotate(new IrisObjectRotation(), 0, 90 * RNG.r.i(0, 3), 0); + pickedObject.place(event.getLocation()); + } + + /** + * Find all sapling types of the given TreeType in the container + * @param container Iris sapling config + * @param tree The tree type to find + * @param size The `size * size` area of the saplings + * @return A list of found object name strings + */ + @NotNull + private KList getSaplingsFrom(KList container, TreeType tree, int size) { + + // Translate TreeType to Iris TreeType + IrisTreeType eventTreeType = IrisTreeType.fromTreeType(tree); + + KList objects = new KList<>(); + + // Loop over all saplings in the container + // and their entered sapling types + // and copy the trees in the list if matching. + for (IrisTree sapling : container) { + for (IrisTreeType configTreeType : sapling.getTreeTypes()) { + if (configTreeType == eventTreeType && size == sapling.getSize()) { + objects.addAll(sapling.getObjects()); + if (debugMe) Iris.info("Added replacements: " + sapling.getObjects().toString()); + } + } + } + return objects; + } + + /** + * Retrieve the `size * size` area of a sapling (any sapling in the area) + * @param location The location to start the search from + * @return The `x * x` area of saplings + */ + private int getSaplingSize(Location location){ + // TODO: Write this + return 1; } } diff --git a/src/main/java/com/volmit/iris/core/edit/DustRevealer.java b/src/main/java/com/volmit/iris/core/edit/DustRevealer.java index 750e975c3..0fa4344c2 100644 --- a/src/main/java/com/volmit/iris/core/edit/DustRevealer.java +++ b/src/main/java/com/volmit/iris/core/edit/DustRevealer.java @@ -49,6 +49,7 @@ public class DustRevealer { if (a.getObject(block.getX(), block.getY(), block.getZ()) != null) { sender.sendMessage("Found object " + a.getObject(block.getX(), block.getY(), block.getZ())); + Iris.info(sender.getName() + " found object " + a.getObject(block.getX(), block.getY(), block.getZ())); J.a(() -> { new DustRevealer(a, world, new BlockPosition(block.getX(), block.getY(), block.getZ()), a.getObject(block.getX(), block.getY(), block.getZ()), new KList<>()); }); diff --git a/src/main/java/com/volmit/iris/engine/object/IrisBiome.java b/src/main/java/com/volmit/iris/engine/object/IrisBiome.java index a7872f6ec..34353d1f7 100644 --- a/src/main/java/com/volmit/iris/engine/object/IrisBiome.java +++ b/src/main/java/com/volmit/iris/engine/object/IrisBiome.java @@ -82,8 +82,8 @@ public class IrisBiome extends IrisRegistrant implements IRare { private KList entityInitialSpawns = new KList<>(); @Desc("Sapling override settings") - @ArrayType(min = 1, type = IrisSapling.class) - private KList saplings = new KList<>(); + @ArrayType(min = 1, type = IrisTree.class) + private KList saplings = new KList<>(); @ArrayType(min = 1, type = IrisEffect.class) @Desc("Effects are ambient effects such as potion effects, random sounds, or even particles around each player. All of these effects are played via packets so two players won't see/hear each others effects.\nDue to performance reasons, effects will play arround the player even if where the effect was played is no longer in the biome the player is in.") diff --git a/src/main/java/com/volmit/iris/engine/object/IrisDimension.java b/src/main/java/com/volmit/iris/engine/object/IrisDimension.java index def631cf6..e3e7bad3a 100644 --- a/src/main/java/com/volmit/iris/engine/object/IrisDimension.java +++ b/src/main/java/com/volmit/iris/engine/object/IrisDimension.java @@ -74,11 +74,7 @@ public class IrisDimension extends IrisRegistrant { private boolean aggressiveBiomeReshuffle = false; @Desc("Sapling override settings") - @ArrayType(min = 1, type = IrisSapling.class) - private KList saplings = new KList<>(); - - @Desc("Enable sapling overrides") - private boolean overrideSaplings = false; + private IrisTreeSettings saplingSettings = new IrisTreeSettings(); @Desc("Instead of a flat bottom, applies a clamp (using this noise style) to the bottom instead of a flat bottom. Useful for carving out center-dimensions in a dimension composite world.") diff --git a/src/main/java/com/volmit/iris/engine/object/IrisRegion.java b/src/main/java/com/volmit/iris/engine/object/IrisRegion.java index 43f8ad114..8f555ce08 100644 --- a/src/main/java/com/volmit/iris/engine/object/IrisRegion.java +++ b/src/main/java/com/volmit/iris/engine/object/IrisRegion.java @@ -80,8 +80,8 @@ public class IrisRegion extends IrisRegistrant implements IRare { private KList entityInitialSpawns = new KList<>(); @Desc("Sapling override settings") - @ArrayType(min = 1, type = IrisSapling.class) - private KList saplings = new KList<>(); + @ArrayType(min = 1, type = IrisTree.class) + private KList saplings = new KList<>(); @MinNumber(1) @MaxNumber(128) diff --git a/src/main/java/com/volmit/iris/engine/object/IrisSapling.java b/src/main/java/com/volmit/iris/engine/object/IrisTree.java similarity index 62% rename from src/main/java/com/volmit/iris/engine/object/IrisSapling.java rename to src/main/java/com/volmit/iris/engine/object/IrisTree.java index cc80a38ba..1a7189857 100644 --- a/src/main/java/com/volmit/iris/engine/object/IrisSapling.java +++ b/src/main/java/com/volmit/iris/engine/object/IrisTree.java @@ -11,23 +11,23 @@ import org.bukkit.TreeType; @Accessors(chain = true) @AllArgsConstructor @NoArgsConstructor -@Desc("Sapling override settings") +@Desc("Tree replace options") @Data -public class IrisSapling { +public class IrisTree { @Required - @Desc("The types of saplings overwritten") + @Desc("The types of trees overwritten") @ArrayType(min = 1, type = TreeType.class) - private KList types; + private KList treeTypes; @RegistryListObject @Required @ArrayType(min = 1, type = String.class) - @Desc("List of objects to overwrite saplings with") - private KList replace = new KList<>(); + @Desc("List of objects to replace trees with") + private KList objects = new KList<>(); - @Desc("The size of the square of saplings this applies to (two means a 2 by 2 sapling area)") + @Desc("The size of the square of saplings this applies to (2 means a 2 * 2 sapling area)") @MinNumber(1) - @MaxNumber(4) + @MaxNumber(5) private int size = 1; } \ No newline at end of file diff --git a/src/main/java/com/volmit/iris/engine/object/IrisTreeModes.java b/src/main/java/com/volmit/iris/engine/object/IrisTreeModes.java new file mode 100644 index 000000000..d3474b036 --- /dev/null +++ b/src/main/java/com/volmit/iris/engine/object/IrisTreeModes.java @@ -0,0 +1,12 @@ +package com.volmit.iris.engine.object; + +import com.volmit.iris.engine.object.annotations.Desc; + +@Desc("Sapling override object picking options") +public enum IrisTreeModes { + @Desc("Check biome, then region, then dimension, pick the first one that has options") + FIRST, + + @Desc("Check biome, regions, and dimensions, and pick any option from the total list") + ALL +} \ No newline at end of file diff --git a/src/main/java/com/volmit/iris/engine/object/IrisTreeSettings.java b/src/main/java/com/volmit/iris/engine/object/IrisTreeSettings.java new file mode 100644 index 000000000..584fc9a64 --- /dev/null +++ b/src/main/java/com/volmit/iris/engine/object/IrisTreeSettings.java @@ -0,0 +1,29 @@ +package com.volmit.iris.engine.object; + +import com.volmit.iris.engine.object.annotations.ArrayType; +import com.volmit.iris.engine.object.annotations.Desc; +import com.volmit.iris.util.collection.KList; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import lombok.experimental.Accessors; + +@Accessors(chain = true) +@AllArgsConstructor +@NoArgsConstructor +@Desc("Tree growth override settings") +@Data +@EqualsAndHashCode(callSuper = false) +public class IrisTreeSettings { + + @Desc("Turn replacing on and off") + boolean enabled = false; + + @Desc("Object picking modes") + IrisTreeModes mode = IrisTreeModes.FIRST; + + @Desc("Tree override list") + @ArrayType(min = 1, type = IrisTree.class) + private KList saplings = new KList<>(); +} diff --git a/src/main/java/com/volmit/iris/engine/object/IrisTreeType.java b/src/main/java/com/volmit/iris/engine/object/IrisTreeType.java new file mode 100644 index 000000000..031336c4c --- /dev/null +++ b/src/main/java/com/volmit/iris/engine/object/IrisTreeType.java @@ -0,0 +1,75 @@ +package com.volmit.iris.engine.object; + +import org.bukkit.TreeType; + +public enum IrisTreeType { + + /** + * Oak tree (BIG_TREE, TREE) + */ + OAK, + /** + * Spruce tree (MEGA_REDWOOD, REDWOOD, SWAMP, TALL_REDWOOD) + */ + SPRUCE, + /** + * Birch tree (BIRCH, TALL_BIRCH) + */ + BIRCH, + /** + * Jungle tree (JUNGLE, SMALL_JUNGLE) + */ + JUNGLE, + /** + * Big red mushroom; short and fat + */ + RED_MUSHROOM, + /** + * Big brown mushroom; tall and umbrella-like + */ + BROWN_MUSHROOM, + /** + * Acacia tree + */ + ACACIA, + /** + * Dark Oak tree + */ + DARK_OAK, + /** + * Large crimson fungus native to the nether + */ + CRIMSON_FUNGUS, + /** + * Large warped fungus native to the nether + */ + WARPED_FUNGUS, + /** + * Tree with large roots which grows above lush caves + */ + AZALEA, + /** + * The fallback type for all other non-supported growth events + */ + NONE; + + public static IrisTreeType fromTreeType(TreeType type){ + IrisTreeType irisType; + switch(type){ + case BIG_TREE, TREE -> irisType = IrisTreeType.OAK; + case MEGA_REDWOOD, REDWOOD, SWAMP, TALL_REDWOOD -> irisType = IrisTreeType.SPRUCE; + case BIRCH, TALL_BIRCH -> irisType = IrisTreeType.BIRCH; + case JUNGLE, SMALL_JUNGLE -> irisType = IrisTreeType.JUNGLE; + case RED_MUSHROOM -> irisType = IrisTreeType.RED_MUSHROOM; + case BROWN_MUSHROOM -> irisType = IrisTreeType.BROWN_MUSHROOM; + case ACACIA -> irisType = IrisTreeType.ACACIA; + case DARK_OAK -> irisType = IrisTreeType.DARK_OAK; + case CRIMSON_FUNGUS -> irisType = IrisTreeType.CRIMSON_FUNGUS; + case WARPED_FUNGUS -> irisType = IrisTreeType.WARPED_FUNGUS; + case AZALEA -> irisType = IrisTreeType.AZALEA; + //case COCOA_TREE, CHORUS_PLANT, JUNGLE_BUSH -> irisType = IrisSaplingType.NONE; + default -> irisType = IrisTreeType.NONE; + } + return irisType; + } +}