Rework according to discussed changes

This commit is contained in:
CocoTheOwner 2021-07-19 12:07:29 +02:00
parent 5b662df7cd
commit 1534dcc932
9 changed files with 80 additions and 190 deletions

View File

@ -68,153 +68,5 @@ public class TreeManager implements Listener {
IrisBiome biome = worldAccess.getBiome(event.getLocation().getBlockX(), event.getLocation().getBlockY(), event.getLocation().getBlockZ()); 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()); IrisRegion region = worldAccess.getCompound().getDefaultEngine().getRegion(event.getLocation().getBlockX(), event.getLocation().getBlockZ());
Iris.debug("Biome name: " + biome.getName() + " | List of saplings: " + biome.getSaplings().toString());
Iris.debug("Region name: " + region.getName() + " | List of saplings: " + region.getSaplings().toString());
Iris.debug("Dimension saplings: " + settings.getSaplings().toString());
// Get sapling location
KList<Location> saplingLocations = getSaplingPlane(event.getLocation());
int saplingSize = getSaplingSize(saplingLocations);
// Check biome, region and dimension
treeObjects.addAll(getSaplingsFrom(biome.getSaplings(), event.getSpecies(), saplingSize));
// Check region
if (settings.getMode() == IrisTreeModes.ALL || treeObjects.size() == 0) {
treeObjects.addAll(getSaplingsFrom(region.getSaplings(), event.getSpecies(), saplingSize));
}
// Check dimension
if (settings.getMode() == IrisTreeModes.ALL || treeObjects.size() == 0) {
treeObjects.addAll(getSaplingsFrom(settings.getSaplings(), event.getSpecies(), saplingSize));
}
Iris.debug("List of saplings (together): " + treeObjects);
// Check to make sure something was found
if (treeObjects.size() == 0) {
return;
}
// Pick a random object from the list of objects found
String pickedObjectString = treeObjects.get(RNG.r.i(0, treeObjects.size() - 1));
// Cancel vanilla event
event.setCancelled(true);
// Retrieve & place the object
Iris.debug("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)
deleteSaplings(saplingLocations);
// Rotate the object randomly
pickedObject.rotate(new IrisObjectRotation(), 0, 90 * RNG.r.i(0, 3), 0);
// TODO: Consider adding a translation to object placement.
// Place the object
pickedObject.place(event.getLocation());
}
/**
* Deletes all saplings at the
* @param locations sapling locations
*/
private void deleteSaplings(KList<Location> locations) {
locations.forEach(l -> {
Iris.debug("Deleting block of type: " + l.getBlock().getType());
l.getBlock().setType(Material.AIR);
});
}
/**
* 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<String> getSaplingsFrom(KList<IrisTree> container, TreeType tree, int size) {
// Translate TreeType to Iris TreeType
IrisTreeType eventTreeType = IrisTreeType.fromTreeType(tree);
KList<String> 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());
}
}
}
return objects;
}
/**
* Retrieve the `size * size` area of a sapling (any sapling in the area)
* @param saplings The locations of the saplings in the plane
* @return The `x * x` area of saplings
*/
private int getSaplingSize(KList<Location> saplings){
double size = Math.sqrt(saplings.size());
if (size % 1 != 0) {
Iris.error("Size of sapling square array is not a power of an integer (not a square)");
return -1;
}
return (int) size;
}
/**
* Retrieve all saplings in a square area around the current sapling.
* This searches around the current sapling, and the next, etc, iteratively
* Note: This is limited by maxSaplingPlane
* @param location The location to search from (the originating sapling)
* @return A list of saplings in a square
*/
private KList<Location> getSaplingPlane(Location location){
KList<Location> locations = new KList<>();
// TODO: Debug getBlockMap
boolean[][] map = getBlockMap(location);
for (boolean[] row : map) {
Iris.info(Arrays.toString(row));
}
// TODO: Write logic here that checks for the largest possible square with the Location included
// TODO: The boolean[][] map has true's where there's another sapling of the same type, and false if not.
// TODO: Fill the locations array with the found sapling locations and return the array. The rest is hopefully done.
// Note: I tested the system. Placing objects works. Removing saplings may not work perfectly.
return locations;
}
/**
* Get a boolean map which indicates if at positions in all directions the same block can be found
* @param location the center location around which we search
* @return A boolean 2d map of trues and false's
*/
private boolean[][] getBlockMap(Location location) {
Material blockMaterial = location.getBlock().getType();
int size = maxSaplingPlane * 2 - 1;
boolean[][] map = new boolean[size][size];
for (int i = 0; i < size; i++){
boolean[] row = new boolean[size];
for (int j = 0; j < size; j++){
Vector zdir = new Vector(0, 0, 1).multiply(i - maxSaplingPlane + 1);
Vector xdir = new Vector(1, 0, 0).multiply(j - maxSaplingPlane + 1);
Material foundBlock = location.add(xdir).add(zdir).getBlock().getType();
row[j] = blockMaterial.name().equals(foundBlock.name());
}
map[i] = row;
}
return map;
} }
} }

View File

@ -81,12 +81,8 @@ public class IrisBiome extends IrisRegistrant implements IRare {
@ArrayType(min = 1, type = IrisEntityInitialSpawn.class) @ArrayType(min = 1, type = IrisEntityInitialSpawn.class)
private KList<IrisEntityInitialSpawn> entityInitialSpawns = new KList<>(); private KList<IrisEntityInitialSpawn> entityInitialSpawns = new KList<>();
@Desc("Sapling override settings")
@ArrayType(min = 1, type = IrisTree.class)
private KList<IrisTree> saplings = new KList<>();
@ArrayType(min = 1, type = IrisEffect.class) @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.") @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 around the player even if where the effect was played is no longer in the biome the player is in.")
private KList<IrisEffect> effects = new KList<>(); private KList<IrisEffect> effects = new KList<>();

View File

@ -151,6 +151,9 @@ public class IrisObjectPlacement {
@Desc("The loot tables to apply to these objects") @Desc("The loot tables to apply to these objects")
private KList<IrisObjectLoot> loot = new KList<>(); private KList<IrisObjectLoot> loot = new KList<>();
@Desc("Tree growth overrides for these object placements")
private IrisTreeOptions treeOptions = new IrisTreeOptions();
public IrisObjectPlacement toPlacement(String... place) { public IrisObjectPlacement toPlacement(String... place) {
IrisObjectPlacement p = new IrisObjectPlacement(); IrisObjectPlacement p = new IrisObjectPlacement();
p.setPlace(new KList<>(place)); p.setPlace(new KList<>(place));

View File

@ -79,10 +79,6 @@ public class IrisRegion extends IrisRegistrant implements IRare {
@ArrayType(min = 1, type = IrisEntityInitialSpawn.class) @ArrayType(min = 1, type = IrisEntityInitialSpawn.class)
private KList<IrisEntityInitialSpawn> entityInitialSpawns = new KList<>(); private KList<IrisEntityInitialSpawn> entityInitialSpawns = new KList<>();
@Desc("Sapling override settings")
@ArrayType(min = 1, type = IrisTree.class)
private KList<IrisTree> saplings = new KList<>();
@MinNumber(1) @MinNumber(1)
@MaxNumber(128) @MaxNumber(128)
@Desc("The rarity of the region") @Desc("The rarity of the region")

View File

@ -12,23 +12,17 @@ import org.bukkit.TreeType;
@Accessors(chain = true) @Accessors(chain = true)
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@Desc("Tree replace options") @Desc("Tree replace options for this object placer")
@Data @Data
public class IrisTree { public class IrisTree {
@Required @Required
@Desc("The types of trees overwritten") @Desc("The types of trees overwritten by this object")
@ArrayType(min = 1, type = IrisTreeType.class) @ArrayType(min = 1, type = IrisTreeType.class)
private KList<IrisTreeType> treeTypes; private KList<IrisTreeType> treeTypes;
@RegistryListObject
@Required @Required
@ArrayType(min = 1, type = String.class)
@Desc("List of objects to replace trees with")
private KList<String> objects = new KList<>();
@Desc("The size of the square of saplings this applies to (2 means a 2 * 2 sapling area)") @Desc("The size of the square of saplings this applies to (2 means a 2 * 2 sapling area)")
@MinNumber(1) @ArrayType(min = 1, type = IrisTreeSize.class)
@MaxNumber(TreeManager.maxSaplingPlane) private KList<IrisTreeSize> sizes = new KList<>();
private int size = 1;
} }

View File

@ -0,0 +1,22 @@
package com.volmit.iris.engine.object;
import com.volmit.iris.engine.object.annotations.Desc;
import com.volmit.iris.util.collection.KList;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@Desc("Tree replace options for this object placer")
@Data
public class IrisTreeOptions {
@Desc("Toggles this object placer's tree overrides")
private boolean enabled = false;
@Desc("Tree overrides affected by these object placements")
private KList<IrisTree> trees = new KList<>();
}

View File

@ -1,8 +1,6 @@
package com.volmit.iris.engine.object; 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.engine.object.annotations.Desc;
import com.volmit.iris.util.collection.KList;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
@ -22,8 +20,4 @@ public class IrisTreeSettings {
@Desc("Object picking modes") @Desc("Object picking modes")
IrisTreeModes mode = IrisTreeModes.FIRST; IrisTreeModes mode = IrisTreeModes.FIRST;
@Desc("Tree override list")
@ArrayType(min = 1, type = IrisTree.class)
private KList<IrisTree> saplings = new KList<>();
} }

View File

@ -0,0 +1,35 @@
package com.volmit.iris.engine.object;
import com.volmit.iris.engine.object.annotations.Desc;
@Desc("Sapling override object picking options")
public enum IrisTreeSize {
@Desc("Only one sapling")
ONE,
@Desc("Two by two area")
TWO,
@Desc("Three by three area with center")
THREE_CENTER,
@Desc("Three by three any location")
THREE_ANY,
@Desc("Four by four")
FOUR,
@Desc("Five by five center")
FIVE_CENTER,
@Desc("Five by five")
FIVE_ANY;
public static boolean isAnySize(IrisTreeSize treeSize){
return switch (treeSize) {
case THREE_CENTER, FIVE_CENTER -> false;
default -> true;
};
}
}

View File

@ -47,22 +47,20 @@ public enum IrisTreeType {
NONE; NONE;
public static IrisTreeType fromTreeType(TreeType type){ public static IrisTreeType fromTreeType(TreeType type){
IrisTreeType irisType; return switch(type){
switch(type){ case BIG_TREE, TREE -> IrisTreeType.OAK;
case BIG_TREE, TREE -> irisType = IrisTreeType.OAK; case MEGA_REDWOOD, REDWOOD, SWAMP, TALL_REDWOOD -> IrisTreeType.SPRUCE;
case MEGA_REDWOOD, REDWOOD, SWAMP, TALL_REDWOOD -> irisType = IrisTreeType.SPRUCE; case BIRCH, TALL_BIRCH -> IrisTreeType.BIRCH;
case BIRCH, TALL_BIRCH -> irisType = IrisTreeType.BIRCH; case JUNGLE, SMALL_JUNGLE -> IrisTreeType.JUNGLE;
case JUNGLE, SMALL_JUNGLE -> irisType = IrisTreeType.JUNGLE; case RED_MUSHROOM -> IrisTreeType.RED_MUSHROOM;
case RED_MUSHROOM -> irisType = IrisTreeType.RED_MUSHROOM; case BROWN_MUSHROOM -> IrisTreeType.BROWN_MUSHROOM;
case BROWN_MUSHROOM -> irisType = IrisTreeType.BROWN_MUSHROOM; case ACACIA -> IrisTreeType.ACACIA;
case ACACIA -> irisType = IrisTreeType.ACACIA; case DARK_OAK -> IrisTreeType.DARK_OAK;
case DARK_OAK -> irisType = IrisTreeType.DARK_OAK; case CRIMSON_FUNGUS -> IrisTreeType.CRIMSON_FUNGUS;
case CRIMSON_FUNGUS -> irisType = IrisTreeType.CRIMSON_FUNGUS; case WARPED_FUNGUS -> IrisTreeType.WARPED_FUNGUS;
case WARPED_FUNGUS -> irisType = IrisTreeType.WARPED_FUNGUS; case AZALEA -> IrisTreeType.AZALEA;
case AZALEA -> irisType = IrisTreeType.AZALEA; //case COCOA_TREE, CHORUS_PLANT, JUNGLE_BUSH -> IrisSaplingType.NONE;
//case COCOA_TREE, CHORUS_PLANT, JUNGLE_BUSH -> irisType = IrisSaplingType.NONE; default -> IrisTreeType.NONE;
default -> irisType = IrisTreeType.NONE; };
}
return irisType;
} }
} }