32
src/org/mcteam/ancientgates/util/DiscUtil.java
Normal file
32
src/org/mcteam/ancientgates/util/DiscUtil.java
Normal file
@ -0,0 +1,32 @@
|
||||
package org.mcteam.ancientgates.util;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Harddisc related methods such as read and write.
|
||||
*/
|
||||
public class DiscUtil {
|
||||
/**
|
||||
* Convenience function for writing a string to a file.
|
||||
*/
|
||||
public static void write(File file, String content) throws IOException {
|
||||
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF8"));
|
||||
out.write(content);
|
||||
out.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience function for reading a file as a string.
|
||||
*/
|
||||
public static String read(File file) throws IOException {
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
|
||||
String ret = new String(new byte[0], "UTF-8");
|
||||
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
ret += line;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
79
src/org/mcteam/ancientgates/util/FloodUtil.java
Normal file
79
src/org/mcteam/ancientgates/util/FloodUtil.java
Normal file
@ -0,0 +1,79 @@
|
||||
package org.mcteam.ancientgates.util;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.mcteam.ancientgates.Conf;
|
||||
|
||||
public class FloodUtil {
|
||||
private static final Set<BlockFace> exp1 = new HashSet<BlockFace>();
|
||||
private static final Set<BlockFace> exp2 = new HashSet<BlockFace>();
|
||||
|
||||
static {
|
||||
exp1.add(BlockFace.UP);
|
||||
exp1.add(BlockFace.DOWN);
|
||||
exp1.add(BlockFace.EAST);
|
||||
exp1.add(BlockFace.WEST);
|
||||
|
||||
exp2.add(BlockFace.UP);
|
||||
exp2.add(BlockFace.DOWN);
|
||||
exp2.add(BlockFace.NORTH);
|
||||
exp2.add(BlockFace.SOUTH);
|
||||
}
|
||||
|
||||
|
||||
// For the same frame and location this set of blocks is deterministic
|
||||
public static Set<Block> getGateFrameBlocks(Block block) {
|
||||
Set<Block> blocks1 = getAirFloodBlocks(block, new HashSet<Block>(), exp1, Conf.getGateMaxArea());
|
||||
Set<Block> blocks2 = getAirFloodBlocks(block, new HashSet<Block>(), exp2, Conf.getGateMaxArea());
|
||||
|
||||
if (blocks1 == null && blocks2 == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (blocks1 == null) {
|
||||
return blocks2;
|
||||
}
|
||||
|
||||
if (blocks2 == null) {
|
||||
return blocks1;
|
||||
}
|
||||
|
||||
if (blocks1.size() > blocks2.size()) {
|
||||
return blocks2;
|
||||
}
|
||||
|
||||
return blocks1;
|
||||
}
|
||||
|
||||
public static Set<Block> getAirFloodBlocks(Block startBlock, Set<Block> foundBlocks, Set<BlockFace> expandFaces, int limit) {
|
||||
if (foundBlocks == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (foundBlocks.size() > limit) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (foundBlocks.contains(startBlock)) {
|
||||
return foundBlocks;
|
||||
}
|
||||
|
||||
if (startBlock.getType() == Material.AIR || startBlock.getType() == Material.PORTAL) {
|
||||
// ... We found a block :D ...
|
||||
foundBlocks.add(startBlock);
|
||||
|
||||
// ... And flood away !
|
||||
for (BlockFace face : expandFaces) {
|
||||
Block potentialBlock = startBlock.getFace(face);
|
||||
foundBlocks = getAirFloodBlocks(potentialBlock, foundBlocks, expandFaces, limit);
|
||||
}
|
||||
}
|
||||
|
||||
return foundBlocks;
|
||||
}
|
||||
|
||||
}
|
15
src/org/mcteam/ancientgates/util/GeometryUtil.java
Normal file
15
src/org/mcteam/ancientgates/util/GeometryUtil.java
Normal file
@ -0,0 +1,15 @@
|
||||
package org.mcteam.ancientgates.util;
|
||||
|
||||
import org.bukkit.Location;
|
||||
|
||||
public class GeometryUtil {
|
||||
|
||||
// How long between two locations?
|
||||
public static double distanceBetweenLocations(Location location1, Location location2) {
|
||||
double X = location1.getX() - location2.getX();
|
||||
double Y = location1.getY() - location2.getY();
|
||||
double Z = location1.getZ() - location2.getZ();
|
||||
return Math.sqrt(X*X+Y*Y+Z*Z);
|
||||
}
|
||||
|
||||
}
|
48
src/org/mcteam/ancientgates/util/TextUtil.java
Normal file
48
src/org/mcteam/ancientgates/util/TextUtil.java
Normal file
@ -0,0 +1,48 @@
|
||||
package org.mcteam.ancientgates.util;
|
||||
import java.util.*;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.mcteam.ancientgates.Conf;
|
||||
|
||||
public class TextUtil {
|
||||
public static String titleize(String str) {
|
||||
String line = Conf.colorChrome+repeat("_", 60);
|
||||
String center = ".[ " + Conf.colorSystem + str + Conf.colorChrome + " ].";
|
||||
int pivot = line.length() / 2;
|
||||
int eatLeft = center.length() / 2;
|
||||
int eatRight = center.length() - eatLeft;
|
||||
return line.substring(0, pivot - eatLeft) + center + line.substring(pivot + eatRight);
|
||||
}
|
||||
|
||||
public static String repeat(String s, int times) {
|
||||
if (times <= 0) return "";
|
||||
else return s + repeat(s, times-1);
|
||||
}
|
||||
|
||||
public static ArrayList<String> split(String str) {
|
||||
return new ArrayList<String>(Arrays.asList(str.trim().split("\\s+")));
|
||||
}
|
||||
|
||||
public static String implode(List<String> list, String glue) {
|
||||
String ret = "";
|
||||
for (int i=0; i<list.size(); i++) {
|
||||
if (i!=0) {
|
||||
ret += glue;
|
||||
}
|
||||
ret += list.get(i);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
public static String implode(List<String> list) {
|
||||
return implode(list, " ");
|
||||
}
|
||||
|
||||
public static String getMaterialName(Material material) {
|
||||
String ret = material.toString();
|
||||
ret = ret.replace('_', ' ');
|
||||
ret = ret.toLowerCase();
|
||||
return ret.substring(0, 1).toUpperCase()+ret.substring(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user