Time update
This commit is contained in:
@@ -8,20 +8,29 @@ import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
@Slf4j
|
||||
public class GameLoop extends Thread {
|
||||
@Autowired
|
||||
PlayerManager playerManager;
|
||||
|
||||
/* TPS */
|
||||
private int tps;
|
||||
private long pause;
|
||||
@Setter
|
||||
private boolean traceTPS = false;
|
||||
private int lowTps;
|
||||
|
||||
/* Time */
|
||||
private long gameTime;
|
||||
private Runnable gameTimeUpdateFunc;
|
||||
|
||||
public GameLoop() {
|
||||
super();
|
||||
setTps(20);
|
||||
setPercentWarnLowTps(5);
|
||||
setStartGameTime(0);
|
||||
}
|
||||
|
||||
public void setPercentWarnLowTps(int value) {
|
||||
@@ -42,6 +51,42 @@ public class GameLoop extends Thread {
|
||||
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
|
||||
public void run() {
|
||||
log.info("Target TPS: {}; Low TPS: {}", tps, lowTps);
|
||||
@@ -61,11 +106,20 @@ public class GameLoop extends Thread {
|
||||
|
||||
long futureTime = System.currentTimeMillis() + pause;
|
||||
|
||||
// code there //
|
||||
/* --- --- --- */
|
||||
|
||||
gameTimeUpdateFunc.run();
|
||||
|
||||
/* --- --- --- */
|
||||
|
||||
playerManager.getBroadcastChannel().sendTimeUpdate(gameTime);
|
||||
|
||||
/* --- --- --- */
|
||||
|
||||
factTps++;
|
||||
try {
|
||||
Thread.sleep(futureTime - System.currentTimeMillis());
|
||||
long pause = futureTime - System.currentTimeMillis();
|
||||
Thread.sleep((pause <= 0 ? 0 : pause));
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
*/
|
||||
package mc.core;
|
||||
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.NetChannel;
|
||||
|
||||
public interface PlayerManager {
|
||||
void addPlayer(Player player);
|
||||
Player getPlayer(String name);
|
||||
Player getPlayerById(int id);
|
||||
void removePlayer(Player player);
|
||||
void bloadcastWrite(SCPacket packet);
|
||||
NetChannel getBroadcastChannel();
|
||||
}
|
||||
|
||||
@@ -9,7 +9,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.Config;
|
||||
import mc.core.Player;
|
||||
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 java.util.ArrayList;
|
||||
@@ -62,8 +63,8 @@ public class InMemoryPlayerManager implements PlayerManager, Runnable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bloadcastWrite(final SCPacket packet) {
|
||||
players.forEach(player -> player.getChannel().writeAndFlush(packet));
|
||||
public NetChannel getBroadcastChannel() {
|
||||
return new BroadcastNetChannel(players.stream());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
30
src/main/java/mc/core/network/BroadcastNetChannel.java
Normal file
30
src/main/java/mc/core/network/BroadcastNetChannel.java
Normal 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));
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ package mc.core.network;
|
||||
|
||||
public interface NetChannel {
|
||||
void sendKeepAlive();
|
||||
void sendTimeUpdate(long value);
|
||||
|
||||
void writeAndFlush(SCPacket pkt);
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ public abstract class NetStream {
|
||||
public abstract void writeBytes(byte[] buffer);
|
||||
public abstract void writeShort(int value);
|
||||
public abstract void writeInt(int value);
|
||||
public abstract void writeLong(long value);
|
||||
public abstract void writeFloat(float value);
|
||||
public abstract void writeDouble(double value);
|
||||
public abstract void writeString(String value);
|
||||
|
||||
@@ -88,6 +88,18 @@ public class ByteArrayOutputNetStream extends NetStream_p125 {
|
||||
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
|
||||
public void writeFloat(float value) {
|
||||
writeInt(Float.floatToIntBits(value));
|
||||
@@ -95,16 +107,7 @@ public class ByteArrayOutputNetStream extends NetStream_p125 {
|
||||
|
||||
@Override
|
||||
public void writeDouble(double value) {
|
||||
long v = 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)));
|
||||
writeLong(Double.doubleToLongBits(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,6 +10,7 @@ import mc.core.network.NetChannel;
|
||||
import mc.core.network.SCPacket;
|
||||
import mc.core.network.proto_125.packets.KeepAlivePacket;
|
||||
import mc.core.network.proto_125.packets.PingPacket;
|
||||
import mc.core.network.proto_125.packets.TimeUpdatePacket;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class WrapperNetChannel implements NetChannel {
|
||||
@@ -20,6 +21,11 @@ public class WrapperNetChannel implements NetChannel {
|
||||
channel.writeAndFlush(new KeepAlivePacket());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendTimeUpdate(long value) {
|
||||
channel.writeAndFlush(new TimeUpdatePacket(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeAndFlush(SCPacket pkt) {
|
||||
channel.writeAndFlush(pkt);
|
||||
|
||||
@@ -86,6 +86,11 @@ public class WrapperNetStream extends NetStream_p125 {
|
||||
byteBuf.writeInt(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeLong(long value) {
|
||||
byteBuf.writeLong(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeFloat(float value) {
|
||||
byteBuf.writeFloat(value);
|
||||
|
||||
@@ -15,6 +15,7 @@ public class PacketManager {
|
||||
.put(0x00, KeepAlivePacket.class)
|
||||
.put(0x01, LoginPacket.class)
|
||||
.put(0x02, HandshakePacket.class)
|
||||
.put(0x04, TimeUpdatePacket.class)
|
||||
.put(0x06, SpawnPositionPacket.class)
|
||||
.put(0x0D, PositionAndLookPacket.class)
|
||||
.put(0x32, ChunkAllocationPacket.class)
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
</bean>
|
||||
|
||||
<bean id="gameLoop" class="mc.core.GameLoop">
|
||||
<property name="tps" value="20"/>
|
||||
<property name="traceTPS" value="true"/>
|
||||
<property name="startGameTime" value="6000"/>
|
||||
<property name="timeMode" value="realtime"/>
|
||||
</bean>
|
||||
|
||||
<bean id="playerManager" class="mc.core.embedded.InMemoryPlayerManager">
|
||||
|
||||
Reference in New Issue
Block a user