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
|
package gregtech.common.covers.redstone;
import java.util.UUID;
import javax.annotation.Nonnull;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
import com.google.common.io.ByteArrayDataInput;
import com.gtnewhorizons.modularui.api.screen.ModularWindow;
import com.gtnewhorizons.modularui.common.widget.TextWidget;
import gregtech.api.gui.modularui.CoverUIBuildContext;
import gregtech.api.gui.modularui.GTUITextures;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.tileentity.ICoverable;
import gregtech.api.interfaces.tileentity.IMachineProgress;
import gregtech.api.util.GTUtility;
import gregtech.api.util.ISerializableObject;
import gregtech.common.gui.modularui.widget.CoverDataControllerWidget;
import gregtech.common.gui.modularui.widget.CoverDataFollowerToggleButtonWidget;
import io.netty.buffer.ByteBuf;
public class CoverWirelessDoesWorkDetector
extends CoverAdvancedRedstoneTransmitterBase<CoverWirelessDoesWorkDetector.ActivityTransmitterData> {
public CoverWirelessDoesWorkDetector(ITexture coverTexture) {
super(ActivityTransmitterData.class, coverTexture);
}
@Override
public ActivityTransmitterData createDataObject() {
return new ActivityTransmitterData();
}
@Override
public ActivityTransmitterData createDataObject(int aLegacyData) {
return createDataObject();
}
private static byte computeSignalBasedOnActivity(ActivityTransmitterData coverVariable, ICoverable tileEntity) {
if (tileEntity instanceof IMachineProgress mProgress) {
boolean inverted = coverVariable.invert;
int signal = 0;
switch (coverVariable.mode) {
case MACHINE_ENABLED -> signal = inverted == mProgress.isAllowedToWork() ? 0 : 15;
case MACHINE_IDLE -> signal = inverted == (mProgress.getMaxProgress() == 0) ? 0 : 15;
case RECIPE_PROGRESS -> {
int tScale = mProgress.getMaxProgress() / 15;
if (tScale > 0 && mProgress.hasThingsToDo()) {
signal = inverted ? (15 - mProgress.getProgress() / tScale)
: (mProgress.getProgress() / tScale);
} else {
signal = inverted ? 15 : 0;
}
}
}
return (byte) signal;
} else {
return (byte) 0;
}
}
@Override
public ActivityTransmitterData doCoverThingsImpl(ForgeDirection side, byte aInputRedstone, int aCoverID,
ActivityTransmitterData aCoverVariable, ICoverable aTileEntity, long aTimer) {
final byte signal = computeSignalBasedOnActivity(aCoverVariable, aTileEntity);
final long hash = hashCoverCoords(aTileEntity, side);
setSignalAt(aCoverVariable.getUuid(), aCoverVariable.getFrequency(), hash, signal);
return aCoverVariable;
}
@Override
public boolean letsRedstoneGoOutImpl(ForgeDirection side, int aCoverID, ActivityTransmitterData aCoverVariable,
ICoverable aTileEntity) {
return true;
}
@Override
protected boolean manipulatesSidedRedstoneOutputImpl(ForgeDirection side, int aCoverID,
ActivityTransmitterData aCoverVariable, ICoverable aTileEntity) {
return true;
}
@Override
public int getTickRateImpl(ForgeDirection side, int aCoverID, ActivityTransmitterData aCoverVariable,
ICoverable aTileEntity) {
return 5;
}
public enum ActivityMode {
RECIPE_PROGRESS,
MACHINE_IDLE,
MACHINE_ENABLED,
}
public static class ActivityTransmitterData extends CoverAdvancedRedstoneTransmitterBase.TransmitterData {
private ActivityMode mode;
public ActivityTransmitterData(int frequency, UUID uuid, boolean invert, ActivityMode mode) {
super(frequency, uuid, invert);
this.mode = mode;
}
public ActivityTransmitterData() {
super();
this.mode = ActivityMode.MACHINE_IDLE;
}
@Nonnull
@Override
public ISerializableObject copy() {
return new ActivityTransmitterData(frequency, uuid, invert, mode);
}
@Nonnull
@Override
public NBTBase saveDataToNBT() {
NBTTagCompound tag = (NBTTagCompound) super.saveDataToNBT();
tag.setInteger("mode", mode.ordinal());
return tag;
}
@Override
public void writeToByteBuf(ByteBuf aBuf) {
super.writeToByteBuf(aBuf);
aBuf.writeInt(mode.ordinal());
}
@Override
public void loadDataFromNBT(NBTBase aNBT) {
super.loadDataFromNBT(aNBT);
NBTTagCompound tag = (NBTTagCompound) aNBT;
mode = ActivityMode.values()[tag.getInteger("mode")];
}
@Nonnull
@Override
public ISerializableObject readFromPacket(ByteArrayDataInput aBuf, EntityPlayerMP aPlayer) {
super.readFromPacket(aBuf, aPlayer);
mode = ActivityMode.values()[aBuf.readInt()];
return this;
}
}
@Override
public ModularWindow createWindow(CoverUIBuildContext buildContext) {
return new WirelessActivityDetectorUIFactory(buildContext).createWindow();
}
private class WirelessActivityDetectorUIFactory extends AdvancedRedstoneTransmitterBaseUIFactory {
public WirelessActivityDetectorUIFactory(CoverUIBuildContext buildContext) {
super(buildContext);
}
@Override
protected int getGUIHeight() {
return 107;
}
@Override
protected int getFrequencyRow() {
return 0;
}
@Override
protected int getButtonRow() {
return 1;
}
@Override
protected void addUIWidgets(ModularWindow.Builder builder) {
super.addUIWidgets(builder);
builder.widget(TextWidget.dynamicString(() -> {
ActivityMode mode = getCoverData().mode;
if (mode == ActivityMode.MACHINE_ENABLED) {
return GTUtility.trans("271", "Machine enabled");
} else if (mode == ActivityMode.MACHINE_IDLE) {
return GTUtility.trans("242", "Machine idle");
} else {
return GTUtility.trans("241", "Recipe progress");
}
})
.setSynced(false)
.setDefaultColor(COLOR_TEXT_GRAY.get())
.setPos(startX + spaceX * 3, 4 + startY + spaceY * 2));
}
@Override
protected void addUIForDataController(CoverDataControllerWidget<ActivityTransmitterData> controller) {
super.addUIForDataController(controller);
controller.addFollower(
CoverDataFollowerToggleButtonWidget.ofDisableable(),
coverData -> coverData.mode == ActivityMode.RECIPE_PROGRESS,
(coverData, state) -> {
coverData.mode = ActivityMode.RECIPE_PROGRESS;
return coverData;
},
widget -> widget.setStaticTexture(GTUITextures.OVERLAY_BUTTON_PROGRESS)
.addTooltip(GTUtility.trans("241", "Recipe progress"))
.setPos(spaceX * 0, spaceY * 2))
.addFollower(
CoverDataFollowerToggleButtonWidget.ofDisableable(),
coverData -> coverData.mode == ActivityMode.MACHINE_IDLE,
(coverData, state) -> {
coverData.mode = ActivityMode.MACHINE_IDLE;
return coverData;
},
widget -> widget.setStaticTexture(GTUITextures.OVERLAY_BUTTON_CHECKMARK)
.addTooltip(GTUtility.trans("242", "Machine idle"))
.setPos(spaceX * 1, spaceY * 2))
.addFollower(
CoverDataFollowerToggleButtonWidget.ofDisableable(),
coverData -> coverData.mode == ActivityMode.MACHINE_ENABLED,
(coverData, state) -> {
coverData.mode = ActivityMode.MACHINE_ENABLED;
return coverData;
},
widget -> widget.setStaticTexture(GTUITextures.OVERLAY_BUTTON_POWER_SWITCH_ON)
.addTooltip(GTUtility.trans("271", "Machine enabled"))
.setPos(spaceX * 2, spaceY * 2));
}
}
}
|