Skip to content

Commit

Permalink
Fix crash when configuring ender tank covers (#3562)
Browse files Browse the repository at this point in the history
  • Loading branch information
RecursivePineapple authored Nov 27, 2024
1 parent 09bd436 commit d6076e7
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions src/main/java/tectech/thing/cover/CoverEnderFluidLink.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package tectech.thing.cover;

import java.util.Objects;
import java.util.UUID;

import net.minecraft.entity.player.EntityPlayer;
Expand All @@ -25,6 +24,7 @@
import gregtech.api.util.ISerializableObject;
import gregtech.common.gui.modularui.widget.CoverDataControllerWidget;
import gregtech.common.gui.modularui.widget.CoverDataFollowerToggleButtonWidget;
import gtPlusPlus.core.tileentities.base.TileEntityBase;
import tectech.mechanics.enderStorage.EnderLinkTag;
import tectech.mechanics.enderStorage.EnderWorldSavedData;

Expand Down Expand Up @@ -61,6 +61,14 @@ public int doCoverThings(ForgeDirection side, byte aInputRedstone, int aCoverID,
EnderLinkTag tag = EnderWorldSavedData.getEnderLinkTag((IFluidHandler) aTileEntity);

if (tag != null) {
boolean shouldBePrivate = testBit(aCoverVariable, PUBLIC_PRIVATE_MASK);
boolean isPrivate = tag.getUUID() != null;

if (shouldBePrivate != isPrivate) {
tag = new EnderLinkTag(tag.getFrequency(), shouldBePrivate ? getOwner(aTileEntity) : null);
EnderWorldSavedData.bindEnderLinkTag(teTank, tag);
}

IFluidHandler enderTank = EnderWorldSavedData.getEnderFluidContainer(tag);

if (testBit(aCoverVariable, IMPORT_EXPORT_MASK)) {
Expand All @@ -73,6 +81,16 @@ public int doCoverThings(ForgeDirection side, byte aInputRedstone, int aCoverID,
return aCoverVariable;
}

private static UUID getOwner(Object te) {
if (te instanceof IGregTechTileEntity igte) {
return igte.getOwnerUuid();
} else if (te instanceof TileEntityBase teb) {
return teb.getOwnerUUID();
} else {
return null;
}
}

@Override
public void onBaseTEDestroyed(ForgeDirection side, int aCoverID, int aCoverVariable, ICoverable aTileEntity) {
if (aTileEntity instanceof IFluidHandler fluidHandlerSelf) {
Expand Down Expand Up @@ -156,7 +174,6 @@ public EnderFluidLinkUIFactory(CoverUIBuildContext buildContext) {
super(buildContext);
}

@SuppressWarnings("PointlessArithmeticExpression")
@Override
protected void addUIWidgets(ModularWindow.Builder builder) {
TextFieldWidget frequencyField = new TextFieldWidget();
Expand All @@ -175,8 +192,7 @@ protected void addUIWidgets(ModularWindow.Builder builder) {

if (testBit(convert(getCoverData()), PUBLIC_PRIVATE_MASK)) {
uuid = getUUID();
if (!(tank instanceof IGregTechTileEntity gte)) return;
if (!uuid.equals(gte.getOwnerUuid())) return;
if (!uuid.equals(getOwner(tank))) return;
}

EnderWorldSavedData.bindEnderLinkTag(tank, new EnderLinkTag(val, uuid));
Expand Down Expand Up @@ -233,30 +249,9 @@ protected void addUIWidgets(ModularWindow.Builder builder) {

private int getNewCoverVariable(int id, int coverVariable) {
switch (id) {
case PUBLIC_BUTTON_ID: {
if (getUIBuildContext().getTile() instanceof IGregTechTileEntity gte) {
EnderLinkTag tag = EnderWorldSavedData.getEnderLinkTag(gte);

if (tag != null) {
EnderWorldSavedData.bindEnderLinkTag(gte, new EnderLinkTag(tag.getFrequency(), null));
}
}

return toggleBit(coverVariable, PUBLIC_PRIVATE_MASK);
}
case PRIVATE_BUTTON_ID: {
if (getUIBuildContext().getTile() instanceof IGregTechTileEntity gte) {
EnderLinkTag tag = EnderWorldSavedData.getEnderLinkTag(gte);

UUID player = getUUID();

if ((gte.getOwnerUuid() == null || Objects.equals(player, gte.getOwnerUuid())) && tag != null) {
EnderWorldSavedData.bindEnderLinkTag(gte, new EnderLinkTag(tag.getFrequency(), player));
}
}

case PUBLIC_BUTTON_ID:
case PRIVATE_BUTTON_ID:
return toggleBit(coverVariable, PUBLIC_PRIVATE_MASK);
}
case IMPORT_BUTTON_ID:
case EXPORT_BUTTON_ID:
return toggleBit(coverVariable, IMPORT_EXPORT_MASK);
Expand Down

0 comments on commit d6076e7

Please sign in to comment.