Reformat all code

Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
This commit is contained in:
solonovamax
2021-08-30 17:34:44 -04:00
parent 1655381413
commit c445a0434d
753 changed files with 10461 additions and 8783 deletions
@@ -1,17 +1,18 @@
package com.dfsek.terra.addons.structure;
import java.util.Set;
import com.dfsek.terra.api.properties.Properties;
import com.dfsek.terra.api.structure.configured.ConfiguredStructure;
import java.util.Set;
public class BiomeStructures implements Properties {
private final Set<ConfiguredStructure> structures;
public BiomeStructures(Set<ConfiguredStructure> structures) {
this.structures = structures;
}
public Set<ConfiguredStructure> getStructures() {
return structures;
}
@@ -3,17 +3,19 @@ package com.dfsek.terra.addons.structure;
import com.dfsek.tectonic.annotations.Default;
import com.dfsek.tectonic.annotations.Value;
import com.dfsek.tectonic.loading.object.ObjectTemplate;
import com.dfsek.terra.api.config.meta.Meta;
import com.dfsek.terra.api.structure.configured.ConfiguredStructure;
import java.util.Collections;
import java.util.Set;
import com.dfsek.terra.api.config.meta.Meta;
import com.dfsek.terra.api.structure.configured.ConfiguredStructure;
public class BiomeStructuresTemplate implements ObjectTemplate<BiomeStructures> {
@Value("structures")
@Default
private @Meta Set<@Meta ConfiguredStructure> structures = Collections.emptySet();
private final @Meta Set<@Meta ConfiguredStructure> structures = Collections.emptySet();
@Override
public BiomeStructures get() {
return new BiomeStructures(structures);
@@ -10,19 +10,20 @@ import com.dfsek.terra.api.event.functional.FunctionalEventHandler;
import com.dfsek.terra.api.injection.annotations.Inject;
import com.dfsek.terra.api.structure.configured.ConfiguredStructure;
@Addon("config-structure")
@Version("1.0.0")
@Author("Terra")
public class StructureAddon extends TerraAddon {
@Inject
private TerraPlugin main;
@Override
public void initialize() {
main.getEventManager()
.getHandler(FunctionalEventHandler.class)
.register(this, ConfigPackPreLoadEvent.class)
.then(event -> event.getPack().applyLoader(ConfiguredStructure.class, (t, o, l) -> null))
.failThrough();
.getHandler(FunctionalEventHandler.class)
.register(this, ConfigPackPreLoadEvent.class)
.then(event -> event.getPack().applyLoader(ConfiguredStructure.class, (t, o, l) -> null))
.failThrough();
}
}
@@ -4,6 +4,7 @@ import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.config.ConfigFactory;
import com.dfsek.terra.api.structure.configured.ConfiguredStructure;
public class StructureFactory implements ConfigFactory<StructureTemplate, ConfiguredStructure> {
@Override
public ConfiguredStructure build(StructureTemplate config, TerraPlugin main) {
@@ -1,5 +1,10 @@
package com.dfsek.terra.addons.structure;
import net.jafama.FastMath;
import org.jetbrains.annotations.NotNull;
import java.util.Random;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.config.WorldConfig;
import com.dfsek.terra.api.profiler.ProfileFrame;
@@ -12,36 +17,35 @@ import com.dfsek.terra.api.world.World;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
import com.dfsek.terra.api.world.generator.Chunkified;
import com.dfsek.terra.api.world.generator.GenerationStage;
import net.jafama.FastMath;
import org.jetbrains.annotations.NotNull;
import java.util.Random;
public class StructurePopulator implements GenerationStage, Chunkified {
private final TerraPlugin main;
public StructurePopulator(TerraPlugin main) {
this.main = main;
}
@SuppressWarnings("try")
@Override
public void populate(@NotNull World world, @NotNull Chunk chunk) {
try(ProfileFrame ignore = main.getProfiler().profile("structure")) {
if(world.getConfig().disableStructures()) return;
int cx = (chunk.getX() << 4);
int cz = (chunk.getZ() << 4);
BiomeProvider provider = world.getBiomeProvider();
WorldConfig config = world.getConfig();
for(ConfiguredStructure conf : config.getRegistry(TerraStructure.class).entries()) {
Vector3 spawn = conf.getSpawn().getNearestSpawn(cx + 8, cz + 8, world.getSeed());
if(!provider.getBiome(spawn, world.getSeed()).getContext().get(BiomeStructures.class).getStructures().contains(conf)) {
continue;
}
Random random = new Random(PopulationUtil.getCarverChunkSeed(FastMath.floorDiv(spawn.getBlockX(), 16), FastMath.floorDiv(spawn.getBlockZ(), 16), world.getSeed()));
conf.getStructure().get(random).generate(spawn.setY(conf.getSpawnStart().get(random)), world, chunk, random, Rotation.fromDegrees(90 * random.nextInt(4)));
Random random = new Random(PopulationUtil.getCarverChunkSeed(FastMath.floorDiv(spawn.getBlockX(), 16),
FastMath.floorDiv(spawn.getBlockZ(), 16), world.getSeed()));
conf.getStructure().get(random).generate(spawn.setY(conf.getSpawnStart().get(random)), world, chunk, random,
Rotation.fromDegrees(90 * random.nextInt(4)));
}
}
}
@@ -2,7 +2,7 @@ package com.dfsek.terra.addons.structure;
import com.dfsek.tectonic.annotations.Final;
import com.dfsek.tectonic.annotations.Value;
import com.dfsek.tectonic.config.ConfigTemplate;
import com.dfsek.terra.api.config.AbstractableTemplate;
import com.dfsek.terra.api.config.meta.Meta;
import com.dfsek.terra.api.structure.Structure;
@@ -10,33 +10,34 @@ import com.dfsek.terra.api.structure.StructureSpawn;
import com.dfsek.terra.api.util.Range;
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
@SuppressWarnings({"unused", "FieldMayBeFinal"})
public class StructureTemplate implements AbstractableTemplate, ConfigTemplate {
@SuppressWarnings({ "unused", "FieldMayBeFinal" })
public class StructureTemplate implements AbstractableTemplate {
@Value("id")
@Final
private String id;
@Value("scripts")
private @Meta ProbabilityCollection<@Meta Structure> structure;
@Value("spawn.start")
private @Meta Range y;
@Value("spawn")
private @Meta StructureSpawn spawn;
public String getID() {
return id;
}
public ProbabilityCollection<Structure> getStructures() {
return structure;
}
public Range getY() {
return y;
}
public StructureSpawn getSpawn() {
return spawn;
}
@@ -1,37 +1,38 @@
package com.dfsek.terra.addons.structure;
import com.dfsek.terra.api.structure.configured.ConfiguredStructure;
import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.structure.StructureSpawn;
import com.dfsek.terra.api.structure.configured.ConfiguredStructure;
import com.dfsek.terra.api.util.Range;
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
public class TerraStructure implements ConfiguredStructure {
private final ProbabilityCollection<Structure> structure;
private final Range spawnStart;
private final StructureSpawn spawn;
public TerraStructure(ProbabilityCollection<Structure> structures, Range spawnStart, StructureSpawn spawn) {
this.structure = structures;
this.spawnStart = spawnStart;
this.spawn = spawn;
}
@Override
public ProbabilityCollection<Structure> getStructure() {
return structure;
}
@Override
public Range getSpawnStart() {
return spawnStart;
}
@Override
public StructureSpawn getSpawn() {
return spawn;
}
@Override
public String getID() {
return null;
@@ -1,13 +1,15 @@
package com.dfsek.terra.addons.structure.command;
import org.jetbrains.annotations.NotNull;
import java.util.function.Consumer;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.structure.configured.ConfiguredStructure;
import com.dfsek.terra.api.vector.Vector3;
import com.dfsek.terra.api.world.World;
import com.dfsek.terra.api.world.biome.generation.BiomeProvider;
import org.jetbrains.annotations.NotNull;
import java.util.function.Consumer;
public class AsyncStructureFinder implements Runnable {
protected final BiomeProvider provider;
@@ -20,8 +22,9 @@ public class AsyncStructureFinder implements Runnable {
protected final TerraPlugin main;
private final Consumer<Vector3> callback;
protected int searchSize = 1;
public AsyncStructureFinder(BiomeProvider provider, ConfiguredStructure target, @NotNull Vector3 origin, World world, int startRadius, int maxRadius, Consumer<Vector3> callback, TerraPlugin main) {
public AsyncStructureFinder(BiomeProvider provider, ConfiguredStructure target, @NotNull Vector3 origin, World world, int startRadius,
int maxRadius, Consumer<Vector3> callback, TerraPlugin main) {
//setSearchSize(target.getSpawn().getWidth() + 2 * target.getSpawn().getSeparation());
this.provider = provider;
this.target = target;
@@ -33,31 +36,23 @@ public class AsyncStructureFinder implements Runnable {
this.world = world;
this.callback = callback;
}
public Vector3 finalizeVector(Vector3 orig) {
return orig;//target.getSpawn().getChunkSpawn(orig.getBlockX(), orig.getBlockZ(), world.getSeed());
}
public boolean isValid(int x, int z, ConfiguredStructure target) {
//Vector3 spawn = target.getSpawn().getChunkSpawn(x, z, world.getSeed());
//if(!((UserDefinedBiome) provider.getBiome(spawn)).getConfig().getStructures().contains(target)) return false;
//Random random = new Random(PopulationUtil.getCarverChunkSeed(FastMath.floorDiv(spawn.getBlockX(), 16), FastMath.floorDiv(spawn.getBlockZ(), 16), world.getSeed()));
//return target.getStructure().get(random).test(spawn.setY(target.getSpawnStart().get(random)), world, random, Rotation.fromDegrees(90 * random.nextInt(4)));
return false;
}
@Override
public void run() {
int x = centerX;
int z = centerZ;
x /= searchSize;
z /= searchSize;
int run = 1;
boolean toggle = true;
boolean found = false;
main:
for(int i = startRadius; i < maxRadius; i++) {
for(int j = 0; j < run; j++) {
@@ -82,23 +77,33 @@ public class AsyncStructureFinder implements Runnable {
Vector3 finalSpawn = found ? finalizeVector(new Vector3(x, 0, z)) : null;
callback.accept(finalSpawn);
}
public boolean isValid(int x, int z, ConfiguredStructure target) {
//Vector3 spawn = target.getSpawn().getChunkSpawn(x, z, world.getSeed());
//if(!((UserDefinedBiome) provider.getBiome(spawn)).getConfig().getStructures().contains(target)) return false;
//Random random = new Random(PopulationUtil.getCarverChunkSeed(FastMath.floorDiv(spawn.getBlockX(), 16), FastMath.floorDiv(spawn
// .getBlockZ(), 16), world.getSeed()));
//return target.getStructure().get(random).test(spawn.setY(target.getSpawnStart().get(random)), world, random, Rotation
// .fromDegrees(90 * random.nextInt(4)));
return false;
}
public ConfiguredStructure getTarget() {
return target;
}
public World getWorld() {
return world;
}
public BiomeProvider getProvider() {
return provider;
}
public int getSearchSize() {
return searchSize;
}
public void setSearchSize(int searchSize) {
this.searchSize = searchSize;
}
@@ -5,6 +5,7 @@ import com.dfsek.terra.api.command.annotation.Command;
import com.dfsek.terra.api.command.annotation.Subcommand;
import com.dfsek.terra.api.entity.CommandSender;
@Command(
subcommands = {
@Subcommand(
@@ -1,5 +1,10 @@
package com.dfsek.terra.addons.structure.command.structure;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.block.entity.BlockEntity;
import com.dfsek.terra.api.block.entity.Sign;
@@ -17,44 +22,33 @@ import com.dfsek.terra.api.injection.annotations.Inject;
import com.dfsek.terra.api.util.generic.pair.Pair;
import com.dfsek.terra.api.vector.Vector3;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
@PlayerCommand
@WorldCommand
@DebugCommand
@Command(
arguments = {
@Argument(
value = "id"
)
},
usage = "/terra structure export <ID>"
)
@Command(arguments = @Argument("id"), usage = "/terra structure export <ID>")
public class StructureExportCommand implements CommandTemplate {
@Inject
private TerraPlugin main;
@ArgumentTarget("id")
private String id;
@Override
public void execute(CommandSender sender) {
Player player = (Player) sender;
Pair<Vector3, Vector3> l = main.getWorldHandle().getSelectedLocation(player);
Vector3 l1 = l.getLeft();
Vector3 l2 = l.getRight();
StringBuilder scriptBuilder = new StringBuilder("id \"" + id + "\";\nnum y = 0;\n");
int centerX = 0;
int centerY = 0;
int centerZ = 0;
for(int x = l1.getBlockX(); x <= l2.getBlockX(); x++) {
for(int y = l1.getBlockY(); y <= l2.getBlockY(); y++) {
for(int z = l1.getBlockZ(); z <= l2.getBlockZ(); z++) {
@@ -70,11 +64,11 @@ public class StructureExportCommand implements CommandTemplate {
}
}
}
for(int x = l1.getBlockX(); x <= l2.getBlockX(); x++) {
for(int y = l1.getBlockY(); y <= l2.getBlockY(); y++) {
for(int z = l1.getBlockZ(); z <= l2.getBlockZ(); z++) {
BlockState data = player.world().getBlockData(x, y, z);
if(data.isStructureVoid()) continue;
BlockEntity state = player.world().getBlockState(x, y, z);
@@ -85,14 +79,15 @@ public class StructureExportCommand implements CommandTemplate {
}
}
if(!data.isStructureVoid()) {
scriptBuilder.append("block(").append(x - l1.getBlockX() - centerX).append(", y + ").append(y - l1.getBlockY() - centerY).append(", ").append(z - l1.getBlockZ() - centerZ).append(", ")
.append("\"");
scriptBuilder.append("block(").append(x - l1.getBlockX() - centerX).append(", y + ").append(
y - l1.getBlockY() - centerY).append(", ").append(z - l1.getBlockZ() - centerZ).append(", ")
.append("\"");
scriptBuilder.append(data.getAsString()).append("\");\n");
}
}
}
}
File file = new File(main.getDataFolder() + File.separator + "export" + File.separator + "structures", id + ".tesf");
try {
file.getParentFile().mkdirs();
@@ -105,7 +100,7 @@ public class StructureExportCommand implements CommandTemplate {
} catch(IOException e) {
e.printStackTrace();
}
sender.sendMessage("Exported structure to " + file.getAbsolutePath());
}
}
@@ -1,5 +1,8 @@
package com.dfsek.terra.addons.structure.command.structure;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import com.dfsek.terra.addons.structure.command.structure.argument.ScriptArgumentParser;
import com.dfsek.terra.addons.structure.command.structure.completer.RotationCompleter;
import com.dfsek.terra.addons.structure.command.structure.completer.ScriptCompleter;
@@ -20,51 +23,43 @@ import com.dfsek.terra.api.injection.annotations.Inject;
import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.structure.rotation.Rotation;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
@PlayerCommand
@DebugCommand
@WorldCommand
@Command(
arguments = {
@Argument(
value = "structure",
tabCompleter = ScriptCompleter.class,
argumentParser = ScriptArgumentParser.class
),
@Argument(
value = "rotation",
required = false,
tabCompleter = RotationCompleter.class,
argumentParser = IntegerArgumentParser.class,
defaultValue = "0"
)
},
switches = {
@Switch(value = "chunk",
aliases = "c"
)
},
usage = "/terra structure load [ROTATION] [-c]"
)
@Command(arguments = {
@Argument(
value = "structure",
tabCompleter = ScriptCompleter.class,
argumentParser = ScriptArgumentParser.class
),
@Argument(
value = "rotation",
required = false,
tabCompleter = RotationCompleter.class,
argumentParser = IntegerArgumentParser.class,
defaultValue = "0"
)
}, switches = @Switch(value = "chunk",
aliases = "c"
), usage = "/terra structure load [ROTATION] [-c]")
public class StructureLoadCommand implements CommandTemplate {
@ArgumentTarget("rotation")
private Integer rotation = 0;
private final Integer rotation = 0;
@SwitchTarget("chunk")
private boolean chunk;
@ArgumentTarget("structure")
private Structure script;
@Inject
private TerraPlugin main;
@Override
public void execute(CommandSender sender) {
Player player = (Player) sender;
long t = System.nanoTime();
Random random = new Random(ThreadLocalRandom.current().nextLong());
Rotation r;
@@ -84,7 +79,7 @@ public class StructureLoadCommand implements CommandTemplate {
script.generate(player.position(), player.world(), random, r);
}
long l = System.nanoTime() - t;
sender.sendMessage("Took " + ((double) l) / 1000000 + "ms");
}
}
@@ -1,5 +1,7 @@
package com.dfsek.terra.addons.structure.command.structure;
import java.util.Locale;
import com.dfsek.terra.addons.structure.command.AsyncStructureFinder;
import com.dfsek.terra.addons.structure.command.structure.argument.StructureArgumentParser;
import com.dfsek.terra.addons.structure.command.structure.completer.StructureCompleter;
@@ -19,53 +21,53 @@ import com.dfsek.terra.api.injection.annotations.Inject;
import com.dfsek.terra.api.structure.configured.ConfiguredStructure;
import com.dfsek.terra.api.vector.Vector3;
import java.util.Locale;
@PlayerCommand
@WorldCommand
@Command(
arguments = {
@Argument(
value = "structure",
tabCompleter = StructureCompleter.class,
argumentParser = StructureArgumentParser.class
),
@Argument(
value = "radius",
required = false,
defaultValue = "100",
argumentParser = IntegerArgumentParser.class
)
},
switches = {
@Switch(
value = "teleport",
aliases = {"t", "tp"}
)
}
)
@Command(arguments = {
@Argument(
value = "structure",
tabCompleter = StructureCompleter.class,
argumentParser = StructureArgumentParser.class
),
@Argument(
value = "radius",
required = false,
defaultValue = "100",
argumentParser = IntegerArgumentParser.class
)
}, switches = @Switch(
value = "teleport",
aliases = { "t", "tp" }
))
public class StructureLocateCommand implements CommandTemplate {
@Inject
private TerraPlugin main;
@ArgumentTarget("structure")
private ConfiguredStructure structure;
@ArgumentTarget("radius")
private Integer radius;
@SwitchTarget("teleport")
private boolean teleport;
@Override
public void execute(CommandSender sender) {
Player player = (Player) sender;
new Thread(new AsyncStructureFinder(player.world().getBiomeProvider(), structure, player.position().clone().multiply((1D / main.getTerraConfig().getBiomeSearchResolution())), player.world(), 0, radius, location -> {
new Thread(new AsyncStructureFinder(player.world().getBiomeProvider(), structure,
player.position().clone().multiply((1D / main.getTerraConfig().getBiomeSearchResolution())),
player.world(), 0, radius, location -> {
if(location != null) {
sender.sendMessage(String.format("The nearest %s is at [%d, ~, %d] (%.1f blocks away)", structure.getID().toLowerCase(Locale.ROOT), location.getBlockX(), location.getBlockZ(), location.add(new Vector3(0, player.position().getY(), 0)).distance(player.position())));
sender.sendMessage(
String.format("The nearest %s is at [%d, ~, %d] (%.1f blocks away)", structure.getID().toLowerCase(Locale.ROOT),
location.getBlockX(), location.getBlockZ(),
location.add(new Vector3(0, player.position().getY(), 0)).distance(player.position())));
if(teleport) {
main.runPossiblyUnsafeTask(() -> player.position(new Vector3(location.getX(), player.position().getY(), location.getZ())));
main.runPossiblyUnsafeTask(
() -> player.position(new Vector3(location.getX(), player.position().getY(), location.getZ())));
}
} //else LangUtil.send("command.biome.unable-to-locate", sender);
}, main), "Biome Location Thread").start();
@@ -7,10 +7,11 @@ import com.dfsek.terra.api.entity.Player;
import com.dfsek.terra.api.injection.annotations.Inject;
import com.dfsek.terra.api.structure.Structure;
public class ScriptArgumentParser implements ArgumentParser<Structure> {
@Inject
private TerraPlugin main;
@Override
public Structure parse(CommandSender sender, String arg) {
return ((Player) sender).world().getConfig().getRegistry(Structure.class).get(arg);
@@ -7,10 +7,11 @@ import com.dfsek.terra.api.entity.Player;
import com.dfsek.terra.api.injection.annotations.Inject;
import com.dfsek.terra.api.structure.configured.ConfiguredStructure;
public class StructureArgumentParser implements ArgumentParser<ConfiguredStructure> {
@Inject
private TerraPlugin main;
@Override
public ConfiguredStructure parse(CommandSender sender, String arg) {
return ((Player) sender).world().getConfig().getRegistry(ConfiguredStructure.class).get(arg);
@@ -1,10 +1,11 @@
package com.dfsek.terra.addons.structure.command.structure.completer;
import java.util.Arrays;
import java.util.List;
import com.dfsek.terra.api.command.tab.TabCompleter;
import com.dfsek.terra.api.entity.CommandSender;
import java.util.Arrays;
import java.util.List;
public class RotationCompleter implements TabCompleter {
@Override
@@ -1,5 +1,8 @@
package com.dfsek.terra.addons.structure.command.structure.completer;
import java.util.List;
import java.util.stream.Collectors;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.command.tab.TabCompleter;
import com.dfsek.terra.api.entity.CommandSender;
@@ -7,15 +10,14 @@ import com.dfsek.terra.api.entity.Player;
import com.dfsek.terra.api.injection.annotations.Inject;
import com.dfsek.terra.api.structure.Structure;
import java.util.List;
import java.util.stream.Collectors;
public class ScriptCompleter implements TabCompleter {
@Inject
private TerraPlugin main;
@Override
public List<String> complete(CommandSender sender) {
return ((Player) sender).world().getConfig().getRegistry(Structure.class).entries().stream().map(Structure::getID).collect(Collectors.toList());
return ((Player) sender).world().getConfig().getRegistry(Structure.class).entries().stream().map(Structure::getID).collect(
Collectors.toList());
}
}
@@ -1,5 +1,8 @@
package com.dfsek.terra.addons.structure.command.structure.completer;
import java.util.ArrayList;
import java.util.List;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.command.tab.TabCompleter;
import com.dfsek.terra.api.entity.CommandSender;
@@ -7,13 +10,11 @@ import com.dfsek.terra.api.entity.Player;
import com.dfsek.terra.api.injection.annotations.Inject;
import com.dfsek.terra.api.structure.configured.ConfiguredStructure;
import java.util.ArrayList;
import java.util.List;
public class StructureCompleter implements TabCompleter {
@Inject
private TerraPlugin main;
@Override
public List<String> complete(CommandSender sender) {
Player player = (Player) sender;
@@ -1,12 +1,5 @@
package com.dfsek.terra.addons.structure.structures.loot;
import com.dfsek.terra.addons.structure.structures.loot.functions.AmountFunction;
import com.dfsek.terra.addons.structure.structures.loot.functions.DamageFunction;
import com.dfsek.terra.addons.structure.structures.loot.functions.EnchantFunction;
import com.dfsek.terra.addons.structure.structures.loot.functions.LootFunction;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.inventory.Item;
import com.dfsek.terra.api.inventory.ItemStack;
import net.jafama.FastMath;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@@ -15,6 +8,15 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import com.dfsek.terra.addons.structure.structures.loot.functions.AmountFunction;
import com.dfsek.terra.addons.structure.structures.loot.functions.DamageFunction;
import com.dfsek.terra.addons.structure.structures.loot.functions.EnchantFunction;
import com.dfsek.terra.addons.structure.structures.loot.functions.LootFunction;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.inventory.Item;
import com.dfsek.terra.api.inventory.ItemStack;
/**
* Representation of a single item entry within a Loot Table pool.
*/
@@ -22,7 +24,7 @@ public class Entry {
private final Item item;
private final long weight;
private final List<LootFunction> functions = new ArrayList<>();
/**
* Instantiates an Entry from a JSON representation.
*
@@ -31,14 +33,14 @@ public class Entry {
public Entry(JSONObject entry, TerraPlugin main) {
String id = entry.get("name").toString();
this.item = main.getItemHandle().createItem(id);
long weight1;
try {
weight1 = (long) entry.get("weight");
} catch(NullPointerException e) {
weight1 = 1;
}
this.weight = weight1;
if(entry.containsKey("functions")) {
for(Object function : (JSONArray) entry.get("functions")) {
@@ -69,17 +71,19 @@ public class Entry {
JSONArray disabled = null;
if(((JSONObject) function).containsKey("disabled_enchants"))
disabled = (JSONArray) ((JSONObject) function).get("disabled_enchants");
functions.add(new EnchantFunction(FastMath.toIntExact(minEnchant), FastMath.toIntExact(maxEnchant), disabled, main));
functions.add(
new EnchantFunction(FastMath.toIntExact(minEnchant), FastMath.toIntExact(maxEnchant), disabled, main));
break;
}
}
}
}
/**
* Fetches a single ItemStack from the Entry, applying all functions to it.
*
* @param r The Random instance to apply functions with
*
* @return ItemStack - The ItemStack with all functions applied.
*/
public ItemStack getItem(Random r) {
@@ -89,7 +93,7 @@ public class Entry {
}
return item;
}
/**
* Gets the weight attribute of the Entry.
*
@@ -1,8 +1,5 @@
package com.dfsek.terra.addons.structure.structures.loot;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.inventory.Inventory;
import com.dfsek.terra.api.inventory.ItemStack;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
@@ -12,16 +9,22 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.inventory.Inventory;
import com.dfsek.terra.api.inventory.ItemStack;
/**
* Class representation of a Loot Table to populate chest loot.
*/
public class LootTableImpl implements com.dfsek.terra.api.structure.LootTable {
private final List<Pool> pools = new ArrayList<>();
/**
* Instantiates a LootTable from a JSON String.
*
* @param json The JSON String representing the loot table.
*
* @throws ParseException if malformed JSON is passed.
*/
public LootTableImpl(String json, TerraPlugin main) throws ParseException {
@@ -32,16 +35,7 @@ public class LootTableImpl implements com.dfsek.terra.api.structure.LootTable {
pools.add(new Pool((JSONObject) pool, main));
}
}
@Override
public List<ItemStack> getLoot(Random r) {
List<ItemStack> itemList = new ArrayList<>();
for(Pool pool : pools) {
itemList.addAll(pool.getItems(r));
}
return itemList;
}
@Override
public void fillInventory(Inventory i, Random r) {
List<ItemStack> loot = getLoot(r);
@@ -67,4 +61,13 @@ public class LootTableImpl implements com.dfsek.terra.api.structure.LootTable {
}
}
}
@Override
public List<ItemStack> getLoot(Random r) {
List<ItemStack> itemList = new ArrayList<>();
for(Pool pool : pools) {
itemList.addAll(pool.getItems(r));
}
return itemList;
}
}
@@ -1,8 +1,5 @@
package com.dfsek.terra.addons.structure.structures.loot;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.inventory.ItemStack;
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
import net.jafama.FastMath;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@@ -11,6 +8,11 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.inventory.ItemStack;
import com.dfsek.terra.api.util.collection.ProbabilityCollection;
/**
* Representation of a Loot Table pool, or a set of items to be fetched independently.
*/
@@ -18,7 +20,7 @@ public class Pool {
private final int max;
private final int min;
private final ProbabilityCollection<Entry> entries;
/**
* Instantiates a Pool from a JSON representation.
*
@@ -34,21 +36,22 @@ public class Pool {
max = FastMath.toIntExact((Long) ((JSONObject) amount).get("max"));
min = FastMath.toIntExact((Long) ((JSONObject) amount).get("min"));
}
for(Object entryJSON : (JSONArray) pool.get("entries")) {
Entry entry = new Entry((JSONObject) entryJSON, main);
entries.add(entry, FastMath.toIntExact(entry.getWeight()));
}
}
/**
* Fetches a list of items from the pool using the provided Random instance.
*
* @param r The Random instance to use.
*
* @return List&lt;ItemStack&gt; - The list of items fetched.
*/
public List<ItemStack> getItems(Random r) {
int rolls = r.nextInt(max - min + 1) + min;
List<ItemStack> items = new ArrayList<>();
for(int i = 0; i < rolls; i++) {
@@ -1,9 +1,10 @@
package com.dfsek.terra.addons.structure.structures.loot.functions;
import java.util.Random;
import com.dfsek.terra.api.inventory.ItemStack;
import java.util.Random;
/**
* Loot LootFunction fot setting the amount of an item.
@@ -11,7 +12,7 @@ import java.util.Random;
public class AmountFunction implements LootFunction {
private final int max;
private final int min;
/**
* Instantiates an AmountFunction.
*
@@ -22,12 +23,13 @@ public class AmountFunction implements LootFunction {
this.min = min;
this.max = max;
}
/**
* Applies the function to an ItemStack.
*
* @param original The ItemStack on which to apply the function.
* @param r The Random instance to use.
*
* @return - ItemStack - The mutated ItemStack.
*/
@Override
@@ -1,10 +1,11 @@
package com.dfsek.terra.addons.structure.structures.loot.functions;
import java.util.Random;
import com.dfsek.terra.api.inventory.ItemStack;
import com.dfsek.terra.api.inventory.item.Damageable;
import com.dfsek.terra.api.inventory.item.ItemMeta;
import java.util.Random;
/**
* Loot LootFunction for setting the damage on items in Loot Tables
@@ -12,7 +13,7 @@ import java.util.Random;
public class DamageFunction implements LootFunction {
private final int max;
private final int min;
/**
* Instantiates a DamageFunction.
*
@@ -23,12 +24,13 @@ public class DamageFunction implements LootFunction {
this.min = min;
this.max = max;
}
/**
* Applies the function to an ItemStack.
*
* @param original The ItemStack on which to apply the function.
* @param r The Random instance to use.
*
* @return - ItemStack - The mutated ItemStack.
*/
@Override
@@ -1,9 +1,5 @@
package com.dfsek.terra.addons.structure.structures.loot.functions;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.inventory.ItemStack;
import com.dfsek.terra.api.inventory.item.Enchantment;
import com.dfsek.terra.api.inventory.item.ItemMeta;
import net.jafama.FastMath;
import org.json.simple.JSONArray;
@@ -12,31 +8,38 @@ import java.util.Collections;
import java.util.List;
import java.util.Random;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.inventory.ItemStack;
import com.dfsek.terra.api.inventory.item.Enchantment;
import com.dfsek.terra.api.inventory.item.ItemMeta;
public class EnchantFunction implements LootFunction {
private final int min;
private final int max;
private final JSONArray disabled;
private final TerraPlugin main;
public EnchantFunction(int min, int max, JSONArray disabled, TerraPlugin main) {
this.max = max;
this.min = min;
this.disabled = disabled;
this.main = main;
}
/**
* Applies the function to an ItemStack.
*
* @param original The ItemStack on which to apply the function.
* @param r The Random instance to use.
*
* @return - ItemStack - The mutated ItemStack.
*/
@Override
public ItemStack apply(ItemStack original, Random r) {
if(original.getItemMeta() == null) return original;
double enchant = (r.nextDouble() * (max - min)) + min;
List<Enchantment> possible = new ArrayList<>();
for(Enchantment ench : main.getItemHandle().getEnchantments()) {
@@ -57,7 +60,9 @@ public class EnchantFunction implements LootFunction {
try {
meta.addEnchantment(chosen, FastMath.max(lvl, 1));
} catch(IllegalArgumentException e) {
main.logger().warning("Attempted to enchant " + original.getType() + " with " + chosen + " at level " + FastMath.max(lvl, 1) + ", but an unexpected exception occurred! Usually this is caused by a misbehaving enchantment plugin.");
main.logger().warning(
"Attempted to enchant " + original.getType() + " with " + chosen + " at level " + FastMath.max(lvl, 1) +
", but an unexpected exception occurred! Usually this is caused by a misbehaving enchantment plugin.");
}
}
original.setItemMeta(meta);
@@ -1,9 +1,10 @@
package com.dfsek.terra.addons.structure.structures.loot.functions;
import java.util.Random;
import com.dfsek.terra.api.inventory.ItemStack;
import java.util.Random;
/**
* Interface for mutating items in Loot Tables.
@@ -14,6 +15,7 @@ public interface LootFunction {
*
* @param original The ItemStack on which to apply the function.
* @param r The Random instance to use.
*
* @return - ItemStack - The mutated ItemStack.
*/
ItemStack apply(ItemStack original, Random r);