aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsowgro <tpoke.ferrari@gmail.com>2024-08-02 17:51:28 -0400
committersowgro <tpoke.ferrari@gmail.com>2024-08-02 17:51:28 -0400
commitec6245a466e285ac479bb1c5f32ec9d9aeaf65ec (patch)
treedb9624658014916b991b83a152ff2d72c999f835
parentd3c2467476f92ad3c5bcbe8fb5aa8b568c971518 (diff)
downloadTumble-ec6245a466e285ac479bb1c5f32ec9d9aeaf65ec.tar.gz
Tumble-ec6245a466e285ac479bb1c5f32ec9d9aeaf65ec.tar.bz2
Tumble-ec6245a466e285ac479bb1c5f32ec9d9aeaf65ec.zip
Add compact material weight notation for layers.yml
-rw-r--r--src/main/java/com/MylesAndMore/Tumble/Main.java10
-rw-r--r--src/main/java/com/MylesAndMore/Tumble/config/LayerManager.java114
-rw-r--r--src/main/resources/layers.yml313
3 files changed, 196 insertions, 241 deletions
diff --git a/src/main/java/com/MylesAndMore/Tumble/Main.java b/src/main/java/com/MylesAndMore/Tumble/Main.java
index b6fdb1f..7cf3628 100644
--- a/src/main/java/com/MylesAndMore/Tumble/Main.java
+++ b/src/main/java/com/MylesAndMore/Tumble/Main.java
@@ -9,8 +9,6 @@ import com.MylesAndMore.Tumble.config.LayerManager;
import com.MylesAndMore.Tumble.game.Arena;
import org.bstats.bukkit.Metrics;
-import org.bukkit.Bukkit;
-import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Objects;
@@ -25,13 +23,7 @@ public class Main extends JavaPlugin {
LanguageManager.readConfig();
ConfigManager.readConfig();
ArenaManager.readConfig();
- try {
- LayerManager.readConfig();
- } catch (InvalidConfigurationException e) {
- this.getLogger().severe(e.getMessage());
- Bukkit.getServer().getPluginManager().disablePlugin(this);
- return;
- }
+ LayerManager.readConfig();
Objects.requireNonNull(this.getCommand("tumble")).setExecutor(new Tumble());
new Metrics(this, 16940);
diff --git a/src/main/java/com/MylesAndMore/Tumble/config/LayerManager.java b/src/main/java/com/MylesAndMore/Tumble/config/LayerManager.java
index 73b301f..f20df6e 100644
--- a/src/main/java/com/MylesAndMore/Tumble/config/LayerManager.java
+++ b/src/main/java/com/MylesAndMore/Tumble/config/LayerManager.java
@@ -2,14 +2,10 @@ package com.MylesAndMore.Tumble.config;
import com.MylesAndMore.Tumble.plugin.CustomConfig;
import org.bukkit.Material;
+import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.ConfigurationSection;
-import org.bukkit.configuration.InvalidConfigurationException;
-import org.bukkit.configuration.file.FileConfiguration;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Random;
+import java.util.*;
import static com.MylesAndMore.Tumble.Main.plugin;
@@ -17,84 +13,100 @@ public class LayerManager {
public static List<List<Material>> layers = new ArrayList<>();
private static final CustomConfig layersYml = new CustomConfig("layers.yml");
- private static final FileConfiguration config = layersYml.getConfig();
+ private static final Configuration config = layersYml.getConfig();
+ private static final Configuration defaultConfig = Objects.requireNonNull(config.getDefaults());
+ private static int layerCount = 0;
/**
* Read layers from layers.yml and populate this.layers
*/
- public static void readConfig() throws InvalidConfigurationException {
+ public static void readConfig() {
layersYml.saveDefaultConfig();
- ConfigurationSection layersSection = config.getConfigurationSection("layers");
- if (layersSection == null) {
- throw new InvalidConfigurationException("layers.yml is missing section 'layers'");
+ readLayers(config.getConfigurationSection("layers"));
+ if (layers.isEmpty()) {
+ plugin.getLogger().warning("layers.yml: No layers were found, using defaults");
+ readLayers(defaultConfig.getConfigurationSection("layers"));
}
- for (String layerPath : layersSection.getKeys(false)) {
- List<Material> layer = readLayer(layerPath);
+
+ // Don't use layers.size() because it includes duplicates for weighted layers
+ plugin.getLogger().info("layers.yml: Loaded " + layerCount + (layerCount == 1 ? " layer" : " layers"));
+ }
+
+ public static void readLayers(ConfigurationSection section) {
+
+ if (section == null) {
+ plugin.getLogger().warning("layers.yml is missing section 'layers', using defaults");
+ return;
+ }
+
+ for (String layerPath : section.getKeys(false)) {
+ ConfigurationSection layerSection = section.getConfigurationSection(layerPath);
+ if (layerSection == null) {
+ plugin.getLogger().warning("layers.yml: Layer '" + layerPath + "' is null");
+ continue;
+ }
+ List<Material> layer = readLayer(layerSection);
if (layer == null) {
plugin.getLogger().warning("layers.yml: Failed to load layer '" + layerPath + "'");
- } else {
- int weight = getLayerWeight(layerPath);
- for (int i = 0; i < weight; i++) {
- layers.add(layer);
- }
+ continue;
}
- }
- if (layers.isEmpty()) {
- throw new InvalidConfigurationException("No layers were found in layers.yml");
+ int weight = layerSection.getInt("layers." + layerPath + ".weight", 1);
+ layerCount++;
+ for (int i = 0; i < weight; i++) {
+ layers.add(layer);
+ }
}
- int numLayers = layersSection.getKeys(false).size(); // Don't use layers.size() because it includes duplicates for weighted layers
- plugin.getLogger().info("layers.yml: Loaded " + numLayers + (numLayers > 1 ? " layers" : " layer"));
}
/**
* Read the list of materials for a layer.
- * @param path The path of the layer in the config
+ * @param section The path of the layer in the config
* @return The list of materials for the layer to be composed of
*/
- public static List<Material> readLayer(String path) {
- List<Map<?, ?>> materialsSection = config.getMapList("layers." + path + ".materials");
- if (materialsSection.isEmpty()) {
- plugin.getLogger().warning("layers.yml: Layer '" + path + "' is missing section 'materials'");
+ public static List<Material> readLayer(ConfigurationSection section) {
+
+ List<String> materialsList = section.getStringList("materials");
+ if (materialsList.isEmpty()) {
+ plugin.getLogger().warning("layers.yml: Layer '" + section.getCurrentPath() + "' is missing section 'materials'");
return null;
}
+
List<Material> materials = new ArrayList<>();
+ for (String s : materialsList) {
+ String[] sp = s.split(" ");
- for (Map<?, ?> materialMap : materialsSection) {
- String matName = (String)materialMap.get("material");
+ if (sp.length < 1) {
+ plugin.getLogger().warning("layers.yml: Invalid format in layer '" + section.getCurrentPath() + "'");
+ continue;
+ }
+ String matName = sp[0];
Material mat = Material.getMaterial(matName);
+ if (mat == null) {
+ plugin.getLogger().warning("layers.yml: Invalid material '" + matName + "' in layer '" + section.getCurrentPath() + "'");
+ continue;
+ }
- Object weightObj = materialMap.get("weight");
- int weight = 1;
- if (weightObj != null) {
- if (weightObj instanceof Integer) {
- weight = (Integer)weightObj;
- } else {
- plugin.getLogger().warning("layers.yml: Invalid weight in layer '" + path + "'");
- return null;
+ int matWeight;
+ if (sp.length < 2) {
+ matWeight = 1;
+ } else {
+ try {
+ matWeight = Integer.parseInt(sp[1]);
+ } catch (NumberFormatException e) {
+ plugin.getLogger().warning("layers.yml: Invalid weight '" + sp[1] + "' in layer '" + section.getCurrentPath() + "'");
+ matWeight = 1;
}
}
- if (mat == null) {
- plugin.getLogger().warning("layers.yml: Invalid material '" + matName + "' in layer '" + path + "'");
- return null;
- }
- if (weight < 1) {
- plugin.getLogger().warning("layers.yml: Invalid weight '" + weight + "' in layer '" + path + "'");
- return null;
- }
- for (int i = 0; i < weight; i++) {
+ for (int i = 0; i < matWeight; i++) {
materials.add(mat);
}
}
return materials;
}
- public static int getLayerWeight(String path) {
- return config.getInt("layers." + path + ".weight", 1);
- }
-
public static List<Material> getRandom() {
return layers.get(new Random().nextInt(layers.size()));
}
diff --git a/src/main/resources/layers.yml b/src/main/resources/layers.yml
index ff0f353..d388759 100644
--- a/src/main/resources/layers.yml
+++ b/src/main/resources/layers.yml
@@ -2,231 +2,182 @@ layers:
ores:
weight: 5
materials:
- - material: COBBLESTONE
- weight: 5
- - material: COAL_ORE
- weight: 3
- - material: GRASS_BLOCK
- weight: 2
- - material: IRON_ORE
- - material: GOLD_ORE
- - material: REDSTONE_ORE
- - material: EMERALD_ORE
- - material: LAPIS_ORE
- - material: DIAMOND_ORE
- - material: COBWEB
+ - COBBLESTONE 5
+ - COAL_ORE 3
+ - GRASS_BLOCK 2
+ - IRON_ORE
+ - GOLD_ORE
+ - REDSTONE_ORE
+ - EMERALD_ORE
+ - LAPIS_ORE
+ - DIAMOND_ORE
+ - COBWEB
ore_blocks:
weight: 5
materials:
- - material: COAL_BLOCK
- weight: 4
- - material: IRON_BLOCK
- weight: 3
- - material: GOLD_BLOCK
- weight: 2
- - material: REDSTONE_BLOCK
- weight: 2
- - material: LAPIS_BLOCK
- - material: DIAMOND_BLOCK
+ - COAL_BLOCK 4
+ - IRON_BLOCK 3
+ - GOLD_BLOCK 2
+ - REDSTONE_BLOCK 2
+ - LAPIS_BLOCK
+ - DIAMOND_BLOCK
nether:
weight: 5
materials:
- - material: NETHERRACK
- weight: 6
- - material: NETHER_BRICKS
- weight: 4
- - material: NETHER_GOLD_ORE
- weight: 2
- - material: NETHER_QUARTZ_ORE
- weight: 2
- - material: CRIMSON_NYLIUM
- - material: WARPED_NYLIUM
- - material: CRACKED_NETHER_BRICKS
- - material: RED_NETHER_BRICKS
- - material: NETHER_WART_BLOCK
- - material: CRYING_OBSIDIAN
- - material: SHROOMLIGHT
- - material: BLACKSTONE
- - material: BASALT
- - material: SOUL_SAND
+ - NETHERRACK 6
+ - NETHER_BRICKS 4
+ - NETHER_GOLD_ORE 2
+ - NETHER_QUARTZ_ORE 2
+ - CRIMSON_NYLIUM
+ - WARPED_NYLIUM
+ - CRACKED_NETHER_BRICKS
+ - RED_NETHER_BRICKS
+ - NETHER_WART_BLOCK
+ - CRYING_OBSIDIAN
+ - SHROOMLIGHT
+ - BLACKSTONE
+ - BASALT
+ - SOUL_SAND
end:
weight: 5
materials:
- - material: END_STONE
- weight: 4
- - material: END_STONE_BRICKS
- weight: 2
- - material: PURPUR_BLOCK
- weight: 2
- - material: PURPUR_PILLAR
- - material: OBSIDIAN
- - material: COBBLESTONE
+ - END_STONE 4
+ - END_STONE_BRICKS 2
+ - PURPUR_BLOCK 2
+ - PURPUR_PILLAR
+ - OBSIDIAN
+ - COBBLESTONE
redstone:
weight: 4
materials:
- - material: REDSTONE_BLOCK
- weight: 2
- - material: REDSTONE_LAMP
- - material: TARGET
- - material: SLIME_BLOCK
- - material: OBSERVER
- - material: DAYLIGHT_DETECTOR
+ - REDSTONE_BLOCK 2
+ - REDSTONE_LAMP
+ - TARGET
+ - SLIME_BLOCK
+ - OBSERVER
+ - DAYLIGHT_DETECTOR
ocean:
weight: 4
materials:
- - material: PRISMARINE
- weight: 2
- - material: DARK_PRISMARINE
- - material: PRISMARINE_BRICKS
- - material: PRISMARINE_BRICK_SLAB
- - material: BLUE_STAINED_GLASS
- - material: SEA_LANTERN
- - material: SPONGE
- - material: TUBE_CORAL_BLOCK
- - material: BRAIN_CORAL_BLOCK
- - material: BUBBLE_CORAL_BLOCK
+ - PRISMARINE 2
+ - DARK_PRISMARINE
+ - PRISMARINE_BRICKS
+ - PRISMARINE_BRICK_SLAB
+ - BLUE_STAINED_GLASS
+ - SEA_LANTERN
+ - SPONGE
+ - TUBE_CORAL_BLOCK
+ - BRAIN_CORAL_BLOCK
+ - BUBBLE_CORAL_BLOCK
desert:
weight: 4
materials:
- - material: SANDSTONE
- weight: 2
- - material: RED_SANDSTONE
- weight: 2
- - material: CHISELED_SANDSTONE
- - material: SMOOTH_SANDSTONE
- - material: CUT_SANDSTONE
- - material: SANDSTONE_SLAB
- - material: RED_SANDSTONE_SLAB
- - material: RED_TERRACOTTA
- - material: ORANGE_TERRACOTTA
- - material: YELLOW_TERRACOTTA
- - material: TERRACOTTA
+ - SANDSTONE 2
+ - RED_SANDSTONE 2
+ - CHISELED_SANDSTONE
+ - SMOOTH_SANDSTONE
+ - CUT_SANDSTONE
+ - SANDSTONE_SLAB
+ - RED_SANDSTONE_SLAB
+ - RED_TERRACOTTA
+ - ORANGE_TERRACOTTA
+ - YELLOW_TERRACOTTA
+ - TERRACOTTA
forest:
weight: 4
materials:
- - material: OAK_LEAVES
- weight: 2
- - material: SPRUCE_LEAVES
- weight: 2
- - material: ACACIA_LEAVES
- weight: 2
- - material: OAK_LOG
- - material: SPRUCE_LOG
- - material: ACACIA_LOG
- - material: STRIPPED_OAK_LOG
- - material: STRIPPED_SPRUCE_LOG
- - material: STRIPPED_ACACIA_LOG
- - material: OAK_WOOD
- - material: SPRUCE_WOOD
- - material: ACACIA_WOOD
+ - OAK_LEAVES 2
+ - SPRUCE_LEAVES 2
+ - ACACIA_LEAVES 2
+ - OAK_LOG
+ - SPRUCE_LOG
+ - ACACIA_LOG
+ - STRIPPED_OAK_LOG
+ - STRIPPED_SPRUCE_LOG
+ - STRIPPED_ACACIA_LOG
+ - OAK_WOOD
+ - SPRUCE_WOOD
+ - ACACIA_WOOD
jungle:
weight: 3
materials:
- - material: MOSSY_COBBLESTONE
- weight: 3
- - material: COBBLESTONE
- weight: 2
- - material: JUNGLE_LEAVES
- weight: 2
- - material: JUNGLE_LOG
- weight: 2
- - material: STRIPPED_JUNGLE_LOG
- - material: JUNGLE_WOOD
- - material: STRIPPED_JUNGLE_WOOD
- - material: JUNGLE_PLANKS
- - material: JUNGLE_SLAB
+ - MOSSY_COBBLESTONE 3
+ - COBBLESTONE 2
+ - JUNGLE_LEAVES 2
+ - JUNGLE_LOG 2
+ - STRIPPED_JUNGLE_LOG
+ - JUNGLE_WOOD
+ - STRIPPED_JUNGLE_WOOD
+ - JUNGLE_PLANKS
+ - JUNGLE_SLAB
overworld:
weight: 3
materials:
- - material: DIRT
- - material: COARSE_DIRT
- - material: GRASS_BLOCK
- - material: DIRT_PATH
- - material: MYCELIUM
- - material: PODZOL
- - material: OAK_SLAB
- - material: BRICK_WALL
- - material: BRICK_STAIRS
+ - DIRT
+ - COARSE_DIRT
+ - GRASS_BLOCK
+ - GRASS_PATH
+ - MYCELIUM
+ - PODZOL
+ - OAK_SLAB
+ - BRICK_WALL
+ - BRICK_STAIRS
terracotta:
weight: 3
materials:
- - material: PINK_TERRACOTTA
- weight: 2
- - material: PURPLE_TERRACOTTA
- weight: 2
- - material: GRAY_TERRACOTTA
- weight: 2
- - material: BLUE_TERRACOTTA
- weight: 2
- - material: LIGHT_BLUE_TERRACOTTA
- weight: 2
- - material: WHITE_TERRACOTTA
- weight: 2
- - material: BROWN_TERRACOTTA
- weight: 2
- - material: GREEN_TERRACOTTA
- weight: 2
- - material: YELLOW_TERRACOTTA
- weight: 2
- - material: HONEYCOMB_BLOCK
- weight: 2
- - material: WHITE_STAINED_GLASS
+ - PINK_TERRACOTTA 2
+ - PURPLE_TERRACOTTA 2
+ - GRAY_TERRACOTTA 2
+ - BLUE_TERRACOTTA 2
+ - LIGHT_BLUE_TERRACOTTA 2
+ - WHITE_TERRACOTTA 2
+ - BROWN_TERRACOTTA 2
+ - GREEN_TERRACOTTA 2
+ - YELLOW_TERRACOTTA 2
+ - HONEYCOMB_BLOCK 2
+ - WHITE_STAINED_GLASS
glazed_terracotta:
weight: 3
materials:
- - material: PODZOL
- weight: 3
- - material: YELLOW_GLAZED_TERRACOTTA
- - material: LIGHT_BLUE_GLAZED_TERRACOTTA
- - material: GRAY_GLAZED_TERRACOTTA
+ - PODZOL 3
+ - YELLOW_GLAZED_TERRACOTTA
+ - LIGHT_BLUE_GLAZED_TERRACOTTA
+ - GRAY_GLAZED_TERRACOTTA
sticky:
weight: 3
materials:
- - material: WHITE_TERRACOTTA
- weight: 3
- - material: BLUE_ICE
- weight: 3
- - material: STONE_SLAB
- weight: 3
- - material: SOUL_SOIL
- weight: 2
- - material: GLOWSTONE
- weight: 2
- - material: SLIME_BLOCK
- - material: HONEY_BLOCK
+ - WHITE_TERRACOTTA 3
+ - BLUE_ICE 3
+ - STONE_SLAB 3
+ - SOUL_SOIL 2
+ - GLOWSTONE 2
+ - SLIME_BLOCK
+ - HONEY_BLOCK
annoying_movement:
weight: 2
materials:
- - material: PACKED_ICE
- weight: 4
- - material: JUKEBOX
- weight: 2
- - material: TNT
- weight: 2
- - material: LIGHT_BLUE_CONCRETE
- weight: 2
- - material: GLASS
- weight: 2
- - material: SMOOTH_STONE_SLAB
- weight: 2
- - material: SOUL_SAND
+ - PACKED_ICE 4
+ - JUKEBOX 2
+ - TNT 2
+ - LIGHT_BLUE_CONCRETE 2
+ - GLASS 2
+ - SMOOTH_STONE_SLAB 2
+ - SOUL_SAND
insanity:
weight: 2
materials:
- - material: OAK_PLANKS
- - material: OBSIDIAN
- - material: SPONGE
- - material: BEEHIVE
- - material: DRIED_KELP_BLOCK
+ - OAK_PLANKS
+ - OBSIDIAN
+ - SPONGE
+ - BEEHIVE
+ - DRIED_KELP_BLOCK
glass:
weight: 1
materials:
- - material: GLASS
- weight: 30
- - material: WHITE_STAINED_GLASS
+ - GLASS 30
+ - WHITE_STAINED_GLASS
-# <NAME>:
-# weight:
+# <LAYER_NAME>:
+# weight: <LAYER_WEIGHT>
# materials:
-# - material:
-# weight:
+# - <MATERIAL> <MATERIAL_WEIGHT>