Creeper support, cleanup entity classes to make less duplicated

This commit is contained in:
bixilon 2020-06-11 01:10:06 +02:00
parent edf1095fdd
commit 8a01fc2b5a
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
9 changed files with 192 additions and 242 deletions

View File

@ -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 <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.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;
}
}

View File

@ -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<Slots.Entity, Slot> 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;
<T extends EntityMetaData> 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);
}
}

View File

@ -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 <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.entities.meta.EntityMetaData;
public interface EntityInterface {
Mobs getEntityType();
EntityMetaData getMetaData();
void setMetaData(EntityMetaData metaData);
float getWidth();
float getHeight();
}

View File

@ -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);
}

View File

@ -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 <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 interface MobInterface extends EntityInterface {
int getMaxHealth();
}

View File

@ -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;

View File

@ -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<Slots.Entity, Slot> 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);
}
}

View File

@ -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<Slots.Entity, Slot> 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;
}
}

View File

@ -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;
}
}