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
|
package gtPlusPlus.xmod.gregtech.common.covers;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import gregtech.api.interfaces.tileentity.ICoverable;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.objects.XSTR;
import gregtech.api.util.CoverBehavior;
import gregtech.api.util.GTUtility;
import gregtech.api.util.ISerializableObject;
import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.api.objects.minecraft.BlockPos;
import gtPlusPlus.core.util.minecraft.PlayerUtils;
public class CoverToggleVisual extends CoverBehavior {
private static final Map<String, Integer> sConnectionStateForEntityMap = new ConcurrentHashMap<>();
private static final Map<String, String> sPrefixMap = new ConcurrentHashMap<>();
private static final int VALUE_OFF = 0;
private static final int VALUE_ON = 1;
public static String generateUniqueKey(ForgeDirection side, ICoverable aEntity) {
try {
BlockPos aPos = new BlockPos(
aEntity.getIGregTechTileEntity(aEntity.getXCoord(), aEntity.getYCoord(), aEntity.getZCoord()));
String s = aEntity.getInventoryName() + "." + aPos.getUniqueIdentifier() + side.name();
return s;
} catch (Throwable t) {}
XSTR x = new XSTR();
return "ERROR." + x.getSeed() + x.hashCode() + x.nextDouble() + ".ID";
}
public boolean onCoverRightclick(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity,
EntityPlayer aPlayer, float aX, float aY, float aZ) {
PlayerUtils
.messagePlayer(aPlayer, GTUtility.trans("756", "Connectable: ") + getConnectionState(aCoverVariable));
return super.onCoverRightclick(side, aCoverID, aCoverVariable, aTileEntity, aPlayer, aX, aY, aZ);
}
public int onCoverScrewdriverclick(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity,
EntityPlayer aPlayer, float aX, float aY, float aZ) {
return super.onCoverScrewdriverclick(side, aCoverID, aCoverVariable, aTileEntity, aPlayer, aX, aY, aZ);
}
public boolean letsEnergyIn(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity) {
return getConnectionState(aCoverVariable);
}
public boolean letsEnergyOut(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity) {
return getConnectionState(aCoverVariable);
}
public boolean letsFluidIn(ForgeDirection side, int aCoverID, int aCoverVariable, Fluid aFluid,
ICoverable aTileEntity) {
return getConnectionState(aCoverVariable);
}
public boolean letsFluidOut(ForgeDirection side, int aCoverID, int aCoverVariable, Fluid aFluid,
ICoverable aTileEntity) {
return getConnectionState(aCoverVariable);
}
public boolean letsItemsIn(ForgeDirection side, int aCoverID, int aCoverVariable, int aSlot,
ICoverable aTileEntity) {
return getConnectionState(aCoverVariable);
}
public boolean letsItemsOut(ForgeDirection side, int aCoverID, int aCoverVariable, int aSlot,
ICoverable aTileEntity) {
return getConnectionState(aCoverVariable);
}
public int getTickRate(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity) {
return 1;
}
@Override
public int doCoverThings(ForgeDirection side, byte aInputRedstone, int aCoverID, int aCoverVariable,
ICoverable aTileEntity, long aTimer) {
try {
String aKey = generateUniqueKey(side, aTileEntity);
Integer b = sConnectionStateForEntityMap.get(aKey);
// Logger.INFO("Val: "+aCoverVariable);
if (b != null && aCoverVariable != b) {
aCoverVariable = b;
}
if (b == null) {
b = aCoverVariable;
sConnectionStateForEntityMap.put(aKey, b);
trySetState(side, b == VALUE_ON ? VALUE_ON : VALUE_OFF, aTileEntity);
}
} catch (Throwable t) {
}
return aCoverVariable;
}
@Override
public boolean letsRedstoneGoIn(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity) {
return getConnectionState(aCoverVariable);
}
@Override
public boolean letsRedstoneGoOut(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity) {
return getConnectionState(aCoverVariable);
}
@Override
public boolean alwaysLookConnected(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity) {
return super.alwaysLookConnected(side, aCoverID, aCoverVariable, aTileEntity);
}
@Override
public byte getRedstoneInput(ForgeDirection side, byte aInputRedstone, int aCoverID, int aCoverVariable,
ICoverable aTileEntity) {
if (!getConnectionState(aCoverVariable)) {
return 0;
}
return super.getRedstoneInput(side, aInputRedstone, aCoverID, aCoverVariable, aTileEntity);
}
@Override
public void placeCover(ForgeDirection side, ItemStack aCover, ICoverable aTileEntity) {
String aKey = generateUniqueKey(side, aTileEntity);
boolean state = getCoverConnections(aCover);
sPrefixMap.put(aKey, aCover.getUnlocalizedName());
Logger.INFO("Mapping key " + aKey + " to " + state);
sConnectionStateForEntityMap.put(aKey, state ? VALUE_ON : VALUE_OFF);
Logger.INFO("Key Value: " + (state ? VALUE_ON : VALUE_OFF));
// Try set cover state directly
// trySetState(aSide, state ? VALUE_ON : VALUE_OFF, aTileEntity);
super.placeCover(side, aCover, aTileEntity);
}
@Override
public boolean onCoverRemoval(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity,
boolean aForced) {
String aKey = generateUniqueKey(side, aTileEntity);
sConnectionStateForEntityMap.remove(aKey);
// Logger.INFO("Unmapping key "+aKey+".");
return true;
}
public static boolean getConnectionState(int aCoverVar) {
return aCoverVar == VALUE_ON;
}
private static void trySetState(ForgeDirection side, int aState, ICoverable aTile) {
// Try set cover state directly
if (aTile instanceof IGregTechTileEntity gTileEntity) {
gTileEntity.setCoverDataAtSide(side, new ISerializableObject.LegacyCoverData(aState));
}
}
public static boolean getConnectionState(ForgeDirection side, ICoverable aTile) {
String aKey = generateUniqueKey(side, aTile);
return getConnectionState(aKey);
}
public static boolean getConnectionState(String aKey) {
Integer b = sConnectionStateForEntityMap.get(aKey);
// Logger.INFO("Get State: "+b+" | "+aKey);
return b != null ? b == VALUE_ON : false;
}
public static final boolean getCoverConnections(final ItemStack aStack) {
NBTTagCompound aNBT = aStack.getTagCompound();
if (aNBT != null) {
aNBT = aNBT.getCompoundTag("CustomCoverMeta");
if (aNBT != null) {
return aNBT.getBoolean("AllowConnections");
}
}
return false;
}
}
|