Skip to content

Commit

Permalink
Minor refactors. Using final, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jul 5, 2023
1 parent beea5e8 commit 3f94cea
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public int getScore(String key) {
/**
* Get the score for this advancement
* @param a - advancement
* @return score of advancement, or 0 if cannot be worked out
* @return score of advancement, or 0 if it cannot be worked out
*/
public int getScore(Advancement a) {
String adv = "advancements." + a.getKey().getKey();
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/world/bentobox/boxed/Boxed.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,8 @@ public class Boxed extends GameModeAddon {
private final Config<Settings> configObject = new Config<>(this, Settings.class);
private AdvancementsManager advManager;
private AbstractBoxedChunkGenerator netherChunkGenerator;
private World baseWorld;
private World baseWorldNether;
private World seedWorld;
private World seedWorldNether;
private Map<World, ChunkGenerator> generatorMap = new HashMap<>();
private final Map<World, ChunkGenerator> generatorMap = new HashMap<>();
//private World seedWorldEnd;
private BiomeProvider boxedBiomeProvider;

Expand Down Expand Up @@ -185,8 +182,8 @@ private void createNether(String worldName) {
log("Creating Boxed Seed Nether world ...");
// This creates a vanilla base world with biomes
AbstractBoxedChunkGenerator seedBaseGen = new BoxedSeedChunkGenerator(this, Environment.NETHER);
baseWorldNether = WorldCreator
.name(worldName + "/" + SEED+NETHER+BASE)
World baseWorldNether = WorldCreator
.name(worldName + "/" + SEED + NETHER + BASE)
.generator(seedBaseGen)
.environment(Environment.NETHER)
.seed(getSettings().getSeed())
Expand All @@ -200,8 +197,8 @@ private void createNether(String worldName) {
// This copies a base world with custom biomes
log("Creating Boxed Biomed Nether world ...");
BoxedSeedChunkGenerator seedWorldNetherGenerator = new BoxedSeedChunkGenerator(this, Environment.NETHER, new NetherSeedBiomeGenerator(this, seedBaseGen));
seedWorldNether = WorldCreator
.name(worldName + "/" + SEED+NETHER)
World seedWorldNether = WorldCreator
.name(worldName + "/" + SEED + NETHER)
.generator(seedWorldNetherGenerator)
.environment(Environment.NETHER)
.seed(getSettings().getSeed())
Expand All @@ -224,8 +221,8 @@ private void createOverWorld(String worldName) {
log("Creating Boxed Seed world ...");
// This creates a vanilla base world with biomes
AbstractBoxedChunkGenerator seedBaseGen = new BoxedSeedChunkGenerator(this, Environment.NORMAL);
baseWorld = WorldCreator
.name(worldName + "/" + SEED+BASE)
World baseWorld = WorldCreator
.name(worldName + "/" + SEED + BASE)
.generator(seedBaseGen)
.environment(Environment.NORMAL)
.seed(getSettings().getSeed())
Expand Down Expand Up @@ -268,7 +265,6 @@ private void createOverWorld(String worldName) {
* Registers a world with world management plugins
*
* @param world the World to register
* @param islandWorld true if this is an island world
*/
private void registerToWorldManagementPlugins(@NonNull World world) {
if (getPlugin().getHooks() != null) {
Expand All @@ -285,7 +281,7 @@ private void registerToWorldManagementPlugins(@NonNull World world) {
}

/**
* Copies chunks from the seed world so they can be pasted in the game world
* Copies chunks from the seed world, so they can be pasted in the game world
* @param world - source world
* @param gen - generator to store the chunks
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public boolean canExecute(User user, String label, List<String> args) {
return false;
}
// First arg must always be the structure name
List<String> options = Bukkit.getStructureManager().getStructures().keySet().stream().map(k -> k.getKey()).toList();
List<String> options = Bukkit.getStructureManager().getStructures().keySet().stream().map(NamespacedKey::getKey).toList();
if (!options.contains(args.get(0).toLowerCase(Locale.ENGLISH))) {
user.sendMessage("boxed.commands.boxadmin.place.unknown-structure");
return false;
Expand Down Expand Up @@ -144,9 +144,9 @@ public boolean canExecute(User user, String label, List<String> args) {
public boolean execute(User user, String label, List<String> args) {
NamespacedKey tag = NamespacedKey.fromString(args.get(0).toLowerCase(Locale.ENGLISH));
Structure s = Bukkit.getStructureManager().loadStructure(tag);
int x = args.size() == 1 || args.get(1).equals("~") ? user.getLocation().getBlockX() : Integer.valueOf(args.get(1).trim());
int y = args.size() == 1 || args.get(2).equals("~") ? user.getLocation().getBlockY() : Integer.valueOf(args.get(2).trim());
int z = args.size() == 1 || args.get(3).equals("~") ? user.getLocation().getBlockZ() : Integer.valueOf(args.get(3).trim());
int x = args.size() == 1 || args.get(1).equals("~") ? user.getLocation().getBlockX() : Integer.parseInt(args.get(1).trim());
int y = args.size() == 1 || args.get(2).equals("~") ? user.getLocation().getBlockY() : Integer.parseInt(args.get(2).trim());
int z = args.size() == 1 || args.get(3).equals("~") ? user.getLocation().getBlockZ() : Integer.parseInt(args.get(3).trim());
Location spot = new Location(user.getWorld(), x, y, z);
s.place(spot, true, sr, mirror, PALETTE, INTEGRITY, new Random());
NewAreaListener.removeJigsaw(new StructureRecord(tag.getKey(), s, spot, sr, mirror, noMobs));
Expand All @@ -168,7 +168,7 @@ private boolean saveStructure(Location spot, NamespacedKey tag, User user, Struc
try {
config.load(structures);
StringBuilder v = new StringBuilder();
v.append(tag.getKey() + "," + sr2.name() + "," + mirror2.name());
v.append(tag.getKey()).append(",").append(sr2.name()).append(",").append(mirror2.name());
if (noMobs) {
v.append(" NO_MOBS");
}
Expand All @@ -189,7 +189,7 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>
{
String lastArg = !args.isEmpty() ? args.get(args.size() - 1) : "";
if (args.size() == 2) {
return Optional.of(Util.tabLimit(Bukkit.getStructureManager().getStructures().keySet().stream().map(k -> k.getKey()).toList(), lastArg));
return Optional.of(Util.tabLimit(Bukkit.getStructureManager().getStructures().keySet().stream().map(NamespacedKey::getKey).toList(), lastArg));
} else if (args.size() == 3) {
return Optional.of(List.of(String.valueOf(user.getLocation().getBlockX()), "~"));
} else if (args.size() == 4) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public Biome getBiome(WorldInfo worldInfo, int x, int y, int z) {
if (c != null) {
int xx = Math.floorMod(x, 16);
int zz = Math.floorMod(z, 16);
Biome biome = c.chunkBiomes().getOrDefault(new Vector(xx, y, zz), defaultBiome);
return biome;
return c.chunkBiomes().getOrDefault(new Vector(xx, y, zz), defaultBiome);
} else {
BentoBox.getInstance().logWarning("Snapshot at " + chunkX + " " + chunkZ + " is not stored");
return defaultBiome;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public List<Biome> getBiomes(WorldInfo worldInfo) {
* Loads the custom biomes from the config file
* @param config - Yaml configuration object
* @param sector - the direction section to load
* @return
* @return sorted map of the biomes and their probabilities as keys
*/
private SortedMap<Double, Biome> loadQuad(YamlConfiguration config, String sector) {
SortedMap<Double, Biome> result = new TreeMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ public abstract class AbstractBoxedChunkGenerator extends ChunkGenerator {

protected final Boxed addon;
protected static int size;
protected Map<Pair<Integer, Integer>, ChunkStore> chunks = new HashMap<>();
public record ChunkStore(ChunkSnapshot snapshot, List<EntityData> bpEnts, List<ChestData> chests, Map<Vector, Biome> chunkBiomes) {};
public record EntityData(Vector relativeLoc, BlueprintEntity entity) {};
public record ChestData(Vector relativeLoc, BlueprintBlock chest) {};
protected final Map<Pair<Integer, Integer>, ChunkStore> chunks = new HashMap<>();
public record ChunkStore(ChunkSnapshot snapshot, List<EntityData> bpEnts, List<ChestData> chests, Map<Vector, Biome> chunkBiomes) {}

public record EntityData(Vector relativeLoc, BlueprintEntity entity) {}

public record ChestData(Vector relativeLoc, BlueprintBlock chest) {}

//private final WorldRef wordRefNether;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
*/
public class BoxedBlockPopulator extends BlockPopulator {

private Boxed addon;
private final Boxed addon;

/**
* @param addon
* @param addon Boxed
*/
public BoxedBlockPopulator(Boxed addon) {
this.addon = addon;
Expand Down Expand Up @@ -82,7 +82,7 @@ private Location getLoc(World w, Vector v, int chunkX, int chunkZ) {
/**
* Handles signs, chests and mob spawner blocks
*
* @param block - block
* @param bs - block state
* @param bpBlock - config
*/
public void setBlockState(BlockState bs, BlueprintBlock bpBlock) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ private String keyToString(User user, NamespacedKey key) {

/**
* Special case Advancement awarding
* Awards the nether and end advancements when they portal for the first time.
* @param e
* Awards the nether and end advancements when they use a portal for the first time.
* @param e PlayerPortalEvent
*/
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPortal(PlayerPortalEvent e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ public void onPlayerTeleport(PlayerTeleportEvent e) {
}

User u = User.getInstance(e.getPlayer());
// If the to is outside the box, cancel it
// If the to-location is outside the box, cancel it
if (e.getTo() != null) {
Island i = addon.getIslands().getIsland(e.getFrom().getWorld(), u);
if (i == null || !i.onIsland(e.getTo())) {
u.sendMessage("boxed.general.errors.no-teleport-outside");
e.setCancelled(true);
return;
}
}
}
Expand Down Expand Up @@ -92,19 +91,16 @@ public void onEnderPearlLand(ProjectileHitEvent e) {
// Moving is allowed
moveBox(u, fromIsland, l);
Util.teleportAsync(player, l);
return;
}
} else {
// Different box. This is never allowed. Cancel the throw
e.setCancelled(true);
u.sendMessage("boxed.general.errors.no-teleport-outside");
return;
}
}, () -> {
// No box. This is never allowed. Cancel the throw
e.setCancelled(true);
u.sendMessage("boxed.general.errors.no-teleport-outside");
return;
});

});
Expand Down
49 changes: 21 additions & 28 deletions src/main/java/world/bentobox/boxed/listeners/NewAreaListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Queue;
import java.util.Random;
import java.util.*;

import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -75,7 +69,7 @@ public class NewAreaListener implements Listener {
* @param mirror - mirror setting
* @param noMobs - if false, mobs not pasted
*/
public record StructureRecord(String name, Structure structure, Location location, StructureRotation rot, Mirror mirror, Boolean noMobs) {};
public record StructureRecord(String name, Structure structure, Location location, StructureRotation rot, Mirror mirror, Boolean noMobs) {}

private static final Map<Integer, EntityType> BUTCHER_ANIMALS = Map.of(0, EntityType.COW, 1, EntityType.SHEEP, 2, EntityType.PIG);
private static final List<BlockFace> CARDINALS = List.of(BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST);
Expand All @@ -89,16 +83,16 @@ public record StructureRecord(String name, Structure structure, Location locatio
"shipwreck", "stronghold", "swamp_hut", "village_desert", "village_plains",
"village_savanna", "village_snowy", "village_taiga");
private final Boxed addon;
private File structureFile;
private Queue<StructureRecord> itemsToBuild = new LinkedList<>();
private static Random rand = new Random();
private final File structureFile;
private final Queue<StructureRecord> itemsToBuild = new LinkedList<>();
private static final Random rand = new Random();
private boolean pasting;
private static Gson gson = new Gson();
Pair<Integer, Integer> min = new Pair<Integer, Integer>(0,0);
Pair<Integer, Integer> max = new Pair<Integer, Integer>(0,0);
private static final Gson gson = new Gson();
Pair<Integer, Integer> min = new Pair<>(0, 0);
Pair<Integer, Integer> max = new Pair<>(0, 0);
// Database handler for structure data
private final Database<IslandStructures> handler;
private Map<String, IslandStructures> islandStructureCache = new HashMap<>();
private final Map<String, IslandStructures> islandStructureCache = new HashMap<>();



Expand All @@ -118,7 +112,7 @@ public NewAreaListener(Boxed addon) {
}

private void runStructurePrinter(Boxed addon2) {
Bukkit.getScheduler().runTaskTimer(addon.getPlugin(), () -> buildStructure(), 20, 20);
Bukkit.getScheduler().runTaskTimer(addon.getPlugin(), this::buildStructure, 20, 20);
for (String js : JAR_STRUCTURES) {
addon.saveResource("structures/" + js + ".nbt", false);
File structureFile = new File(addon.getDataFolder(), "structures/" + js + ".nbt");
Expand Down Expand Up @@ -259,13 +253,13 @@ private void setUpIsland(Island island) {
if (e == null) {
addon.logError("Error in structures.yml - unknown environment " + env);
} else {
place("structure",config.getConfigurationSection(env), center, e);
place(config.getConfigurationSection(env), center, e);
}
}

}

private void place(String string, ConfigurationSection section, Location center, Environment env) {
private void place(ConfigurationSection section, Location center, Environment env) {
World world = env.equals(Environment.NORMAL) ? addon.getOverWorld() : addon.getNetherWorld();
// Loop through the structures in the file - there could be more than one
for (String vector : section.getKeys(false)) {
Expand Down Expand Up @@ -296,13 +290,13 @@ private void place(String string, ConfigurationSection section, Location center,
// Extract coords
String[] value = vector.split(",");
if (value.length > 2) {
int x = Integer.valueOf(value[0].strip()) + center.getBlockX();
int y = Integer.valueOf(value[1].strip());
int z = Integer.valueOf(value[2].strip()) + center.getBlockZ();
int x = Integer.parseInt(value[0].strip()) + center.getBlockX();
int y = Integer.parseInt(value[1].strip());
int z = Integer.parseInt(value[2].strip()) + center.getBlockZ();
Location l = new Location(world, x, y, z);
itemsToBuild.add(new StructureRecord(name, s, l, rot, mirror, noMobs));
} else {
addon.logError("Structure file syntax error: " + vector + ": " + value);
addon.logError("Structure file syntax error: " + vector + ": " + Arrays.toString(value));
}
}
}
Expand Down Expand Up @@ -376,13 +370,13 @@ public static BoundingBox removeJigsaw(StructureRecord item) {

/**
* Process a structure block. Sets it to a structure void at a minimum.
* If the structure block has meta data indicating it is a chest, then it will fill
* If the structure block has metadata indicating it is a chest, then it will fill
* the chest with a buried treasure loot. If it is waterlogged, then it will change
* the void to water.
* @param b structure block block
* @param b structure block
*/
private static void processStructureBlock(Block b) {
// I would like to read the data from the block an do something with it!
// I would like to read the data from the block and do something with it!
String data = nmsData(b);
BoxedStructureBlock bsb = gson.fromJson(data, BoxedStructureBlock.class);
b.setType(Material.STRUCTURE_VOID);
Expand Down Expand Up @@ -428,9 +422,8 @@ private static void spawnMob(Block b, BoxedJigsawBlock bjb) {
case "minecraft:village/common/pigs" -> EntityType.PIG;
case "minecraft:village/common/cows" -> EntityType.COW;
case "minecraft:village/common/iron_golem" -> EntityType.IRON_GOLEM;
case "minecraft:village/common/butcher_animals" -> BUTCHER_ANIMALS.get(rand.nextInt(3));
case "minecraft:village/common/animals" -> BUTCHER_ANIMALS.get(rand.nextInt(3));
default -> null;
case "minecraft:village/common/butcher_animals", "minecraft:village/common/animals" -> BUTCHER_ANIMALS.get(rand.nextInt(3));
default -> null;
};
// Boxed
if (type == null && bjb.getPool().startsWith("minecraft:boxed/")) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ boxed:
admin: boxadmin
# The default action for new player command call.
# Sub-command of main player command that will be run on first player command call.
# By default it is sub-command 'create'.
# By default, it is sub-command 'create'.
new-player-action: create
# The default action for player command.
# Sub-command of main player command that will be run on each player command call.
# By default it is sub-command 'go'.
# By default, it is sub-command 'go'.
default-action: go
# Announce advancements. We recommend you set the game rule `/gamerule announceAdvancements false`
# but that blocks all new advancement announcements. This setting tells Boxed to broadcast new advancements.
Expand Down

0 comments on commit 3f94cea

Please sign in to comment.