lag on command

added a utility that delays server tick by a set time and a command to control it
This commit is contained in:
zelophed 2019-12-15 01:13:36 +01:00 committed by Zelophed
parent 43980d550d
commit 3e8ac662f1
6 changed files with 128 additions and 3 deletions

View file

@ -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<VillagerProfession> 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() {

View file

@ -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();

View file

@ -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<CommandSource> dispatcher){
KillTPSCommand.register(dispatcher);
}
}

View file

@ -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<CommandSource> 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 <tickTime> 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 <tickTime> 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;
})
)
);
}
}

View file

@ -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;
}
}

View file

@ -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)),