minimal internal restructuring (world now a object of player), Chunk::setBlock function, correct chunk data storage, save data of join packet

This commit is contained in:
bixilon 2020-06-05 03:59:02 +02:00
parent d98c6f511e
commit ac516fa59c
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
10 changed files with 110 additions and 24 deletions

View File

@ -19,15 +19,10 @@ public class Chunk {
throw new IllegalArgumentException(String.format("Invalid chunk location %s %s %s", x, y, z)); throw new IllegalArgumentException(String.format("Invalid chunk location %s %s %s", x, y, z));
} }
byte section = (byte) (y / 16); byte section = (byte) (y / 16);
if (section == 0) { return chunks.get(section).getBlock(x, y % 16, z);
return Block.COBBLESTONE;
} else if (section == 1) {
return Block.DIRT;
} else {
return Block.AIR;
}
//ToDo
//return chunks.get(section).getBlock(x, y % 16,z);
} }
public void setBlock(int x, int y, int z, Block block) {
chunks.get(y / 16).setBlock(x, y % 16, z, block);
}
} }

View File

@ -21,4 +21,8 @@ public class ChunkNibble {
public Block getBlock(int x, int y, int z) { public Block getBlock(int x, int y, int z) {
return getBlock(new ChunkNibbleLocation(x, y, z)); return getBlock(new ChunkNibbleLocation(x, y, z));
} }
public void setBlock(int x, int y, int z, Block block) {
blocks.replace(new ChunkNibbleLocation(x, y, z), block);
}
} }

View File

@ -3,6 +3,7 @@ package de.bixilon.minosoft.game.datatypes;
import de.bixilon.minosoft.game.datatypes.blocks.Block; import de.bixilon.minosoft.game.datatypes.blocks.Block;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
/** /**
* Collection of ChunkColumns * Collection of ChunkColumns
@ -10,6 +11,7 @@ import java.util.HashMap;
public class World { public class World {
public final HashMap<ChunkLocation, Chunk> chunks; public final HashMap<ChunkLocation, Chunk> chunks;
final String name; final String name;
boolean hardcore;
public World(String name) { public World(String name) {
this.name = name; this.name = name;
@ -32,4 +34,35 @@ public class World {
} }
return Block.AIR; return Block.AIR;
} }
public void setBlock(BlockPosition pos, Block block) {
if (getChunk(pos.getChunkLocation()) != null) {
getChunk(pos.getChunkLocation()).setBlock(pos.getX() % 16, pos.getX(), pos.getZ() % 16, block);
} else {
//throw new IllegalAccessException("Chunk is not loaded!");
// ToDo
}
}
public void unloadChunk(ChunkLocation location) {
chunks.remove(location);
}
public void setChunk(ChunkLocation location, Chunk chunk) {
chunks.replace(location, chunk);
}
public void setChunks(HashMap<ChunkLocation, Chunk> chunkMap) {
for (Map.Entry<ChunkLocation, Chunk> set : chunkMap.entrySet()) {
chunks.replace(set.getKey(), set.getValue());
}
}
public boolean isHardcore() {
return hardcore;
}
public void setHardcore(boolean hardcore) {
this.hardcore = hardcore;
}
} }

View File

@ -1,5 +1,7 @@
package de.bixilon.minosoft.objects; package de.bixilon.minosoft.objects;
import de.bixilon.minosoft.game.datatypes.GameMode;
import de.bixilon.minosoft.game.datatypes.World;
import de.bixilon.minosoft.game.datatypes.player.Location; import de.bixilon.minosoft.game.datatypes.player.Location;
import java.util.UUID; import java.util.UUID;
@ -10,6 +12,9 @@ public class Player {
short food; short food;
float saturation; float saturation;
Location spawnLocation; Location spawnLocation;
int entityId;
GameMode gameMode;
World world = new World("world");
public Player(Account acc) { public Player(Account acc) {
this.acc = acc; this.acc = acc;
@ -48,6 +53,10 @@ public class Player {
this.saturation = saturation; this.saturation = saturation;
} }
public float getSaturation() {
return saturation;
}
public Location getSpawnLocation() { public Location getSpawnLocation() {
return spawnLocation; return spawnLocation;
} }
@ -55,4 +64,24 @@ public class Player {
public void setSpawnLocation(Location spawnLocation) { public void setSpawnLocation(Location spawnLocation) {
this.spawnLocation = spawnLocation; this.spawnLocation = spawnLocation;
} }
public GameMode getGameMode() {
return gameMode;
}
public void setGameMode(GameMode gameMode) {
this.gameMode = gameMode;
}
public int getEntityId() {
return entityId;
}
public void setEntityId(int entityId) {
this.entityId = entityId;
}
public World getWorld() {
return world;
}
} }

View File

@ -1,7 +1,6 @@
package de.bixilon.minosoft.protocol.network; package de.bixilon.minosoft.protocol.network;
import de.bixilon.minosoft.Config; import de.bixilon.minosoft.Config;
import de.bixilon.minosoft.game.datatypes.World;
import de.bixilon.minosoft.logging.Log; import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.objects.Account; import de.bixilon.minosoft.objects.Account;
import de.bixilon.minosoft.objects.Player; import de.bixilon.minosoft.objects.Player;
@ -25,7 +24,6 @@ public class Connection {
private final PacketHandler handler; private final PacketHandler handler;
private final ArrayList<ClientboundPacket> handlingQueue; private final ArrayList<ClientboundPacket> handlingQueue;
private Player player = new Player(new Account(Config.username, Config.password)); private Player player = new Player(new Account(Config.username, Config.password));
private World world = new World("world");
private ConnectionState state = ConnectionState.DISCONNECTED; private ConnectionState state = ConnectionState.DISCONNECTED;
private boolean onlyPing; private boolean onlyPing;
@ -135,8 +133,4 @@ public class Connection {
}); });
handleThread.start(); handleThread.start();
} }
public World getWorld() {
return this.world;
}
} }

View File

@ -43,11 +43,39 @@ public class PacketJoinGame implements ClientboundPacket {
@Override @Override
public void log() { public void log() {
Log.protocol(String.format("Receiving join game packet (entityId=%s, gameMode=%s, dimension=%s, difficulty=%s)", entityId, gameMode.name(), dimension.name(), difficulty.name())); Log.protocol(String.format("Receiving join game packet (entityId=%s, gameMode=%s, dimension=%s, difficulty=%s, hardcore=%s)", entityId, gameMode.name(), dimension.name(), difficulty.name(), hardcore));
} }
@Override @Override
public void handle(PacketHandler h) { public void handle(PacketHandler h) {
h.handle(this); h.handle(this);
} }
public boolean isHardcore() {
return hardcore;
}
public int getEntityId() {
return entityId;
}
public GameMode getGameMode() {
return gameMode;
}
public int getMaxPlayers() {
return maxPlayers;
}
public LevelType getLevelType() {
return levelType;
}
public Difficulty getDifficulty() {
return difficulty;
}
public Dimension getDimension() {
return dimension;
}
} }

View File

@ -6,7 +6,7 @@ import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
import de.bixilon.minosoft.protocol.protocol.PacketHandler; import de.bixilon.minosoft.protocol.protocol.PacketHandler;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
public class PacketPluginMessageReceived implements ClientboundPacket { public class PacketPluginMessageReceiving implements ClientboundPacket {
String channel; String channel;
byte[] data; byte[] data;

View File

@ -6,12 +6,12 @@ import de.bixilon.minosoft.protocol.protocol.OutPacketBuffer;
import de.bixilon.minosoft.protocol.protocol.Packets; import de.bixilon.minosoft.protocol.protocol.Packets;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
public class PacketPluginMessageSended implements ServerboundPacket { public class PacketPluginMessageSending implements ServerboundPacket {
public final String channel; public final String channel;
public final byte[] data; public final byte[] data;
public PacketPluginMessageSended(String channel, byte[] data) { public PacketPluginMessageSending(String channel, byte[] data) {
this.channel = channel; this.channel = channel;
this.data = data; this.data = data;
} }

View File

@ -10,7 +10,7 @@ import de.bixilon.minosoft.protocol.packets.clientbound.status.PacketStatusPong;
import de.bixilon.minosoft.protocol.packets.clientbound.status.PacketStatusResponse; import de.bixilon.minosoft.protocol.packets.clientbound.status.PacketStatusResponse;
import de.bixilon.minosoft.protocol.packets.serverbound.login.PacketEncryptionResponse; import de.bixilon.minosoft.protocol.packets.serverbound.login.PacketEncryptionResponse;
import de.bixilon.minosoft.protocol.packets.serverbound.play.PacketKeepAliveResponse; import de.bixilon.minosoft.protocol.packets.serverbound.play.PacketKeepAliveResponse;
import de.bixilon.minosoft.protocol.packets.serverbound.play.PacketPluginMessageSended; import de.bixilon.minosoft.protocol.packets.serverbound.play.PacketPluginMessageSending;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import java.math.BigInteger; import java.math.BigInteger;
@ -51,6 +51,9 @@ public class PacketHandler {
} }
public void handle(PacketJoinGame pkg) { public void handle(PacketJoinGame pkg) {
connection.getPlayer().setGameMode(pkg.getGameMode());
connection.getPlayer().setEntityId(pkg.getEntityId());
connection.getPlayer().getWorld().setHardcore(pkg.isHardcore());
} }
public void handle(PacketLoginDisconnect pkg) { public void handle(PacketLoginDisconnect pkg) {
@ -69,7 +72,7 @@ public class PacketHandler {
} }
public void handle(PacketChunkBulk pkg) { public void handle(PacketChunkBulk pkg) {
//ToDo connection.getPlayer().getWorld().setChunks(pkg.getChunkMap());
} }
public void handle(PacketUpdateHealth pkg) { public void handle(PacketUpdateHealth pkg) {
@ -78,14 +81,14 @@ public class PacketHandler {
connection.getPlayer().setSaturation(pkg.getSaturation()); connection.getPlayer().setSaturation(pkg.getSaturation());
} }
public void handle(PacketPluginMessageReceived pkg) { public void handle(PacketPluginMessageReceiving pkg) {
if (pkg.getChannel().equals("MC|Brand")) { if (pkg.getChannel().equals("MC|Brand")) {
// server brand received // server brand received
Log.info(String.format("Server is running %s on version %s", new String(pkg.getData()), connection.getVersion().getName())); Log.info(String.format("Server is running %s on version %s", new String(pkg.getData()), connection.getVersion().getName()));
// send back own brand // send back own brand
// ToDo option to toggle for minosoft or original minecraft // ToDo option to toggle for minosoft or original minecraft
connection.sendPacket(new PacketPluginMessageSended("MC|Brand", "Minosoft".getBytes())); connection.sendPacket(new PacketPluginMessageSending("MC|Brand", "Minosoft".getBytes()));
} }
} }

View File

@ -42,7 +42,7 @@ public interface Protocol {
packetClassMapping.put(Packets.Clientbound.PLAY_KEEP_ALIVE, PacketKeepAlive.class); packetClassMapping.put(Packets.Clientbound.PLAY_KEEP_ALIVE, PacketKeepAlive.class);
packetClassMapping.put(Packets.Clientbound.PLAY_CHUNK_BULK, PacketChunkBulk.class); packetClassMapping.put(Packets.Clientbound.PLAY_CHUNK_BULK, PacketChunkBulk.class);
packetClassMapping.put(Packets.Clientbound.PLAY_UPDATE_HEALTH, PacketUpdateHealth.class); packetClassMapping.put(Packets.Clientbound.PLAY_UPDATE_HEALTH, PacketUpdateHealth.class);
packetClassMapping.put(Packets.Clientbound.PLAY_PLUGIN_MESSAGE, PacketPluginMessageReceived.class); packetClassMapping.put(Packets.Clientbound.PLAY_PLUGIN_MESSAGE, PacketPluginMessageReceiving.class);
packetClassMapping.put(Packets.Clientbound.PLAY_SPAWN_POSITION, PacketSpawnLocation.class); packetClassMapping.put(Packets.Clientbound.PLAY_SPAWN_POSITION, PacketSpawnLocation.class);
packetClassMapping.put(Packets.Clientbound.PLAY_CHAT_MESSAGE, PacketChatMessage.class); packetClassMapping.put(Packets.Clientbound.PLAY_CHAT_MESSAGE, PacketChatMessage.class);
packetClassMapping.put(Packets.Clientbound.PLAY_DISCONNECT, PacketDisconnect.class); packetClassMapping.put(Packets.Clientbound.PLAY_DISCONNECT, PacketDisconnect.class);