Skip to content

Commit

Permalink
Don't drop empty items when breaking containers, added ImmutableItemS…
Browse files Browse the repository at this point in the history
…tack.

ImmutableItemStack should be moved to Glowkit as an API. This is temporary and might be broken.
  • Loading branch information
aramperes committed Jun 8, 2017
1 parent 8b3d13e commit cdc53f4
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.glowstone.block.entity.ContainerEntity;
import net.glowstone.entity.GlowPlayer;
import net.glowstone.inventory.MaterialMatcher;
import net.glowstone.util.InventoryUtil;
import org.bukkit.Statistic;
import org.bukkit.block.BlockFace;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -59,7 +60,7 @@ public Collection<ItemStack> getDrops(GlowBlock block, ItemStack tool) {

MaterialMatcher neededTool = getNeededMiningTool(block);
if (neededTool == null ||
tool != null && neededTool.matches(tool.getType())) {
!InventoryUtil.isEmpty(tool) && neededTool.matches(InventoryUtil.itemOrEmpty(tool).getType())) {
drops.addAll(getBlockDrops(block));
}

Expand All @@ -76,7 +77,7 @@ public Collection<ItemStack> getMinedDrops(GlowBlock block) {
public Collection<ItemStack> getContentDrops(GlowBlock block) {
LinkedList<ItemStack> drops = new LinkedList<>();
for (ItemStack i : ((ContainerEntity) block.getBlockEntity()).getInventory().getContents()) {
if (i != null) {
if (!InventoryUtil.isEmpty(i)) {
drops.add(i);
}
}
Expand Down
86 changes: 86 additions & 0 deletions src/main/java/net/glowstone/util/ImmutableItemStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package net.glowstone.util;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.material.MaterialData;

public class ImmutableItemStack extends ItemStack {

private ItemMeta originalMeta = null;

public ImmutableItemStack(int type) {
super(type);
originalMeta = Bukkit.getItemFactory().getItemMeta(getType()).clone();
}

public ImmutableItemStack(Material type) {
super(type);
}

public ImmutableItemStack(int type, int amount) {
super(type, amount);
}

public ImmutableItemStack(Material type, int amount) {
super(type, amount);
}

public ImmutableItemStack(int type, int amount, short damage) {
super(type, amount, damage);
}

public ImmutableItemStack(Material type, int amount, short damage) {
super(type, amount, damage);
}

public ImmutableItemStack(int type, int amount, short damage, Byte data) {
super(type, amount, damage, data);
}

public ImmutableItemStack(Material type, int amount, short damage, Byte data) {
super(type, amount, damage, data);
}

public ImmutableItemStack(ItemStack stack) throws IllegalArgumentException {
super(stack);
originalMeta = stack.getItemMeta().clone();
}

@Deprecated
@Override
public void setType(Material type) {
}

@Deprecated
@Override
public void setTypeId(int type) {
}

@Deprecated
@Override
public boolean setItemMeta(ItemMeta itemMeta) {
return false;
}

@Deprecated
@Override
public void setAmount(int amount) {
}

@Deprecated
@Override
public void setData(MaterialData data) {
}

@Deprecated
@Override
public void setDurability(short durability) {
}

@Override
public ItemMeta getItemMeta() {
return originalMeta;
}
}
5 changes: 3 additions & 2 deletions src/main/java/net/glowstone/util/InventoryUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class InventoryUtil {

public static final ItemStack[] NO_ITEMS = new ItemStack[0];
public static final ImmutableItemStack EMPTY_STACK = new ImmutableItemStack(Material.AIR, 0);

/**
* Checks whether the given ItemStack is empty.
Expand All @@ -19,7 +20,7 @@ public class InventoryUtil {
* @return whether the given ItemStack is empty
*/
public static boolean isEmpty(ItemStack stack) {
return stack == null || stack.getType() == Material.AIR || stack.getAmount() == 0;
return stack == null || stack.equals(EMPTY_STACK) || stack.getType() == Material.AIR || stack.getAmount() == 0;
}

/**
Expand All @@ -38,7 +39,7 @@ public static ItemStack itemOrEmpty(ItemStack stack) {
* @return an empty ItemStack
*/
public static ItemStack createEmptyStack() {
return new ItemStack(Material.AIR, 0);
return EMPTY_STACK;
}

/**
Expand Down

0 comments on commit cdc53f4

Please sign in to comment.