Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement donkey, mule & llama inventories #872

Merged
merged 11 commits into from Mar 23, 2018
41 changes: 39 additions & 2 deletions src/main/java/net/glowstone/entity/passive/GlowChestedHorse.java
@@ -1,14 +1,31 @@
package net.glowstone.entity.passive;

import lombok.Getter;
import net.glowstone.entity.meta.MetadataIndex;
import org.bukkit.Location;
import org.bukkit.entity.ChestedHorse;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Item;
import org.bukkit.inventory.AbstractHorseInventory;
import org.bukkit.inventory.ItemStack;

public abstract class GlowChestedHorse extends GlowAbstractHorse implements ChestedHorse {
/**
* A horse or similar mount (donkey, mule, llama...) whose inventory may include a chest.
*
* @param <InventoryT> the inventory class this entity uses
*/
public abstract class GlowChestedHorse<InventoryT extends AbstractHorseInventory>
extends GlowAbstractHorse implements ChestedHorse {

/**
* Null when not carrying a chest; otherwise, a 15-slot container.
*/
@Getter
protected InventoryT inventory;

public GlowChestedHorse(Location location, EntityType type, double maxHealth) {
super(location, type, maxHealth);
inventory = createNewInventory();
}

@Override
Expand All @@ -18,6 +35,26 @@ public boolean isCarryingChest() {

@Override
public void setCarryingChest(boolean carryingChest) {
metadata.set(MetadataIndex.CHESTED_HORSE_HAS_CHEST, carryingChest);
if (carryingChest != isCarryingChest()) {
metadata.set(MetadataIndex.CHESTED_HORSE_HAS_CHEST, carryingChest);
inventory = createNewInventory();
}
}

/**
* Move all items from one inventory's chest to another, and drop those that don't fit.
*
* @param from the inventory to transfer from
* @param to the inventory to transfer to
*/
protected void moveChestContents(InventoryT from, InventoryT to) {
for (ItemStack remaining : to.addItem(from.getContents()).values()) {
world.spawn(location, Item.class).setItemStack(remaining);
}
}

/**
* Creates and sets a new inventory, and copies equipment over from the existing inventory.
*/
protected abstract InventoryT createNewInventory();
}
18 changes: 12 additions & 6 deletions src/main/java/net/glowstone/entity/passive/GlowDonkey.java
Expand Up @@ -6,17 +6,12 @@
import org.bukkit.entity.Donkey;
import org.bukkit.entity.EntityType;

public class GlowDonkey extends GlowChestedHorse implements Donkey {
public class GlowDonkey extends GlowChestedHorse<GlowHorseInventory> implements Donkey {

public GlowDonkey(Location location) {
super(location, EntityType.DONKEY, 15);
}

@Override
public GlowHorseInventory getInventory() {
return null;
}

@Override
protected Sound getHurtSound() {
return Sound.ENTITY_DONKEY_HURT;
Expand All @@ -31,4 +26,15 @@ protected Sound getDeathSound() {
protected Sound getAmbientSound() {
return Sound.ENTITY_DONKEY_AMBIENT;
}

@Override
protected GlowHorseInventory createNewInventory() {
GlowHorseInventory oldInventory = inventory;
GlowHorseInventory newInventory = new GlowHorseInventory(this);
if (oldInventory != null) {
newInventory.setSaddle(oldInventory.getSaddle());
moveChestContents(oldInventory, newInventory);
}
return newInventory;
}
}
25 changes: 18 additions & 7 deletions src/main/java/net/glowstone/entity/passive/GlowLlama.java
Expand Up @@ -2,13 +2,13 @@

import java.util.concurrent.ThreadLocalRandom;
import net.glowstone.entity.meta.MetadataIndex;
import net.glowstone.inventory.GlowLlamaInventory;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Llama;
import org.bukkit.inventory.LlamaInventory;

public class GlowLlama extends GlowChestedHorse implements Llama {
public class GlowLlama extends GlowChestedHorse<GlowLlamaInventory> implements Llama {

/**
* Creates a llama entity.
Expand Down Expand Up @@ -39,11 +39,9 @@ public int getStrength() {
@Override
public void setStrength(int strength) {
metadata.set(MetadataIndex.LLAMA_STRENGTH, strength);
}

@Override
public LlamaInventory getInventory() {
return null; // todo
if (isCarryingChest()) {
inventory = createNewInventory();
}
}

@Override
Expand All @@ -60,4 +58,17 @@ protected Sound getHurtSound() {
protected Sound getAmbientSound() {
return Sound.ENTITY_LLAMA_AMBIENT;
}

@Override
protected GlowLlamaInventory createNewInventory() {
GlowLlamaInventory oldInventory = inventory;
GlowLlamaInventory newInventory
= new GlowLlamaInventory(this, isCarryingChest() ? 3 * getStrength() : 0);
if (oldInventory != null) {
newInventory.setSaddle(oldInventory.getSaddle());
newInventory.setDecor(oldInventory.getDecor());
moveChestContents(oldInventory, newInventory);
}
return newInventory;
}
}
18 changes: 12 additions & 6 deletions src/main/java/net/glowstone/entity/passive/GlowMule.java
Expand Up @@ -6,17 +6,12 @@
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Mule;

public class GlowMule extends GlowChestedHorse implements Mule {
public class GlowMule extends GlowChestedHorse<GlowHorseInventory> implements Mule {

public GlowMule(Location location) {
super(location, EntityType.MULE, 15);
}

@Override
public GlowHorseInventory getInventory() {
return null; // todo
}

@Override
protected Sound getDeathSound() {
return Sound.ENTITY_MULE_DEATH;
Expand All @@ -31,4 +26,15 @@ protected Sound getHurtSound() {
protected Sound getAmbientSound() {
return Sound.ENTITY_MULE_AMBIENT;
}

@Override
protected GlowHorseInventory createNewInventory() {
GlowHorseInventory oldInventory = inventory;
GlowHorseInventory newInventory = new GlowHorseInventory(this);
if (oldInventory != null) {
newInventory.setSaddle(oldInventory.getSaddle());
moveChestContents(oldInventory, newInventory);
}
return newInventory;
}
}
12 changes: 9 additions & 3 deletions src/main/java/net/glowstone/inventory/GlowHorseInventory.java
@@ -1,5 +1,7 @@
package net.glowstone.inventory;

import org.bukkit.entity.AbstractHorse;
import org.bukkit.entity.ChestedHorse;
import org.bukkit.entity.Horse;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.HorseInventory;
Expand All @@ -13,15 +15,19 @@ public class GlowHorseInventory extends GlowInventory implements HorseInventory
private ItemStack saddle;
private ItemStack armor;

public GlowHorseInventory(Horse owner) {
public GlowHorseInventory(ChestedHorse owner) {
this(owner, owner.isCarryingChest() ? 16 : 2);
}

public GlowHorseInventory(Horse owner, int size) {
public GlowHorseInventory(Horse owner) {
this(owner, 2);
}

public GlowHorseInventory(AbstractHorse owner, int size) {
this(owner, size, "EntityHorse");
}

public GlowHorseInventory(Horse owner, int size, String title) {
public GlowHorseInventory(AbstractHorse owner, int size, String title) {
super(owner, InventoryType.CHEST, size, title); //TODO fix this.
}

Expand Down
25 changes: 25 additions & 0 deletions src/main/java/net/glowstone/inventory/GlowLlamaInventory.java
@@ -0,0 +1,25 @@
package net.glowstone.inventory;

import lombok.Getter;
import lombok.Setter;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.LlamaInventory;

/**
* A llama inventory. A llama can wear a carpet, and if it's carrying a chest, that chest can have 3
* to 15 slots.
*/
public class GlowLlamaInventory extends GlowInventory implements LlamaInventory {
@Getter
@Setter
protected ItemStack decor;
@Getter
@Setter
protected ItemStack saddle;

public GlowLlamaInventory(InventoryHolder holder, int slots) {
super(holder, InventoryType.CHEST, slots);
}
}