Change gate configuration to use Material names

This commit is contained in:
PseudoKnight 2018-05-23 16:27:48 -07:00
parent 624c9b52d7
commit f8e95eacbe
5 changed files with 99 additions and 79 deletions

View File

@ -77,12 +77,12 @@ public class Blox {
return makeRelativeLoc(0.5 + -right * modX + distance * modZ, depth, 0.5 + -right * modZ + -distance * modX, rotX, 0); return makeRelativeLoc(0.5 + -right * modX + distance * modZ, depth, 0.5 + -right * modZ + -distance * modX, rotX, 0);
} }
public void setType(int type) { public void setType(Material type) {
world.getBlockAt(x, y, z).setTypeId(type); world.getBlockAt(x, y, z).setType(type);
} }
public int getType() { public Material getType() {
return world.getBlockAt(x, y, z).getTypeId(); return world.getBlockAt(x, y, z).getType();
} }
public void setData(int data) { public void setData(int data) {

View File

@ -1,17 +1,19 @@
package net.TheDgtl.Stargate; package net.TheDgtl.Stargate;
import org.bukkit.Material;
public class BloxPopulator { public class BloxPopulator {
private Blox blox; private Blox blox;
private int nextMat; private Material nextMat;
private byte nextData; private byte nextData;
public BloxPopulator(Blox b, int m) { public BloxPopulator(Blox b, Material m) {
blox = b; blox = b;
nextMat = m; nextMat = m;
nextData = 0; nextData = 0;
} }
public BloxPopulator(Blox b, int m, byte d) { public BloxPopulator(Blox b, Material m, byte d) {
blox = b; blox = b;
nextMat = m; nextMat = m;
nextData = d; nextData = d;
@ -21,7 +23,7 @@ public class BloxPopulator {
blox = b; blox = b;
} }
public void setMat(int m) { public void setMat(Material m) {
nextMat = m; nextMat = m;
} }
@ -33,7 +35,7 @@ public class BloxPopulator {
return blox; return blox;
} }
public int getMat() { public Material getMat() {
return nextMat; return nextMat;
} }

View File

@ -8,6 +8,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Scanner; import java.util.Scanner;
import java.util.logging.Level; import java.util.logging.Level;
@ -35,25 +36,24 @@ import org.bukkit.block.Block;
*/ */
public class Gate { public class Gate {
public static final int ANYTHING = -1; private static final Character ANYTHING = ' ';
public static final int ENTRANCE = -2; private static final Character ENTRANCE = '.';
public static final int CONTROL = -3; private static final Character EXIT = '*';
public static final int EXIT = -4;
private static HashMap<String, Gate> gates = new HashMap<>(); private static HashMap<String, Gate> gates = new HashMap<>();
private static HashMap<Integer, ArrayList<Gate>> controlBlocks = new HashMap<>(); private static HashMap<Material, ArrayList<Gate>> controlBlocks = new HashMap<>();
private static HashSet<Integer> frameBlocks = new HashSet<>(); private static HashSet<Material> frameBlocks = new HashSet<>();
private String filename; private String filename;
private Character[][] layout; private Character[][] layout;
private HashMap<Character, Integer> types; private HashMap<Character, Material> types;
private HashMap<Character, Integer> metadata; private HashMap<Character, Integer> metadata;
private RelativeBlockVector[] entrances = new RelativeBlockVector[0]; private RelativeBlockVector[] entrances = new RelativeBlockVector[0];
private RelativeBlockVector[] border = new RelativeBlockVector[0]; private RelativeBlockVector[] border = new RelativeBlockVector[0];
private RelativeBlockVector[] controls = new RelativeBlockVector[0]; private RelativeBlockVector[] controls = new RelativeBlockVector[0];
private RelativeBlockVector exitBlock = null; private RelativeBlockVector exitBlock = null;
private HashMap<RelativeBlockVector, Integer> exits = new HashMap<>(); private HashMap<RelativeBlockVector, Integer> exits = new HashMap<>();
private int portalBlockOpen = Material.PORTAL.getId(); private Material portalBlockOpen = Material.PORTAL;
private int portalBlockClosed = Material.AIR.getId(); private Material portalBlockClosed = Material.AIR;
// Economy information // Economy information
private int useCost = -1; private int useCost = -1;
@ -61,7 +61,7 @@ public class Gate {
private int destroyCost = -1; private int destroyCost = -1;
private boolean toOwner = false; private boolean toOwner = false;
public Gate(String filename, Character[][] layout, HashMap<Character, Integer> types, HashMap<Character, Integer> metadata) { public Gate(String filename, Character[][] layout, HashMap<Character, Material> types, HashMap<Character, Integer> metadata) {
this.filename = filename; this.filename = filename;
this.layout = layout; this.layout = layout;
this.metadata = metadata; this.metadata = metadata;
@ -80,17 +80,18 @@ public class Gate {
for (int y = 0; y < layout.length; y++) { for (int y = 0; y < layout.length; y++) {
for (int x = 0; x < layout[y].length; x++) { for (int x = 0; x < layout[y].length; x++) {
Integer id = types.get(layout[y][x]); Character key = layout[y][x];
if (layout[y][x] == '-') { if (key.equals('-')) {
controlList.add(new RelativeBlockVector(x, y, 0)); controlList.add(new RelativeBlockVector(x, y, 0));
} }
if (id == ENTRANCE || id == EXIT) { if (key.equals(ENTRANCE) || key.equals(EXIT)) {
entranceList.add(new RelativeBlockVector(x, y, 0)); entranceList.add(new RelativeBlockVector(x, y, 0));
exitDepths[x] = y; exitDepths[x] = y;
if (id == EXIT) if (key.equals(EXIT)) {
this.exitBlock = new RelativeBlockVector(x, y, 0); this.exitBlock = new RelativeBlockVector(x, y, 0);
} else if (id != ANYTHING) { }
} else if (!key.equals(ANYTHING)) {
borderList.add(new RelativeBlockVector(x, y, 0)); borderList.add(new RelativeBlockVector(x, y, 0));
} }
} }
@ -119,8 +120,8 @@ public class Gate {
try { try {
BufferedWriter bw = new BufferedWriter(new FileWriter(gateFolder + filename)); BufferedWriter bw = new BufferedWriter(new FileWriter(gateFolder + filename));
writeConfig(bw, "portal-open", portalBlockOpen); writeConfig(bw, "portal-open", portalBlockOpen.name());
writeConfig(bw, "portal-closed", portalBlockClosed); writeConfig(bw, "portal-closed", portalBlockClosed.name());
if (useCost != -1) if (useCost != -1)
writeConfig(bw, "usecost", useCost); writeConfig(bw, "usecost", useCost);
if (createCost != -1) if (createCost != -1)
@ -129,10 +130,13 @@ public class Gate {
writeConfig(bw, "destroycost", destroyCost); writeConfig(bw, "destroycost", destroyCost);
writeConfig(bw, "toowner", toOwner); writeConfig(bw, "toowner", toOwner);
for (Character type : types.keySet()) { for (Map.Entry<Character, Material> entry : types.entrySet()) {
Integer value = types.get(type); Character type = entry.getKey();
Material value = entry.getValue();
// Skip control values // Skip control values
if (value < 0) continue; if (type.equals(ANYTHING) || type.equals(ENTRANCE) || type.equals(EXIT)) {
continue;
}
bw.append(type); bw.append(type);
bw.append('='); bw.append('=');
@ -148,9 +152,8 @@ public class Gate {
bw.newLine(); bw.newLine();
for (int y = 0; y < layout.length; y++) { for(Character[] aLayout : layout) {
for (int x = 0; x < layout[y].length; x++) { for(Character symbol : aLayout) {
Character symbol = layout[y][x];
bw.append(symbol); bw.append(symbol);
} }
bw.newLine(); bw.newLine();
@ -172,11 +175,16 @@ public class Gate {
bw.newLine(); bw.newLine();
} }
private void writeConfig(BufferedWriter bw, String key, String value) throws IOException {
bw.append(String.format("%s=%s", key, value));
bw.newLine();
}
public Character[][] getLayout() { public Character[][] getLayout() {
return layout; return layout;
} }
public HashMap<Character, Integer> getTypes() { public HashMap<Character, Material> getTypes() {
return types; return types;
} }
@ -203,7 +211,7 @@ public class Gate {
return exitBlock; return exitBlock;
} }
public int getControlBlock() { public Material getControlBlock() {
return types.get('-'); return types.get('-');
} }
@ -211,19 +219,19 @@ public class Gate {
return filename; return filename;
} }
public int getPortalBlockOpen() { public Material getPortalBlockOpen() {
return portalBlockOpen; return portalBlockOpen;
} }
public void setPortalBlockOpen(int type) { public void setPortalBlockOpen(Material type) {
portalBlockOpen = type; portalBlockOpen = type;
} }
public int getPortalBlockClosed() { public Material getPortalBlockClosed() {
return portalBlockClosed; return portalBlockClosed;
} }
public void setPortalBlockClosed(int type) { public void setPortalBlockClosed(Material type) {
portalBlockClosed = type; portalBlockClosed = type;
} }
@ -253,34 +261,34 @@ public class Gate {
public boolean matches(Blox topleft, int modX, int modZ, boolean onCreate) { public boolean matches(Blox topleft, int modX, int modZ, boolean onCreate) {
for (int y = 0; y < layout.length; y++) { for (int y = 0; y < layout.length; y++) {
for (int x = 0; x < layout[y].length; x++) { for (int x = 0; x < layout[y].length; x++) {
int id = types.get(layout[y][x]); Character key = layout[y][x];
if (id == ENTRANCE || id == EXIT) { if (key.equals(ENTRANCE) || key.equals(EXIT)) {
// TODO: Remove once snowmanTrailEvent is added
if (Stargate.ignoreEntrance) continue; if (Stargate.ignoreEntrance) continue;
int type = topleft.modRelative(x, y, 0, modX, 1, modZ).getType(); Material type = topleft.modRelative(x, y, 0, modX, 1, modZ).getType();
// Ignore entrance if it's air and we're creating a new gate // Ignore entrance if it's air and we're creating a new gate
if (onCreate && type == Material.AIR.getId()) continue; if (onCreate && type == Material.AIR) continue;
if (type != portalBlockClosed && type != portalBlockOpen) { if (type != portalBlockClosed && type != portalBlockOpen) {
// Special case for water gates // Special case for water gates
if (portalBlockOpen == Material.WATER.getId() || portalBlockOpen == Material.STATIONARY_WATER.getId()) { if (portalBlockOpen == Material.WATER || portalBlockOpen == Material.STATIONARY_WATER) {
if (type == Material.WATER.getId() || type == Material.STATIONARY_WATER.getId()) { if (type == Material.WATER || type == Material.STATIONARY_WATER) {
continue; continue;
} }
} }
// Special case for lava gates // Special case for lava gates
if (portalBlockOpen == Material.LAVA.getId() || portalBlockOpen == Material.STATIONARY_LAVA.getId()) { if (portalBlockOpen == Material.LAVA || portalBlockOpen == Material.STATIONARY_LAVA) {
if (type == Material.LAVA.getId() || type == Material.STATIONARY_LAVA.getId()) { if (type == Material.LAVA || type == Material.STATIONARY_LAVA) {
continue; continue;
} }
} }
Stargate.debug("Gate::Matches", "Entrance/Exit Material Mismatch: " + type); Stargate.debug("Gate::Matches", "Entrance/Exit Material Mismatch: " + type);
return false; return false;
} }
} else if (id != ANYTHING) { } else if (!key.equals(ANYTHING)) {
Material id = types.get(key);
if (topleft.modRelative(x, y, 0, modX, 1, modZ).getType() != id) { if (topleft.modRelative(x, y, 0, modX, 1, modZ).getType() != id) {
Stargate.debug("Gate::Matches", "Block Type Mismatch: " + topleft.modRelative(x, y, 0, modX, 1, modZ).getType() + " != " + id); Stargate.debug("Gate::Matches", "Block Type Mismatch: " + topleft.modRelative(x, y, 0, modX, 1, modZ).getType() + " != " + id);
return false; return false;
@ -300,7 +308,7 @@ public class Gate {
public static void registerGate(Gate gate) { public static void registerGate(Gate gate) {
gates.put(gate.getFilename(), gate); gates.put(gate.getFilename(), gate);
int blockID = gate.getControlBlock(); Material blockID = gate.getControlBlock();
if (!controlBlocks.containsKey(blockID)) { if (!controlBlocks.containsKey(blockID)) {
controlBlocks.put(blockID, new ArrayList<Gate>()); controlBlocks.put(blockID, new ArrayList<Gate>());
@ -313,16 +321,16 @@ public class Gate {
Scanner scanner = null; Scanner scanner = null;
boolean designing = false; boolean designing = false;
ArrayList<ArrayList<Character>> design = new ArrayList<>(); ArrayList<ArrayList<Character>> design = new ArrayList<>();
HashMap<Character, Integer> types = new HashMap<>(); HashMap<Character, Material> types = new HashMap<>();
HashMap<Character, Integer> metadata = new HashMap<>(); HashMap<Character, Integer> metadata = new HashMap<>();
HashMap<String, String> config = new HashMap<>(); HashMap<String, String> config = new HashMap<>();
HashSet<Integer> frameTypes = new HashSet<>(); HashSet<Material> frameTypes = new HashSet<>();
int cols = 0; int cols = 0;
// Init types map // Init types map
types.put('.', ENTRANCE); types.put(ENTRANCE, Material.AIR);
types.put('*', EXIT); types.put(EXIT, Material.AIR);
types.put(' ', ANYTHING); types.put(ANYTHING, Material.AIR);
try { try {
scanner = new Scanner(file); scanner = new Scanner(file);
@ -363,7 +371,7 @@ public class Gate {
String mData = split[1].trim(); String mData = split[1].trim();
metadata.put(symbol, Integer.parseInt(mData)); metadata.put(symbol, Integer.parseInt(mData));
} }
Integer id = Integer.parseInt(value); Material id = Material.valueOf(value);
types.put(symbol, id); types.put(symbol, id);
frameTypes.add(id); frameTypes.add(id);
@ -430,6 +438,17 @@ public class Gate {
return def; return def;
} }
private static Material readConfig(HashMap<String, String> config, Gate gate, File file, String key, Material def) {
if (config.containsKey(key)) {
Material mat = Material.getMaterial(config.get(key));
if(mat != null) {
return mat;
}
Stargate.log.log(Level.WARNING, String.format("Error reading %s: %s is not a material", file, key));
}
return def;
}
public static void loadGates(String gateFolder) { public static void loadGates(String gateFolder) {
File dir = new File(gateFolder); File dir = new File(gateFolder);
File[] files; File[] files;
@ -440,9 +459,10 @@ public class Gate {
files = new File[0]; files = new File[0];
} }
if (files.length == 0) { if (files == null || files.length == 0) {
dir.mkdir(); if (dir.mkdir()) {
populateDefaults(gateFolder); populateDefaults(gateFolder);
}
} else { } else {
for (File file : files) { for (File file : files) {
Gate gate = loadGate(file); Gate gate = loadGate(file);
@ -452,7 +472,6 @@ public class Gate {
} }
public static void populateDefaults(String gateFolder) { public static void populateDefaults(String gateFolder) {
int Obsidian = Material.OBSIDIAN.getId();
Character[][] layout = new Character[][] { Character[][] layout = new Character[][] {
{' ', 'X','X', ' '}, {' ', 'X','X', ' '},
{'X', '.', '.', 'X'}, {'X', '.', '.', 'X'},
@ -460,12 +479,12 @@ public class Gate {
{'X', '*', '.', 'X'}, {'X', '*', '.', 'X'},
{' ', 'X', 'X', ' '}, {' ', 'X', 'X', ' '},
}; };
HashMap<Character, Integer> types = new HashMap<>(); HashMap<Character, Material> types = new HashMap<>();
types.put('.', ENTRANCE); types.put(ENTRANCE, Material.AIR);
types.put('*', EXIT); types.put(EXIT, Material.AIR);
types.put(' ', ANYTHING); types.put(ANYTHING, Material.AIR);
types.put('X', Obsidian); types.put('X', Material.OBSIDIAN);
types.put('-', Obsidian); types.put('-', Material.OBSIDIAN);
HashMap<Character, Integer> metadata = new HashMap<>(); HashMap<Character, Integer> metadata = new HashMap<>();
Gate gate = new Gate("nethergate.gate", layout, types, metadata); Gate gate = new Gate("nethergate.gate", layout, types, metadata);
@ -474,10 +493,10 @@ public class Gate {
} }
public static Gate[] getGatesByControlBlock(Block block) { public static Gate[] getGatesByControlBlock(Block block) {
return getGatesByControlBlock(block.getTypeId()); return getGatesByControlBlock(block.getType());
} }
public static Gate[] getGatesByControlBlock(int type) { public static Gate[] getGatesByControlBlock(Material type) {
Gate[] result = new Gate[0]; Gate[] result = new Gate[0];
ArrayList<Gate> lookup = controlBlocks.get(type); ArrayList<Gate> lookup = controlBlocks.get(type);
@ -494,7 +513,7 @@ public class Gate {
return gates.size(); return gates.size();
} }
public static boolean isGateBlock(int type) { public static boolean isGateBlock(Material type) {
return frameBlocks.contains(type); return frameBlocks.contains(type);
} }

View File

@ -7,7 +7,6 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.Scanner; import java.util.Scanner;
import java.util.logging.Level; import java.util.logging.Level;
@ -366,7 +365,7 @@ public class Portal {
getWorld().loadChunk(getWorld().getChunkAt(topLeft.getBlock())); getWorld().loadChunk(getWorld().getChunkAt(topLeft.getBlock()));
int openType = gate.getPortalBlockOpen(); Material openType = gate.getPortalBlockOpen();
for (Blox inside : getEntrances()) { for (Blox inside : getEntrances()) {
Stargate.blockPopulatorQueue.add(new BloxPopulator(inside, openType)); Stargate.blockPopulatorQueue.add(new BloxPopulator(inside, openType));
} }
@ -403,7 +402,7 @@ public class Portal {
if (isAlwaysOn() && !force) return; // Only close always-open if forced if (isAlwaysOn() && !force) return; // Only close always-open if forced
// Close this gate, then the dest gate. // Close this gate, then the dest gate.
int closedType = gate.getPortalBlockClosed(); Material closedType = gate.getPortalBlockClosed();
for (Blox inside : getEntrances()) { for (Blox inside : getEntrances()) {
Stargate.blockPopulatorQueue.add(new BloxPopulator(inside, closedType)); Stargate.blockPopulatorQueue.add(new BloxPopulator(inside, closedType));
} }
@ -569,8 +568,9 @@ public class Portal {
public boolean isVerified() { public boolean isVerified() {
verified = true; verified = true;
for (RelativeBlockVector control : gate.getControls()) for (RelativeBlockVector control : gate.getControls()) {
verified = verified && getBlockAt(control).getBlock().getTypeId() == gate.getControlBlock(); verified = verified && getBlockAt(control).getBlock().getType().equals(gate.getControlBlock());
}
return verified; return verified;
} }
@ -1144,7 +1144,7 @@ public class Portal {
// No button on an always-open gate. // No button on an always-open gate.
if (!alwaysOn) { if (!alwaysOn) {
button = topleft.modRelative(buttonVector.getRight(), buttonVector.getDepth(), buttonVector.getDistance() + 1, modX, 1, modZ); button = topleft.modRelative(buttonVector.getRight(), buttonVector.getDepth(), buttonVector.getDistance() + 1, modX, 1, modZ);
button.setType(Material.STONE_BUTTON.getId()); button.setType(Material.STONE_BUTTON);
button.setData(facing); button.setData(facing);
portal.setButton(button); portal.setButton(button);
} }
@ -1354,7 +1354,7 @@ public class Portal {
if (!portal.isVerified() || !portal.checkIntegrity()) { if (!portal.isVerified() || !portal.checkIntegrity()) {
// DEBUG // DEBUG
for (RelativeBlockVector control : portal.getGate().getControls()) { for (RelativeBlockVector control : portal.getGate().getControls()) {
if (portal.getBlockAt(control).getBlock().getTypeId() != portal.getGate().getControlBlock()) { if (!portal.getBlockAt(control).getBlock().getType().equals(portal.getGate().getControlBlock())) {
Stargate.debug("loadAllGates", "Control Block Type == " + portal.getBlockAt(control).getBlock().getTypeId()); Stargate.debug("loadAllGates", "Control Block Type == " + portal.getBlockAt(control).getBlock().getTypeId());
} }
} }

View File

@ -29,7 +29,6 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Minecart;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Vehicle; import org.bukkit.entity.Vehicle;
import org.bukkit.event.Event.Result; import org.bukkit.event.Event.Result;
@ -1095,7 +1094,7 @@ public class Stargate extends JavaPlugin {
if (destroyExplosion) { if (destroyExplosion) {
portal.unregister(true); portal.unregister(true);
} else { } else {
Stargate.blockPopulatorQueue.add(new BloxPopulator(new Blox(b), b.getTypeId(), b.getData())); Stargate.blockPopulatorQueue.add(new BloxPopulator(new Blox(b), b.getType(), b.getData()));
event.setCancelled(true); event.setCancelled(true);
} }
} }
@ -1124,7 +1123,7 @@ public class Stargate extends JavaPlugin {
while (System.nanoTime() - sTime < 50000000) { while (System.nanoTime() - sTime < 50000000) {
BloxPopulator b = Stargate.blockPopulatorQueue.poll(); BloxPopulator b = Stargate.blockPopulatorQueue.poll();
if (b == null) return; if (b == null) return;
b.getBlox().getBlock().setTypeId(b.getMat(), false); b.getBlox().getBlock().setType(b.getMat(), false);
b.getBlox().getBlock().setData(b.getData(), false); b.getBlox().getBlock().setData(b.getData(), false);
} }
} }