Skip to content

Commit

Permalink
Fix #521
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Sep 18, 2024
1 parent 41a3f34 commit 43d5b9f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,24 @@
import com.railwayteam.railways.mixin_interfaces.IContraptionFuel;
import com.railwayteam.railways.mixin_interfaces.IFuelInventory;
import com.railwayteam.railways.mixin_interfaces.IPreAssembleCallback;
import com.railwayteam.railways.util.AbstractionUtils;
import com.simibubi.create.content.contraptions.Contraption;
import com.simibubi.create.content.contraptions.MountedStorageManager;
import com.simibubi.create.foundation.blockEntity.SmartBlockEntity;
import com.simibubi.create.foundation.blockEntity.behaviour.BlockEntityBehaviour;
import com.simibubi.create.foundation.fluid.CombinedTankWrapper;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(Contraption.class)
public abstract class MixinContraption implements IContraptionFuel {
@Shadow(remap = false) protected MountedStorageManager storage;

@Shadow protected abstract BlockPos toLocalPos(BlockPos globalPos);

@Inject(method = "getBlockEntityNBT", at = @At("RETURN"))
private void getBlockEntityNBT(Level world, BlockPos pos, CallbackInfoReturnable<CompoundTag> cir) {
BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity == null)
return;
// This is necessary because simply calling saveWithFullMetadata again will return a _different_ NBT tag
CompoundTag nbt = cir.getReturnValue();
if (nbt == null)
return;

if (AbstractionUtils.isInstanceOfFuelTankBlockEntity(blockEntity) && nbt.contains("Controller"))
nbt.put("Controller", NbtUtils.writeBlockPos(toLocalPos(NbtUtils.readBlockPos(nbt.getCompound("Controller")))));
}

@Inject(method = "removeBlocksFromWorld", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/Level;removeBlockEntity(Lnet/minecraft/core/BlockPos;)V"))
private void applyPreTransformCallback(Level world, BlockPos offset, CallbackInfo ci, @Local(name="add") BlockPos add) {
BlockEntity be = world.getBlockEntity(add);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class FuelTankBlockEntity extends SmartBlockEntity implements IHaveGoggle
protected BlockPos controller;
protected BlockPos lastKnownPos;
protected boolean updateConnectivity;
protected boolean updateCapability;
protected boolean window;
protected int luminosity;
protected int width;
Expand All @@ -82,6 +83,7 @@ public FuelTankBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState sta
tankInventory = createInventory();
forceFluidLevelUpdate = true;
updateConnectivity = false;
updateCapability = false;
window = true;
height = 1;
width = 1;
Expand Down Expand Up @@ -117,6 +119,10 @@ else if (!lastKnownPos.equals(worldPosition)) {
return;
}

if (updateCapability) {
updateCapability = false;
refreshCapability();
}
if (updateConnectivity)
updateConnectivity();
if (fluidLevel != null)
Expand Down Expand Up @@ -401,11 +407,12 @@ protected void read(CompoundTag compound, boolean clientPacket) {
fluidLevel = LerpedFloat.linear()
.startWithValue(getFillState());

updateCapability = true;

if (!clientPacket)
return;

boolean changeOfController =
!Objects.equals(controllerBefore, controller);
boolean changeOfController = !Objects.equals(controllerBefore, controller);
if (changeOfController || prevSize != width || prevHeight != height) {
if (level != null && hasLevel())
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 16);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

import static java.lang.Math.abs;
Expand All @@ -63,6 +64,7 @@ public class FuelTankBlockEntity extends SmartBlockEntity implements IHaveGoggle
protected BlockPos controller;
protected BlockPos lastKnownPos;
protected boolean updateConnectivity;
protected boolean updateCapability;
protected boolean window;
protected int luminosity;
protected int width;
Expand All @@ -81,6 +83,7 @@ public FuelTankBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState sta
fluidCapability = LazyOptional.of(() -> tankInventory);
forceFluidLevelUpdate = true;
updateConnectivity = false;
updateCapability = false;
window = true;
height = 1;
width = 1;
Expand Down Expand Up @@ -116,6 +119,10 @@ else if (!lastKnownPos.equals(worldPosition) && worldPosition != null) {
return;
}

if (updateCapability) {
updateCapability = false;
refreshCapability();
}
if (updateConnectivity)
updateConnectivity();
if (fluidLevel != null)
Expand Down Expand Up @@ -312,7 +319,7 @@ public void setController(BlockPos controller) {

private void refreshCapability() {
LazyOptional<IFluidHandler> oldCap = fluidCapability;
fluidCapability = LazyOptional.of(() -> handlerForCapability());
fluidCapability = LazyOptional.of(this::handlerForCapability);
oldCap.invalidate();
}

Expand Down Expand Up @@ -376,11 +383,12 @@ protected void read(CompoundTag compound, boolean clientPacket) {
fluidLevel = LerpedFloat.linear()
.startWithValue(getFillState());

updateCapability = true;

if (!clientPacket)
return;

boolean changeOfController =
controllerBefore == null ? controller != null : !controllerBefore.equals(controller);
boolean changeOfController = !Objects.equals(controllerBefore, controller);
if (changeOfController || prevSize != width || prevHeight != height) {
if (hasLevel())
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 16);
Expand Down

0 comments on commit 43d5b9f

Please sign in to comment.