From 13349d9ad5a17cdc21ec8530678fdd8740deed1d Mon Sep 17 00:00:00 2001 From: vini2003 Date: Wed, 27 Dec 2023 13:56:36 -0300 Subject: [PATCH] fix: rocket fluid storage being reset every item update --- .../block/entity/storage/TankBlockEntity.java | 7 +- .../common/entity/rocket/RocketEntity.java | 2 +- .../common/manager/RocketManager.java | 7 +- .../astromine/common/rocket/Rocket.java | 66 +++++++++++++------ .../astromine/registry/common/AMEvents.java | 4 +- 5 files changed, 57 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/github/mixinors/astromine/common/block/entity/storage/TankBlockEntity.java b/src/main/java/com/github/mixinors/astromine/common/block/entity/storage/TankBlockEntity.java index e60d9214f..d2dd3d777 100644 --- a/src/main/java/com/github/mixinors/astromine/common/block/entity/storage/TankBlockEntity.java +++ b/src/main/java/com/github/mixinors/astromine/common/block/entity/storage/TankBlockEntity.java @@ -114,17 +114,12 @@ public void tick() { var wildFluidStorage = fluidStorage.getWildProxy(); var itemInputStorage = wildItemStorage.getStorage(ITEM_INPUT_SLOT); - var itemBufferStorage = wildItemStorage.getStorage(ITEM_BUFFER_SLOT); - var itemOutputStorage2 = wildItemStorage.getStorage(ITEM_OUTPUT_SLOT); - var fluidInputStorage = wildFluidStorage.getStorage(FLUID_INPUT_SLOT); - var fluidOutputStorage = wildFluidStorage.getStorage(FLUID_OUTPUT_SLOT); - + var unloadFluidStorages = FluidStorage.ITEM.find(itemInputStorage.getStack(), ContainerItemContext.ofSingleSlot(itemInputStorage)); - var loadFluidStorages = FluidStorage.ITEM.find(itemOutputStorage2.getStack(), ContainerItemContext.ofSingleSlot(itemOutputStorage2)); try (var transaction = Transaction.openOuter()) { diff --git a/src/main/java/com/github/mixinors/astromine/common/entity/rocket/RocketEntity.java b/src/main/java/com/github/mixinors/astromine/common/entity/rocket/RocketEntity.java index fbbf212c4..60b76bbcf 100644 --- a/src/main/java/com/github/mixinors/astromine/common/entity/rocket/RocketEntity.java +++ b/src/main/java/com/github/mixinors/astromine/common/entity/rocket/RocketEntity.java @@ -118,7 +118,7 @@ public Packet createSpawnPacket() { public void tick() { super.tick(); - if (!getWorld().isClient) { + if (!getWorld().isClient()) { if (isRunning()) { var acceleration = getAcceleration(); diff --git a/src/main/java/com/github/mixinors/astromine/common/manager/RocketManager.java b/src/main/java/com/github/mixinors/astromine/common/manager/RocketManager.java index e21a6b76a..987b15920 100644 --- a/src/main/java/com/github/mixinors/astromine/common/manager/RocketManager.java +++ b/src/main/java/com/github/mixinors/astromine/common/manager/RocketManager.java @@ -2,6 +2,7 @@ import com.github.mixinors.astromine.AMCommon; import com.github.mixinors.astromine.common.rocket.Rocket; +import com.github.mixinors.astromine.common.screen.handler.base.entity.ExtendedEntityScreenHandler; import com.github.mixinors.astromine.registry.common.AMStaticComponents; import com.github.mixinors.astromine.registry.common.AMWorlds; import dev.architectury.networking.NetworkManager; @@ -156,7 +157,11 @@ public static void onSync(PacketByteBuf buf, NetworkManager.PacketContext contex component.readFromNbt(nbt); }); } - + + public static void onServerPre(MinecraftServer server) { + getRockets().forEach(Rocket::tick); + } + public static void onPlayerJoin(ServerPlayNetworkHandler handler, PacketSender sender, MinecraftServer server) { sync(server); } diff --git a/src/main/java/com/github/mixinors/astromine/common/rocket/Rocket.java b/src/main/java/com/github/mixinors/astromine/common/rocket/Rocket.java index 48e5954f9..2f6f01e65 100644 --- a/src/main/java/com/github/mixinors/astromine/common/rocket/Rocket.java +++ b/src/main/java/com/github/mixinors/astromine/common/rocket/Rocket.java @@ -184,11 +184,11 @@ public void writeToNbt(NbtCompound nbt) { @SuppressWarnings("DuplicatedCode") @Override public void tick() { - tickJourney(); + // tickJourney(); var wildItemStorage = itemStorage.getWildProxy(); var wildFluidStorage = fluidStorage.getWildProxy(); - + var itemInputStorage1 = wildItemStorage.getStorage(OXYGEN_TANK_UNLOAD_SLOT); var itemBufferStorage1 = wildItemStorage.getStorage(OXYGEN_TANK_BUFFER_SLOT); var itemOutputStorage1 = wildItemStorage.getStorage(OXYGEN_TANK_OUTPUT_SLOT); @@ -198,18 +198,32 @@ public void tick() { var unloadFluidStorages1 = FluidStorage.ITEM.find(itemInputStorage1.getStack(), ContainerItemContext.ofSingleSlot(itemInputStorage1)); var loadFluidStorages1 = FluidStorage.ITEM.find(itemOutputStorage1.getStack(), ContainerItemContext.ofSingleSlot(itemOutputStorage1)); - try (var transaction = Transaction.openOuter()) { - StorageUtil.move(unloadFluidStorages1, fluidInputStorage1, fluidVariant -> !fluidVariant.isBlank(), FluidConstants.BUCKET, transaction); - StorageUtil.move(fluidOutputStorage1, loadFluidStorages1, fluidVariant -> !fluidVariant.isBlank(), FluidConstants.BUCKET, transaction); - - StorageUtil.move(itemInputStorage1, itemBufferStorage1, (variant) -> { - var stored = StorageUtil.findStoredResource(unloadFluidStorages1); - return stored == null || stored.isBlank(); - }, 1, transaction); - - transaction.commit(); + if (itemInputStorage1.getResource().getItem() == AMFluids.OXYGEN.getBucketItem()) { + try (var transaction = Transaction.openOuter()) { + StorageUtil.move(unloadFluidStorages1, fluidInputStorage1, fluidVariant -> !fluidVariant.isBlank(), FluidConstants.BUCKET, transaction); + StorageUtil.move(fluidOutputStorage1, loadFluidStorages1, fluidVariant -> !fluidVariant.isBlank(), FluidConstants.BUCKET, transaction); + + StorageUtil.move(itemInputStorage1, itemBufferStorage1, (variant) -> { + var stored = StorageUtil.findStoredResource(unloadFluidStorages1); + return stored==null || stored.isBlank(); + }, 1, transaction); + + transaction.commit(); + } } - + + if (itemOutputStorage1.supportsInsertion()) { + try (var transaction = Transaction.openOuter()) { + StorageUtil.move(fluidOutputStorage1, loadFluidStorages1, fluidVariant -> !fluidVariant.isBlank(), FluidConstants.BUCKET, transaction); + StorageUtil.move(itemOutputStorage1, itemBufferStorage1, (variant) -> { + var stored = StorageUtil.findStoredResource(unloadFluidStorages1); + return stored==null || stored.isBlank(); + }, 1, transaction); + + transaction.commit(); + } + } + var itemInputStorage2 = wildItemStorage.getStorage(FUEL_TANK_UNLOAD_SLOT); var itemBufferStorage2 = wildItemStorage.getStorage(FUEL_TANK_BUFFER_SLOT); var itemOutputStorage2 = wildItemStorage.getStorage(FUEL_TANK_OUTPUT_SLOT); @@ -227,18 +241,28 @@ public void tick() { var stored = StorageUtil.findStoredResource(unloadFluidStorages2); return stored == null || stored.isBlank(); }, 2, transaction); - + transaction.commit(); } } private void updateFluidStorage() { Optional fuelTank = parts.getPart(PartType.FUEL_TANK); - long capacity = fuelTank.map(rocketFuelTankPart -> rocketFuelTankPart.getCapacity().getSize()).orElse(1_000L); - - fluidStorage = new SimpleFluidStorage(2, capacity); - - if (fuelTank.isPresent()) { + + var capacity = fuelTank.map(rocketFuelTankPart -> rocketFuelTankPart.getCapacity().getSize()).orElse(81_000L); + + if (fluidStorage == null) { + fluidStorage = new SimpleFluidStorage(2, capacity); + } else { + for (var i = 0; i < 2; ++i) { + fluidStorage.getStorage(i).setCapacity(capacity); + } + } + + // TODO: Investigate if we actually need the presence check, + // since the listener, at least, should always be triggered; + // and the filters should always be present. + if (fuelTank.isPresent() || true) { fluidStorage.extractPredicate((variant, slot) -> false) .extractPredicate((variant, slot) -> slot == OXYGEN_TANK_FLUID_OUT || slot == FUEL_TANK_FLUID_OUT) .insertPredicate((variant, slot) -> (slot == OXYGEN_TANK_FLUID_IN && OXYGEN_INGREDIENT.testVariant(variant)) || (slot == FUEL_TANK_FLUID_IN && FUEL_INGREDIENT.testVariant(variant))) @@ -247,8 +271,10 @@ private void updateFluidStorage() { if (server == null) return; // TODO Find out why the heck updateFluidStorage gets called so often - //RocketManager.sync(server); + RocketManager.sync(server); }).insertSlots(FLUID_INSERT_SLOTS).extractSlots(FLUID_EXTRACT_SLOTS); + } else { + // Disable extraction, insertion and listeners. } } diff --git a/src/main/java/com/github/mixinors/astromine/registry/common/AMEvents.java b/src/main/java/com/github/mixinors/astromine/registry/common/AMEvents.java index 837b38122..b30ace3cb 100644 --- a/src/main/java/com/github/mixinors/astromine/registry/common/AMEvents.java +++ b/src/main/java/com/github/mixinors/astromine/registry/common/AMEvents.java @@ -48,7 +48,9 @@ public static void init() { TickEvent.SERVER_PRE.register(ExtendedBlockEntityScreenHandler::onServerPre); TickEvent.SERVER_PRE.register(ExtendedEntityScreenHandler::onServerPre); - + + TickEvent.SERVER_PRE.register(RocketManager::onServerPre); + TickEvent.SERVER_LEVEL_PRE.register(NetworksComponent::onServerLevelPre); ServerWorldEvents.LOAD.register(BodyManager::onWorldLoad);