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);
}
public void setType(int type) {
world.getBlockAt(x, y, z).setTypeId(type);
public void setType(Material type) {
world.getBlockAt(x, y, z).setType(type);
}
public int getType() {
return world.getBlockAt(x, y, z).getTypeId();
public Material getType() {
return world.getBlockAt(x, y, z).getType();
}
public void setData(int data) {

View File

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

View File

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

View File

@ -7,7 +7,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.logging.Level;
@ -366,7 +365,7 @@ public class Portal {
getWorld().loadChunk(getWorld().getChunkAt(topLeft.getBlock()));
int openType = gate.getPortalBlockOpen();
Material openType = gate.getPortalBlockOpen();
for (Blox inside : getEntrances()) {
Stargate.blockPopulatorQueue.add(new BloxPopulator(inside, openType));
}
@ -403,7 +402,7 @@ public class Portal {
if (isAlwaysOn() && !force) return; // Only close always-open if forced
// Close this gate, then the dest gate.
int closedType = gate.getPortalBlockClosed();
Material closedType = gate.getPortalBlockClosed();
for (Blox inside : getEntrances()) {
Stargate.blockPopulatorQueue.add(new BloxPopulator(inside, closedType));
}
@ -569,8 +568,9 @@ public class Portal {
public boolean isVerified() {
verified = true;
for (RelativeBlockVector control : gate.getControls())
verified = verified && getBlockAt(control).getBlock().getTypeId() == gate.getControlBlock();
for (RelativeBlockVector control : gate.getControls()) {
verified = verified && getBlockAt(control).getBlock().getType().equals(gate.getControlBlock());
}
return verified;
}
@ -1144,7 +1144,7 @@ public class Portal {
// No button on an always-open gate.
if (!alwaysOn) {
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);
portal.setButton(button);
}
@ -1354,7 +1354,7 @@ public class Portal {
if (!portal.isVerified() || !portal.checkIntegrity()) {
// DEBUG
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());
}
}

View File

@ -29,7 +29,6 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Minecart;
import org.bukkit.entity.Player;
import org.bukkit.entity.Vehicle;
import org.bukkit.event.Event.Result;
@ -1095,7 +1094,7 @@ public class Stargate extends JavaPlugin {
if (destroyExplosion) {
portal.unregister(true);
} 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);
}
}
@ -1124,7 +1123,7 @@ public class Stargate extends JavaPlugin {
while (System.nanoTime() - sTime < 50000000) {
BloxPopulator b = Stargate.blockPopulatorQueue.poll();
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);
}
}