From 8a01fc2b5ace96da818645abc221e968f54d7d90 Mon Sep 17 00:00:00 2001 From: bixilon Date: Thu, 11 Jun 2020 01:10:06 +0200 Subject: [PATCH] Creeper support, cleanup entity classes to make less duplicated --- .../game/datatypes/entities/Creeper.java | 58 ++++++++ .../game/datatypes/entities/Entity.java | 73 +++++++--- .../datatypes/entities/EntityInterface.java | 30 +++++ .../minosoft/game/datatypes/entities/Mob.java | 18 ++- .../game/datatypes/entities/MobInterface.java | 18 +++ .../game/datatypes/entities/Mobs.java | 4 +- .../game/datatypes/entities/OtherPlayer.java | 105 +-------------- .../game/datatypes/entities/Zombie.java | 126 ++---------------- .../clientbound/play/PacketSpawnPlayer.java | 2 +- 9 files changed, 192 insertions(+), 242 deletions(-) create mode 100644 src/main/java/de/bixilon/minosoft/game/datatypes/entities/Creeper.java create mode 100644 src/main/java/de/bixilon/minosoft/game/datatypes/entities/EntityInterface.java create mode 100644 src/main/java/de/bixilon/minosoft/game/datatypes/entities/MobInterface.java diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Creeper.java b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Creeper.java new file mode 100644 index 000000000..193f14758 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Creeper.java @@ -0,0 +1,58 @@ +/* + * 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 . + * + * 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.entities.meta.CreeperMetaData; +import de.bixilon.minosoft.game.datatypes.entities.meta.EntityMetaData; +import de.bixilon.minosoft.protocol.protocol.InByteBuffer; +import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; + +public class Creeper extends Mob implements MobInterface { + CreeperMetaData metaData; + + public Creeper(int id, Location location, int yaw, int pitch, Velocity velocity, InByteBuffer buffer, ProtocolVersion v) { + super(id, location, yaw, pitch, velocity); + this.metaData = new CreeperMetaData(buffer, v); + } + + @Override + public Mobs getEntityType() { + return Mobs.CREEPER; + } + + @Override + public EntityMetaData getMetaData() { + return metaData; + } + + @Override + public void setMetaData(EntityMetaData metaData) { + this.metaData = (CreeperMetaData) metaData; + } + + @Override + public float getWidth() { + return 0.6F; + } + + @Override + public float getHeight() { + return 1.7F; + } + + @Override + public int getMaxHealth() { + return 40; + } +} diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Entity.java b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Entity.java index 0d965bced..a8f2f234e 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Entity.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Entity.java @@ -15,42 +15,77 @@ package de.bixilon.minosoft.game.datatypes.entities; import de.bixilon.minosoft.game.datatypes.Slot; import de.bixilon.minosoft.game.datatypes.Slots; -import de.bixilon.minosoft.game.datatypes.entities.meta.EntityMetaData; -public interface Entity { - Mobs getEntityType(); +import java.util.HashMap; - int getId(); +public abstract class Entity implements EntityInterface { + final int id; + final HashMap equipment; + Location location; + Velocity velocity; + int yaw; + int pitch; - Location getLocation(); + public Entity(int id, Location location, int yaw, int pitch, Velocity velocity) { + this.id = id; + this.location = location; + this.yaw = yaw; + this.pitch = pitch; + this.velocity = velocity; + this.equipment = new HashMap<>(); + } - void setLocation(Location location); - void setLocation(RelativeLocation location); + public int getId() { + return id; + } - Velocity getVelocity(); + public Location getLocation() { + return location; + } - void setVelocity(Velocity velocity); + public void setLocation(Location location) { + this.location = location; - int getYaw(); + } - void setYaw(int yaw); + public void setLocation(RelativeLocation relativeLocation) { + // change relative location + location = new Location(location.getX() + relativeLocation.getX(), location.getY() + relativeLocation.getY(), location.getZ() + relativeLocation.getZ()); + } - int getPitch(); + public Velocity getVelocity() { + return velocity; + } - void setPitch(int pitch); + public void setVelocity(Velocity velocity) { + this.velocity = velocity; + } - float getWidth(); + public int getYaw() { + return 0; + } - float getHeight(); + public void setYaw(int yaw) { + this.yaw = yaw; - EntityMetaData getMetaData(); + } - void setMetaData(EntityMetaData data); + public int getPitch() { + return 0; + } - void setEquipment(Slots.Entity slot, Slot data); + public void setPitch(int pitch) { + this.pitch = pitch; - Slot getEquipment(Slots.Entity slot); + } + public void setEquipment(Slots.Entity slot, Slot data) { + equipment.put(slot, data); + } + + public Slot getEquipment(Slots.Entity slot) { + return equipment.get(slot); + } } diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/EntityInterface.java b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/EntityInterface.java new file mode 100644 index 000000000..2c6ff08f3 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/EntityInterface.java @@ -0,0 +1,30 @@ +/* + * 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 . + * + * 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.entities.meta.EntityMetaData; + +public interface EntityInterface { + + Mobs getEntityType(); + + EntityMetaData getMetaData(); + + void setMetaData(EntityMetaData metaData); + + float getWidth(); + + float getHeight(); + +} diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Mob.java b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Mob.java index 3b14f0cc1..4506814e4 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Mob.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Mob.java @@ -13,14 +13,20 @@ package de.bixilon.minosoft.game.datatypes.entities; -public interface Mob extends Entity { - float getHealth(); +public abstract class Mob extends Entity implements MobInterface { + int headYaw; - void setHealth(float health); + public Mob(int id, Location location, int yaw, int pitch, Velocity velocity) { + super(id, location, yaw, pitch, velocity); + } - int getMaxHealth(); - int getHeadYaw(); + public int getHeadYaw() { + return headYaw; + } + + public void setHeadYaw(int headYaw) { + this.headYaw = headYaw; + } - void setHeadYaw(int headYaw); } diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/MobInterface.java b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/MobInterface.java new file mode 100644 index 000000000..a10dae313 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/MobInterface.java @@ -0,0 +1,18 @@ +/* + * 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 . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.game.datatypes.entities; + +public interface MobInterface extends EntityInterface { + int getMaxHealth(); +} diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Mobs.java b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Mobs.java index 4cbb7077d..60ab2b5db 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Mobs.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Mobs.java @@ -17,8 +17,8 @@ import de.bixilon.minosoft.game.datatypes.Identifier; public enum Mobs { ZOMBIE(new Identifier("zombie"), 54, Zombie.class), - PLAYER(null, 92, OtherPlayer.class); - // ToDo all mobs + PLAYER(null, 92, OtherPlayer.class), + CREEPER(new Identifier("creeper"), 50, Creeper.class); final Identifier identifier; final int type; diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/OtherPlayer.java b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/OtherPlayer.java index 40465fd5f..103e0f441 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/OtherPlayer.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/OtherPlayer.java @@ -15,41 +15,26 @@ package de.bixilon.minosoft.game.datatypes.entities; import de.bixilon.minosoft.game.datatypes.PlayerPropertyData; -import de.bixilon.minosoft.game.datatypes.Slot; -import de.bixilon.minosoft.game.datatypes.Slots; import de.bixilon.minosoft.game.datatypes.entities.meta.EntityMetaData; import de.bixilon.minosoft.game.datatypes.entities.meta.HumanMetaData; -import java.util.HashMap; import java.util.UUID; -public class OtherPlayer implements Mob { - final int id; +public class OtherPlayer extends Mob implements MobInterface { final String name; final UUID uuid; PlayerPropertyData[] properties; - Location location; - Velocity velocity; - int yaw; - int pitch; - int headYaw; short currentItem; HumanMetaData metaData; - float health; Pose status = Pose.STANDING; - final HashMap equipment; - public OtherPlayer(int id, String name, UUID uuid, PlayerPropertyData[] properties, Location location, int yaw, int pitch, short currentItem, HumanMetaData metaData) { - this.id = id; + public OtherPlayer(int id, String name, UUID uuid, PlayerPropertyData[] properties, Location location, Velocity velocity, int yaw, int pitch, short currentItem, HumanMetaData metaData) { + super(id, location, yaw, pitch, velocity); this.name = name; this.uuid = uuid; this.properties = properties; - this.location = location; - this.yaw = yaw; - this.pitch = pitch; this.currentItem = currentItem; this.metaData = metaData; - equipment = new HashMap<>(); } @Override @@ -57,59 +42,6 @@ public class OtherPlayer implements Mob { 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()); - } - - @Override - public Velocity getVelocity() { - return velocity; - } - - @Override - public void setVelocity(Velocity velocity) { - this.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() { switch (status) { @@ -148,19 +80,10 @@ public class OtherPlayer implements Mob { this.metaData = (HumanMetaData) data; } - @Override - public float getHealth() { - return health; - } - - @Override - public void setHealth(float health) { - this.health = health; - } - @Override public int getMaxHealth() { return 40; + //ToDo } public String getName() { @@ -179,28 +102,8 @@ public class OtherPlayer implements Mob { return currentItem; } - - @Override - public int getHeadYaw() { - return headYaw; - } - - @Override - public void setHeadYaw(int headYaw) { - this.headYaw = headYaw; - } - public Pose getStatus() { return status; } - @Override - public void setEquipment(Slots.Entity slot, Slot data) { - equipment.put(slot, data); - } - - @Override - public Slot getEquipment(Slots.Entity slot) { - return equipment.get(slot); - } } diff --git a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Zombie.java b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Zombie.java index 4d9a92054..11ee0f0a7 100644 --- a/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Zombie.java +++ b/src/main/java/de/bixilon/minosoft/game/datatypes/entities/Zombie.java @@ -13,34 +13,22 @@ package de.bixilon.minosoft.game.datatypes.entities; -import de.bixilon.minosoft.game.datatypes.Slot; -import de.bixilon.minosoft.game.datatypes.Slots; import de.bixilon.minosoft.game.datatypes.entities.meta.EntityMetaData; import de.bixilon.minosoft.game.datatypes.entities.meta.ZombieMetaData; import de.bixilon.minosoft.protocol.protocol.InByteBuffer; import de.bixilon.minosoft.protocol.protocol.ProtocolVersion; -import java.util.HashMap; - -public class Zombie implements Mob { - final int id; - Location location; - Velocity velocity; - int yaw; - int pitch; - int headYaw; +public class Zombie extends Mob implements MobInterface { ZombieMetaData metaData; - float health; - final HashMap equipment; public Zombie(int id, Location location, int yaw, int pitch, Velocity velocity, InByteBuffer buffer, ProtocolVersion v) { - this.id = id; - this.location = location; - this.yaw = yaw; - this.pitch = pitch; - this.velocity = velocity; + super(id, location, yaw, pitch, velocity); this.metaData = new ZombieMetaData(buffer, v); - this.equipment = new HashMap<>(); + } + + @Override + public int getMaxHealth() { + return 40; } @Override @@ -48,57 +36,14 @@ public class Zombie implements Mob { return Mobs.ZOMBIE; } - public int getId() { - return id; + @Override + public EntityMetaData getMetaData() { + return metaData; } @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()); - } - - @Override - public Velocity getVelocity() { - return velocity; - } - - @Override - public void setVelocity(Velocity velocity) { - this.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; - + public void setMetaData(EntityMetaData metaData) { + this.metaData = (ZombieMetaData) metaData; } @Override @@ -108,51 +53,6 @@ public class Zombie implements Mob { @Override public float getHeight() { - return 1.95F; - } - - @Override - public ZombieMetaData getMetaData() { - return metaData; - } - - @Override - public void setMetaData(EntityMetaData data) { - this.metaData = (ZombieMetaData) data; - } - - @Override - public void setEquipment(Slots.Entity slot, Slot data) { - equipment.put(slot, data); - } - - @Override - public Slot getEquipment(Slots.Entity slot) { - return equipment.get(slot); - } - - @Override - public float getHealth() { - return health; - } - - @Override - public void setHealth(float health) { - this.health = health; - } - - @Override - public int getMaxHealth() { - return 40; - } - - @Override - public int getHeadYaw() { - return headYaw; - } - - @Override - public void setHeadYaw(int headYaw) { - this.headYaw = headYaw; + return 1.8F; } } diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnPlayer.java b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnPlayer.java index 3c9248621..5543e4fed 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnPlayer.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnPlayer.java @@ -48,7 +48,7 @@ public class PacketSpawnPlayer implements ClientboundPacket { short currentItem = buffer.readShort(); HumanMetaData metaData = new HumanMetaData(buffer, v); - this.player = new OtherPlayer(entityId, name, uuid, properties, location, yaw, pitch, currentItem, metaData); + this.player = new OtherPlayer(entityId, name, uuid, properties, location, null, yaw, pitch, currentItem, metaData); break; } }