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
|
package miscutil.core.util;
import static gregtech.api.enums.GT_Values.F;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import miscutil.core.lib.CORE;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.registry.GameRegistry;
public class Utils {
public static final int WILDCARD_VALUE = Short.MAX_VALUE;
/**
* Returns a psuedo-random number between min and max, inclusive.
* The difference between min and max can be at most
* <code>Integer.MAX_VALUE - 1</code>.
*
* @param min Minimim value
* @param max Maximim value. Must be greater than min.
* @return Integer between min and max, inclusive.
* @see java.util.Random#nextInt(int)
*/
public static int randInt(int min, int max) {
// Usually this can be a field rather than a method variable
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public static long randLong(long min, long max) {
// Usually this can be a field rather than a method variable
Random rand = new Random();
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
long randomNum = nextLong(rand,(max - min) + 1) + min;
return randomNum;
}
private static long nextLong(Random rng, long n) {
// error checking and 2^x checking removed for simplicity.
long bits, val;
do {
bits = (rng.nextLong() << 1) >>> 1;
val = bits % n;
} while (bits-val+(n-1) < 0L);
return val;
}
public static boolean containsMatch(boolean strict, ItemStack[] inputs, ItemStack... targets)
{
for (ItemStack input : inputs)
{
for (ItemStack target : targets)
{
if (itemMatches(target, input, strict))
{
return true;
}
}
}
return false;
}
public static boolean itemMatches(ItemStack target, ItemStack input, boolean strict)
{
if (input == null && target != null || input != null && target == null)
{
return false;
}
return (target.getItem() == input.getItem() && ((target.getItemDamage() == WILDCARD_VALUE && !strict) || target.getItemDamage() == input.getItemDamage()));
}
//Non-Dev Comments
public static void LOG_INFO(String s){
//if (CORE.DEBUG){
FMLLog.info("MiscUtils: "+s);
//}
}
//Developer Comments
public static void LOG_WARNING(String s){
if (CORE.DEBUG){
FMLLog.warning("MiscUtils: "+s);
}
}
//Errors
public static void LOG_ERROR(String s){
if (CORE.DEBUG){
FMLLog.severe("MiscUtils: "+s);
}
}
public static void paintBox(Graphics g, int MinA, int MinB, int MaxA, int MaxB){
g.drawRect (MinA, MinB, MaxA, MaxB);
}
public static void messagePlayer(EntityPlayer P, String S){
gregtech.api.util.GT_Utility.sendChatToPlayer(P, S);
}
/**
* Returns if that Liquid is IC2Steam.
*/
public static boolean isIC2Steam(FluidStack aFluid) {
if (aFluid == null) return F;
return aFluid.isFluidEqual(getIC2Steam(1));
}
/**
* Returns a Liquid Stack with given amount of IC2Steam.
*/
public static FluidStack getIC2Steam(long aAmount) {
return FluidRegistry.getFluidStack("ic2steam", (int)aAmount);
}
public static Item getItem(String fqrn) // fqrn = fully qualified resource name
{
String[] fqrnSplit = fqrn.split(":");
return GameRegistry.findItem(fqrnSplit[0], fqrnSplit[1]);
}
public static ItemStack getItemStack(String fqrn, int Size) // fqrn = fully qualified resource name
{
String[] fqrnSplit = fqrn.split(":");
return GameRegistry.findItemStack(fqrnSplit[0], fqrnSplit[1], Size);
}
public static Item getItemInPlayersHand(){
Minecraft mc = Minecraft.getMinecraft();
Item heldItem = null;
try{heldItem = mc.thePlayer.getHeldItem().getItem();
}catch(NullPointerException e){return null;}
if (heldItem != null){
return heldItem;
}
return null;
}
/*public static void recipeBuilderBlock(ItemStack slot_1, ItemStack slot_2, ItemStack slot_3, ItemStack slot_4, ItemStack slot_5, ItemStack slot_6, ItemStack slot_7, ItemStack slot_8, ItemStack slot_9, Block resultBlock){
GameRegistry.addRecipe(new ItemStack(resultBlock),
new Object[] {"ABC", "DEF", "GHI",
'A',slot_1,'B',slot_2,'C',slot_3,
'D',slot_4,'E',slot_5,'F',slot_6,
'G',slot_7,'H',slot_8,'I',slot_9
});
}*/
public static String checkCorrectMiningToolForBlock(Block currentBlock, World currentWorld){
String correctTool = "";
if (!currentWorld.isRemote){
try {
correctTool = currentBlock.getHarvestTool(0);
Utils.LOG_WARNING(correctTool);
} catch (NullPointerException e){
}
}
return correctTool;
}
/**
*
* @param colorStr e.g. "#FFFFFF"
* @return String - formatted "rgb(0,0,0)"
*/
public static String hex2Rgb(String hexString) {
Color c = new Color(
Integer.valueOf(hexString.substring(1, 3), 16),
Integer.valueOf(hexString.substring(3, 5), 16),
Integer.valueOf(hexString.substring(5, 7), 16));
StringBuffer sb = new StringBuffer();
sb.append("rgb(");
sb.append(c.getRed());
sb.append(",");
sb.append(c.getGreen());
sb.append(",");
sb.append(c.getBlue());
sb.append(")");
return sb.toString();
}
}
|