Skip to content

Commit

Permalink
fixed issue where setting connections on an itme pipes takes too long
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinsdar committed Dec 20, 2023
1 parent 3b365c7 commit ba8fe3c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import muramasa.antimatter.gui.IGuiElement;
import muramasa.antimatter.gui.widget.InfoRenderWidget;
import muramasa.antimatter.integration.jeirei.renderer.IInfoRenderer;
import muramasa.antimatter.pipe.FluidPipeTicker;
import muramasa.antimatter.pipe.PipeTicker;
import muramasa.antimatter.pipe.PipeSize;
import muramasa.antimatter.pipe.types.FluidPipe;
import muramasa.antimatter.util.AntimatterPlatformUtils;
Expand Down Expand Up @@ -69,9 +69,9 @@ public void onLoad() {
holder = new PipeFluidHolder(this);
super.onLoad();
if (even(this.getBlockPos().getX(), this.getBlockPos().getY(), this.getBlockPos().getZ())) {
FluidPipeTicker.SERVER_TICK_PRE.add(this);
PipeTicker.SERVER_TICK_PRE.add(this);
} else {
FluidPipeTicker.SERVER_TICK_PR2.add(this);
PipeTicker.SERVER_TICK_PR2.add(this);
}
}

Expand Down Expand Up @@ -122,8 +122,8 @@ public void saveAdditional(CompoundTag tag) {
@Override
public void onRemove() {
fluidHandler.ifPresent(FluidHandler::onRemove);
FluidPipeTicker.SERVER_TICK_PR2.remove(this);
FluidPipeTicker.SERVER_TICK_PRE.remove(this);
PipeTicker.SERVER_TICK_PR2.remove(this);
PipeTicker.SERVER_TICK_PRE.remove(this);
super.onRemove();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import muramasa.antimatter.capability.*;
import muramasa.antimatter.capability.pipe.PipeCoverHandler;
import muramasa.antimatter.cover.CoverFactory;
import muramasa.antimatter.cover.CoverPlate;
import muramasa.antimatter.cover.ICover;
import muramasa.antimatter.gui.GuiData;
import muramasa.antimatter.gui.GuiInstance;
Expand All @@ -18,6 +17,7 @@
import muramasa.antimatter.network.packets.AbstractGuiEventPacket;
import muramasa.antimatter.pipe.BlockPipe;
import muramasa.antimatter.pipe.PipeSize;
import muramasa.antimatter.pipe.PipeTicker;
import muramasa.antimatter.pipe.types.PipeType;
import muramasa.antimatter.util.Utils;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -59,6 +59,7 @@ public abstract class BlockEntityPipe<T extends PipeType<T>> extends BlockEntity
* Connection data
**/
private byte connection, virtualConnection;
private boolean refreshConnection = false;

@Getter
protected Holder pipeCapHolder;
Expand Down Expand Up @@ -185,7 +186,7 @@ public void setConnection(Direction side) {
return;
}*/

refreshConnection();
PipeTicker.addTickFunction(this::refreshConnection);
if (pipe != null) {
pipe.setConnection(side.getOpposite());
}
Expand All @@ -197,7 +198,7 @@ public void clearConnection(Direction side) {
connection = Connectivity.clear(connection, side.get3DDataValue());
virtualConnection = Connectivity.clear(virtualConnection, side.get3DDataValue());
dispatch.invalidate(side);
refreshConnection();
PipeTicker.addTickFunction(this::refreshConnection);
BlockEntityPipe<?> pipe = getPipe(side);
if (pipe != null) {
pipe.clearConnection(side.getOpposite());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
import java.util.ArrayList;
import java.util.List;

public class FluidPipeTicker {
public class PipeTicker {
public static final List<BlockEntityFluidPipe<?>> SERVER_TICK_PRE = new ArrayList<>(), SERVER_TICK_PR2 = new ArrayList<>();

private static final List<Runnable> TICK_FUNCTIONS = new ArrayList<>();

public static void onServerWorldTick(MinecraftServer server){
synchronized (TICK_FUNCTIONS){
TICK_FUNCTIONS.forEach(Runnable::run);
TICK_FUNCTIONS.clear();
}
for (int i = 0; i < SERVER_TICK_PRE.size(); i++) {
BlockEntityFluidPipe<?> tTileEntity = SERVER_TICK_PRE.get(i);
if (tTileEntity == null || tTileEntity.isRemoved()) {
Expand Down Expand Up @@ -46,4 +52,10 @@ public static void onServerWorldTick(MinecraftServer server){
}
}
}

public static void addTickFunction(Runnable function){
synchronized (TICK_FUNCTIONS){
TICK_FUNCTIONS.add(function);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import muramasa.antimatter.fluid.AntimatterFluid;
import muramasa.antimatter.fluid.fabric.FluidAttributesVariantWrapper;
import muramasa.antimatter.integration.kubejs.KubeJSRegistrar;
import muramasa.antimatter.pipe.FluidPipeTicker;
import muramasa.antimatter.pipe.PipeTicker;
import muramasa.antimatter.proxy.CommonHandler;
import muramasa.antimatter.recipe.fabric.RecipeConditions;
import muramasa.antimatter.registration.IAntimatterRegistrarInitializer;
Expand Down Expand Up @@ -71,7 +71,7 @@ public void initialize(boolean run){
ProviderEvents.PROVIDERS.register(this::providers);
ServerWorldEvents.UNLOAD.register((server, world) -> StructureCache.onWorldUnload(world));
ItemCraftedCallback.EVENT.register(((player, crafted, container) -> CommonEvents.onItemCrafted(container, player)));
ServerTickEvents.START_SERVER_TICK.register(FluidPipeTicker::onServerWorldTick);
ServerTickEvents.START_SERVER_TICK.register(PipeTicker::onServerWorldTick);
CommonLifecycleEvents.TAGS_LOADED.register((registries, client) -> CommonEvents.tagsEvent());
//TODO figure out variables to insert
BlockPlaceCallback.EVENT.register(context -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import muramasa.antimatter.data.AntimatterMaterialTypes;
import muramasa.antimatter.material.Material;
import muramasa.antimatter.ore.BlockOre;
import muramasa.antimatter.pipe.FluidPipeTicker;
import muramasa.antimatter.pipe.PipeTicker;
import muramasa.antimatter.structure.StructureCache;
import muramasa.antimatter.worldgen.AntimatterWorldGenerator;
import net.minecraft.core.Direction;
Expand All @@ -17,7 +17,6 @@
import net.minecraft.world.item.Items;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.client.event.RecipesUpdatedEvent;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.*;
Expand Down Expand Up @@ -52,7 +51,7 @@ public static void onItemCrafted(PlayerEvent.ItemCraftedEvent e) {
@SubscribeEvent
public static void onServerTick(TickEvent.ServerTickEvent event){
if (event.phase == TickEvent.Phase.START){
FluidPipeTicker.onServerWorldTick(ServerLifecycleHooks.getCurrentServer());
PipeTicker.onServerWorldTick(ServerLifecycleHooks.getCurrentServer());
}
}

Expand Down

0 comments on commit ba8fe3c

Please sign in to comment.