From 3e8ac662f1abeb8ebb84467fbf3a47cc531737b1 Mon Sep 17 00:00:00 2001 From: zelophed Date: Sun, 15 Dec 2019 01:13:36 +0100 Subject: [PATCH] lag on command added a utility that delays server tick by a set time and a command to control it --- src/main/java/com/simibubi/create/Create.java | 14 ++++ src/main/java/com/simibubi/create/Events.java | 2 +- .../foundation/command/CreateCommand.java | 12 ++++ .../foundation/command/KillTPSCommand.java | 64 +++++++++++++++++++ .../foundation/command/ServerLagger.java | 35 ++++++++++ .../create/foundation/utility/AllShapes.java | 4 +- 6 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/simibubi/create/foundation/command/CreateCommand.java create mode 100644 src/main/java/com/simibubi/create/foundation/command/KillTPSCommand.java create mode 100644 src/main/java/com/simibubi/create/foundation/command/ServerLagger.java diff --git a/src/main/java/com/simibubi/create/Create.java b/src/main/java/com/simibubi/create/Create.java index 058f77956..bf27a6751 100644 --- a/src/main/java/com/simibubi/create/Create.java +++ b/src/main/java/com/simibubi/create/Create.java @@ -1,5 +1,9 @@ package com.simibubi.create; +import com.simibubi.create.foundation.command.CreateCommand; +import com.simibubi.create.foundation.command.ServerLagger; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.event.server.FMLServerStartingEvent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -44,6 +48,7 @@ public class Create { public static LogisticalNetworkHandler logisticalNetworkHandler; public static TorquePropagator torquePropagator; public static LogisticianHandler logisticianHandler; + public static ServerLagger lagger; public static ModConfig config; @@ -51,6 +56,8 @@ public class Create { IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); modEventBus.addListener(Create::init); + MinecraftForge.EVENT_BUS.addListener(Create::serverStarting); + modEventBus.addGenericListener(Block.class, AllBlocks::register); modEventBus.addGenericListener(Item.class, AllItems::register); modEventBus.addGenericListener(IRecipeSerializer.class, AllRecipes::register); @@ -74,11 +81,16 @@ public class Create { frequencyHandler = new FrequencyHandler(); logisticalNetworkHandler = new LogisticalNetworkHandler(); torquePropagator = new TorquePropagator(); + lagger = new ServerLagger(); CraftingHelper.register(new ModuleLoadedCondition.Serializer()); AllPackets.registerPackets(); } + public static void serverStarting(FMLServerStartingEvent event){ + new CreateCommand(event.getCommandDispatcher()); + } + public static void registerVillagerProfessions(RegistryEvent.Register event) { LogisticianHandler.registerVillagerProfessions(event); } @@ -96,6 +108,8 @@ public class Create { if (schematicReceiver == null) schematicReceiver = new ServerSchematicLoader(); schematicReceiver.tick(); + + lagger.tick(); } public static void shutdown() { diff --git a/src/main/java/com/simibubi/create/Events.java b/src/main/java/com/simibubi/create/Events.java index d3cbd8410..aa3fd170f 100644 --- a/src/main/java/com/simibubi/create/Events.java +++ b/src/main/java/com/simibubi/create/Events.java @@ -29,7 +29,7 @@ public class Events { @SubscribeEvent public static void onTick(ServerTickEvent event) { - if (event.phase == Phase.START) + if (event.phase == Phase.END) return; Create.tick(); diff --git a/src/main/java/com/simibubi/create/foundation/command/CreateCommand.java b/src/main/java/com/simibubi/create/foundation/command/CreateCommand.java new file mode 100644 index 000000000..3e78bd3fc --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/command/CreateCommand.java @@ -0,0 +1,12 @@ +package com.simibubi.create.foundation.command; + +import com.mojang.brigadier.CommandDispatcher; +import net.minecraft.command.CommandSource; + +public class CreateCommand { + + public CreateCommand(CommandDispatcher dispatcher){ + + KillTPSCommand.register(dispatcher); + } +} diff --git a/src/main/java/com/simibubi/create/foundation/command/KillTPSCommand.java b/src/main/java/com/simibubi/create/foundation/command/KillTPSCommand.java new file mode 100644 index 000000000..127886d87 --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/command/KillTPSCommand.java @@ -0,0 +1,64 @@ +package com.simibubi.create.foundation.command; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.simibubi.create.Create; +import net.minecraft.command.CommandSource; +import net.minecraft.command.Commands; +import net.minecraft.util.text.StringTextComponent; + +public class KillTPSCommand { + + public static void register(CommandDispatcher dispatcher) { + dispatcher.register( + Commands.literal("killtps")//todo replace String Components with Translation Components + .requires(cs -> cs.hasPermissionLevel(2)) + .executes(ctx -> { + //killtps no arguments + ctx.getSource().sendFeedback(new StringTextComponent(String.format("[Create]: Server tick is currently slowed by %s ms",Create.lagger.isLagging() ? Create.lagger.getTickTime() : 0)), true); + if (Create.lagger.isLagging()) + ctx.getSource().sendFeedback(new StringTextComponent("[Create]: use /killtps stop to bring back server tick to regular speed"), true); + else + ctx.getSource().sendFeedback(new StringTextComponent("[Create]: use /killtps start to artificially slow down the server tick"),true); + + return 1; + }) + .then(Commands.literal("start") + .executes(ctx -> { + //killtps start no time + int tickTime = Create.lagger.getTickTime(); + if (tickTime > 0){ + Create.lagger.setLagging(true); + ctx.getSource().sendFeedback(new StringTextComponent(String.format("[Create]: Server tick is slowed by %s ms now :)", tickTime)),true); + ctx.getSource().sendFeedback(new StringTextComponent("[Create]: use /killtps stop to bring back server tick to regular speed"),true); + } else { + ctx.getSource().sendFeedback(new StringTextComponent("[Create]: use /killtps start to artificially slow down the server tick"),true); + } + + return 1; + }) + .then(Commands.argument("tickTime", IntegerArgumentType.integer(1)) + .executes(ctx -> { + //killtps start tickTime + int tickTime = IntegerArgumentType.getInteger(ctx, "tickTime"); + Create.lagger.setTickTime(tickTime); + Create.lagger.setLagging(true); + ctx.getSource().sendFeedback(new StringTextComponent(String.format("[Create]: Server tick is slowed by %s ms now :)", tickTime)),true); + ctx.getSource().sendFeedback(new StringTextComponent("[Create]: use /killtps stop to bring server tick to regular speed again"),true); + + return 1; + }) + ) + ) + .then(Commands.literal("stop") + .executes(ctx -> { + //killtps stop + Create.lagger.setLagging(false); + ctx.getSource().sendFeedback(new StringTextComponent("[Create]: Server tick is back to regular speed"), false); + + return 1; + }) + ) + ); + } +} diff --git a/src/main/java/com/simibubi/create/foundation/command/ServerLagger.java b/src/main/java/com/simibubi/create/foundation/command/ServerLagger.java new file mode 100644 index 000000000..fed8e3a39 --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/command/ServerLagger.java @@ -0,0 +1,35 @@ +package com.simibubi.create.foundation.command; + +public class ServerLagger { + + private int tickTime; + private boolean isLagging = false; + + public void tick(){ + if (!isLagging || tickTime <= 0) + return; + + + try { + Thread.sleep(tickTime); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public void setTickTime(int tickTime){ + this.tickTime = Math.max(tickTime, 0); + } + + public void setLagging(boolean lagging){ + this.isLagging = lagging; + } + + public int getTickTime() { + return tickTime; + } + + public boolean isLagging() { + return isLagging; + } +} diff --git a/src/main/java/com/simibubi/create/foundation/utility/AllShapes.java b/src/main/java/com/simibubi/create/foundation/utility/AllShapes.java index 7966f7f61..0032fc6d4 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/AllShapes.java +++ b/src/main/java/com/simibubi/create/foundation/utility/AllShapes.java @@ -28,7 +28,7 @@ public class AllShapes { ; - @SuppressWarnings("deprecation") + private static final VoxelShape LOGISTICAL_CASING_MIDDLE_SHAPE = VoxelShapes.or( makeCuboidShape(1,0,1,15,16,15), @@ -42,7 +42,7 @@ public class AllShapes { CART_ASSEMBLER_SHAPE = VoxelShapes.or( VoxelShapes.fullCube(), makeCuboidShape(-2, 0, 1, 18, 13, 15)), - MECHANICAL_PISTON_HEAD_SHAPE_UP = Blocks.PISTON_HEAD.getShape(Blocks.PISTON_HEAD.getStateContainer().getBaseState().with(DirectionalBlock.FACING, Direction.UP).with(PistonHeadBlock.SHORT, true), null, null, null), + MECHANICAL_PISTON_HEAD_SHAPE_UP = Blocks.PISTON_HEAD.getStateContainer().getBaseState().with(DirectionalBlock.FACING, Direction.UP).with(PistonHeadBlock.SHORT, true).getShape(null, null), MECHANICAL_PISTON_EXTENDED_SHAPE_UP = VoxelShapes.or( SHORT_CASING_12_VOXEL.get(Direction.UP), FOUR_VOXEL_POLE.get(Direction.Axis.Y)),