entity wip (4)

This commit is contained in:
bixilon 2020-06-07 00:26:01 +02:00
parent f4f244db3b
commit 3ca32b7e07
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
20 changed files with 679 additions and 5 deletions

View File

@ -0,0 +1,38 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.game.datatypes;
public class PlayerPropertyData {
final String name;
final String value;
final String signature;
public PlayerPropertyData(String name, String value, String signature) {
this.name = name;
this.value = value;
this.signature = signature;
}
public String getName() {
return this.name;
}
public String getSignature() {
return signature;
}
public String getValue() {
return value;
}
}

View File

@ -97,6 +97,10 @@ public class World {
}
public void removeEntity(Entity entity) {
this.entities.remove(entity.getId());
removeEntity(entity.getId());
}
public void removeEntity(int entityId) {
this.entities.remove(entityId);
}
}

View File

@ -22,6 +22,8 @@ public interface Entity {
void setLocation(Location location);
void setLocation(RelativeLocation location);
Velocity getVelocity();
void setVelocity(Velocity velocity);

View File

@ -44,4 +44,9 @@ public class Location {
Location that = (Location) obj;
return that.getX() == getX() && that.getY() == getY() && that.getZ() == getZ();
}
@Override
public String toString() {
return String.format("%s %s %s", getX(), getY(), getZ());
}
}

View File

@ -17,4 +17,6 @@ public interface Mob extends Entity {
float getHealth();
void setHealth(float health);
int getMaxHealth();
}

View File

@ -16,7 +16,8 @@ package de.bixilon.minosoft.game.datatypes.entities;
import de.bixilon.minosoft.game.datatypes.Identifier;
public enum Mobs {
ZOMBIE(new Identifier("zombie"), 54, Zombie.class);
ZOMBIE(new Identifier("zombie"), 54, Zombie.class),
PLAYER(null, 92, OtherPlayer.class);
final Identifier identifier;
final int type;

View File

@ -0,0 +1,155 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.game.datatypes.entities;
import de.bixilon.minosoft.game.datatypes.PlayerPropertyData;
import java.util.UUID;
public class OtherPlayer implements Mob {
final int id;
final String name;
final UUID uuid;
PlayerPropertyData[] properties;
Location location;
int yaw;
int pitch;
short currentItem;
EntityMetaData metaData;
float health;
public OtherPlayer(int id, String name, UUID uuid, PlayerPropertyData[] properties, Location location, int yaw, int pitch, short currentItem, EntityMetaData metaData) {
this.id = id;
this.name = name;
this.uuid = uuid;
this.properties = properties;
this.location = location;
this.yaw = yaw;
this.pitch = pitch;
this.currentItem = currentItem;
this.metaData = metaData;
}
@Override
public Mobs getEntityType() {
return Mobs.PLAYER;
}
public int getId() {
return id;
}
@Override
public Location getLocation() {
return location;
}
@Override
public void setLocation(Location location) {
this.location = location;
}
@Override
public void setLocation(RelativeLocation relativeLocation) {
// change relative location
location = new Location(location.getX() + relativeLocation.getX(), location.getY() + relativeLocation.getY(), location.getZ() + relativeLocation.getZ());
}
@Deprecated
@Override
public Velocity getVelocity() {
return null;
}
@Deprecated
@Override
public void setVelocity(Velocity velocity) {
}
@Override
public int getYaw() {
return 0;
}
@Override
public void setYaw(int yaw) {
this.yaw = yaw;
}
@Override
public int getPitch() {
return 0;
}
@Override
public void setPitch(int pitch) {
this.pitch = pitch;
}
@Override
public float getWidth() {
return 0.6F;
}
@Override
public float getHeight() {
return 1.8F;
}
@Override
public EntityMetaData getMetaData() {
return metaData;
}
@Override
public void setMetaData(EntityMetaData data) {
this.metaData = data;
}
@Override
public float getHealth() {
return health;
}
@Override
public void setHealth(float health) {
this.health = health;
}
@Override
public int getMaxHealth() {
return 40;
}
public String getName() {
return name;
}
public PlayerPropertyData[] getProperties() {
return properties;
}
public UUID getUUID() {
return uuid;
}
public short getCurrentItem() {
return currentItem;
}
}

View File

@ -0,0 +1,47 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.game.datatypes.entities;
public class RelativeLocation {
private final double x;
private final double y;
private final double z;
public RelativeLocation(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
@Override
public boolean equals(Object obj) {
if (super.equals(obj)) {
return true;
}
RelativeLocation that = (RelativeLocation) obj;
return that.getX() == getX() && that.getY() == getY() && that.getZ() == getZ();
}
}

View File

@ -52,6 +52,12 @@ public class Zombie implements Mob {
}
@Override
public void setLocation(RelativeLocation relativeLocation) {
// change relative location
location = new Location(location.getX() + relativeLocation.getX(), location.getY() + relativeLocation.getY(), location.getZ() + relativeLocation.getZ());
}
@Override
public Velocity getVelocity() {
return velocity;
@ -113,4 +119,9 @@ public class Zombie implements Mob {
public void setHealth(float health) {
this.health = health;
}
@Override
public int getMaxHealth() {
return 40;
}
}

View File

@ -0,0 +1,54 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.protocol.packets.clientbound.play;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
import java.util.Arrays;
public class PacketDestoryEntity implements ClientboundPacket {
int[] entityIds;
@Override
public void read(InPacketBuffer buffer, ProtocolVersion v) {
switch (v) {
case VERSION_1_7_10:
this.entityIds = new int[buffer.readByte()];
for (int i = 0; i < entityIds.length; i++) {
entityIds[i] = buffer.readInteger();
}
break;
}
}
@Override
public void log() {
Log.protocol(String.format("Despawning the following entities: %s", Arrays.toString(entityIds)));
}
public int[] getEntityIds() {
return entityIds;
}
@Override
public void handle(PacketHandler h) {
h.handle(this);
}
}

View File

@ -0,0 +1,55 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.protocol.packets.clientbound.play;
import de.bixilon.minosoft.game.datatypes.entities.RelativeLocation;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
public class PacketEntityPosition implements ClientboundPacket {
int entityId;
RelativeLocation location;
@Override
public void read(InPacketBuffer buffer, ProtocolVersion v) {
switch (v) {
case VERSION_1_7_10:
this.entityId = buffer.readInteger();
this.location = new RelativeLocation(buffer.readFixedPointNumberByte(), buffer.readFixedPointNumberByte(), buffer.readFixedPointNumberByte());
break;
}
}
@Override
public void log() {
Log.protocol(String.format("Entity %d moved relative %s %s %s", entityId, location.getX(), location.getY(), location.getZ()));
}
public int getEntityId() {
return entityId;
}
public RelativeLocation getRelativeLocation() {
return location;
}
@Override
public void handle(PacketHandler h) {
h.handle(this);
}
}

View File

@ -0,0 +1,67 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.protocol.packets.clientbound.play;
import de.bixilon.minosoft.game.datatypes.entities.RelativeLocation;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
public class PacketEntityPositionAndRotation implements ClientboundPacket {
int entityId;
RelativeLocation location;
int yaw;
int pitch;
@Override
public void read(InPacketBuffer buffer, ProtocolVersion v) {
switch (v) {
case VERSION_1_7_10:
this.entityId = buffer.readInteger();
this.location = new RelativeLocation(buffer.readFixedPointNumberByte(), buffer.readFixedPointNumberByte(), buffer.readFixedPointNumberByte());
this.yaw = buffer.readByte(); //ToDo
this.pitch = buffer.readByte();
break;
}
}
@Override
public void log() {
Log.protocol(String.format("Entity %d moved relative %s %s %s - %s %s", entityId, location.getX(), location.getY(), location.getZ(), yaw, pitch));
}
public int getEntityId() {
return entityId;
}
public RelativeLocation getRelativeLocation() {
return location;
}
public int getYaw() {
return yaw;
}
public int getPitch() {
return pitch;
}
@Override
public void handle(PacketHandler h) {
h.handle(this);
}
}

View File

@ -0,0 +1,61 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.protocol.packets.clientbound.play;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
public class PacketEntityRotation implements ClientboundPacket {
int entityId;
int yaw;
int pitch;
@Override
public void read(InPacketBuffer buffer, ProtocolVersion v) {
switch (v) {
case VERSION_1_7_10:
this.entityId = buffer.readInteger();
this.yaw = buffer.readByte(); //ToDo
this.pitch = buffer.readByte();
break;
}
}
@Override
public void log() {
Log.protocol(String.format("Entity %d moved relative %s %s", entityId, yaw, pitch));
}
public int getEntityId() {
return entityId;
}
public int getYaw() {
return yaw;
}
public int getPitch() {
return pitch;
}
@Override
public void handle(PacketHandler h) {
h.handle(this);
}
}

View File

@ -0,0 +1,55 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.protocol.packets.clientbound.play;
import de.bixilon.minosoft.game.datatypes.entities.Velocity;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
public class PacketEntityVelocity implements ClientboundPacket {
int entityId;
Velocity velocity;
@Override
public void read(InPacketBuffer buffer, ProtocolVersion v) {
switch (v) {
case VERSION_1_7_10:
this.entityId = buffer.readInteger();
this.velocity = new Velocity(buffer.readShort(), buffer.readShort(), buffer.readShort());
break;
}
}
@Override
public void log() {
Log.protocol(String.format("Entity velocity changed %d: %d %d %d", entityId, velocity.getX(), velocity.getY(), velocity.getZ()));
}
public int getEntityId() {
return entityId;
}
public Velocity getVelocity() {
return velocity;
}
@Override
public void handle(PacketHandler h) {
h.handle(this);
}
}

View File

@ -31,7 +31,7 @@ public class PacketSpawnMob implements ClientboundPacket {
case VERSION_1_7_10:
int entityId = buffer.readVarInt();
Mobs type = Mobs.byType(buffer.readByte());
Location location = new Location(buffer.readFixedPointNumber(), buffer.readFixedPointNumber(), buffer.readFixedPointNumber());
Location location = new Location(buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger());
int yaw = buffer.readByte(); //ToDo
int pitch = buffer.readByte();
int headPitch = buffer.readByte();

View File

@ -0,0 +1,73 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.protocol.packets.clientbound.play;
import de.bixilon.minosoft.game.datatypes.PlayerPropertyData;
import de.bixilon.minosoft.game.datatypes.entities.EntityMetaData;
import de.bixilon.minosoft.game.datatypes.entities.Location;
import de.bixilon.minosoft.game.datatypes.entities.OtherPlayer;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
import java.util.UUID;
public class PacketSpawnPlayer implements ClientboundPacket {
int entityId;
OtherPlayer player;
@Override
public void read(InPacketBuffer buffer, ProtocolVersion v) {
switch (v) {
case VERSION_1_7_10:
this.entityId = buffer.readVarInt();
UUID uuid = UUID.fromString(buffer.readString());
String name = buffer.readString();
PlayerPropertyData[] properties = new PlayerPropertyData[buffer.readVarInt()];
for (int i = 0; i < properties.length; i++) {
properties[i] = new PlayerPropertyData(buffer.readString(), buffer.readString(), buffer.readString());
}
Location location = new Location(buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger());
int yaw = buffer.readByte();
int pitch = buffer.readByte();
short currentItem = buffer.readShort();
EntityMetaData metaData = buffer.readEntityMetaData();
this.player = new OtherPlayer(entityId, name, uuid, properties, location, yaw, pitch, currentItem, metaData);
break;
}
}
@Override
public void log() {
Log.protocol(String.format("Player spawned (name=%s, uuid=%s, location=%s", player.getName(), player.getUUID(), player.getLocation().toString()));
}
public int getEntityId() {
return entityId;
}
public OtherPlayer getPlayer() {
return player;
}
@Override
public void handle(PacketHandler h) {
h.handle(this);
}
}

View File

@ -142,10 +142,14 @@ public class InByteBuffer {
return result;
}
public double readFixedPointNumber() {
public double readFixedPointNumberInteger() {
return readInteger() / 32.0D;
}
public double readFixedPointNumberByte() {
return readByte() / 32.0D;
}
public JSONObject readJson() {
return new JSONObject(readString());
}

View File

@ -136,7 +136,11 @@ public class OutByteBuffer {
} while (value != 0);
}
public void writeFixedPointNumber(double d) {
public void writeFixedPointNumberInteger(double d) {
writeInteger((int) (d * 32.0D));
}
public void writeFixedPointNumberByte(double d) {
writeInteger((int) (d * 32.0D));
}

View File

@ -146,4 +146,34 @@ public class PacketHandler {
public void handle(PacketSpawnMob pkg) {
connection.getPlayer().getWorld().addEntity(pkg.getMob());
}
public void handle(PacketEntityPositionAndRotation pkg) {
connection.getPlayer().getWorld().getEntity(pkg.getEntityId()).setLocation(pkg.getRelativeLocation());
connection.getPlayer().getWorld().getEntity(pkg.getEntityId()).setYaw(pkg.getYaw());
connection.getPlayer().getWorld().getEntity(pkg.getEntityId()).setPitch(pkg.getPitch());
}
public void handle(PacketEntityPosition pkg) {
connection.getPlayer().getWorld().getEntity(pkg.getEntityId()).setLocation(pkg.getRelativeLocation());
}
public void handle(PacketEntityRotation pkg) {
connection.getPlayer().getWorld().getEntity(pkg.getEntityId()).setYaw(pkg.getYaw());
connection.getPlayer().getWorld().getEntity(pkg.getEntityId()).setPitch(pkg.getPitch());
}
public void handle(PacketDestoryEntity pkg) {
for (int entityId : pkg.getEntityIds()) {
connection.getPlayer().getWorld().removeEntity(entityId);
}
}
public void handle(PacketEntityVelocity pkg) {
connection.getPlayer().getWorld().getEntity(pkg.getEntityId()).setVelocity(pkg.getVelocity());
}
public void handle(PacketSpawnPlayer pkg) {
connection.getPlayer().getWorld().addEntity(pkg.getPlayer());
}
}

View File

@ -63,5 +63,11 @@ public interface Protocol {
packetClassMapping.put(Packets.Clientbound.PLAY_SET_EXPERIENCE, PacketSetExperience.class);
packetClassMapping.put(Packets.Clientbound.PLAY_CHANGE_GAME_STATE, PacketChangeGameState.class);
packetClassMapping.put(Packets.Clientbound.PLAY_SPAWN_MOB, PacketSpawnMob.class);
packetClassMapping.put(Packets.Clientbound.PLAY_ENTITY_POSITION_AND_ROTATION, PacketEntityPositionAndRotation.class);
packetClassMapping.put(Packets.Clientbound.PLAY_ENTITY_POSITION, PacketEntityPosition.class);
packetClassMapping.put(Packets.Clientbound.PLAY_ENTITY_ROTATION, PacketEntityRotation.class);
packetClassMapping.put(Packets.Clientbound.PLAY_DESTROY_ENTITIES, PacketDestoryEntity.class);
packetClassMapping.put(Packets.Clientbound.PLAY_ENTITY_VELOCITY, PacketEntityVelocity.class);
packetClassMapping.put(Packets.Clientbound.PLAY_SPAWN_PLAYER, PacketSpawnPlayer.class);
}
}