Allow use of Tags in block ID lists

This commit is contained in:
dfsek 2020-11-10 00:33:54 -07:00
parent c0dde090cd
commit 548cd8a30a
16 changed files with 84 additions and 19 deletions

View File

@ -6,6 +6,7 @@ import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.biome.failsafe.FailType; import com.dfsek.terra.biome.failsafe.FailType;
import com.dfsek.terra.config.exception.ConfigException; import com.dfsek.terra.config.exception.ConfigException;
import com.dfsek.terra.config.lang.LangUtil; import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.util.TagUtil;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.InvalidConfigurationException;
@ -73,9 +74,20 @@ public final class ConfigUtil {
Set<Material> bl = new HashSet<>(); Set<Material> bl = new HashSet<>();
for(String s : list) { for(String s : list) {
try { try {
if(bl.contains(Bukkit.createBlockData(s).getMaterial())) if(s.startsWith("#")) {
Bukkit.getLogger().warning("Duplicate material in " + phase + " list: " + s); Debug.info("Loading Tag " + s);
bl.add(Bukkit.createBlockData(s).getMaterial()); Set<Material> tag = TagUtil.getTag(s.substring(1));
for(Material m : tag) {
if(bl.contains(m)) {
Bukkit.getLogger().warning("Duplicate material in " + phase + " list: " + m); // Check for duplicates in this tag
}
}
bl.addAll(tag);
} else {
if(bl.contains(Bukkit.createBlockData(s).getMaterial()))
Bukkit.getLogger().warning("Duplicate material in " + phase + " list: " + s);
bl.add(Bukkit.createBlockData(s).getMaterial());
}
} catch(NullPointerException | IllegalArgumentException e) { } catch(NullPointerException | IllegalArgumentException e) {
throw new ConfigException("Could not load BlockData data for \"" + s + "\"", id); throw new ConfigException("Could not load BlockData data for \"" + s + "\"", id);
} }

View File

@ -0,0 +1,55 @@
package com.dfsek.terra.util;
import com.dfsek.terra.Debug;
import org.bukkit.Material;
import org.bukkit.Tag;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@SuppressWarnings("unchecked")
public class TagUtil {
private static final Map<String, Set<Material>> tagMap;
static {
Debug.info("Loading tags...");
tagMap = new HashMap<>();
Field[] tags = Tag.class.getFields(); // Add Bukkit tags
for(Field field : tags) {
if(Modifier.isStatic(field.getModifiers())) {
try {
Tag<Material> tag = (Tag<Material>) field.get(new Object());
tagMap.put(tag.getKey().toString(), tag.getValues());
Debug.info("Loaded tag: #" + tag.getKey().toString());
} catch(IllegalAccessException e) {
e.printStackTrace();
} catch(ClassCastException ignore) {
}
}
}
putCustomSet("minecraft:base_stone_nether", Material.NETHERRACK, Material.BASALT, Material.BLACKSTONE);
putCustomSet("minecraft:base_stone_overworld", Material.STONE, Material.GRANITE, Material.DIORITE, Material.ANDESITE);
}
private static Set<Material> getSet(Material... materials) {
return Stream.of(materials).collect(Collectors.toSet());
}
private static void putCustomSet(String key, Material... materials) {
tagMap.put(key, getSet(materials));
Debug.info("Loaded tag: #" + key);
}
@NotNull
public static Set<Material> getTag(String tag) {
return Objects.requireNonNull(tagMap.get(tag));
}
}

View File

@ -26,8 +26,7 @@ flora:
erodible: true erodible: true
trees: trees:
chance: 100 density: 7
density: 1
items: items:
CACTUS: CACTUS:
weight: 1 weight: 1

View File

@ -49,8 +49,7 @@ slabs:
- "minecraft:red_sand": "BLOCK:minecraft:red_sandstone_stairs" - "minecraft:red_sand": "BLOCK:minecraft:red_sandstone_stairs"
trees: trees:
chance: 100 density: 7
density: 1
items: items:
CACTUS: CACTUS:
weight: 1 weight: 1

View File

@ -6,4 +6,4 @@ deform: 0.75
deform-frequency: 0.1 deform-frequency: 0.1
id: "ANDESITE" id: "ANDESITE"
replace: replace:
- "minecraft:stone" - "#minecraft:base_stone_overworld"

View File

@ -6,4 +6,4 @@ deform: 0.75
deform-frequency: 0.1 deform-frequency: 0.1
id: "DIORITE" id: "DIORITE"
replace: replace:
- "minecraft:stone" - "#minecraft:base_stone_overworld"

View File

@ -6,4 +6,4 @@ deform: 0.75
deform-frequency: 0.1 deform-frequency: 0.1
id: "DIRT" id: "DIRT"
replace: replace:
- "minecraft:stone" - "#minecraft:base_stone_overworld"

View File

@ -6,4 +6,4 @@ deform: 0.75
deform-frequency: 0.1 deform-frequency: 0.1
id: "GRANITE" id: "GRANITE"
replace: replace:
- "minecraft:stone" - "#minecraft:base_stone_overworld"

View File

@ -6,4 +6,4 @@ deform: 0.75
deform-frequency: 0.1 deform-frequency: 0.1
id: "GRAVEL" id: "GRAVEL"
replace: replace:
- "minecraft:stone" - "#minecraft:base_stone_overworld"

View File

@ -6,4 +6,4 @@ deform: 0.75
deform-frequency: 0.2 deform-frequency: 0.2
id: "COAL_ORE" id: "COAL_ORE"
replace: replace:
- "minecraft:stone" - "#minecraft:base_stone_overworld"

View File

@ -6,4 +6,4 @@ deform: 0.75
deform-frequency: 0.2 deform-frequency: 0.2
id: "DIAMOND_ORE" id: "DIAMOND_ORE"
replace: replace:
- "minecraft:stone" - "#minecraft:base_stone_overworld"

View File

@ -6,4 +6,4 @@ deform: 0.75
deform-frequency: 0.2 deform-frequency: 0.2
id: "GOLD_ORE" id: "GOLD_ORE"
replace: replace:
- "minecraft:stone" - "#minecraft:base_stone_overworld"

View File

@ -6,4 +6,4 @@ deform: 0.75
deform-frequency: 0.2 deform-frequency: 0.2
id: "IRON_ORE" id: "IRON_ORE"
replace: replace:
- "minecraft:stone" - "#minecraft:base_stone_overworld"

View File

@ -6,4 +6,4 @@ deform: 0.75
deform-frequency: 0.2 deform-frequency: 0.2
id: "LAPIS_ORE" id: "LAPIS_ORE"
replace: replace:
- "minecraft:stone" - "#minecraft:base_stone_overworld"

View File

@ -6,4 +6,4 @@ deform: 0.75
deform-frequency: 0.2 deform-frequency: 0.2
id: "REDSTONE_ORE" id: "REDSTONE_ORE"
replace: replace:
- "minecraft:stone" - "#minecraft:base_stone_overworld"

View File

@ -1,7 +1,7 @@
name: "Terra" name: "Terra"
depend: [ "Gaea" ] depend: [ "Gaea" ]
main: "com.dfsek.terra.Terra" main: "com.dfsek.terra.Terra"
version: "1.2.0-BETA" version: "1.2.1-BETA"
load: "STARTUP" load: "STARTUP"
api-version: "1.16" api-version: "1.16"
softdepend: [ "WorldEdit" ] softdepend: [ "WorldEdit" ]