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
|
/*
* spotless:off
* KubaTech - Gregtech Addon
* Copyright (C) 2022 - 2024 kuba6000
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <https://www.gnu.org/licenses/>.
* spotless:on
*/
package kubatech.api.implementations;
import static gregtech.api.metatileentity.BaseTileEntity.TOOLTIP_DELAY;
import static kubatech.api.Variables.ln2;
import static kubatech.api.Variables.ln4;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumChatFormatting;
import net.minecraftforge.fluids.FluidStack;
import com.gtnewhorizons.modularui.api.drawable.IDrawable;
import com.gtnewhorizons.modularui.api.drawable.Text;
import com.gtnewhorizons.modularui.api.drawable.UITexture;
import com.gtnewhorizons.modularui.api.math.Color;
import com.gtnewhorizons.modularui.api.math.MainAxisAlignment;
import com.gtnewhorizons.modularui.api.math.Pos2d;
import com.gtnewhorizons.modularui.api.screen.ITileWithModularUI;
import com.gtnewhorizons.modularui.api.screen.ModularUIContext;
import com.gtnewhorizons.modularui.api.screen.ModularWindow;
import com.gtnewhorizons.modularui.api.screen.UIBuildContext;
import com.gtnewhorizons.modularui.api.widget.Widget;
import com.gtnewhorizons.modularui.common.builder.UIBuilder;
import com.gtnewhorizons.modularui.common.builder.UIInfo;
import com.gtnewhorizons.modularui.common.internal.wrapper.ModularGui;
import com.gtnewhorizons.modularui.common.internal.wrapper.ModularUIContainer;
import com.gtnewhorizons.modularui.common.widget.Column;
import com.gtnewhorizons.modularui.common.widget.DrawableWidget;
import com.gtnewhorizons.modularui.common.widget.DynamicPositionedColumn;
import com.gtnewhorizons.modularui.common.widget.DynamicPositionedRow;
import com.gtnewhorizons.modularui.common.widget.SlotWidget;
import gregtech.api.enums.GT_Values;
import gregtech.api.gui.modularui.GT_UITextures;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.metatileentity.BaseMetaTileEntity;
import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_ExtendedPowerMultiBlockBase;
import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_Hatch_OutputBus;
import gregtech.common.tileentities.machines.GT_MetaTileEntity_Hatch_OutputBus_ME;
import kubatech.Tags;
public abstract class KubaTechGTMultiBlockBase<T extends GT_MetaTileEntity_ExtendedPowerMultiBlockBase<T>>
extends GT_MetaTileEntity_ExtendedPowerMultiBlockBase<T> {
@Deprecated
public final int mEUt = 0;
@SuppressWarnings("unchecked")
protected static <K extends KubaTechGTMultiBlockBase<?>> UIInfo<?, ?> createKTMetaTileEntityUI(
KTContainerConstructor<K> containerConstructor) {
return UIBuilder.of()
.container((player, world, x, y, z) -> {
TileEntity te = world.getTileEntity(x, y, z);
if (te instanceof BaseMetaTileEntity) {
IMetaTileEntity mte = ((BaseMetaTileEntity) te).getMetaTileEntity();
if (!(mte instanceof KubaTechGTMultiBlockBase)) return null;
final UIBuildContext buildContext = new UIBuildContext(player);
final ModularWindow window = ((ITileWithModularUI) te).createWindow(buildContext);
return containerConstructor.of(new ModularUIContext(buildContext, te::markDirty), window, (K) mte);
}
return null;
})
.gui(((player, world, x, y, z) -> {
if (!world.isRemote) return null;
TileEntity te = world.getTileEntity(x, y, z);
if (te instanceof BaseMetaTileEntity) {
IMetaTileEntity mte = ((BaseMetaTileEntity) te).getMetaTileEntity();
if (!(mte instanceof KubaTechGTMultiBlockBase)) return null;
final UIBuildContext buildContext = new UIBuildContext(player);
final ModularWindow window = ((ITileWithModularUI) te).createWindow(buildContext);
return new ModularGui(
containerConstructor.of(new ModularUIContext(buildContext, null), window, (K) mte));
}
return null;
}))
.build();
}
@FunctionalInterface
protected interface KTContainerConstructor<T extends KubaTechGTMultiBlockBase<?>> {
ModularUIContainer of(ModularUIContext context, ModularWindow mainWindow, T multiBlock);
}
protected KubaTechGTMultiBlockBase(int aID, String aName, String aNameRegional) {
super(aID, aName, aNameRegional);
}
protected KubaTechGTMultiBlockBase(String aName) {
super(aName);
}
/**
* Enables infinite overclocking (will give more outputs with more energy past 1 tick) Currently doesn't support
* recipe inputs
*
* @return If this supports infinite overclock
*/
protected boolean isOverclockingInfinite() {
return false;
}
/**
* @return The minimum amount of ticks this multiblock can overclock to
*/
protected int getOverclockTimeLimit() {
return 1;
}
@Override
protected void calculateOverclockedNessMultiInternal(long aEUt, int aDuration, int mAmperage, long maxInputVoltage,
boolean perfectOC) {
calculateOverclock(aEUt, aDuration, getMaxInputEu(), perfectOC);
}
/**
* @param aEUt Recipe EU/t
* @param aDuration Recipe duration (in ticks)
* @param maxInputEU The amount of energy we want to overclock to
* @param isPerfect Is this overclock perfect ?
* @return The amount of overclocks
*/
protected int calculateOverclock(long aEUt, int aDuration, final long maxInputEU, final boolean isPerfect) {
final int minDuration = getOverclockTimeLimit();
int tiers = (int) (Math.log((double) maxInputEU / (double) aEUt) / ln4);
if (tiers <= 0) {
this.lEUt = aEUt;
this.mMaxProgresstime = aDuration;
return 0;
}
int durationTiers = (int) Math
.ceil(Math.log((double) aDuration / (double) minDuration) / (isPerfect ? ln4 : ln2));
if (durationTiers < 0) durationTiers = 0; // We do not support downclocks (yet)
if (durationTiers > tiers) durationTiers = tiers;
if (!isOverclockingInfinite()) {
tiers = durationTiers;
if (tiers == 0) {
this.lEUt = aEUt;
this.mMaxProgresstime = aDuration;
return 0;
}
this.lEUt = aEUt << (tiers << 1);
aDuration >>= isPerfect ? (tiers << 1) : tiers;
if (aDuration < minDuration) aDuration = minDuration;
this.mMaxProgresstime = aDuration;
return tiers;
}
this.lEUt = aEUt << (tiers << 1);
aDuration >>= isPerfect ? (durationTiers << 1) : durationTiers;
int dMulti = tiers - durationTiers;
if (dMulti > 0) {
dMulti = 1 << (isPerfect ? (dMulti << 1) : dMulti);
// TODO: Use more inputs???
for (ItemStack mOutputItem : this.mOutputItems) mOutputItem.stackSize *= dMulti;
for (FluidStack mOutputFluid : this.mOutputFluids) mOutputFluid.amount *= dMulti;
}
if (aDuration < minDuration) aDuration = minDuration;
this.mMaxProgresstime = aDuration;
return tiers;
}
protected int calculateOverclock(long aEUt, int aDuration, boolean isPerfect) {
return calculateOverclock(aEUt, aDuration, getMaxInputEu(), isPerfect);
}
protected int calculateOverclock(long aEUt, int aDuration) {
return calculateOverclock(aEUt, aDuration, false);
}
protected int calculatePerfectOverclock(long aEUt, int aDuration) {
return calculateOverclock(aEUt, aDuration, true);
}
public int getVoltageTier() {
return (int) getVoltageTierExact();
}
public double getVoltageTierExact() {
return Math.log((double) getMaxInputEu() / 8d) / ln4 + 1e-8d;
}
protected boolean tryOutputAll(List<ItemStack> list) {
return tryOutputAll(list, l -> Collections.singletonList((ItemStack) l));
}
protected <Y> boolean tryOutputAll(List<Y> list, Function<Y, List<ItemStack>> mappingFunction) {
if (list == null || list.isEmpty() || mappingFunction == null) return false;
int emptySlots = 0;
boolean ignoreEmptiness = false;
for (GT_MetaTileEntity_Hatch_OutputBus i : mOutputBusses) {
if (i instanceof GT_MetaTileEntity_Hatch_OutputBus_ME) {
ignoreEmptiness = true;
break;
}
for (int j = 0; j < i.getSizeInventory(); j++)
if (i.isValidSlot(j)) if (i.getStackInSlot(j) == null) emptySlots++;
}
if (emptySlots == 0 && !ignoreEmptiness) return false;
boolean wasSomethingRemoved = false;
while (!list.isEmpty()) {
List<ItemStack> toOutputNow = mappingFunction.apply(list.get(0));
if (toOutputNow == null) {
list.remove(0);
continue;
}
if (!ignoreEmptiness && emptySlots < toOutputNow.size()) break;
emptySlots -= toOutputNow.size();
list.remove(0);
wasSomethingRemoved = true;
for (ItemStack stack : toOutputNow) {
addOutput(stack);
}
}
return wasSomethingRemoved;
}
@Override
public boolean isCorrectMachinePart(ItemStack aStack) {
return true;
}
@Override
public int getMaxEfficiency(ItemStack aStack) {
return 10000;
}
@Override
public int getDamageToComponent(ItemStack aStack) {
return 0;
}
@Override
public boolean explodesOnComponentBreak(ItemStack aStack) {
return false;
}
// UI stuff
public static final UITexture PICTURE_KUBATECH_LOGO = UITexture.fullImage(Tags.MODID, "gui/logo_13x15_dark");
@Override
public boolean useModularUI() {
return true;
}
@Override
public void addGregTechLogo(ModularWindow.Builder builder) {
builder.widget(
new DrawableWidget().setDrawable(PICTURE_KUBATECH_LOGO)
.setSize(13, 15)
.setPos(191 - 13, 86 - 15)
.addTooltip(new Text(Tags.MODNAME).color(Color.GRAY.normal))
.setTooltipShowUpDelay(TOOLTIP_DELAY));
}
protected List<SlotWidget> slotWidgets = new ArrayList<>(1);
public void createInventorySlots() {
final SlotWidget inventorySlot = new SlotWidget(inventoryHandler, 1);
inventorySlot.setBackground(GT_UITextures.SLOT_DARK_GRAY);
slotWidgets.add(inventorySlot);
}
@Override
public Pos2d getPowerSwitchButtonPos() {
return new Pos2d(174, 166 - (slotWidgets.size() * 18));
}
@Override
public void addUIWidgets(ModularWindow.Builder builder, UIBuildContext buildContext) {
builder.widget(
new DrawableWidget().setDrawable(GT_UITextures.PICTURE_SCREEN_BLACK)
.setPos(4, 4)
.setSize(190, 85));
slotWidgets.clear();
createInventorySlots();
Column slotsColumn = new Column();
for (int i = slotWidgets.size() - 1; i >= 0; i--) {
slotsColumn.widget(slotWidgets.get(i));
}
builder.widget(
slotsColumn.setAlignment(MainAxisAlignment.END)
.setPos(173, 167 - 1));
final DynamicPositionedColumn screenElements = new DynamicPositionedColumn();
drawTexts(screenElements, slotWidgets.size() > 0 ? slotWidgets.get(0) : null);
builder.widget(screenElements);
builder.widget(createPowerSwitchButton(builder))
.widget(createVoidExcessButton(builder))
.widget(createInputSeparationButton(builder))
.widget(createBatchModeButton(builder))
.widget(createLockToSingleRecipeButton(builder));
DynamicPositionedRow configurationElements = new DynamicPositionedRow();
addConfigurationWidgets(configurationElements, buildContext);
builder.widget(
configurationElements.setSpace(2)
.setAlignment(MainAxisAlignment.SPACE_BETWEEN)
.setPos(getRecipeLockingButtonPos().add(18, 0)));
}
protected void addConfigurationWidgets(DynamicPositionedRow configurationElements, UIBuildContext buildContext) {
}
protected static String voltageTooltipFormatted(int tier) {
return GT_Values.TIER_COLORS[tier] + GT_Values.VN[tier] + EnumChatFormatting.GRAY;
}
protected final Function<Widget, Boolean> isFixed = widget -> getIdealStatus() == getRepairStatus() && mMachine;
protected static final Function<Integer, IDrawable> toggleButtonTextureGetter = val -> val == 0
? GT_UITextures.OVERLAY_BUTTON_CROSS
: GT_UITextures.OVERLAY_BUTTON_CHECKMARK;
protected static final Function<Integer, IDrawable[]> toggleButtonBackgroundGetter = val -> new IDrawable[] {
val == 0 ? GT_UITextures.BUTTON_STANDARD : GT_UITextures.BUTTON_STANDARD_PRESSED };
}
|