forked from daveyliam/mapwriter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMwUtil.java
120 lines (101 loc) · 3.24 KB
/
MwUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package mapwriter;
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;
import mapwriter.forge.MwForge;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.world.chunk.Chunk;
public class MwUtil {
public final static Pattern patternInvalidChars = Pattern.compile("[^a-zA-Z0-9_]");
public static void logInfo(String s, Object...args) {
MwForge.logger.info(String.format(s, args));
}
public static void logWarning(String s, Object...args) {
MwForge.logger.warning(String.format(s, args));
}
public static void logError(String s, Object...args) {
MwForge.logger.severe(String.format(s, args));
}
public static void debug(String s, Object...args) {
MwForge.logger.finest(String.format(s, args));
}
public static void log(String s, Object...args) {
logInfo(String.format(s, args));
}
public static String mungeString(String s) {
s = s.replace('.', '_');
s = s.replace('-', '_');
s = s.replace(' ', '_');
s = s.replace('/', '_');
s = s.replace('\\', '_');
return patternInvalidChars.matcher(s).replaceAll("");
}
public static File getFreeFilename(File dir, String baseName, String ext) {
int i = 0;
File outputFile;
if (dir != null) {
outputFile = new File(dir, baseName + "." + ext);
} else {
outputFile = new File(baseName + "." + ext);
}
while (outputFile.exists() && (i < 1000)) {
if (dir != null) {
outputFile = new File(dir, baseName + "." + i + "." + ext);
} else {
outputFile = new File(baseName + "." + i + "." + ext);
}
i++;
}
return (i < 1000) ? outputFile : null;
}
public static void printBoth(String msg) {
EntityClientPlayerMP thePlayer = Minecraft.getMinecraft().thePlayer;
if (thePlayer != null) {
thePlayer.addChatMessage(msg);
}
MwUtil.log("%s", msg);
}
public static File getDimensionDir(File worldDir, int dimension) {
File dimDir;
if (dimension != 0) {
dimDir = new File(worldDir, "DIM" + dimension);
} else {
dimDir = worldDir;
}
return dimDir;
}
public static IntBuffer allocateDirectIntBuffer(int size) {
return ByteBuffer.allocateDirect(size * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
}
// algorithm from http://graphics.stanford.edu/~seander/bithacks.html (Sean Anderson)
// works by making sure all bits to the right of the highest set bit are 1, then
// adding 1 to get the answer.
public static int nextHighestPowerOf2(int v) {
// decrement by 1 (to handle cases where v is already a power of two)
v--;
// set all bits to the right of the uppermost set bit.
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
// v |= v >> 32; // uncomment for 64 bit input values
// add 1 to get the power of two result
return v + 1;
}
public static String getCurrentDateString() {
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmm");
return dateFormat.format(new Date());
}
public static int distToChunkSq(int x, int z, Chunk chunk) {
int dx = (chunk.xPosition << 4) + 8 - x;
int dz = (chunk.zPosition << 4) + 8 - z;
return (dx * dx) + (dz * dz);
}
}