Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when configuring ender tank covers #3562

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading