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
|
package gtPlusPlus.core.item.circuit;
import static gregtech.api.enums.Mods.GTPlusPlus;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.EnumRarity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.IIcon;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.FakePlayer;
import org.apache.commons.lang3.tuple.Pair;
import com.gtnewhorizons.modularui.api.UIInfos;
import cpw.mods.fml.common.registry.GameRegistry;
import gregtech.api.GregTechAPI;
import gregtech.api.enums.GTValues;
import gregtech.api.interfaces.INetworkUpdatableItem;
import gregtech.api.net.GTPacketUpdateItem;
import gregtech.api.objects.XSTR;
import gregtech.api.util.GTLanguageManager;
import gregtech.api.util.GTUtility;
import gregtech.common.gui.modularui.uifactory.SelectItemUIFactory;
import gtPlusPlus.core.util.math.MathUtils;
import gtPlusPlus.core.util.minecraft.ItemUtils;
public class GTPPIntegratedCircuitItem extends Item implements INetworkUpdatableItem {
private final List<ItemStack> ALL_VARIANTS = new ArrayList<>();
private final String iconLocation;
protected final IIcon[] iconDamage = new IIcon[25];
public GTPPIntegratedCircuitItem(String unlocalizedName, String iconLocation) {
this.setHasSubtypes(true);
this.setNoRepair();
this.setMaxStackSize(64);
this.setMaxDamage(0);
this.setUnlocalizedName(unlocalizedName);
this.iconLocation = iconLocation;
GameRegistry.registerItem(this, this.getUnlocalizedName());
ALL_VARIANTS.add(new ItemStack(this, 0, 0));
for (int i = 1; i <= 24; i++) {
ItemStack aStack = new ItemStack(this, 0, i);
ALL_VARIANTS.add(aStack);
}
}
@Override
public boolean isDamageable() {
return false;
}
@Override
public void addInformation(ItemStack aStack, EntityPlayer p_77624_2_, List aList, boolean p_77624_4_) {
try {
aList.add("Configuration == " + aStack.getItemDamage());
aList.add(
GTLanguageManager
.addStringLocalization(getUnlocalizedName() + ".tooltip.0", "Right click to reconfigure"));
aList.add(
GTLanguageManager.addStringLocalization(
getUnlocalizedName() + ".tooltip.1",
"Needs a screwdriver or circuit programming tool"));
} catch (Throwable t) {
t.printStackTrace();
}
super.addInformation(aStack, p_77624_2_, aList, p_77624_4_);
}
@Override
public EnumRarity getRarity(ItemStack p_77613_1_) {
return EnumRarity.common;
}
@Override
public void getSubItems(Item aItem, CreativeTabs p_150895_2_, List aList) {
aList.add(ItemUtils.simpleMetaStack(aItem, 0, 1));
}
@Override
public boolean isRepairable() {
return false;
}
@Override
public boolean isBookEnchantable(ItemStack stack, ItemStack book) {
return false;
}
@Override
public boolean showDurabilityBar(ItemStack stack) {
return false;
}
@Override
public void registerIcons(final IIconRegister u) {
for (int i = 0; i < iconDamage.length; i++) {
this.iconDamage[i] = u.registerIcon(GTPlusPlus.ID + ":" + iconLocation + "/" + i);
}
}
@Override
public IIcon getIconFromDamage(int meta) {
return this.iconDamage[MathUtils.balance(meta, 0, 24)];
}
@Override
public boolean receive(ItemStack stack, EntityPlayerMP player, NBTTagCompound tag) {
int meta = tag.hasKey("meta", Constants.NBT.TAG_BYTE) ? tag.getByte("meta") : -1;
if (meta < 0 || meta > 24) return true;
if (!player.capabilities.isCreativeMode) {
Pair<Integer, BiFunction<ItemStack, EntityPlayerMP, ItemStack>> toolIndex = findConfiguratorInInv(player);
if (toolIndex == null) return true;
ItemStack[] mainInventory = player.inventory.mainInventory;
mainInventory[toolIndex.getKey()] = toolIndex.getValue()
.apply(mainInventory[toolIndex.getKey()], player);
}
stack.setItemDamage(meta);
return true;
}
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
// nothing on server side or fake player
if (player instanceof FakePlayer || !world.isRemote) return stack;
// check if any screwdriver
ItemStack configuratorStack;
if (player.capabilities.isCreativeMode) {
configuratorStack = null;
} else {
Pair<Integer, ?> configurator = findConfiguratorInInv(player);
if (configurator == null) {
int count;
try {
count = Integer
.parseInt(StatCollector.translateToLocal("GT5U.item.programmed_circuit.no_screwdriver.count"));
} catch (NumberFormatException e) {
player.addChatComponentMessage(
new ChatComponentText(
"Error in translation GT5U.item.programmed_circuit.no_screwdriver.count: "
+ e.getMessage()));
count = 1;
}
player.addChatComponentMessage(
new ChatComponentTranslation(
"GT5U.item.programmed_circuit.no_screwdriver." + XSTR.XSTR_INSTANCE.nextInt(count)));
return stack;
}
configuratorStack = player.inventory.mainInventory[configurator.getKey()];
}
openSelectorGui(configuratorStack, stack.getItemDamage(), player);
return stack;
}
private void openSelectorGui(ItemStack configurator, int meta, EntityPlayer player) {
UIInfos.openClientUI(
player,
buildContext -> new SelectItemUIFactory(
StatCollector.translateToLocal("GT5U.item.programmed_circuit.select.header"),
configurator,
GTPPIntegratedCircuitItem::onConfigured,
ALL_VARIANTS,
meta,
true).createWindow(buildContext));
}
private static void onConfigured(ItemStack stack) {
NBTTagCompound tag = new NBTTagCompound();
tag.setByte("meta", (byte) stack.getItemDamage());
GTValues.NW.sendToServer(new GTPacketUpdateItem(tag));
}
private static Pair<Integer, BiFunction<ItemStack, EntityPlayerMP, ItemStack>> findConfiguratorInInv(
EntityPlayer player) {
ItemStack[] mainInventory = player.inventory.mainInventory;
for (int j = 0, mainInventoryLength = mainInventory.length; j < mainInventoryLength; j++) {
ItemStack toolStack = mainInventory[j];
if (!GTUtility.isStackValid(toolStack)) continue;
for (Map.Entry<Predicate<ItemStack>, BiFunction<ItemStack, EntityPlayerMP, ItemStack>> p : GregTechAPI.sCircuitProgrammerList
.entrySet())
if (p.getKey()
.test(toolStack)) return Pair.of(j, p.getValue());
}
return null;
}
}
|