初始化并且写了些东西
This commit is contained in:
111
src/main/java/top/sunsetlab/utils/JsonStructure.java
Normal file
111
src/main/java/top/sunsetlab/utils/JsonStructure.java
Normal file
@@ -0,0 +1,111 @@
|
||||
package top.sunsetlab.utils;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.Registry;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.BlockType;
|
||||
import top.sunsetlab.PluginMain;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Level;
|
||||
|
||||
class JsonStructureData {
|
||||
ArrayList<String> structure;
|
||||
HashMap<String,String> marks;
|
||||
int[] center;
|
||||
String id;
|
||||
ArrayList<String> actions;
|
||||
}
|
||||
|
||||
public class JsonStructure {
|
||||
private final JsonStructureData data;
|
||||
private final String structure_loc;
|
||||
|
||||
public JsonStructure(String location) throws RuntimeException {
|
||||
InputStream file = JsonStructure.class.getResourceAsStream("/" + location);
|
||||
if (file == null) {
|
||||
throw new RuntimeException(location + " not found");
|
||||
}
|
||||
InputStreamReader reader = new InputStreamReader(file);
|
||||
BufferedReader bufferedReader = new BufferedReader(reader);
|
||||
Gson gson = new Gson();
|
||||
data = gson.fromJson(bufferedReader, JsonStructureData.class);
|
||||
structure_loc = location;
|
||||
}
|
||||
|
||||
public boolean match(Location pos) {
|
||||
World world = pos.getWorld();
|
||||
Location currentpos = new Location(
|
||||
world,
|
||||
pos.getX() - data.center[0],
|
||||
pos.getY(),
|
||||
pos.getZ() - data.center[1]
|
||||
);
|
||||
Location startpos = currentpos.clone();
|
||||
Location endpos = new Location(
|
||||
world,
|
||||
pos.getX() + data.center[0],
|
||||
pos.getY(),
|
||||
pos.getZ() + data.center[1]
|
||||
);
|
||||
if (PluginMain.DEBUG) {
|
||||
PluginMain.LOGGER.log(Level.INFO, "Matching structure " + structure_loc
|
||||
+ "; From " + currentpos + " To " + endpos);
|
||||
}
|
||||
while (!currentpos.equals(endpos)) {
|
||||
int rx = currentpos.getBlockX() - startpos.getBlockX();
|
||||
int rz = currentpos.getBlockZ() - startpos.getBlockZ();
|
||||
// if (PluginMain.DEBUG) {
|
||||
// PluginMain.LOGGER.log(Level.INFO, "Matching location "
|
||||
// + currentpos
|
||||
// + "(relative (" + rx + "," + rz + "))");
|
||||
// }
|
||||
String mark = String.valueOf(data.structure.get(rz).charAt(rx));
|
||||
String type = data.marks.get(mark);
|
||||
if (type == null) {
|
||||
throw new RuntimeException("In Structure "+ structure_loc +"; Undefined mark: " + mark);
|
||||
}
|
||||
NamespacedKey key = NamespacedKey.fromString(type);
|
||||
if (key == null) {
|
||||
throw new RuntimeException("In Structure "+ structure_loc +"; Invalid key: " + type);
|
||||
}
|
||||
BlockType blockType = Registry.BLOCK.get(key);
|
||||
if (blockType == null) {
|
||||
throw new RuntimeException("In Structure "+ structure_loc +"; Invalid block type: " + type);
|
||||
}
|
||||
if (!Objects.equals(world.getBlockAt(currentpos).getType().asBlockType(), blockType)) {
|
||||
if (PluginMain.DEBUG) {
|
||||
PluginMain.LOGGER.log(Level.INFO, "Block Mismatch at " + currentpos
|
||||
+ "(relative (" + rx + "," + rz + "))"
|
||||
+ "; Expected " + blockType
|
||||
+ ", but got " + world.getBlockAt(currentpos).getType());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
currentpos.add(1,0,0);
|
||||
if (currentpos.getBlockX() == endpos.getBlockX() + 1) {
|
||||
currentpos.setX(startpos.getBlockX());
|
||||
currentpos.setZ(currentpos.getBlockZ() + 1);
|
||||
if (currentpos.getBlockZ() == endpos.getBlockZ() + 1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return data.id;
|
||||
}
|
||||
|
||||
public ArrayList<String> getActions() {
|
||||
return data.actions;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user