Implements support for character tags
This commit is contained in:
		@@ -73,7 +73,7 @@ public class PortalStructure {
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
        for (RelativeBlockVector control : gate.getLayout().getControls()) {
 | 
			
		||||
            verified = verified && portal.getBlockAt(control).getBlock().getType().equals(gate.getControlBlock());
 | 
			
		||||
            verified = verified && gate.isValidControlBlock(portal.getBlockAt(control).getBlock().getType());
 | 
			
		||||
        }
 | 
			
		||||
        this.verified = verified;
 | 
			
		||||
        return verified;
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ import net.knarcraft.stargate.Stargate;
 | 
			
		||||
import net.knarcraft.stargate.container.BlockLocation;
 | 
			
		||||
import net.knarcraft.stargate.container.RelativeBlockVector;
 | 
			
		||||
import org.bukkit.Material;
 | 
			
		||||
import org.bukkit.Tag;
 | 
			
		||||
 | 
			
		||||
import java.io.BufferedWriter;
 | 
			
		||||
import java.io.FileWriter;
 | 
			
		||||
@@ -21,6 +22,7 @@ public class Gate {
 | 
			
		||||
    private final String filename;
 | 
			
		||||
    private final GateLayout layout;
 | 
			
		||||
    private final Map<Character, Material> characterMaterialMap;
 | 
			
		||||
    private final Map<Character, Tag<Material>> characterTagMap;
 | 
			
		||||
    //Gate materials
 | 
			
		||||
    private final Material portalOpenBlock;
 | 
			
		||||
    private final Material portalClosedBlock;
 | 
			
		||||
@@ -37,6 +39,7 @@ public class Gate {
 | 
			
		||||
     * @param filename             <p>The name of the gate file, including extension</p>
 | 
			
		||||
     * @param layout               <p>The gate layout defined in the gate file</p>
 | 
			
		||||
     * @param characterMaterialMap <p>The material types the different layout characters represent</p>
 | 
			
		||||
     * @param characterTagMap      <p>The material tag types the different layout characters represent</p>
 | 
			
		||||
     * @param portalOpenBlock      <p>The material to set the opening to when the portal is open</p>
 | 
			
		||||
     * @param portalClosedBlock    <p>The material to set the opening to when the portal is closed</p>
 | 
			
		||||
     * @param portalButton         <p>The material to use for the portal button</p>
 | 
			
		||||
@@ -45,7 +48,8 @@ public class Gate {
 | 
			
		||||
     * @param destroyCost          <p>The cost of destroying a portal with this gate layout (-1 to disable)</p>
 | 
			
		||||
     * @param toOwner              <p>Whether any payment should go to the owner of the gate, as opposed to just disappearing</p>
 | 
			
		||||
     */
 | 
			
		||||
    public Gate(String filename, GateLayout layout, Map<Character, Material> characterMaterialMap, Material portalOpenBlock,
 | 
			
		||||
    public Gate(String filename, GateLayout layout, Map<Character, Material> characterMaterialMap,
 | 
			
		||||
                Map<Character, Tag<Material>> characterTagMap, Material portalOpenBlock,
 | 
			
		||||
                Material portalClosedBlock, Material portalButton, int useCost, int createCost, int destroyCost,
 | 
			
		||||
                boolean toOwner) {
 | 
			
		||||
        this.filename = filename;
 | 
			
		||||
@@ -58,6 +62,7 @@ public class Gate {
 | 
			
		||||
        this.createCost = createCost;
 | 
			
		||||
        this.destroyCost = destroyCost;
 | 
			
		||||
        this.toOwner = toOwner;
 | 
			
		||||
        this.characterTagMap = characterTagMap;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -78,6 +83,25 @@ public class Gate {
 | 
			
		||||
        return new HashMap<>(characterMaterialMap);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks whether the given material is valid for control blocks
 | 
			
		||||
     *
 | 
			
		||||
     * @param material <p>The material to check</p>
 | 
			
		||||
     * @return <p>True if the material is valid for control blocks</p>
 | 
			
		||||
     */
 | 
			
		||||
    public boolean isValidControlBlock(Material material) {
 | 
			
		||||
        return (getControlBlock() != null) ? getControlBlock().equals(material) : getControlBlockTag().isTagged(material);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Gets the material tag used for this gate's control blocks
 | 
			
		||||
     *
 | 
			
		||||
     * @return <p>The material tag type used for control blocks</p>
 | 
			
		||||
     */
 | 
			
		||||
    public Tag<Material> getControlBlockTag() {
 | 
			
		||||
        return characterTagMap.get(GateHandler.getControlBlockCharacter());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Gets the material type used for this gate's control blocks
 | 
			
		||||
     *
 | 
			
		||||
@@ -195,15 +219,29 @@ public class Gate {
 | 
			
		||||
     */
 | 
			
		||||
    private boolean verifyGateBorderMatches(BlockLocation topLeft, double yaw) {
 | 
			
		||||
        Map<Character, Material> characterMaterialMap = new HashMap<>(this.characterMaterialMap);
 | 
			
		||||
        Map<Character, Tag<Material>> characterTagMap = new HashMap<>(this.characterTagMap);
 | 
			
		||||
        for (RelativeBlockVector borderVector : layout.getBorder()) {
 | 
			
		||||
            int rowIndex = borderVector.getRight();
 | 
			
		||||
            int lineIndex = borderVector.getDown();
 | 
			
		||||
            Character key = layout.getLayout()[lineIndex][rowIndex];
 | 
			
		||||
 | 
			
		||||
            Material materialInLayout = characterMaterialMap.get(key);
 | 
			
		||||
            Tag<Material> tagInLayout = characterTagMap.get(key);
 | 
			
		||||
            Material materialAtLocation = topLeft.getRelativeLocation(borderVector, yaw).getType();
 | 
			
		||||
 | 
			
		||||
            if (materialInLayout == null) {
 | 
			
		||||
            if (materialInLayout != null) {
 | 
			
		||||
                if (materialAtLocation != materialInLayout) {
 | 
			
		||||
                    Stargate.debug("Gate::Matches", String.format("Block Type Mismatch: %s != %s",
 | 
			
		||||
                            materialAtLocation, materialInLayout));
 | 
			
		||||
                    return false;
 | 
			
		||||
                }
 | 
			
		||||
            } else if (tagInLayout != null) {
 | 
			
		||||
                if (!tagInLayout.isTagged(materialAtLocation)) {
 | 
			
		||||
                    Stargate.debug("Gate::Matches", String.format("Block Type Mismatch: %s != %s",
 | 
			
		||||
                            materialAtLocation, tagInLayout));
 | 
			
		||||
                    return false;
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                /* This generally should not happen with proper checking, but just in case a material character is not
 | 
			
		||||
                 * recognized, but still allowed in previous checks, verify the gate as long as all such instances of
 | 
			
		||||
                 * the character correspond to the same material in the physical gate. All subsequent gates will also
 | 
			
		||||
@@ -211,10 +249,6 @@ public class Gate {
 | 
			
		||||
                characterMaterialMap.put(key, materialAtLocation);
 | 
			
		||||
                Stargate.debug("Gate::Matches", String.format("Missing layout material in %s. Using %s from the" +
 | 
			
		||||
                        " physical portal.", getFilename(), materialAtLocation));
 | 
			
		||||
            } else if (materialAtLocation != materialInLayout) {
 | 
			
		||||
                Stargate.debug("Gate::Matches", String.format("Block Type Mismatch: %s != %s",
 | 
			
		||||
                        materialAtLocation, materialInLayout));
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return true;
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ import net.knarcraft.stargate.Stargate;
 | 
			
		||||
import net.knarcraft.stargate.utility.GateReader;
 | 
			
		||||
import net.knarcraft.stargate.utility.MaterialHelper;
 | 
			
		||||
import org.bukkit.Material;
 | 
			
		||||
import org.bukkit.Tag;
 | 
			
		||||
import org.bukkit.block.Block;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
@@ -34,8 +35,9 @@ public class GateHandler {
 | 
			
		||||
    private static final Material defaultPortalBlockClosed = Material.AIR;
 | 
			
		||||
    private static final Material defaultButton = Material.STONE_BUTTON;
 | 
			
		||||
 | 
			
		||||
    private static final HashMap<String, Gate> gates = new HashMap<>();
 | 
			
		||||
    private static final HashMap<Material, List<Gate>> controlBlocks = new HashMap<>();
 | 
			
		||||
    private static final Map<String, Gate> gates = new HashMap<>();
 | 
			
		||||
    private static final Map<Material, List<Gate>> controlBlocks = new HashMap<>();
 | 
			
		||||
    private static final Map<String, List<Gate>> controlBlockTags = new HashMap<>();
 | 
			
		||||
 | 
			
		||||
    private GateHandler() {
 | 
			
		||||
 | 
			
		||||
@@ -86,13 +88,22 @@ public class GateHandler {
 | 
			
		||||
    private static void registerGate(Gate gate) {
 | 
			
		||||
        gates.put(gate.getFilename(), gate);
 | 
			
		||||
 | 
			
		||||
        Material blockID = gate.getControlBlock();
 | 
			
		||||
 | 
			
		||||
        if (!controlBlocks.containsKey(blockID)) {
 | 
			
		||||
            controlBlocks.put(blockID, new ArrayList<>());
 | 
			
		||||
        Material blockId = gate.getControlBlock();
 | 
			
		||||
        if (blockId != null) {
 | 
			
		||||
            if (!controlBlocks.containsKey(blockId)) {
 | 
			
		||||
                controlBlocks.put(blockId, new ArrayList<>());
 | 
			
		||||
            } else {
 | 
			
		||||
                controlBlocks.get(blockId).add(gate);
 | 
			
		||||
            }
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        controlBlocks.get(blockID).add(gate);
 | 
			
		||||
        Tag<Material> materialTag = gate.getControlBlockTag();
 | 
			
		||||
        if (!controlBlockTags.containsKey(materialTag.getKey().toString())) {
 | 
			
		||||
            controlBlockTags.put(materialTag.getKey().toString(), new ArrayList<>());
 | 
			
		||||
        } else {
 | 
			
		||||
            controlBlockTags.get(materialTag.getKey().toString()).add(gate);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -121,6 +132,7 @@ public class GateHandler {
 | 
			
		||||
    private static Gate loadGate(String fileName, String parentFolder, Scanner scanner) {
 | 
			
		||||
        List<List<Character>> design = new ArrayList<>();
 | 
			
		||||
        Map<Character, Material> characterMaterialMap = new HashMap<>();
 | 
			
		||||
        Map<Character, Tag<Material>> characterTagMap = new HashMap<>();
 | 
			
		||||
        Map<String, String> config = new HashMap<>();
 | 
			
		||||
        Set<Material> frameTypes = new HashSet<>();
 | 
			
		||||
 | 
			
		||||
@@ -130,14 +142,14 @@ public class GateHandler {
 | 
			
		||||
        characterMaterialMap.put(ANYTHING, Material.AIR);
 | 
			
		||||
 | 
			
		||||
        //Read the file into appropriate lists and maps
 | 
			
		||||
        int columns = readGateFile(scanner, characterMaterialMap, fileName, design, frameTypes, config);
 | 
			
		||||
        int columns = readGateFile(scanner, characterMaterialMap, characterTagMap, fileName, design, frameTypes, config);
 | 
			
		||||
        if (columns < 0) {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
        Character[][] layout = generateLayoutMatrix(design, columns);
 | 
			
		||||
 | 
			
		||||
        //Create and validate the new gate
 | 
			
		||||
        Gate gate = createGate(config, fileName, layout, characterMaterialMap);
 | 
			
		||||
        Gate gate = createGate(config, fileName, layout, characterMaterialMap, characterTagMap);
 | 
			
		||||
        if (gate == null) {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
@@ -154,10 +166,12 @@ public class GateHandler {
 | 
			
		||||
     * @param fileName             <p>The name of the saved gate config file</p>
 | 
			
		||||
     * @param layout               <p>The layout matrix of the new gate</p>
 | 
			
		||||
     * @param characterMaterialMap <p>A map between layout characters and the material to use</p>
 | 
			
		||||
     * @param materialTagMap       <p>A map between layout characters and the material tags to use</p>
 | 
			
		||||
     * @return <p>A new gate, or null if the config is invalid</p>
 | 
			
		||||
     */
 | 
			
		||||
    private static Gate createGate(Map<String, String> config, String fileName, Character[][] layout,
 | 
			
		||||
                                   Map<Character, Material> characterMaterialMap) {
 | 
			
		||||
                                   Map<Character, Material> characterMaterialMap,
 | 
			
		||||
                                   Map<Character, Tag<Material>> materialTagMap) {
 | 
			
		||||
        //Read relevant material types
 | 
			
		||||
        Material portalOpenBlock = readGateConfig(config, fileName, "portal-open", defaultPortalBlockOpen);
 | 
			
		||||
        Material portalClosedBlock = readGateConfig(config, fileName, "portal-closed", defaultPortalBlockClosed);
 | 
			
		||||
@@ -171,8 +185,8 @@ public class GateHandler {
 | 
			
		||||
                Stargate.getEconomyConfig().sendPaymentToOwner());
 | 
			
		||||
 | 
			
		||||
        //Create the new gate
 | 
			
		||||
        Gate gate = new Gate(fileName, new GateLayout(layout), characterMaterialMap, portalOpenBlock, portalClosedBlock,
 | 
			
		||||
                portalButton, useCost, createCost, destroyCost, toOwner);
 | 
			
		||||
        Gate gate = new Gate(fileName, new GateLayout(layout), characterMaterialMap, materialTagMap, portalOpenBlock,
 | 
			
		||||
                portalClosedBlock, portalButton, useCost, createCost, destroyCost, toOwner);
 | 
			
		||||
 | 
			
		||||
        if (!validateGate(gate, fileName)) {
 | 
			
		||||
            return null;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,10 @@
 | 
			
		||||
package net.knarcraft.stargate.utility;
 | 
			
		||||
 | 
			
		||||
import net.knarcraft.stargate.Stargate;
 | 
			
		||||
import org.bukkit.Bukkit;
 | 
			
		||||
import org.bukkit.Material;
 | 
			
		||||
import org.bukkit.NamespacedKey;
 | 
			
		||||
import org.bukkit.Tag;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
@@ -23,13 +26,15 @@ public final class GateReader {
 | 
			
		||||
     *
 | 
			
		||||
     * @param scanner              <p>The scanner to read from</p>
 | 
			
		||||
     * @param characterMaterialMap <p>The map of characters to store valid symbols in</p>
 | 
			
		||||
     * @param materialTagMap       <p>The map of characters to store valid tag symbols in</p>
 | 
			
		||||
     * @param fileName             <p>The filename of the loaded gate config file</p>
 | 
			
		||||
     * @param design               <p>The list to store the loaded design/layout to</p>
 | 
			
		||||
     * @param frameTypes           <p>The set to store frame/border materials to</p>
 | 
			
		||||
     * @param config               <p>The map of config values to store to</p>
 | 
			
		||||
     * @return <p>The column count/width of the loaded gate</p>
 | 
			
		||||
     */
 | 
			
		||||
    public static int readGateFile(Scanner scanner, Map<Character, Material> characterMaterialMap, String fileName,
 | 
			
		||||
    public static int readGateFile(Scanner scanner, Map<Character, Material> characterMaterialMap,
 | 
			
		||||
                                   Map<Character, Tag<Material>> materialTagMap, String fileName,
 | 
			
		||||
                                   List<List<Character>> design, Set<Material> frameTypes, Map<String, String> config) {
 | 
			
		||||
        boolean designing = false;
 | 
			
		||||
        int columns = 0;
 | 
			
		||||
@@ -39,14 +44,14 @@ public final class GateReader {
 | 
			
		||||
 | 
			
		||||
                if (designing) {
 | 
			
		||||
                    //If we have reached the gate's layout/design, read it
 | 
			
		||||
                    columns = readGateDesignLine(line, columns, characterMaterialMap, fileName, design);
 | 
			
		||||
                    columns = readGateDesignLine(line, columns, characterMaterialMap, materialTagMap, fileName, design);
 | 
			
		||||
                    if (columns < 0) {
 | 
			
		||||
                        return -1;
 | 
			
		||||
                    }
 | 
			
		||||
                } else {
 | 
			
		||||
                    if (!line.isEmpty() && !line.startsWith("#")) {
 | 
			
		||||
                        //Read a normal config value
 | 
			
		||||
                        readGateConfigValue(line, characterMaterialMap, frameTypes, config);
 | 
			
		||||
                        readGateConfigValue(line, characterMaterialMap, materialTagMap, frameTypes, config);
 | 
			
		||||
                    } else if ((line.isEmpty()) || (!line.contains("=") && !line.startsWith("#"))) {
 | 
			
		||||
                        //An empty line marks the start of the gate's layout/design
 | 
			
		||||
                        designing = true;
 | 
			
		||||
@@ -73,11 +78,13 @@ public final class GateReader {
 | 
			
		||||
     * @param line                 <p>The line to read</p>
 | 
			
		||||
     * @param maxColumns           <p>The current max columns value of the design</p>
 | 
			
		||||
     * @param characterMaterialMap <p>The map between characters and the corresponding materials to use</p>
 | 
			
		||||
     * @param materialTagMap       <p>The map between characters and the corresponding material tags to use</p>
 | 
			
		||||
     * @param fileName             <p>The filename of the loaded gate config file</p>
 | 
			
		||||
     * @param design               <p>The two-dimensional list to store the loaded design to</p>
 | 
			
		||||
     * @return <p>The new max columns value of the design</p>
 | 
			
		||||
     */
 | 
			
		||||
    private static int readGateDesignLine(String line, int maxColumns, Map<Character, Material> characterMaterialMap,
 | 
			
		||||
                                          Map<Character, Tag<Material>> materialTagMap,
 | 
			
		||||
                                          String fileName, List<List<Character>> design) {
 | 
			
		||||
        List<Character> row = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
@@ -88,7 +95,7 @@ public final class GateReader {
 | 
			
		||||
 | 
			
		||||
        for (Character symbol : line.toCharArray()) {
 | 
			
		||||
            //Refuse read gate designs with unknown characters
 | 
			
		||||
            if (symbol.equals('?') || (!characterMaterialMap.containsKey(symbol))) {
 | 
			
		||||
            if (symbol.equals('?') || (!characterMaterialMap.containsKey(symbol) && !materialTagMap.containsKey(symbol))) {
 | 
			
		||||
                Stargate.logSevere(String.format("Could not load Gate %s - Unknown symbol '%s' in diagram", fileName,
 | 
			
		||||
                        symbol));
 | 
			
		||||
                return -1;
 | 
			
		||||
@@ -107,12 +114,14 @@ public final class GateReader {
 | 
			
		||||
     *
 | 
			
		||||
     * @param line                 <p>The line to read</p>
 | 
			
		||||
     * @param characterMaterialMap <p>The character to material map to store to</p>
 | 
			
		||||
     * @param materialTagMap       <p>The character to material tag map to store to</p>
 | 
			
		||||
     * @param frameTypes           <p>The set to store gate frame/border types to</p>
 | 
			
		||||
     * @param config               <p>The config value map to store to</p>
 | 
			
		||||
     * @throws Exception <p>If an invalid material is encountered</p>
 | 
			
		||||
     */
 | 
			
		||||
    private static void readGateConfigValue(String line, Map<Character, Material> characterMaterialMap,
 | 
			
		||||
                                            Set<Material> frameTypes, Map<String, String> config) throws Exception {
 | 
			
		||||
                                            Map<Character, Tag<Material>> materialTagMap, Set<Material> frameTypes,
 | 
			
		||||
                                            Map<String, String> config) throws Exception {
 | 
			
		||||
        String[] split = line.split("=");
 | 
			
		||||
        String key = split[0].trim();
 | 
			
		||||
        String value = split[1].trim();
 | 
			
		||||
@@ -120,14 +129,26 @@ public final class GateReader {
 | 
			
		||||
        if (key.length() == 1) {
 | 
			
		||||
            //Read a gate frame material
 | 
			
		||||
            Character symbol = key.charAt(0);
 | 
			
		||||
            Material material = Material.getMaterial(value);
 | 
			
		||||
            if (material == null) {
 | 
			
		||||
                throw new Exception("Invalid material in line: " + line);
 | 
			
		||||
 | 
			
		||||
            if (value.startsWith("#")) {
 | 
			
		||||
                String tagString = value.replaceFirst("#", "");
 | 
			
		||||
                Tag<Material> tag = Bukkit.getTag(Tag.REGISTRY_BLOCKS, NamespacedKey.minecraft(tagString.toLowerCase()),
 | 
			
		||||
                        Material.class);
 | 
			
		||||
                if (tag != null) {
 | 
			
		||||
                    materialTagMap.put(symbol, tag);
 | 
			
		||||
                    return;
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                Material material = Material.getMaterial(value);
 | 
			
		||||
                if (material != null) {
 | 
			
		||||
                    //Register the map between the read symbol and the corresponding material
 | 
			
		||||
                    characterMaterialMap.put(symbol, material);
 | 
			
		||||
                    //Save the material as one of the frame materials used for this kind of gate
 | 
			
		||||
                    frameTypes.add(material);
 | 
			
		||||
                    return;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            //Register the map between the read symbol and the corresponding material
 | 
			
		||||
            characterMaterialMap.put(symbol, material);
 | 
			
		||||
            //Save the material as one of the frame materials used for this kind of gate
 | 
			
		||||
            frameTypes.add(material);
 | 
			
		||||
            throw new Exception("Invalid material in line: " + line);
 | 
			
		||||
        } else {
 | 
			
		||||
            //Read a normal config value
 | 
			
		||||
            config.put(key, value);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user