Gamemode switching

This commit is contained in:
LOOHP
2020-08-08 03:51:49 +08:00
parent 82a5198e78
commit e6005abc5a
11 changed files with 212 additions and 14 deletions
@@ -57,6 +57,7 @@ import com.loohp.limbo.Utils.CustomStringUtils;
import com.loohp.limbo.Utils.DataTypeIO;
import com.loohp.limbo.Utils.MojangAPIUtils;
import com.loohp.limbo.Utils.MojangAPIUtils.SkinResponse;
import com.loohp.limbo.Utils.NamespacedKey;
import com.loohp.limbo.World.BlockPosition;
import com.loohp.limbo.World.DimensionRegistry;
import com.loohp.limbo.World.World;
@@ -154,6 +155,7 @@ public class ClientConnection extends Thread {
} catch (IOException e) {}
}
@SuppressWarnings("deprecation")
@Override
public void run() {
running = true;
@@ -258,8 +260,9 @@ public class ClientConnection extends Thread {
TimeUnit.MILLISECONDS.sleep(500);
ServerProperties p = Limbo.getInstance().getServerProperties();
PacketPlayOutLogin join = new PacketPlayOutLogin(player.getEntityId(), false, p.getDefaultGamemode(), new String[] {p.getLevelName().toString()}, DimensionRegistry.getCodec(), p.getLevelDimension().toString(), p.getLevelName().toString(), 0, (byte) p.getMaxPlayers(), 8, p.isReducedDebugInfo(), true, false, false);
PacketPlayOutLogin join = new PacketPlayOutLogin(player.getEntityId(), false, p.getDefaultGamemode(), Limbo.getInstance().getWorlds().stream().map(each -> new NamespacedKey(each.getName()).toString()).collect(Collectors.toList()).toArray(new String[Limbo.getInstance().getWorlds().size()]), DimensionRegistry.getCodec(), p.getWorldSpawn().getWorld(), 0, (byte) p.getMaxPlayers(), 8, p.isReducedDebugInfo(), true, false, false);
sendPacket(join);
player.setGamemodeSilent(p.getDefaultGamemode());
Location s = p.getWorldSpawn();
@@ -0,0 +1,37 @@
package com.loohp.limbo.Server.Packets;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class PacketPlayOutGameState extends PacketOut {
private int reason;
private float value;
public PacketPlayOutGameState(int reason, float value) {
this.reason = reason;
this.value = value;
}
public int getReason() {
return reason;
}
public float getValue() {
return value;
}
@Override
public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer);
output.writeByte(Packet.getPlayOut().get(getClass()));
output.writeByte(reason);
output.writeFloat(value);
return buffer.toByteArray();
}
}
@@ -7,6 +7,8 @@ import java.nio.charset.StandardCharsets;
import com.loohp.limbo.Utils.DataTypeIO;
import com.loohp.limbo.Utils.GameMode;
import com.loohp.limbo.Utils.NamespacedKey;
import com.loohp.limbo.World.World;
import net.querz.nbt.tag.CompoundTag;
@@ -28,7 +30,7 @@ public class PacketPlayOutLogin extends PacketOut {
private boolean isFlat;
public PacketPlayOutLogin(int entityId, boolean isHardcore, GameMode gamemode,
String[] worldsNames, CompoundTag dimensionCodec, String dimension, String worldName, long hashedSeed,
String[] worldsNames, CompoundTag dimensionCodec, World world, long hashedSeed,
byte maxPlayers, int viewDistance, boolean reducedDebugInfo, boolean enableRespawnScreen, boolean isDebug,
boolean isFlat) {
this.entityId = entityId;
@@ -36,8 +38,8 @@ public class PacketPlayOutLogin extends PacketOut {
this.gamemode = gamemode;
this.worldsNames = worldsNames;
this.dimensionCodec = dimensionCodec;
this.dimension = dimension;
this.worldName = worldName;
this.dimension = world.getEnvironment().getNamespacedKey().toString();
this.worldName = new NamespacedKey(world.getName()).toString();
this.hashedSeed = hashedSeed;
this.maxPlayers = maxPlayers;
this.viewDistance = viewDistance;
@@ -0,0 +1,80 @@
package com.loohp.limbo.Server.Packets;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import com.loohp.limbo.Utils.DataTypeIO;
import com.loohp.limbo.Utils.GameMode;
import com.loohp.limbo.Utils.NamespacedKey;
import com.loohp.limbo.World.World;
public class PacketPlayOutRespawn extends PacketOut {
private String dimension;
private String worldName;
private long hashedSeed;
private GameMode gamemode;
private boolean isDebug;
private boolean isFlat;
private boolean copyMetaData;
public PacketPlayOutRespawn(World world, long hashedSeed, GameMode gamemode, boolean isDebug,
boolean isFlat, boolean copyMetaData) {
this.dimension = world.getEnvironment().getNamespacedKey().toString();
this.worldName = new NamespacedKey(world.getName()).toString();
this.hashedSeed = hashedSeed;
this.gamemode = gamemode;
this.isDebug = isDebug;
this.isFlat = isFlat;
this.copyMetaData = copyMetaData;
}
public String getDimension() {
return dimension;
}
public String getWorldName() {
return worldName;
}
public long getHashedSeed() {
return hashedSeed;
}
public GameMode getGamemode() {
return gamemode;
}
public boolean isDebug() {
return isDebug;
}
public boolean isFlat() {
return isFlat;
}
public boolean isCopyMetaData() {
return copyMetaData;
}
@Override
public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer);
output.writeByte(Packet.getPlayOut().get(getClass()));
DataTypeIO.writeString(output, dimension, StandardCharsets.UTF_8);
DataTypeIO.writeString(output, worldName, StandardCharsets.UTF_8);
output.writeLong(hashedSeed);
output.writeByte((byte) gamemode.getId());
output.writeByte((byte) gamemode.getId());
output.writeBoolean(isDebug);
output.writeBoolean(isFlat);
output.writeBoolean(copyMetaData);
return buffer.toByteArray();
}
}