Skip to content

Commit

Permalink
Fix #110, improvements on server side
Browse files Browse the repository at this point in the history
-Added the ability to join the dedicated server when in tickrate 0
-Fixed commands not sending in the server interface when in tickrate 0
-Improved tickrate command a bit
  • Loading branch information
Scribble authored and Scribble committed Oct 1, 2021
1 parent af8681e commit 1b020f8
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ apply plugin: 'org.spongepowered.mixin'
//Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.


version = "Alpha6"
version = "Alpha7-WIP"
group = "de.scribble.lp.tastools" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "TASmod-1.12.2"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static void firePlayerJoinedServerSide(EntityPlayerMP player) {
TASmod.logger.info("Firing login events for {} on the SERVER", player.getName());
TickSyncServer.joinServer(player);
TASmod.containerStateServer.joinServer(player);
TickrateChangerServer.joinServer(player);
}

/**
Expand All @@ -32,8 +33,7 @@ public static void firePlayerJoinedServerSide(EntityPlayerMP player) {
* @param player The player that left the server
*/
public static void firePlayerLeaveServerSide(EntityPlayerMP player) {
TASmod.logger.info("Firing logout events for {} on the SERVER", player.getName());
TickrateChangerServer.leaveServer(player);
// TASmod.logger.info("Firing logout events for {} on the SERVER", player.getName());
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/de/scribble/lp/tasmod/mixin/MixinMinecraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public abstract class MixinMinecraft {
@Shadow
private GuiScreen currentScreen;

// private int faketick=0;

@Inject(method = "runGameLoop", at = @At(value = "HEAD"))
public void injectRunGameLoop(CallbackInfo ci) {
// TASmod
Expand All @@ -44,6 +46,15 @@ public void injectRunGameLoop(CallbackInfo ci) {
if(((Minecraft) (Object) this).player!=null) {
ClientProxy.hud.tick();
}

//Fake tickrate loop when you are in tickrate 0
// if(TickrateChangerClient.TICKS_PER_SECOND==0) {
// faketick++;
// if(faketick>=Minecraft.getDebugFPS()/3) { //Not yet used but maybe in the future
// faketick=0;
// }
// }

while (Keyboard.next()) {
ClientProxy.virtual.updateNextKeyboard(Keyboard.getEventKey(), Keyboard.getEventKeyState(), Keyboard.getEventCharacter());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import de.scribble.lp.tasmod.tickratechanger.TickrateChangerServer;
import de.scribble.lp.tasmod.ticksync.TickSyncPackage;
import de.scribble.lp.tasmod.ticksync.TickSyncServer;
import net.minecraft.network.NetworkSystem;
import net.minecraft.server.MinecraftServer;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@Mixin(MinecraftServer.class)
public abstract class MixinMinecraftServer {
Expand Down Expand Up @@ -74,6 +77,11 @@ public long redirectMathMax(long oneLong, long i) {

@Shadow
private Queue<FutureTask<?>> futureTaskQueue;

@Shadow
private NetworkSystem networkSystem;

private int faketick=0;

@Redirect(method = "run", at = @At(value = "INVOKE", target = "Ljava/lang/Thread;sleep(J)V"))
public void redirectThreadSleep(long msToTick) {
Expand All @@ -87,6 +95,12 @@ public void redirectThreadSleep(long msToTick) {
for (long o = 0; o < msToTick; o++) {
if(TickrateChangerServer.TICKS_PER_SECOND==0) {
currentTime=System.currentTimeMillis();
faketick++;
if(faketick>=20) {
faketick=0;
networkSystem.networkTick();
runPendingCommands();
}
}
if (TickrateChangerServer.INTERRUPT) {
currentTime = System.currentTimeMillis();
Expand All @@ -102,6 +116,7 @@ public void redirectThreadSleep(long msToTick) {
}
}
}

try {
Thread.sleep(1L);
} catch (InterruptedException e) {
Expand All @@ -111,6 +126,14 @@ public void redirectThreadSleep(long msToTick) {
}
}

@SideOnly(Side.SERVER)
private void runPendingCommands() {
if((MinecraftServer)(Object)this instanceof net.minecraft.server.dedicated.DedicatedServer) {
net.minecraft.server.dedicated.DedicatedServer server=(net.minecraft.server.dedicated.DedicatedServer)(MinecraftServer)(Object)this;
server.executePendingCommands();
}
}

// =====================================================================================================================================

// @Inject(method = "tick", at = @At("HEAD"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.TextComponentString;

public class CommandTickrate extends CommandBase{
@Override
Expand All @@ -27,6 +28,10 @@ public String getUsage(ICommandSender sender) {

@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
if(args.length==0) {
sender.sendMessage(new TextComponentString("Current tickrate: "+ TickrateChangerServer.TICKS_PER_SECOND));
return;
}
float tickrate= Float.parseFloat(args[0]);
TickrateChangerServer.changeServerTickrate(tickrate);
TickrateChangerServer.changeClientTickrate(tickrate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,20 @@ public static void pauseUnpauseGame() {
pauseUnpauseClient();
}
}

public static void advanceTick() {
if(Minecraft.getMinecraft().world!=null) {
advanceServerTick();
}else {
advanceClientTick();
}
}

/**
* Bypasses the tick system
*/
public static void bypass() {
//Stopping any playback or recording if you are in tickrate 0 and the GuiControls
if(Minecraft.getMinecraft().currentScreen instanceof GuiControls) {
if(TICKS_PER_SECOND==0&&WASZERO==false) {
changeClientTickrate(20);
Expand All @@ -58,6 +61,7 @@ public static void bypass() {
changeClientTickrate(0);
WASZERO=false;
}

}

public static void advanceClientTick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import de.scribble.lp.tasmod.CommonProxy;
import de.scribble.lp.tasmod.TASmod;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;

public class TickrateChangerServer {
public static float TICKS_PER_SECOND=20F;
Expand Down Expand Up @@ -50,16 +49,13 @@ public static void advanceTick() {
}

/**
* Fired when a player left the server
* Fired when a player joined the server
* @param player
*/
public static void leaveServer(EntityPlayerMP player) {
//TODO Test this in multiplayer
if(TASmod.getServerInstance().getPlayerList().getCurrentPlayerCount()==1) {
if (TickrateChangerServer.TICKS_PER_SECOND == 0 || ADVANCE_TICK) {
TASmod.logger.info("Changing tickrate to 20 since the last player left the server");
TickrateChangerServer.changeServerTickrate(20F);
}
public static void joinServer(EntityPlayerMP player) {
if(TASmod.getServerInstance().isDedicatedServer()) {
TASmod.logger.info("Sending the current tickrate ({}) to {}", TICKS_PER_SECOND, player.getName());
CommonProxy.NETWORK.sendTo(new TickratePacket(false, TICKS_PER_SECOND, false), player);
}
}
}

0 comments on commit 1b020f8

Please sign in to comment.