Added ItemStack#getAirItem

This commit is contained in:
Felix Cravic 2020-04-23 12:30:49 +02:00
parent 3f15701256
commit 4570e7a013
14 changed files with 35 additions and 26 deletions

View File

@ -247,7 +247,7 @@ public abstract class Entity implements Viewable, DataContainer {
int firstBlock = 0; int firstBlock = 0;
for (int y = (int) position.getY(); y > 0; y--) { for (int y = (int) position.getY(); y > 0; y--) {
BlockPosition blockPosition = new BlockPosition(position.getX(), y + 1, position.getZ()); BlockPosition blockPosition = new BlockPosition(position.getX(), y, position.getZ());
short blockId = instance.getBlockId(blockPosition); short blockId = instance.getBlockId(blockPosition);
//if (y == 70) //if (y == 70)
// System.out.println("id: " + blockId); // System.out.println("id: " + blockId);

View File

@ -139,7 +139,7 @@ public abstract class EntityCreature extends LivingEntity {
pathFinder.getPath(position, blockPositions -> { pathFinder.getPath(position, blockPositions -> {
if (blockPositions.isEmpty()) { if (blockPositions.isEmpty()) {
// Didn't find path // Didn't find path
System.out.println("NOT FOUND"); System.out.println("PATH NOT FOUND");
return; return;
} }
this.blockPositions = blockPositions; this.blockPositions = blockPositions;

View File

@ -82,10 +82,10 @@ public class Player extends LivingEntity {
private float fieldViewModifier = 0.1f; private float fieldViewModifier = 0.1f;
// Statistics // Statistics
private Map<PlayerStatistic, Integer> statisticValueMap = new HashMap<>(); private Map<PlayerStatistic, Integer> statisticValueMap = new Hashtable<>();
// Vehicle // Vehicle
private PlayerVehicleInformation vehicleInformation; private PlayerVehicleInformation vehicleInformation = new PlayerVehicleInformation();
public Player(UUID uuid, String username, PlayerConnection playerConnection) { public Player(UUID uuid, String username, PlayerConnection playerConnection) {
super(EntityType.PLAYER.getId()); super(EntityType.PLAYER.getId());

View File

@ -42,7 +42,10 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
this.offset = inventoryType.getAdditionalSlot(); this.offset = inventoryType.getAdditionalSlot();
this.itemStacks = new ItemStack[inventoryType.getAdditionalSlot()]; this.itemStacks = new ItemStack[inventoryType.getAdditionalSlot()];
Arrays.fill(itemStacks, ItemStack.AIR_ITEM);
for (int i = 0; i < itemStacks.length; i++) {
itemStacks[i] = ItemStack.getAirItem();
}
} }
private static int generateId() { private static int generateId() {
@ -117,12 +120,12 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
} }
public ItemStack getCursorItem(Player player) { public ItemStack getCursorItem(Player player) {
return cursorPlayersItem.getOrDefault(player, ItemStack.AIR_ITEM); return cursorPlayersItem.getOrDefault(player, ItemStack.getAirItem());
} }
private void safeItemInsert(int slot, ItemStack itemStack) { private void safeItemInsert(int slot, ItemStack itemStack) {
synchronized (this) { synchronized (this) {
itemStack = itemStack == null ? ItemStack.AIR_ITEM : itemStack; itemStack = itemStack == null ? ItemStack.getAirItem() : itemStack;
this.itemStacks[slot] = itemStack; this.itemStacks[slot] = itemStack;
SetSlotPacket setSlotPacket = new SetSlotPacket(); SetSlotPacket setSlotPacket = new SetSlotPacket();
setSlotPacket.windowId = 1; setSlotPacket.windowId = 1;

View File

@ -23,7 +23,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
private Player player; private Player player;
private ItemStack[] items = new ItemStack[INVENTORY_SIZE]; private ItemStack[] items = new ItemStack[INVENTORY_SIZE];
private ItemStack cursorItem = ItemStack.AIR_ITEM; private ItemStack cursorItem = ItemStack.getAirItem();
private InventoryCondition inventoryCondition; private InventoryCondition inventoryCondition;
private InventoryClickProcessor clickProcessor = new InventoryClickProcessor(); private InventoryClickProcessor clickProcessor = new InventoryClickProcessor();
@ -31,7 +31,9 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
public PlayerInventory(Player player) { public PlayerInventory(Player player) {
this.player = player; this.player = player;
Arrays.fill(items, ItemStack.AIR_ITEM); for (int i = 0; i < items.length; i++) {
items[i] = ItemStack.getAirItem();
}
} }
@Override @Override
@ -176,13 +178,14 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
return getLeggings(); return getLeggings();
case BOOTS: case BOOTS:
return getBoots(); return getBoots();
default:
throw new NullPointerException("Equipment slot cannot be null");
} }
return ItemStack.AIR_ITEM;
} }
private void safeItemInsert(int slot, ItemStack itemStack) { private void safeItemInsert(int slot, ItemStack itemStack) {
synchronized (this) { synchronized (this) {
itemStack = itemStack == null ? ItemStack.AIR_ITEM : itemStack; itemStack = itemStack == null ? ItemStack.getAirItem() : itemStack;
EntityEquipmentPacket.Slot equipmentSlot; EntityEquipmentPacket.Slot equipmentSlot;

View File

@ -136,12 +136,12 @@ public class InventoryClickProcessor {
if (clicked.isAir()) { if (clicked.isAir()) {
// Set held item [key] to slot // Set held item [key] to slot
resultClicked = ItemStack.AIR_ITEM; resultClicked = ItemStack.getAirItem();
resultHeld = clicked.clone(); resultHeld = clicked.clone();
} else { } else {
if (cursor.isAir()) { if (cursor.isAir()) {
// if held item [key] is air then set clicked to held // if held item [key] is air then set clicked to held
resultClicked = ItemStack.AIR_ITEM; resultClicked = ItemStack.getAirItem();
resultHeld = clicked.clone(); resultHeld = clicked.clone();
} else { } else {
// Otherwise replace held item and held // Otherwise replace held item and held
@ -218,7 +218,7 @@ public class InventoryClickProcessor {
// Switch // Switch
itemSetter.accept(index, resultClicked); itemSetter.accept(index, resultClicked);
itemSetter.accept(slot, ItemStack.AIR_ITEM); itemSetter.accept(slot, ItemStack.getAirItem());
filled = true; filled = true;
break; break;
} }
@ -347,7 +347,7 @@ public class InventoryClickProcessor {
public InventoryClickResult doubleClick(InventoryCondition inventoryCondition, Player player, int slot, public InventoryClickResult doubleClick(InventoryCondition inventoryCondition, Player player, int slot,
ItemStack cursor, InventoryClickLoopHandler... loopHandlers) { ItemStack cursor, InventoryClickLoopHandler... loopHandlers) {
InventoryClickResult clickResult = new InventoryClickResult(ItemStack.AIR_ITEM, cursor); InventoryClickResult clickResult = new InventoryClickResult(ItemStack.getAirItem(), cursor);
if (clickResult.isCancel()) { if (clickResult.isCancel()) {
return clickResult; return clickResult;

View File

@ -8,7 +8,10 @@ import java.util.ArrayList;
public class ItemStack implements DataContainer { public class ItemStack implements DataContainer {
public static final ItemStack AIR_ITEM = new ItemStack((short) 0, (byte) 0); public static ItemStack getAirItem() {
return new ItemStack((short) 0, (byte) 0);
}
private static StackingRule defaultStackingRule; private static StackingRule defaultStackingRule;
private short materialId; private short materialId;

View File

@ -22,7 +22,7 @@ public class VanillaStackingRule extends StackingRule {
@Override @Override
public ItemStack apply(ItemStack item, int newAmount) { public ItemStack apply(ItemStack item, int newAmount) {
if (newAmount <= 0) if (newAmount <= 0)
return ItemStack.AIR_ITEM; return ItemStack.getAirItem();
item.setAmount((byte) newAmount); item.setAmount((byte) newAmount);
return item; return item;

View File

@ -66,7 +66,7 @@ public class PlayerDiggingListener {
break; break;
case DROP_ITEM_STACK: case DROP_ITEM_STACK:
ItemStack droppedItemStack = player.getInventory().getItemInMainHand().clone(); ItemStack droppedItemStack = player.getInventory().getItemInMainHand().clone();
dropItem(player, droppedItemStack, ItemStack.AIR_ITEM); dropItem(player, droppedItemStack, ItemStack.getAirItem());
break; break;
case DROP_ITEM: case DROP_ITEM:
ItemStack droppedItemStack2 = player.getInventory().getItemInMainHand().clone(); ItemStack droppedItemStack2 = player.getInventory().getItemInMainHand().clone();
@ -74,7 +74,7 @@ public class PlayerDiggingListener {
ItemStack handItem = player.getInventory().getItemInMainHand(); ItemStack handItem = player.getInventory().getItemInMainHand();
handItem.setAmount((byte) (handItem.getAmount() - 1)); handItem.setAmount((byte) (handItem.getAmount() - 1));
handItem = handItem.getAmount() <= 0 ? ItemStack.AIR_ITEM : handItem; handItem = handItem.getAmount() <= 0 ? ItemStack.getAirItem() : handItem;
dropItem(player, droppedItemStack2, handItem); dropItem(player, droppedItemStack2, handItem);
break; break;

View File

@ -42,9 +42,9 @@ public class UseItemListener {
ItemStack armorItem = armorEquipEvent.getArmorItem(); ItemStack armorItem = armorEquipEvent.getArmorItem();
if (hand == Player.Hand.MAIN) { if (hand == Player.Hand.MAIN) {
playerInventory.setItemInMainHand(ItemStack.AIR_ITEM); playerInventory.setItemInMainHand(ItemStack.getAirItem());
} else { } else {
playerInventory.setItemInOffHand(ItemStack.AIR_ITEM); playerInventory.setItemInOffHand(ItemStack.getAirItem());
} }
switch (armorSlot) { switch (armorSlot) {

View File

@ -12,7 +12,7 @@ public class ShapedRecipe extends Recipe {
private List<DeclareRecipesPacket.Ingredient> ingredients = new ArrayList<>(); private List<DeclareRecipesPacket.Ingredient> ingredients = new ArrayList<>();
private ItemStack result = ItemStack.AIR_ITEM; private ItemStack result = ItemStack.getAirItem();
public ShapedRecipe(String recipeId, String group, int width, int height) { public ShapedRecipe(String recipeId, String group, int width, int height) {
super(RecipeType.SHAPED, recipeId); super(RecipeType.SHAPED, recipeId);

View File

@ -10,7 +10,7 @@ public class ShapelessRecipe extends Recipe {
private List<DeclareRecipesPacket.Ingredient> ingredients = new ArrayList<>(); private List<DeclareRecipesPacket.Ingredient> ingredients = new ArrayList<>();
private ItemStack result = ItemStack.AIR_ITEM; private ItemStack result = ItemStack.getAirItem();
public ShapelessRecipe(String recipeId, String group) { public ShapelessRecipe(String recipeId, String group) {
super(RecipeType.SHAPELESS, recipeId); super(RecipeType.SHAPELESS, recipeId);

View File

@ -7,7 +7,7 @@ public class SmeltingRecipe extends Recipe {
private DeclareRecipesPacket.Ingredient ingredient; private DeclareRecipesPacket.Ingredient ingredient;
private ItemStack result = ItemStack.AIR_ITEM; private ItemStack result = ItemStack.getAirItem();
private float experience; private float experience;

View File

@ -141,13 +141,13 @@ public class Utils {
boolean present = reader.readBoolean(); boolean present = reader.readBoolean();
if (!present) { if (!present) {
return ItemStack.AIR_ITEM; return ItemStack.getAirItem();
} }
int id = reader.readVarInt(); int id = reader.readVarInt();
if (id == -1) { if (id == -1) {
// Drop mode // Drop mode
return ItemStack.AIR_ITEM; return ItemStack.getAirItem();
} }
byte count = reader.readByte(); byte count = reader.readByte();