Skip to content

Commit

Permalink
fix: rocket fluid storage being reset every item update
Browse files Browse the repository at this point in the history
  • Loading branch information
vini2003 committed Dec 27, 2023
1 parent 7f7b8fb commit 13349d9
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public Packet<ClientPlayPacketListener> createSpawnPacket() {
public void tick() {
super.tick();

if (!getWorld().isClient) {
if (!getWorld().isClient()) {
if (isRunning()) {
var acceleration = getAcceleration();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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<RocketFuelTankPart> 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)))
Expand All @@ -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.
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 13349d9

Please sign in to comment.