Skip to content

Commit 9b3e7fc

Browse files
committed
Fix handler init stuff
1 parent cb63af8 commit 9b3e7fc

File tree

7 files changed

+64
-52
lines changed

7 files changed

+64
-52
lines changed

src/main/java/gregtech/api/capability/impl/ItemHandlerList.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ public class ItemHandlerList implements IItemHandlerModifiable {
2020
private final Int2ObjectMap<IItemHandler> handlerBySlotIndex = new Int2ObjectOpenHashMap<>();
2121
private final Object2IntMap<IItemHandler> baseIndexOffset = new Object2IntArrayMap<>();
2222

23-
public ItemHandlerList(List<? extends IItemHandler> itemHandlerList) {
23+
public ItemHandlerList(@NotNull List<? extends @NotNull IItemHandler> itemHandlerList) {
2424
int currentSlotIndex = 0;
2525
for (IItemHandler itemHandler : itemHandlerList) {
26+
Objects.requireNonNull(itemHandler, "Handler passed to ItemHandlerList was null.");
27+
2628
if (baseIndexOffset.containsKey(itemHandler)) {
2729
throw new IllegalArgumentException("Attempted to add item handler " + itemHandler + " twice");
2830
}

src/main/java/gregtech/common/metatileentities/multi/multiblockpart/appeng/MetaTileEntityMEInputBase.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public abstract class MetaTileEntityMEInputBase<AEStackType extends IAEStack<AES
5454
public static final String WORKING_TAG = "WorkingEnabled";
5555
public static final String SYNC_HANDLER_NAME = "aeSync";
5656

57+
protected IExportOnlyAEStackList<AEStackType> aeHandler;
5758
protected GhostCircuitItemStackHandler circuitInventory;
5859
protected boolean workingEnabled = true;
5960

@@ -64,9 +65,15 @@ public MetaTileEntityMEInputBase(ResourceLocation metaTileEntityId, int tier, bo
6465

6566
@Override
6667
protected void initializeInventory() {
68+
super.initializeInventory();
69+
this.aeHandler = initializeAEHandler();
6770
this.circuitInventory = new GhostCircuitItemStackHandler(this);
6871
}
6972

73+
protected abstract @NotNull IExportOnlyAEStackList<AEStackType> initializeAEHandler();
74+
75+
public abstract @NotNull IExportOnlyAEStackList<AEStackType> getAEHandler();
76+
7077
@Override
7178
public void update() {
7279
super.update();
@@ -75,8 +82,6 @@ public void update() {
7582
}
7683
}
7784

78-
public abstract @NotNull IExportOnlyAEStackList<AEStackType> getAEHandler();
79-
8085
public boolean isAutoPull() {
8186
return getAEHandler().isAutoPull();
8287
}
@@ -359,10 +364,16 @@ public void receiveInitialSyncData(PacketBuffer buf) {
359364
this.workingEnabled = buf.readBoolean();
360365
}
361366

367+
@Override
368+
protected boolean shouldSerializeInventories() {
369+
return false;
370+
}
371+
362372
@Override
363373
public NBTTagCompound writeToNBT(NBTTagCompound data) {
364374
super.writeToNBT(data);
365375
data.setBoolean(WORKING_TAG, this.workingEnabled);
376+
this.circuitInventory.write(data);
366377
return data;
367378
}
368379

@@ -372,5 +383,6 @@ public void readFromNBT(NBTTagCompound data) {
372383
if (data.hasKey(WORKING_TAG, Constants.NBT.TAG_BYTE)) {
373384
this.workingEnabled = data.getBoolean(WORKING_TAG);
374385
}
386+
this.circuitInventory.read(data);
375387
}
376388
}

src/main/java/gregtech/common/metatileentities/multi/multiblockpart/appeng/MetaTileEntityMEInputBus.java

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import gregtech.client.renderer.texture.Textures;
1818
import gregtech.common.metatileentities.multi.multiblockpart.appeng.slot.ExportOnlyAEItemList;
1919
import gregtech.common.metatileentities.multi.multiblockpart.appeng.slot.ExportOnlyAEItemSlot;
20+
import gregtech.common.metatileentities.multi.multiblockpart.appeng.slot.IExportOnlyAEStackList;
2021
import gregtech.common.metatileentities.multi.multiblockpart.appeng.stack.WrappedItemStack;
2122

2223
import net.minecraft.client.resources.I18n;
@@ -56,9 +57,7 @@ public class MetaTileEntityMEInputBus extends MetaTileEntityMEInputBase<IAEItemS
5657

5758
public static final String ITEM_BUFFER_TAG = "ItemSlots";
5859

59-
protected ExportOnlyAEItemList aeItemHandler;
6060
protected NotifiableItemStackHandler extraSlotInventory;
61-
private ItemHandlerList actualImportItems;
6261

6362
public MetaTileEntityMEInputBus(ResourceLocation metaTileEntityId, int tier) {
6463
super(metaTileEntityId, tier, false, IItemStorageChannel.class);
@@ -69,27 +68,22 @@ public MetaTileEntity createMetaTileEntity(IGregTechTileEntity iGregTechTileEnti
6968
return new MetaTileEntityMEInputBus(metaTileEntityId, getTier());
7069
}
7170

72-
public @NotNull ExportOnlyAEItemList getAEHandler() {
73-
if (aeItemHandler == null) {
74-
aeItemHandler = new ExportOnlyAEItemList(this, CONFIG_SIZE, this.getController());
75-
}
76-
77-
return aeItemHandler;
78-
}
79-
8071
@Override
8172
protected void initializeInventory() {
8273
super.initializeInventory();
83-
this.aeItemHandler = getAEHandler();
8474
this.extraSlotInventory = new NotifiableItemStackHandler(this, 1, this, false);
8575
this.extraSlotInventory.addNotifiableMetaTileEntity(this);
86-
this.actualImportItems = new ItemHandlerList(
87-
Arrays.asList(this.aeItemHandler, this.circuitInventory, this.extraSlotInventory));
88-
this.importItems = this.actualImportItems;
76+
this.importItems = new ItemHandlerList(
77+
Arrays.asList(getAEHandler(), this.circuitInventory, this.extraSlotInventory));
8978
}
9079

91-
public IItemHandlerModifiable getImportItems() {
92-
return this.actualImportItems;
80+
@Override
81+
protected @NotNull IExportOnlyAEStackList<IAEItemStack> initializeAEHandler() {
82+
return new ExportOnlyAEItemList(this, CONFIG_SIZE, this.getController());
83+
}
84+
85+
public @NotNull ExportOnlyAEItemList getAEHandler() {
86+
return (ExportOnlyAEItemList) aeHandler;
9387
}
9488

9589
@Override
@@ -103,7 +97,7 @@ public void clearMachineInventory(@NotNull List<@NotNull ItemStack> itemBuffer)
10397
@Override
10498
public void addToMultiBlock(MultiblockControllerBase controllerBase) {
10599
super.addToMultiBlock(controllerBase);
106-
for (IItemHandler handler : this.actualImportItems.getBackingHandlers()) {
100+
for (IItemHandler handler : ((ItemHandlerList) this.importItems).getBackingHandlers()) {
107101
if (handler instanceof INotifiableHandler notifiable) {
108102
notifiable.addNotifiableMetaTileEntity(controllerBase);
109103
notifiable.addToNotifiedList(this, handler, false);
@@ -114,7 +108,7 @@ public void addToMultiBlock(MultiblockControllerBase controllerBase) {
114108
@Override
115109
public void removeFromMultiBlock(MultiblockControllerBase controllerBase) {
116110
super.removeFromMultiBlock(controllerBase);
117-
for (IItemHandler handler : this.actualImportItems.getBackingHandlers()) {
111+
for (IItemHandler handler : ((ItemHandlerList) this.importItems).getBackingHandlers()) {
118112
if (handler instanceof INotifiableHandler notifiable) {
119113
notifiable.removeNotifiableMetaTileEntity(controllerBase);
120114
}
@@ -190,7 +184,6 @@ public NBTTagCompound writeToNBT(NBTTagCompound data) {
190184
}
191185
data.setTag(ITEM_BUFFER_TAG, slots);
192186

193-
this.circuitInventory.write(data);
194187
GTUtility.writeItems(this.extraSlotInventory, "ExtraInventory", data);
195188

196189
return data;
@@ -209,7 +202,6 @@ public void readFromNBT(NBTTagCompound data) {
209202
}
210203
}
211204

212-
this.circuitInventory.read(data);
213205
GTUtility.readItems(this.extraSlotInventory, "ExtraInventory", data);
214206
this.importItems = createImportItemHandler();
215207
}
@@ -245,7 +237,7 @@ public MultiblockAbility<IItemHandlerModifiable> getAbility() {
245237

246238
@Override
247239
public void registerAbilities(@NotNull AbilityInstances abilityInstances) {
248-
abilityInstances.add(this.actualImportItems);
240+
abilityInstances.add(this.importItems);
249241
}
250242

251243
@Override
@@ -262,7 +254,7 @@ protected NBTTagCompound writeConfigToTag() {
262254
NBTTagCompound configStacks = new NBTTagCompound();
263255
tag.setTag("ConfigStacks", configStacks);
264256
for (int i = 0; i < CONFIG_SIZE; i++) {
265-
var slot = this.aeItemHandler.getInventory()[i];
257+
var slot = this.aeHandler.getInventory()[i];
266258
IAEItemStack config = slot.getConfig();
267259
if (config == null) {
268260
continue;
@@ -298,9 +290,9 @@ protected void readConfigFromTag(NBTTagCompound tag) {
298290
String key = Integer.toString(i);
299291
if (configStacks.hasKey(key)) {
300292
NBTTagCompound configTag = configStacks.getCompoundTag(key);
301-
this.aeItemHandler.getInventory()[i].setConfig(WrappedItemStack.fromNBT(configTag));
293+
this.aeHandler.getInventory()[i].setConfig(WrappedItemStack.fromNBT(configTag));
302294
} else {
303-
this.aeItemHandler.getInventory()[i].setConfig(null);
295+
this.aeHandler.getInventory()[i].setConfig(null);
304296
}
305297
}
306298
}

src/main/java/gregtech/common/metatileentities/multi/multiblockpart/appeng/MetaTileEntityMEInputHatch.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import gregtech.client.renderer.texture.Textures;
1414
import gregtech.common.metatileentities.multi.multiblockpart.appeng.slot.ExportOnlyAEFluidList;
1515
import gregtech.common.metatileentities.multi.multiblockpart.appeng.slot.ExportOnlyAEFluidSlot;
16+
import gregtech.common.metatileentities.multi.multiblockpart.appeng.slot.IExportOnlyAEStackList;
1617
import gregtech.common.metatileentities.multi.multiblockpart.appeng.stack.WrappedFluidStack;
1718

1819
import net.minecraft.client.resources.I18n;
@@ -47,8 +48,6 @@ public class MetaTileEntityMEInputHatch extends MetaTileEntityMEInputBase<IAEFlu
4748

4849
public static final String FLUID_BUFFER_TAG = "FluidTanks";
4950

50-
protected ExportOnlyAEFluidList aeFluidHandler;
51-
5251
public MetaTileEntityMEInputHatch(ResourceLocation metaTileEntityId, int tier) {
5352
super(metaTileEntityId, tier, false, IFluidStorageChannel.class);
5453
}
@@ -59,23 +58,19 @@ public MetaTileEntity createMetaTileEntity(IGregTechTileEntity iGregTechTileEnti
5958
}
6059

6160
@Override
62-
public @NotNull ExportOnlyAEFluidList getAEHandler() {
63-
if (aeFluidHandler == null) {
64-
aeFluidHandler = new ExportOnlyAEFluidList(this, CONFIG_SIZE, this.getController());
65-
}
66-
67-
return aeFluidHandler;
61+
protected void initializeInventory() {
62+
super.initializeInventory();
63+
this.importFluids = new FluidTankList(false, getAEHandler().getInventory());
6864
}
6965

7066
@Override
71-
protected void initializeInventory() {
72-
getAEHandler(); // initialize it
73-
super.initializeInventory();
67+
protected @NotNull IExportOnlyAEStackList<IAEFluidStack> initializeAEHandler() {
68+
return new ExportOnlyAEFluidList(this, CONFIG_SIZE, this.getController());
7469
}
7570

7671
@Override
77-
protected FluidTankList createImportFluidHandler() {
78-
return new FluidTankList(false, getAEHandler().getInventory());
72+
public @NotNull ExportOnlyAEFluidList getAEHandler() {
73+
return (ExportOnlyAEFluidList) aeHandler;
7974
}
8075

8176
@Override
@@ -198,7 +193,7 @@ protected NBTTagCompound writeConfigToTag() {
198193
NBTTagCompound configStacks = new NBTTagCompound();
199194
tag.setTag("ConfigStacks", configStacks);
200195
for (int i = 0; i < CONFIG_SIZE; i++) {
201-
var slot = this.aeFluidHandler.getInventory()[i];
196+
var slot = this.aeHandler.getInventory()[i];
202197
IAEFluidStack config = slot.getConfig();
203198
if (config == null) {
204199
continue;
@@ -232,9 +227,9 @@ protected void readConfigFromTag(NBTTagCompound tag) {
232227
String key = Integer.toString(i);
233228
if (configStacks.hasKey(key)) {
234229
NBTTagCompound configTag = configStacks.getCompoundTag(key);
235-
this.aeFluidHandler.getInventory()[i].setConfig(WrappedFluidStack.fromNBT(configTag));
230+
this.aeHandler.getInventory()[i].setConfig(WrappedFluidStack.fromNBT(configTag));
236231
} else {
237-
this.aeFluidHandler.getInventory()[i].setConfig(null);
232+
this.aeHandler.getInventory()[i].setConfig(null);
238233
}
239234
}
240235
}

src/main/java/gregtech/common/metatileentities/multi/multiblockpart/appeng/MetaTileEntityMEOutputBase.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ public void receiveInitialSyncData(PacketBuffer buf) {
180180
this.workingEnabled = buf.readBoolean();
181181
}
182182

183+
@Override
184+
protected boolean shouldSerializeInventories() {
185+
return false;
186+
}
187+
183188
@Override
184189
public NBTTagCompound writeToNBT(NBTTagCompound data) {
185190
super.writeToNBT(data);

src/main/java/gregtech/common/metatileentities/multi/multiblockpart/appeng/MetaTileEntityMEStockingBus.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import gregtech.common.metatileentities.multi.multiblockpart.appeng.slot.ExportOnlyAEItemList;
1111
import gregtech.common.metatileentities.multi.multiblockpart.appeng.slot.ExportOnlyAEItemSlot;
1212
import gregtech.common.metatileentities.multi.multiblockpart.appeng.slot.IConfigurableSlot;
13+
import gregtech.common.metatileentities.multi.multiblockpart.appeng.slot.IExportOnlyAEStackList;
1314
import gregtech.common.metatileentities.multi.multiblockpart.appeng.stack.WrappedItemStack;
1415

1516
import net.minecraft.client.resources.I18n;
@@ -67,12 +68,14 @@ public MetaTileEntity createMetaTileEntity(IGregTechTileEntity iGregTechTileEnti
6768
return new MetaTileEntityMEStockingBus(metaTileEntityId, getTier());
6869
}
6970

71+
@Override
72+
protected @NotNull IExportOnlyAEStackList<IAEItemStack> initializeAEHandler() {
73+
return new ExportOnlyAEStockingItemList(this, CONFIG_SIZE, getController());
74+
}
75+
7076
@Override
7177
public @NotNull ExportOnlyAEStockingItemList getAEHandler() {
72-
if (this.aeItemHandler == null) {
73-
this.aeItemHandler = new ExportOnlyAEStockingItemList(this, CONFIG_SIZE, getController());
74-
}
75-
return (ExportOnlyAEStockingItemList) this.aeItemHandler;
78+
return (ExportOnlyAEStockingItemList) aeHandler;
7679
}
7780

7881
@Override
@@ -209,7 +212,7 @@ private boolean testConfiguredInOtherBus(@Nullable ItemStack stack) {
209212

210213
private boolean checkHandler(@NotNull IItemHandler itemHandler, @NotNull ItemStack stack) {
211214
if (itemHandler instanceof ExportOnlyAEStockingItemList itemList) {
212-
if (itemList == this.aeItemHandler) return false;
215+
if (itemList == this.aeHandler) return false;
213216
return itemList.hasStackInConfig(stack, false);
214217
}
215218

src/main/java/gregtech/common/metatileentities/multi/multiblockpart/appeng/MetaTileEntityMEStockingHatch.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import gregtech.common.metatileentities.multi.multiblockpart.appeng.slot.ExportOnlyAEFluidList;
99
import gregtech.common.metatileentities.multi.multiblockpart.appeng.slot.ExportOnlyAEFluidSlot;
1010
import gregtech.common.metatileentities.multi.multiblockpart.appeng.slot.IConfigurableSlot;
11+
import gregtech.common.metatileentities.multi.multiblockpart.appeng.slot.IExportOnlyAEStackList;
1112
import gregtech.common.metatileentities.multi.multiblockpart.appeng.stack.WrappedFluidStack;
1213

1314
import net.minecraft.client.resources.I18n;
@@ -65,12 +66,14 @@ public MetaTileEntity createMetaTileEntity(IGregTechTileEntity iGregTechTileEnti
6566
return new MetaTileEntityMEStockingHatch(metaTileEntityId, getTier());
6667
}
6768

69+
@Override
70+
protected @NotNull IExportOnlyAEStackList<IAEFluidStack> initializeAEHandler() {
71+
return new ExportOnlyAEStockingFluidList(this, CONFIG_SIZE, getController());
72+
}
73+
6874
@Override
6975
public @NotNull ExportOnlyAEStockingFluidList getAEHandler() {
70-
if (this.aeFluidHandler == null) {
71-
this.aeFluidHandler = new ExportOnlyAEStockingFluidList(this, CONFIG_SIZE, getController());
72-
}
73-
return (ExportOnlyAEStockingFluidList) this.aeFluidHandler;
76+
return (ExportOnlyAEStockingFluidList) this.aeHandler;
7477
}
7578

7679
@Override

0 commit comments

Comments
 (0)