Simplify logic

This commit is contained in:
DoctorMacc 2020-04-13 10:46:13 -04:00
parent daa1888c09
commit 5c62b47175
2 changed files with 16 additions and 27 deletions

View file

@ -66,12 +66,6 @@
<version>8.3.1</version> <version>8.3.1</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>com.nukkitx.fastutil</groupId>
<artifactId>fastutil-long-boolean-maps</artifactId>
<version>8.3.1</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>com.nukkitx.fastutil</groupId> <groupId>com.nukkitx.fastutil</groupId>
<artifactId>fastutil-object-long-maps</artifactId> <artifactId>fastutil-object-long-maps</artifactId>

View file

@ -42,44 +42,39 @@ import com.nukkitx.protocol.bedrock.packet.SetTimePacket;
public class JavaUpdateTimeTranslator extends PacketTranslator<ServerUpdateTimePacket> { public class JavaUpdateTimeTranslator extends PacketTranslator<ServerUpdateTimePacket> {
// doDaylightCycle per-player for multi-world support // doDaylightCycle per-player for multi-world support
static Long2BooleanMap daylightCycles = new Long2BooleanOpenHashMap(); //static Long2BooleanMap daylightCycles = new Long2BooleanOpenHashMap();
// If negative, the last time is stored so we know it's not some plugin behavior doing weird things. // If negative, the last time is stored so we know it's not some plugin behavior doing weird things.
// Per-player for multi-world support
static Long2LongMap lastRecordedTimes = new Long2LongOpenHashMap(); static Long2LongMap lastRecordedTimes = new Long2LongOpenHashMap();
@Override @Override
public void translate(ServerUpdateTimePacket packet, GeyserSession session) { public void translate(ServerUpdateTimePacket packet, GeyserSession session) {
boolean doDayLightCycle = daylightCycles.getOrDefault(session.getPlayerEntity().getEntityId(), true); // Bedrock sends a GameRulesChangedPacket if there is no daylight cycle
// Java just sends a negative long if there is no daylight cycle
//boolean doDayLightCycle = daylightCycles.getOrDefault(session.getPlayerEntity().getEntityId(), true);
long lastTime = lastRecordedTimes.getOrDefault(session.getPlayerEntity().getEntityId(), 0); long lastTime = lastRecordedTimes.getOrDefault(session.getPlayerEntity().getEntityId(), 0);
long time = packet.getTime(); long time = packet.getTime();
if ((!doDayLightCycle && time > 0 && lastTime != time) || (doDayLightCycle && time < 0)) { if (lastTime != time) {
// doDaylightCycle is different than the client and we don't know // https://minecraft.gamepedia.com/Day-night_cycle#24-hour_Minecraft_day
// Time is set either way as a reference point for the current time SetTimePacket setTimePacket = new SetTimePacket();
setTime(time, session); setTimePacket.setTime((int) Math.abs(time) % 24000);
setDoDayLightGamerule(session, !doDayLightCycle); session.getUpstream().sendPacket(setTimePacket);
} else if (time > 0) { // TODO: Performance efficient to always do this?
// doDaylightCycle is true and we know
setTime(time, session);
} else if (time < 0) {
setTime(time, session);
// Only written to if negative to ease performance
lastRecordedTimes.put(session.getPlayerEntity().getEntityId(), time); lastRecordedTimes.put(session.getPlayerEntity().getEntityId(), time);
} }
} if (lastTime < 0 && time > 0) {
setDoDayLightGamerule(session, true);
private void setTime(long time, GeyserSession session) { } else if (lastTime != time && time < 0) {
// https://minecraft.gamepedia.com/Day-night_cycle#24-hour_Minecraft_day setDoDayLightGamerule(session, false);
SetTimePacket setTimePacket = new SetTimePacket(); }
setTimePacket.setTime((int) Math.abs(time) % 24000);
session.getUpstream().sendPacket(setTimePacket);
} }
private void setDoDayLightGamerule(GeyserSession session, boolean doCycle) { private void setDoDayLightGamerule(GeyserSession session, boolean doCycle) {
GameRulesChangedPacket gameRulesChangedPacket = new GameRulesChangedPacket(); GameRulesChangedPacket gameRulesChangedPacket = new GameRulesChangedPacket();
gameRulesChangedPacket.getGameRules().add(new GameRuleData<>("dodaylightcycle", doCycle)); gameRulesChangedPacket.getGameRules().add(new GameRuleData<>("dodaylightcycle", doCycle));
session.getUpstream().sendPacket(gameRulesChangedPacket); session.getUpstream().sendPacket(gameRulesChangedPacket);
daylightCycles.put(session.getPlayerEntity().getEntityId(), doCycle);
} }
} }