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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
package gtPlusPlus.core.util;
import java.awt.Color;
import java.io.File;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.IChatComponent;
import net.minecraftforge.common.util.EnumHelper;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import org.apache.commons.lang3.EnumUtils;
import cpw.mods.fml.common.FMLCommonHandler;
import gregtech.api.GregTechAPI;
import gregtech.api.enums.TCAspects;
import gregtech.api.enums.TCAspects.TC_AspectStack;
import gregtech.api.util.GTLanguageManager;
import gregtech.api.util.GTLog;
import gregtech.api.util.GTUtility;
import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.core.item.ModItems;
import gtPlusPlus.core.lib.GTPPCore;
import gtPlusPlus.core.util.minecraft.FluidUtils;
import gtPlusPlus.core.util.minecraft.ItemUtils;
import gtPlusPlus.core.util.minecraft.NBTUtils;
import ic2.core.Ic2Items;
import ic2.core.init.InternalName;
import ic2.core.item.resources.ItemCell;
public class Utils {
public static boolean isServer() {
return FMLCommonHandler.instance()
.getEffectiveSide()
.isServer();
}
public static boolean isClient() {
return FMLCommonHandler.instance()
.getEffectiveSide()
.isClient();
}
public static TC_AspectStack getTcAspectStack(final TCAspects aspect, final long size) {
return getTcAspectStack(aspect.name(), (int) size);
}
public static TC_AspectStack getTcAspectStack(final String aspect, final long size) {
return getTcAspectStack(aspect, (int) size);
}
public static TC_AspectStack getTcAspectStack(final TCAspects aspect, final int size) {
return getTcAspectStack(aspect.name(), size);
}
public static TC_AspectStack getTcAspectStack(final String aspect, final int size) {
TC_AspectStack returnValue = null;
if (aspect.equalsIgnoreCase("COGNITIO")) {
// Adds in Compat for older GT Versions which Misspell aspects.
try {
if (EnumUtils.isValidEnum(TCAspects.class, "COGNITIO")) {
Logger.WARNING("TC Aspect found - " + aspect);
returnValue = new TC_AspectStack(TCAspects.valueOf("COGNITIO"), size);
} else {
Logger.INFO(
"Fallback TC Aspect found - " + aspect
+ " - PLEASE UPDATE GREGTECH TO A NEWER VERSION TO REMOVE THIS MESSAGE - THIS IS NOT AN ERROR");
returnValue = new TC_AspectStack(TCAspects.valueOf("COGNITO"), size);
}
} catch (final NoSuchFieldError r) {
Logger.INFO("Invalid Thaumcraft Aspects - Report this issue to Alkalus");
}
} else if (aspect.equalsIgnoreCase("EXANIMUS")) {
// Adds in Compat for older GT Versions which Misspell aspects.
try {
if (EnumUtils.isValidEnum(TCAspects.class, "EXANIMUS")) {
Logger.WARNING("TC Aspect found - " + aspect);
returnValue = new TC_AspectStack(TCAspects.valueOf("EXANIMUS"), size);
} else {
Logger.INFO(
"Fallback TC Aspect found - " + aspect
+ " - PLEASE UPDATE GREGTECH TO A NEWER VERSION TO REMOVE THIS MESSAGE - THIS IS NOT AN ERROR");
returnValue = new TC_AspectStack(TCAspects.valueOf("EXAMINIS"), size);
}
} catch (final NoSuchFieldError r) {
Logger.INFO("Invalid Thaumcraft Aspects - Report this issue to Alkalus");
}
} else if (aspect.equalsIgnoreCase("PRAECANTATIO")) {
// Adds in Compat for older GT Versions which Misspell aspects.
try {
if (EnumUtils.isValidEnum(TCAspects.class, "PRAECANTATIO")) {
Logger.WARNING("TC Aspect found - " + aspect);
returnValue = new TC_AspectStack(TCAspects.valueOf("PRAECANTATIO"), size);
} else {
Logger.INFO(
"Fallback TC Aspect found - " + aspect
+ " - PLEASE UPDATE GREGTECH TO A NEWER VERSION TO REMOVE THIS MESSAGE - THIS IS NOT AN ERROR");
returnValue = new TC_AspectStack(TCAspects.valueOf("PRAECANTIO"), size);
}
} catch (final NoSuchFieldError r) {
Logger.INFO("Invalid Thaumcraft Aspects - Report this issue to Alkalus");
}
} else {
Logger.WARNING("TC Aspect found - " + aspect);
returnValue = new TC_AspectStack(TCAspects.valueOf(aspect), size);
}
return returnValue;
}
// Send a message to all players on the server
public static void sendServerMessage(final String translationKey) {
sendServerMessage(new ChatComponentText(translationKey));
}
// Send a message to all players on the server
public static void sendServerMessage(final IChatComponent chatComponent) {
MinecraftServer.getServer()
.getConfigurationManager()
.sendChatMsg(chatComponent);
}
/**
* Returns if that Liquid is IC2Steam.
*/
public static boolean isIC2Steam(final FluidStack aFluid) {
if (aFluid == null) {
return false;
}
return aFluid.isFluidEqual(getIC2Steam(1));
}
/**
* Returns a Liquid Stack with given amount of IC2Steam.
*/
public static FluidStack getIC2Steam(final long aAmount) {
return FluidRegistry.getFluidStack("ic2steam", (int) aAmount);
}
public static int rgbtoHexValue(final int r, final int g, final int b) {
if ((r > 255) || (g > 255) || (b > 255) || (r < 0) || (g < 0) || (b < 0)) {
return 0;
}
final Color c = new Color(r, g, b);
String temp = Integer.toHexString(c.getRGB() & 0xFFFFFF)
.toUpperCase();
temp = Utils.appenedHexNotationToString(temp);
return Integer.decode(temp);
}
/*
* http://javadevnotes.com/java-left-pad-string-with-zeros-examples
*/
public static String padWithZerosLefts(final String originalString, final int length) {
final StringBuilder sb = new StringBuilder();
while ((sb.length() + originalString.length()) < length) {
sb.append('0');
}
sb.append(originalString);
return sb.toString();
}
/*
* Original Code by Chandana Napagoda - https://cnapagoda.blogspot.com.au/2011/03/java-hex-color-code-generator.
* html
*/
public static Map<Integer, String> hexColourGeneratorRandom(final int colorCount) {
final HashMap<Integer, String> hexColorMap = new HashMap<>();
for (int a = 0; a < colorCount; a++) {
String code = "" + (int) (Math.random() * 256);
code = code + code + code;
final int i = Integer.parseInt(code);
hexColorMap.put(
a,
Integer.toHexString(0x1000000 | i)
.substring(1)
.toUpperCase());
Logger.WARNING(
Integer.toHexString(0x1000000 | i)
.substring(1)
.toUpperCase());
}
return hexColorMap;
}
public static String appenedHexNotationToString(final Object hexAsStringOrInt) {
final String hexChar = "0x";
String result;
if (hexAsStringOrInt.getClass() == String.class) {
if (((String) hexAsStringOrInt).length() != 6) {
final String temp = padWithZerosLefts((String) hexAsStringOrInt, 6);
}
result = hexChar + hexAsStringOrInt;
return result;
} else if (hexAsStringOrInt.getClass() == Integer.class) {
String aa = String.valueOf(hexAsStringOrInt);
if (aa.length() != 6) {
result = padWithZerosLefts(aa, 6);
} else {
result = hexChar + hexAsStringOrInt;
}
return result;
} else {
return null;
}
}
public static File getMcDir() {
if (Utils.isClient()) {
if (Minecraft.getMinecraft() != null) {
return Minecraft.getMinecraft().mcDataDir;
}
}
return new File(".");
}
private static short cellID = 15;
public static ItemStack createInternalNameAndFluidCell(final String s) {
Logger.WARNING("1");
final InternalName yourName = EnumHelper.addEnum(InternalName.class, s, new Class[0], new Object[0]);
Logger.WARNING("2 " + yourName.name());
final ItemCell item = (ItemCell) Ic2Items.cell.getItem();
Logger.WARNING("3 " + item.getUnlocalizedName());
try {
Logger.WARNING("4");
final Class<? extends ItemCell> clz = item.getClass();
Logger.WARNING("5 " + clz.getSimpleName());
final Method methode = clz.getDeclaredMethod("addCell", int.class, InternalName.class, Block[].class);
Logger.WARNING("6 " + methode.getName());
methode.setAccessible(true);
Logger.WARNING("7 " + methode.isAccessible());
final ItemStack temp = (ItemStack) methode.invoke(item, cellID++, yourName, new Block[0]);
Logger.WARNING("Successfully created " + temp.getDisplayName() + "s.");
FluidContainerRegistry.registerFluidContainer(
FluidUtils.getFluidStack(s.toLowerCase(), 1000),
temp.copy(),
Ic2Items.cell.copy());
ItemUtils.addItemToOreDictionary(temp.copy(), "cell" + s);
return temp;
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}
public static String sanitizeString(final String input, final char[] dontRemove) {
// List of characters to remove
final HashSet<Character> toRemoveSet = new HashSet<>();
Collections.addAll(
toRemoveSet,
' ',
'-',
'_',
'~',
'?',
'!',
'@',
'#',
'$',
'%',
'^',
'&',
'*',
'(',
')',
'{',
'}',
'[',
']');
// Remove characters from the toRemoveSet if they are in dontRemove
for (char e : dontRemove) {
toRemoveSet.remove(e);
}
// Construct a sanitized string
StringBuilder sanitized = new StringBuilder();
for (char c : input.toCharArray()) {
if (!toRemoveSet.contains(c)) {
sanitized.append(c);
}
}
return sanitized.toString();
}
public static String sanitizeString(final String input) {
String temp;
String output;
temp = input.replace(" ", "");
temp = temp.replace("-", "");
temp = temp.replace("_", "");
temp = temp.replace("?", "");
temp = temp.replace("!", "");
temp = temp.replace("@", "");
temp = temp.replace("#", "");
temp = temp.replace("(", "");
temp = temp.replace(")", "");
temp = temp.replace("{", "");
temp = temp.replace("}", "");
temp = temp.replace("[", "");
temp = temp.replace("]", "");
temp = temp.replace(" ", "");
output = temp;
return output;
}
public static String sanitizeStringKeepBrackets(final String input) {
String temp;
String output;
temp = input.replace(" ", "");
temp = temp.replace("-", "");
temp = temp.replace("_", "");
temp = temp.replace("?", "");
temp = temp.replace("!", "");
temp = temp.replace("@", "");
temp = temp.replace("#", "");
temp = temp.replace(" ", "");
output = temp;
return output;
}
public static String addBookTitleLocalization(final String aTitle) {
return GTLanguageManager
.addStringLocalization("Book." + aTitle + ".Name", aTitle, !GregTechAPI.sPostloadFinished);
}
public static String[] addBookPagesLocalization(final String aTitle, final String[] aPages) {
String[] aLocalizationPages = new String[aPages.length];
for (byte i = 0; i < aPages.length; i = (byte) (i + 1)) {
aLocalizationPages[i] = GTLanguageManager.addStringLocalization(
"Book." + aTitle + ".Page" + ((i < 10) ? "0" + i : Byte.valueOf(i)),
aPages[i],
!GregTechAPI.sPostloadFinished);
}
return aLocalizationPages;
}
public static ItemStack getWrittenBook(ItemStack book, int ID, String mapping, String title, String author,
String[] pages) {
if (GTUtility.isStringInvalid(mapping)) {
return null;
}
ItemStack stack = GTPPCore.sBookList.get(mapping);
if (stack != null) {
return GTUtility.copyAmount(1L, stack);
}
if (GTUtility.isStringInvalid(title) || GTUtility.isStringInvalid(author) || pages.length <= 0) {
return null;
}
stack = (book == null) ? new ItemStack(ModItems.itemCustomBook, 1, ID) : book;
NBTTagCompound NBT = new NBTTagCompound();
String localizationTitle = addBookTitleLocalization(title);
NBT.setString("title", localizationTitle);
NBT.setString("author", author);
NBTTagList NBTList = new NBTTagList();
String[] localizationPages = addBookPagesLocalization(title, pages);
for (byte i = 0; i < pages.length; i++) {
pages[i] = localizationPages[i].replaceAll("<BR>", "\n");
if (i < 48) {
if (pages[i].length() < 256) {
NBTList.appendTag(new NBTTagString(pages[i]));
} else {
Logger.INFO("WARNING: String for written Book too long! -> " + pages[i]);
GTLog.err.println("WARNING: String for written Book too long! -> " + pages[i]);
}
} else {
Logger.INFO("WARNING: Too much Pages for written Book! -> " + title);
GTLog.err.println("WARNING: Too much Pages for written Book! -> " + title);
break;
}
}
String credits = String.format(
"Credits to %s for writing this Book. This was Book Nr. %d at its creation. Gotta get 'em all!",
author,
ID);
NBTList.appendTag(new NBTTagString(credits));
NBT.setTag("pages", NBTList);
stack.setTagCompound(NBT);
String logMessage = String.format(
"GT++_Mod: Added Book to Book++ List - Mapping: '%s' - Name: '%s' - Author: '%s'",
mapping,
title,
author);
GTLog.out.println(logMessage);
NBTUtils.createIntegerTagCompound(stack, "stats", "mMeta", ID);
GTPPCore.sBookList.put(mapping, stack);
Logger.INFO(String.format("Creating book: %s by %s. Using Meta %d.", title, author, ID));
return GTUtility.copy(stack);
}
}
|