Archived
0

Time update

This commit is contained in:
2018-04-21 15:34:25 +03:00
parent abfbfda926
commit 6e8f6f14d1
12 changed files with 149 additions and 19 deletions

View File

@@ -8,20 +8,29 @@ import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import java.util.Calendar;
@Slf4j @Slf4j
public class GameLoop extends Thread { public class GameLoop extends Thread {
@Autowired @Autowired
PlayerManager playerManager; PlayerManager playerManager;
/* TPS */
private int tps; private int tps;
private long pause; private long pause;
@Setter @Setter
private boolean traceTPS = false; private boolean traceTPS = false;
private int lowTps; private int lowTps;
/* Time */
private long gameTime;
private Runnable gameTimeUpdateFunc;
public GameLoop() { public GameLoop() {
super(); super();
setTps(20); setTps(20);
setPercentWarnLowTps(5); setPercentWarnLowTps(5);
setStartGameTime(0);
} }
public void setPercentWarnLowTps(int value) { public void setPercentWarnLowTps(int value) {
@@ -42,6 +51,42 @@ public class GameLoop extends Thread {
this.pause = (1000 / tps); this.pause = (1000 / tps);
} }
public void setStartGameTime(long value) {
this.gameTime = value;
}
public void setTimeMode(String mode) {
if (mode.equals("0") || mode.equalsIgnoreCase("idle")) {
gameTimeUpdateFunc = () -> {};
} else if (mode.equalsIgnoreCase("realtime")) {
gameTimeUpdateFunc = () -> {
final long DIFF = 21600L;
final long HOUR24 = 86400L;
final long SYSTIME = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(SYSTIME);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long time = (SYSTIME - calendar.getTimeInMillis())/1000;
if (time < DIFF) time += HOUR24;
gameTime = (long) ((time - DIFF) / 3.6);
};
} else {
if (!mode.equalsIgnoreCase("normal")) {
log.warn("Unknown time mode: {}. Set normal mode", mode);
}
gameTimeUpdateFunc = () -> {
gameTime++;
if (gameTime > 24000) gameTime = 0;
};
}
}
@Override @Override
public void run() { public void run() {
log.info("Target TPS: {}; Low TPS: {}", tps, lowTps); log.info("Target TPS: {}; Low TPS: {}", tps, lowTps);
@@ -61,11 +106,20 @@ public class GameLoop extends Thread {
long futureTime = System.currentTimeMillis() + pause; long futureTime = System.currentTimeMillis() + pause;
// code there // /* --- --- --- */
gameTimeUpdateFunc.run();
/* --- --- --- */
playerManager.getBroadcastChannel().sendTimeUpdate(gameTime);
/* --- --- --- */
factTps++; factTps++;
try { try {
Thread.sleep(futureTime - System.currentTimeMillis()); long pause = futureTime - System.currentTimeMillis();
Thread.sleep((pause <= 0 ? 0 : pause));
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
} }
} }

View File

@@ -4,12 +4,12 @@
*/ */
package mc.core; package mc.core;
import mc.core.network.SCPacket; import mc.core.network.NetChannel;
public interface PlayerManager { public interface PlayerManager {
void addPlayer(Player player); void addPlayer(Player player);
Player getPlayer(String name); Player getPlayer(String name);
Player getPlayerById(int id); Player getPlayerById(int id);
void removePlayer(Player player); void removePlayer(Player player);
void bloadcastWrite(SCPacket packet); NetChannel getBroadcastChannel();
} }

View File

@@ -9,7 +9,8 @@ import lombok.extern.slf4j.Slf4j;
import mc.core.Config; import mc.core.Config;
import mc.core.Player; import mc.core.Player;
import mc.core.PlayerManager; import mc.core.PlayerManager;
import mc.core.network.SCPacket; import mc.core.network.BroadcastNetChannel;
import mc.core.network.NetChannel;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList; import java.util.ArrayList;
@@ -62,8 +63,8 @@ public class InMemoryPlayerManager implements PlayerManager, Runnable {
} }
@Override @Override
public void bloadcastWrite(final SCPacket packet) { public NetChannel getBroadcastChannel() {
players.forEach(player -> player.getChannel().writeAndFlush(packet)); return new BroadcastNetChannel(players.stream());
} }
@Override @Override

View File

@@ -0,0 +1,30 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-04-21
*/
package mc.core.network;
import lombok.RequiredArgsConstructor;
import mc.core.Player;
import java.util.stream.Stream;
@RequiredArgsConstructor
public class BroadcastNetChannel implements NetChannel {
private final Stream<Player> playerStream;
@Override
public void sendKeepAlive() {
playerStream.forEach(player -> player.getChannel().sendKeepAlive());
}
@Override
public void sendTimeUpdate(final long value) {
playerStream.forEach(player -> player.getChannel().sendTimeUpdate(value));
}
@Override
public void writeAndFlush(final SCPacket pkt) {
playerStream.forEach(player -> player.getChannel().writeAndFlush(pkt));
}
}

View File

@@ -6,6 +6,7 @@ package mc.core.network;
public interface NetChannel { public interface NetChannel {
void sendKeepAlive(); void sendKeepAlive();
void sendTimeUpdate(long value);
void writeAndFlush(SCPacket pkt); void writeAndFlush(SCPacket pkt);
} }

View File

@@ -28,6 +28,7 @@ public abstract class NetStream {
public abstract void writeBytes(byte[] buffer); public abstract void writeBytes(byte[] buffer);
public abstract void writeShort(int value); public abstract void writeShort(int value);
public abstract void writeInt(int value); public abstract void writeInt(int value);
public abstract void writeLong(long value);
public abstract void writeFloat(float value); public abstract void writeFloat(float value);
public abstract void writeDouble(double value); public abstract void writeDouble(double value);
public abstract void writeString(String value); public abstract void writeString(String value);

View File

@@ -88,6 +88,18 @@ public class ByteArrayOutputNetStream extends NetStream_p125 {
baos.write((byte) value); baos.write((byte) value);
} }
@Override
public void writeLong(long value) {
baos.write((byte)((int)(value >>> 56)));
baos.write((byte)((int)(value >>> 48)));
baos.write((byte)((int)(value >>> 40)));
baos.write((byte)((int)(value >>> 32)));
baos.write((byte)((int)(value >>> 24)));
baos.write((byte)((int)(value >>> 16)));
baos.write((byte)((int)(value >>> 8)));
baos.write((byte)((int)(value)));
}
@Override @Override
public void writeFloat(float value) { public void writeFloat(float value) {
writeInt(Float.floatToIntBits(value)); writeInt(Float.floatToIntBits(value));
@@ -95,16 +107,7 @@ public class ByteArrayOutputNetStream extends NetStream_p125 {
@Override @Override
public void writeDouble(double value) { public void writeDouble(double value) {
long v = Double.doubleToLongBits(value); writeLong(Double.doubleToLongBits(value));
baos.write((byte)((int)(v >>> 56)));
baos.write((byte)((int)(v >>> 48)));
baos.write((byte)((int)(v >>> 40)));
baos.write((byte)((int)(v >>> 32)));
baos.write((byte)((int)(v >>> 24)));
baos.write((byte)((int)(v >>> 16)));
baos.write((byte)((int)(v >>> 8)));
baos.write((byte)((int)(v)));
} }
@Override @Override

View File

@@ -10,6 +10,7 @@ import mc.core.network.NetChannel;
import mc.core.network.SCPacket; import mc.core.network.SCPacket;
import mc.core.network.proto_125.packets.KeepAlivePacket; import mc.core.network.proto_125.packets.KeepAlivePacket;
import mc.core.network.proto_125.packets.PingPacket; import mc.core.network.proto_125.packets.PingPacket;
import mc.core.network.proto_125.packets.TimeUpdatePacket;
@RequiredArgsConstructor @RequiredArgsConstructor
public class WrapperNetChannel implements NetChannel { public class WrapperNetChannel implements NetChannel {
@@ -20,6 +21,11 @@ public class WrapperNetChannel implements NetChannel {
channel.writeAndFlush(new KeepAlivePacket()); channel.writeAndFlush(new KeepAlivePacket());
} }
@Override
public void sendTimeUpdate(long value) {
channel.writeAndFlush(new TimeUpdatePacket(value));
}
@Override @Override
public void writeAndFlush(SCPacket pkt) { public void writeAndFlush(SCPacket pkt) {
channel.writeAndFlush(pkt); channel.writeAndFlush(pkt);

View File

@@ -86,6 +86,11 @@ public class WrapperNetStream extends NetStream_p125 {
byteBuf.writeInt(value); byteBuf.writeInt(value);
} }
@Override
public void writeLong(long value) {
byteBuf.writeLong(value);
}
@Override @Override
public void writeFloat(float value) { public void writeFloat(float value) {
byteBuf.writeFloat(value); byteBuf.writeFloat(value);

View File

@@ -15,6 +15,7 @@ public class PacketManager {
.put(0x00, KeepAlivePacket.class) .put(0x00, KeepAlivePacket.class)
.put(0x01, LoginPacket.class) .put(0x01, LoginPacket.class)
.put(0x02, HandshakePacket.class) .put(0x02, HandshakePacket.class)
.put(0x04, TimeUpdatePacket.class)
.put(0x06, SpawnPositionPacket.class) .put(0x06, SpawnPositionPacket.class)
.put(0x0D, PositionAndLookPacket.class) .put(0x0D, PositionAndLookPacket.class)
.put(0x32, ChunkAllocationPacket.class) .put(0x32, ChunkAllocationPacket.class)

View File

@@ -0,0 +1,28 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-04-21
*/
package mc.core.network.proto_125.packets;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import mc.core.network.SCPacket;
import mc.core.network.proto_125.ByteArrayOutputNetStream;
@NoArgsConstructor
@AllArgsConstructor
@Setter
@ToString
public class TimeUpdatePacket implements SCPacket {
private long time;
@Override
public byte[] toByteArray() {
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
netStream.writeLong(time);
return netStream.toByteArray();
}
}

View File

@@ -12,8 +12,8 @@
</bean> </bean>
<bean id="gameLoop" class="mc.core.GameLoop"> <bean id="gameLoop" class="mc.core.GameLoop">
<property name="tps" value="20"/> <property name="startGameTime" value="6000"/>
<property name="traceTPS" value="true"/> <property name="timeMode" value="realtime"/>
</bean> </bean>
<bean id="playerManager" class="mc.core.embedded.InMemoryPlayerManager"> <bean id="playerManager" class="mc.core.embedded.InMemoryPlayerManager">