From 97bd1ecdb1f0de97a4dd6d0a643c2ea06af0baad Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Sat, 23 May 2020 04:20:01 +0200 Subject: [PATCH] Cleanup --- .../server/benchmark/BenchmarkManager.java | 4 +- .../server/command/CommandManager.java | 7 +- .../net/minestom/server/entity/Entity.java | 20 +++--- .../server/entity/EntityCreature.java | 15 +++- .../minestom/server/entity/EntityManager.java | 5 +- .../minestom/server/entity/LivingEntity.java | 2 - .../net/minestom/server/entity/Player.java | 69 +++++++++++++------ .../event/inventory/InventoryClickEvent.java | 2 +- .../event/inventory/InventoryOpenEvent.java | 5 +- .../inventory/InventoryPreClickEvent.java | 7 +- .../server/event/item/ArmorEquipEvent.java | 21 +----- .../net/minestom/server/instance/Chunk.java | 4 +- .../server/instance/InstanceManager.java | 7 +- .../server/inventory/EquipmentHandler.java | 2 +- .../minestom/server/inventory/Inventory.java | 9 ++- .../server/inventory/PlayerInventory.java | 16 ++--- .../net/minestom/server/item/ItemStack.java | 17 ++--- .../server/listener/ChatMessageListener.java | 4 +- .../server/play/DeclareRecipesPacket.java | 5 +- .../server/play/EntityEquipmentPacket.java | 19 ++++- .../minestom/server/scoreboard/Sidebar.java | 11 ++- .../server/storage/StorageFolder.java | 7 +- .../server/storage/StorageManager.java | 5 +- .../server/utils/consumer/StringConsumer.java | 7 -- .../utils/inventory/PlayerInventoryUtils.java | 1 + .../server/utils/item/ItemStackUtils.java | 15 ++++ .../minestom/server/utils/validate/Check.java | 22 ++++++ 27 files changed, 187 insertions(+), 121 deletions(-) delete mode 100644 src/main/java/net/minestom/server/utils/consumer/StringConsumer.java create mode 100644 src/main/java/net/minestom/server/utils/item/ItemStackUtils.java create mode 100644 src/main/java/net/minestom/server/utils/validate/Check.java diff --git a/src/main/java/net/minestom/server/benchmark/BenchmarkManager.java b/src/main/java/net/minestom/server/benchmark/BenchmarkManager.java index 5d48cd202..a554249f8 100644 --- a/src/main/java/net/minestom/server/benchmark/BenchmarkManager.java +++ b/src/main/java/net/minestom/server/benchmark/BenchmarkManager.java @@ -2,6 +2,7 @@ package net.minestom.server.benchmark; import net.minestom.server.MinecraftServer; import net.minestom.server.utils.time.UpdateOption; +import net.minestom.server.utils.validate.Check; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; @@ -37,8 +38,7 @@ public class BenchmarkManager { private long time; public void enable(UpdateOption updateOption) { - if (enabled) - throw new IllegalStateException("A benchmark is already running, please disable it first."); + Check.stateCondition(enabled, "A benchmark is already running, please disable it first."); this.updateOption = updateOption; time = updateOption.getTimeUnit().toMilliseconds(updateOption.getValue()); diff --git a/src/main/java/net/minestom/server/command/CommandManager.java b/src/main/java/net/minestom/server/command/CommandManager.java index ede863e71..166c32aa1 100644 --- a/src/main/java/net/minestom/server/command/CommandManager.java +++ b/src/main/java/net/minestom/server/command/CommandManager.java @@ -7,6 +7,7 @@ import net.minestom.server.entity.Player; import net.minestom.server.event.player.PlayerCommandEvent; import net.minestom.server.network.packet.server.play.DeclareCommandsPacket; import net.minestom.server.utils.ArrayUtils; +import net.minestom.server.utils.validate.Check; import java.util.ArrayList; import java.util.HashMap; @@ -29,10 +30,8 @@ public class CommandManager { } public boolean execute(Player source, String command) { - if (source == null) - throw new NullPointerException("Source cannot be null"); - if (command == null) - throw new NullPointerException("Command string cannot be null"); + Check.notNull(source, "Source cannot be null"); + Check.notNull(command, "Command string cannot be null"); PlayerCommandEvent playerCommandEvent = new PlayerCommandEvent(source, command); source.callEvent(PlayerCommandEvent.class, playerCommandEvent); diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 294085ece..74a284923 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -22,6 +22,7 @@ import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.utils.Vector; import net.minestom.server.utils.*; import net.minestom.server.utils.time.TimeUnit; +import net.minestom.server.utils.validate.Check; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -172,8 +173,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { } public void teleport(Position position, Runnable callback) { - if (instance == null) - throw new IllegalStateException("You need to use Entity#setInstance before teleporting an entity!"); + Check.stateCondition(instance == null, "You need to use Entity#setInstance before teleporting an entity!"); Runnable runnable = () -> { refreshPosition(position.getX(), position.getY(), position.getZ()); @@ -429,11 +429,9 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { } public void setInstance(Instance instance) { - if (instance == null) - throw new IllegalArgumentException("instance cannot be null!"); - - if (!MinecraftServer.getInstanceManager().getInstances().contains(instance)) - throw new IllegalStateException("Instances need to be registered with InstanceManager#createInstanceContainer or InstanceManager#createSharedInstance"); + Check.notNull(instance, "instance cannot be null!"); + Check.stateCondition(!MinecraftServer.getInstanceManager().getInstances().contains(instance), + "Instances need to be registered with InstanceManager#createInstanceContainer or InstanceManager#createSharedInstance"); if (this.instance != null) { this.instance.removeEntity(this); @@ -472,8 +470,8 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { } public void addPassenger(Entity entity) { - if (instance == null) - throw new IllegalStateException("You need to set an instance using Entity#setInstance"); + Check.stateCondition(instance == null, "You need to set an instance using Entity#setInstance"); + if (entity.getVehicle() != null) { entity.getVehicle().removePassenger(entity); } @@ -485,8 +483,8 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { } public void removePassenger(Entity entity) { - if (instance == null) - throw new IllegalStateException("You need to set an instance using Entity#setInstance"); + Check.stateCondition(instance == null, "You need to set an instance using Entity#setInstance"); + if (!passengers.contains(entity)) return; this.passengers.remove(entity); diff --git a/src/main/java/net/minestom/server/entity/EntityCreature.java b/src/main/java/net/minestom/server/entity/EntityCreature.java index 0ea384206..9eda80aff 100644 --- a/src/main/java/net/minestom/server/entity/EntityCreature.java +++ b/src/main/java/net/minestom/server/entity/EntityCreature.java @@ -11,6 +11,7 @@ import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.ChunkUtils; import net.minestom.server.utils.Position; import net.minestom.server.utils.Vector; +import net.minestom.server.utils.item.ItemStackUtils; import net.minestom.server.utils.time.TimeUnit; import java.util.LinkedList; @@ -34,6 +35,14 @@ public abstract class EntityCreature extends LivingEntity { public EntityCreature(EntityType entityType, Position spawnPosition) { super(entityType.getId(), spawnPosition); + + this.mainHandItem = ItemStack.getAirItem(); + this.offHandItem = ItemStack.getAirItem(); + + this.helmet = ItemStack.getAirItem(); + this.chestplate = ItemStack.getAirItem(); + this.leggings = ItemStack.getAirItem(); + this.boots = ItemStack.getAirItem(); } @Override @@ -150,7 +159,7 @@ public abstract class EntityCreature extends LivingEntity { @Override public void setItemInMainHand(ItemStack itemStack) { - this.mainHandItem = itemStack; + this.mainHandItem = ItemStackUtils.notNull(itemStack); syncEquipment(EntityEquipmentPacket.Slot.MAIN_HAND); } @@ -161,7 +170,7 @@ public abstract class EntityCreature extends LivingEntity { @Override public void setItemInOffHand(ItemStack itemStack) { - this.offHandItem = itemStack; + this.offHandItem = ItemStackUtils.notNull(itemStack); syncEquipment(EntityEquipmentPacket.Slot.OFF_HAND); } @@ -264,6 +273,8 @@ public abstract class EntityCreature extends LivingEntity { } private ItemStack getEquipmentItem(ItemStack itemStack, ArmorEquipEvent.ArmorSlot armorSlot) { + itemStack = ItemStackUtils.notNull(itemStack); + ArmorEquipEvent armorEquipEvent = new ArmorEquipEvent(this, itemStack, armorSlot); callEvent(ArmorEquipEvent.class, armorEquipEvent); return armorEquipEvent.getArmorItem(); diff --git a/src/main/java/net/minestom/server/entity/EntityManager.java b/src/main/java/net/minestom/server/entity/EntityManager.java index e3c78975c..85836b6a5 100644 --- a/src/main/java/net/minestom/server/entity/EntityManager.java +++ b/src/main/java/net/minestom/server/entity/EntityManager.java @@ -6,6 +6,7 @@ import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Instance; import net.minestom.server.instance.InstanceManager; import net.minestom.server.utils.thread.MinestomThread; +import net.minestom.server.utils.validate.Check; import java.util.Set; import java.util.concurrent.ConcurrentLinkedQueue; @@ -162,8 +163,8 @@ public class EntityManager { PlayerLoginEvent loginEvent = new PlayerLoginEvent(); playerCache.callEvent(PlayerLoginEvent.class, loginEvent); Instance spawningInstance = loginEvent.getSpawningInstance(); - if (spawningInstance == null) - throw new NullPointerException("You need to specify a spawning instance in the PlayerLoginEvent"); + + Check.notNull(spawningInstance, "You need to specify a spawning instance in the PlayerLoginEvent"); playerCache.setInstance(spawningInstance); }); diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index 67c1910fc..b4c660c0b 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -272,8 +272,6 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler { protected EntityEquipmentPacket getEquipmentPacket(EntityEquipmentPacket.Slot slot) { ItemStack itemStack = getEquipment(slot); - if (itemStack == null) - return null; EntityEquipmentPacket equipmentPacket = new EntityEquipmentPacket(); equipmentPacket.entityId = getEntityId(); diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 9a2ad0093..72ece6c5b 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -32,6 +32,7 @@ import net.minestom.server.sound.Sound; import net.minestom.server.sound.SoundCategory; import net.minestom.server.stat.PlayerStatistic; import net.minestom.server.utils.*; +import net.minestom.server.utils.validate.Check; import net.minestom.server.world.Dimension; import net.minestom.server.world.LevelType; @@ -159,7 +160,7 @@ public class Player extends LivingEntity { if (targetCustomBlock != null) { int animationCount = 10; long since = System.currentTimeMillis() - targetBlockTime; - byte stage = (byte) (since / (blockBreakTime / animationCount)); + byte stage = (byte) (since / (blockBreakTime / animationCount) - 1); if (stage != targetLastStage) { sendBlockBreakAnimation(targetBlockPosition, stage); } @@ -374,10 +375,8 @@ public class Player extends LivingEntity { @Override public void setInstance(Instance instance) { - if (instance == null) - throw new IllegalArgumentException("instance cannot be null!"); - if (this.instance == instance) - throw new IllegalArgumentException("Instance should be different than the current one"); + Check.notNull(instance, "instance cannot be null!"); + Check.argCondition(this.instance == instance, "Instance should be different than the current one"); boolean firstSpawn = this.instance == null; // TODO: Handle player reconnections, must be false in that case too for (Chunk viewableChunk : viewableChunks) { @@ -596,10 +595,16 @@ public class Player extends LivingEntity { sendUpdateHealthPacket(); } + /** + * @return true if the player is currently eating, false otherwise + */ public boolean isEating() { return isEating; } + /** + * @return the default eating time for the player + */ public long getDefaultEatingTime() { return defaultEatingTime; } @@ -690,8 +695,8 @@ public class Player extends LivingEntity { } public void setExp(float exp) { - if (!MathUtils.isBetween(exp, 0, 1)) - throw new IllegalArgumentException("Exp should be between 0 and 1"); + Check.argCondition(!MathUtils.isBetween(exp, 0, 1), "Exp should be between 0 and 1"); + this.exp = exp; sendExperienceUpdatePacket(); } @@ -811,10 +816,8 @@ public class Player extends LivingEntity { // Require sending chunk data after public void sendDimension(Dimension dimension) { - if (dimension == null) - throw new IllegalArgumentException("Dimension cannot be null!"); - if (dimension.equals(getDimension())) - throw new IllegalArgumentException("The dimension need to be different than the current one!"); + Check.notNull(dimension, "Dimension cannot be null!"); + Check.argCondition(dimension.equals(getDimension()), "The dimension need to be different than the current one!"); refreshDimension(dimension); RespawnPacket respawnPacket = new RespawnPacket(); @@ -847,15 +850,28 @@ public class Player extends LivingEntity { refreshGameMode(gameMode); } + /** + * Change the current held slot for the player + * + * @param slot the slot that the player has to held + * @throws IllegalArgumentException if {@code slot} is not between 0 and 8 + */ public void setHeldItemSlot(short slot) { - if (slot < 0 || slot > 8) - throw new IllegalArgumentException("Slot has to be between 0 and 8"); + Check.argCondition(!MathUtils.isBetween(slot, 0, 8), "Slot has to be between 0 and 8"); + HeldItemChangePacket heldItemChangePacket = new HeldItemChangePacket(); heldItemChangePacket.slot = slot; playerConnection.sendPacket(heldItemChangePacket); refreshHeldSlot(slot); } + /** + * @return the current held slot for the player + */ + public short getHeldSlot() { + return heldSlot; + } + public void setTeam(Team team) { if (this.team == team) return; @@ -887,18 +903,25 @@ public class Player extends LivingEntity { } } - public short getHeldSlot() { - return heldSlot; - } - + /** + * @return the currently open inventory, null if there is not (player inventory is not detected) + */ public Inventory getOpenInventory() { return openInventory; } + /** + * Used to get the {@link CustomBlock} that the player is currently mining + * + * @return the currently mined {@link CustomBlock} by the player, null if there is not + */ public CustomBlock getCustomBlockTarget() { return targetCustomBlock; } + /** + * @return an unmodifiable {@link Set} containing all the current player viewable boss bars + */ public Set getBossBars() { return Collections.unmodifiableSet(bossBars); } @@ -910,8 +933,7 @@ public class Player extends LivingEntity { * @return true if the inventory has been opened/sent to the player, false otherwise (cancelled by event) */ public boolean openInventory(Inventory inventory) { - if (inventory == null) - throw new IllegalArgumentException("Inventory cannot be null, use Player#closeInventory() to close current"); + Check.notNull(inventory, "Inventory cannot be null, use Player#closeInventory() to close current"); InventoryOpenEvent inventoryOpenEvent = new InventoryOpenEvent(this, inventory); @@ -970,10 +992,16 @@ public class Player extends LivingEntity { inventory.update(); } + /** + * @return an unmodifiable {@link Set} containing all the chunks that the player sees + */ public Set getViewableChunks() { return Collections.unmodifiableSet(viewableChunks); } + /** + * Remove all the boss bars that the player has + */ public void clearBossBars() { this.bossBars.forEach(bossBar -> bossBar.removeViewer(this)); } @@ -996,8 +1024,7 @@ public class Player extends LivingEntity { } public void setPermissionLevel(int permissionLevel) { - if (!MathUtils.isBetween(permissionLevel, 0, 4)) - throw new IllegalArgumentException("permissionLevel has to be between 0 and 4"); + Check.argCondition(!MathUtils.isBetween(permissionLevel, 0, 4), "permissionLevel has to be between 0 and 4"); this.permissionLevel = permissionLevel; diff --git a/src/main/java/net/minestom/server/event/inventory/InventoryClickEvent.java b/src/main/java/net/minestom/server/event/inventory/InventoryClickEvent.java index 0f490ff98..bb3416b41 100644 --- a/src/main/java/net/minestom/server/event/inventory/InventoryClickEvent.java +++ b/src/main/java/net/minestom/server/event/inventory/InventoryClickEvent.java @@ -24,7 +24,7 @@ public class InventoryClickEvent extends Event { /** * Can be null if the clicked inventory is the player one * - * @return + * @return the inventory where the click happened, null if this is the player's inventory */ public Inventory getInventory() { return inventory; diff --git a/src/main/java/net/minestom/server/event/inventory/InventoryOpenEvent.java b/src/main/java/net/minestom/server/event/inventory/InventoryOpenEvent.java index e58a64b80..bb823b4d3 100644 --- a/src/main/java/net/minestom/server/event/inventory/InventoryOpenEvent.java +++ b/src/main/java/net/minestom/server/event/inventory/InventoryOpenEvent.java @@ -3,6 +3,7 @@ package net.minestom.server.event.inventory; import net.minestom.server.entity.Player; import net.minestom.server.event.CancellableEvent; import net.minestom.server.inventory.Inventory; +import net.minestom.server.utils.validate.Check; public class InventoryOpenEvent extends CancellableEvent { @@ -23,9 +24,7 @@ public class InventoryOpenEvent extends CancellableEvent { } public void setInventory(Inventory inventory) { - if (inventory == null) - throw new NullPointerException("Inventory cannot be null!"); - + Check.notNull(inventory, "Inventory cannot be null!"); this.inventory = inventory; } } diff --git a/src/main/java/net/minestom/server/event/inventory/InventoryPreClickEvent.java b/src/main/java/net/minestom/server/event/inventory/InventoryPreClickEvent.java index 214a3125f..99a9c4377 100644 --- a/src/main/java/net/minestom/server/event/inventory/InventoryPreClickEvent.java +++ b/src/main/java/net/minestom/server/event/inventory/InventoryPreClickEvent.java @@ -4,6 +4,7 @@ import net.minestom.server.event.CancellableEvent; import net.minestom.server.inventory.Inventory; import net.minestom.server.inventory.click.ClickType; import net.minestom.server.item.ItemStack; +import net.minestom.server.utils.item.ItemStackUtils; public class InventoryPreClickEvent extends CancellableEvent { @@ -24,7 +25,7 @@ public class InventoryPreClickEvent extends CancellableEvent { /** * Can be null if the clicked inventory is the player one * - * @return + * @return the inventory where the click happened, null if this is the player's inventory */ public Inventory getInventory() { return inventory; @@ -43,7 +44,7 @@ public class InventoryPreClickEvent extends CancellableEvent { } public void setClickedItem(ItemStack clickedItem) { - this.clickedItem = clickedItem; + this.clickedItem = ItemStackUtils.notNull(clickedItem); } public ItemStack getCursorItem() { @@ -51,6 +52,6 @@ public class InventoryPreClickEvent extends CancellableEvent { } public void setCursorItem(ItemStack cursorItem) { - this.cursorItem = cursorItem; + this.cursorItem = ItemStackUtils.notNull(cursorItem); } } diff --git a/src/main/java/net/minestom/server/event/item/ArmorEquipEvent.java b/src/main/java/net/minestom/server/event/item/ArmorEquipEvent.java index 5c95f1587..04e32f680 100644 --- a/src/main/java/net/minestom/server/event/item/ArmorEquipEvent.java +++ b/src/main/java/net/minestom/server/event/item/ArmorEquipEvent.java @@ -3,7 +3,7 @@ package net.minestom.server.event.item; import net.minestom.server.entity.LivingEntity; import net.minestom.server.event.Event; import net.minestom.server.item.ItemStack; -import net.minestom.server.network.packet.server.play.EntityEquipmentPacket; +import net.minestom.server.utils.item.ItemStackUtils; public class ArmorEquipEvent extends Event { @@ -26,7 +26,7 @@ public class ArmorEquipEvent extends Event { } public void setArmorItem(ItemStack armorItem) { - this.armorItem = armorItem; + this.armorItem = ItemStackUtils.notNull(armorItem); } public ArmorSlot getArmorSlot() { @@ -37,21 +37,6 @@ public class ArmorEquipEvent extends Event { HELMET, CHESTPLATE, LEGGINGS, - BOOTS; - - public EntityEquipmentPacket.Slot toEquipmentPacketSlot() { - switch (this) { - case HELMET: - return EntityEquipmentPacket.Slot.HELMET; - case CHESTPLATE: - return EntityEquipmentPacket.Slot.CHESTPLATE; - case LEGGINGS: - return EntityEquipmentPacket.Slot.LEGGINGS; - case BOOTS: - return EntityEquipmentPacket.Slot.BOOTS; - default: - return null; - } - } + BOOTS } } diff --git a/src/main/java/net/minestom/server/instance/Chunk.java b/src/main/java/net/minestom/server/instance/Chunk.java index d83379659..2160b40f6 100644 --- a/src/main/java/net/minestom/server/instance/Chunk.java +++ b/src/main/java/net/minestom/server/instance/Chunk.java @@ -20,6 +20,7 @@ import net.minestom.server.utils.PacketUtils; import net.minestom.server.utils.SerializerUtils; import net.minestom.server.utils.time.CooldownUtils; import net.minestom.server.utils.time.UpdateOption; +import net.minestom.server.utils.validate.Check; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; @@ -78,8 +79,7 @@ public class Chunk implements Viewable { public void UNSAFE_setCustomBlock(int x, int y, int z, short visualBlockId, short customBlockId, Data data) { CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId); - if (customBlock == null) - throw new IllegalArgumentException("The custom block " + customBlockId + " does not exist or isn't registered"); + Check.notNull(customBlock, "The custom block " + customBlockId + " does not exist or isn't registered"); UNSAFE_setCustomBlock(x, y, z, visualBlockId, customBlock, data); } diff --git a/src/main/java/net/minestom/server/instance/InstanceManager.java b/src/main/java/net/minestom/server/instance/InstanceManager.java index 67cc4df7d..964746b1e 100644 --- a/src/main/java/net/minestom/server/instance/InstanceManager.java +++ b/src/main/java/net/minestom/server/instance/InstanceManager.java @@ -3,6 +3,7 @@ package net.minestom.server.instance; import net.minestom.server.MinecraftServer; import net.minestom.server.storage.StorageFolder; import net.minestom.server.utils.thread.MinestomThread; +import net.minestom.server.utils.validate.Check; import net.minestom.server.world.Dimension; import java.util.Collections; @@ -42,8 +43,7 @@ public class InstanceManager { public SharedInstance createSharedInstance(SharedInstance sharedInstance) { InstanceContainer instanceContainer = sharedInstance.getInstanceContainer(); - if (instanceContainer == null) - throw new NullPointerException("SharedInstance needs to have an InstanceContainer to be created!"); + Check.notNull(instanceContainer, "SharedInstance needs to have an InstanceContainer to be created!"); instanceContainer.addSharedInstance(sharedInstance); this.instances.add(sharedInstance); @@ -51,8 +51,7 @@ public class InstanceManager { } public SharedInstance createSharedInstance(InstanceContainer instanceContainer) { - if (instanceContainer == null) - throw new IllegalArgumentException("Instance container cannot be null when creating a SharedInstance!"); + Check.notNull(instanceContainer, "Instance container cannot be null when creating a SharedInstance!"); SharedInstance sharedInstance = new SharedInstance(UUID.randomUUID(), instanceContainer); return createSharedInstance(sharedInstance); diff --git a/src/main/java/net/minestom/server/inventory/EquipmentHandler.java b/src/main/java/net/minestom/server/inventory/EquipmentHandler.java index 932355205..a0f74295b 100644 --- a/src/main/java/net/minestom/server/inventory/EquipmentHandler.java +++ b/src/main/java/net/minestom/server/inventory/EquipmentHandler.java @@ -70,7 +70,7 @@ public interface EquipmentHandler { case BOOTS: return getBoots(); default: - throw new NullPointerException("Equipment slot cannot be null"); + return null; } } diff --git a/src/main/java/net/minestom/server/inventory/Inventory.java b/src/main/java/net/minestom/server/inventory/Inventory.java index a89e4f2a3..563963c77 100644 --- a/src/main/java/net/minestom/server/inventory/Inventory.java +++ b/src/main/java/net/minestom/server/inventory/Inventory.java @@ -11,7 +11,10 @@ import net.minestom.server.item.ItemStack; import net.minestom.server.network.packet.server.play.SetSlotPacket; import net.minestom.server.network.packet.server.play.WindowItemsPacket; import net.minestom.server.network.packet.server.play.WindowPropertyPacket; +import net.minestom.server.utils.MathUtils; import net.minestom.server.utils.inventory.PlayerInventoryUtils; +import net.minestom.server.utils.item.ItemStackUtils; +import net.minestom.server.utils.validate.Check; import java.util.Arrays; import java.util.Collections; @@ -77,8 +80,8 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View @Override public void setItemStack(int slot, ItemStack itemStack) { - if (slot < 0 || slot > getSize()) - throw new IllegalArgumentException(inventoryType.toString() + " does not have slot " + slot); + Check.argCondition(!MathUtils.isBetween(slot, 0, getSize()), + inventoryType.toString() + " does not have slot " + slot); safeItemInsert(slot, itemStack); } @@ -160,7 +163,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View private void safeItemInsert(int slot, ItemStack itemStack) { synchronized (this) { - itemStack = itemStack == null ? ItemStack.getAirItem() : itemStack; + itemStack = ItemStackUtils.notNull(itemStack); this.itemStacks[slot] = itemStack; SetSlotPacket setSlotPacket = new SetSlotPacket(); setSlotPacket.windowId = getWindowId(); diff --git a/src/main/java/net/minestom/server/inventory/PlayerInventory.java b/src/main/java/net/minestom/server/inventory/PlayerInventory.java index 60467b83f..851ea9b02 100644 --- a/src/main/java/net/minestom/server/inventory/PlayerInventory.java +++ b/src/main/java/net/minestom/server/inventory/PlayerInventory.java @@ -14,6 +14,7 @@ import net.minestom.server.network.packet.server.play.SetSlotPacket; import net.minestom.server.network.packet.server.play.WindowItemsPacket; import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.utils.MathUtils; +import net.minestom.server.utils.item.ItemStackUtils; import java.util.Arrays; import java.util.List; @@ -181,12 +182,12 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler } public void setCursorItem(ItemStack cursorItem) { - this.cursorItem = cursorItem; + this.cursorItem = ItemStackUtils.notNull(cursorItem); } private void safeItemInsert(int slot, ItemStack itemStack) { synchronized (this) { - itemStack = itemStack == null ? ItemStack.getAirItem() : itemStack; + itemStack = ItemStackUtils.notNull(itemStack); EntityEquipmentPacket.Slot equipmentSlot; @@ -208,7 +209,8 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler } if (armorEquipEvent != null) { - equipmentSlot = armorEquipEvent.getArmorSlot().toEquipmentPacketSlot(); + ArmorEquipEvent.ArmorSlot armorSlot = armorEquipEvent.getArmorSlot(); + equipmentSlot = EntityEquipmentPacket.Slot.fromArmorSlot(armorSlot); player.callEvent(ArmorEquipEvent.class, armorEquipEvent); itemStack = armorEquipEvent.getArmorItem(); } else { @@ -216,13 +218,11 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler } } - if (itemStack != null) { - this.items[slot] = itemStack; - } + this.items[slot] = itemStack; // Refresh slot - refreshSlot(slot); - //update(); in case of problems + update(); + //refreshSlot(slot); seems to break things concerning +64 stacks // Sync equipment if (equipmentSlot != null) { diff --git a/src/main/java/net/minestom/server/item/ItemStack.java b/src/main/java/net/minestom/server/item/ItemStack.java index 511492d5a..8360d99f9 100644 --- a/src/main/java/net/minestom/server/item/ItemStack.java +++ b/src/main/java/net/minestom/server/item/ItemStack.java @@ -3,6 +3,7 @@ package net.minestom.server.item; import net.minestom.server.data.Data; import net.minestom.server.data.DataContainer; import net.minestom.server.item.rule.VanillaStackingRule; +import net.minestom.server.utils.validate.Check; import java.util.*; @@ -208,11 +209,9 @@ public class ItemStack implements DataContainer { return stackingRule; } - public void setStackingRule(StackingRule stackingRule) { - if (stackingRule == null) - throw new NullPointerException("StackingRule cannot be null!"); - - this.stackingRule = stackingRule; + public static void setDefaultStackingRule(StackingRule defaultStackingRule) { + Check.notNull(defaultStackingRule, "StackingRule cannot be null!"); + ItemStack.defaultStackingRule = defaultStackingRule; } @Override @@ -229,11 +228,9 @@ public class ItemStack implements DataContainer { return defaultStackingRule; } - public static void setDefaultStackingRule(StackingRule defaultStackingRule) { - if (defaultStackingRule == null) - throw new NullPointerException("StackingRule cannot be null!"); - - ItemStack.defaultStackingRule = defaultStackingRule; + public void setStackingRule(StackingRule stackingRule) { + Check.notNull(stackingRule, "StackingRule cannot be null!"); + this.stackingRule = stackingRule; } private byte getBitModifier(ItemFlag hideFlag) { diff --git a/src/main/java/net/minestom/server/listener/ChatMessageListener.java b/src/main/java/net/minestom/server/listener/ChatMessageListener.java index 9bb8e642b..afb0f156a 100644 --- a/src/main/java/net/minestom/server/listener/ChatMessageListener.java +++ b/src/main/java/net/minestom/server/listener/ChatMessageListener.java @@ -12,6 +12,7 @@ import net.minestom.server.entity.Player; import net.minestom.server.event.player.PlayerChatEvent; import net.minestom.server.event.player.PlayerCommandEvent; import net.minestom.server.network.packet.client.play.ClientChatMessagePacket; +import net.minestom.server.utils.validate.Check; import java.util.function.Function; @@ -55,8 +56,7 @@ public class ChatMessageListener { player.callCancellableEvent(PlayerChatEvent.class, playerChatEvent, () -> { Function formatFunction = playerChatEvent.getChatFormatFunction(); - if (formatFunction == null) - throw new NullPointerException("PlayerChatEvent#chatFormat cannot be null!"); + Check.notNull(formatFunction, "PlayerChatEvent#getChatFormatFunction cannot be null!"); TextComponent textObject = formatFunction.apply(playerChatEvent); diff --git a/src/main/java/net/minestom/server/network/packet/server/play/DeclareRecipesPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/DeclareRecipesPacket.java index 50c182e84..4f7128806 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/DeclareRecipesPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/DeclareRecipesPacket.java @@ -4,6 +4,7 @@ import net.minestom.server.item.ItemStack; import net.minestom.server.network.packet.PacketWriter; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; +import net.minestom.server.utils.validate.Check; public class DeclareRecipesPacket implements ServerPacket { @@ -11,8 +12,8 @@ public class DeclareRecipesPacket implements ServerPacket { @Override public void write(PacketWriter writer) { - if (recipes == null) - throw new NullPointerException("Recipes cannot be null!"); + Check.notNull(recipes, "Recipes cannot be null!"); + writer.writeVarInt(recipes.length); for (Recipe recipe : recipes) { recipe.write(writer); diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EntityEquipmentPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EntityEquipmentPacket.java index 612c439dc..d1a1a4769 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EntityEquipmentPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EntityEquipmentPacket.java @@ -1,5 +1,6 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.event.item.ArmorEquipEvent; import net.minestom.server.item.ItemStack; import net.minestom.server.network.packet.PacketWriter; import net.minestom.server.network.packet.server.ServerPacket; @@ -29,7 +30,23 @@ public class EntityEquipmentPacket implements ServerPacket { BOOTS, LEGGINGS, CHESTPLATE, - HELMET + HELMET; + + public static Slot fromArmorSlot(ArmorEquipEvent.ArmorSlot armorSlot) { + switch (armorSlot) { + case HELMET: + return HELMET; + case CHESTPLATE: + return CHESTPLATE; + case LEGGINGS: + return LEGGINGS; + case BOOTS: + return BOOTS; + default: + return null; + } + } + } } diff --git a/src/main/java/net/minestom/server/scoreboard/Sidebar.java b/src/main/java/net/minestom/server/scoreboard/Sidebar.java index faf651907..fa976f062 100644 --- a/src/main/java/net/minestom/server/scoreboard/Sidebar.java +++ b/src/main/java/net/minestom/server/scoreboard/Sidebar.java @@ -7,6 +7,7 @@ import net.minestom.server.network.packet.server.play.DisplayScoreboardPacket; import net.minestom.server.network.packet.server.play.ScoreboardObjectivePacket; import net.minestom.server.network.packet.server.play.UpdateScorePacket; import net.minestom.server.network.player.PlayerConnection; +import net.minestom.server.utils.validate.Check; import java.util.Iterator; import java.util.LinkedList; @@ -60,15 +61,13 @@ public class Sidebar implements Viewable { public void createLine(ScoreboardLine scoreboardLine) { synchronized (lines) { - if (lines.size() >= MAX_LINES_COUNT) - throw new IllegalStateException("You cannot have more than " + MAX_LINES_COUNT + " lines"); - if (lines.contains(scoreboardLine)) - throw new IllegalArgumentException("You cannot add two times the same ScoreboardLine"); + Check.stateCondition(lines.size() >= MAX_LINES_COUNT, "You cannot have more than " + MAX_LINES_COUNT + " lines"); + Check.argCondition(lines.contains(scoreboardLine), "You cannot add two times the same ScoreboardLine"); // Check ID duplication for (ScoreboardLine line : lines) { - if (line.id.equals(scoreboardLine.id)) - throw new IllegalArgumentException("You cannot add two ScoreboardLine with the same id"); + Check.argCondition(line.id.equals(scoreboardLine.id), + "You cannot add two ScoreboardLine with the same id"); } // Setup line diff --git a/src/main/java/net/minestom/server/storage/StorageFolder.java b/src/main/java/net/minestom/server/storage/StorageFolder.java index 344fc14ef..710588007 100644 --- a/src/main/java/net/minestom/server/storage/StorageFolder.java +++ b/src/main/java/net/minestom/server/storage/StorageFolder.java @@ -9,6 +9,7 @@ import net.minestom.server.data.SerializableData; import net.minestom.server.network.packet.PacketReader; import net.minestom.server.network.packet.PacketWriter; import net.minestom.server.reader.DataReader; +import net.minestom.server.utils.validate.Check; import java.io.IOException; import java.util.HashMap; @@ -50,8 +51,7 @@ public class StorageFolder { public void set(String key, T object, Class type) { DataType dataType = DATA_MANAGER.getDataType(type); - if (dataType == null) - throw new NullPointerException("You can only save registered DataType type!"); + Check.notNull(dataType, "You can only save registered DataType type!"); PacketWriter packetWriter = new PacketWriter(); dataType.encode(packetWriter, object); // Encode @@ -62,8 +62,7 @@ public class StorageFolder { public T get(String key, Class type) { DataType dataType = DATA_MANAGER.getDataType(type); - if (dataType == null) - throw new NullPointerException("You can only save registered DataType type!"); + Check.notNull(dataType, "You can only save registered DataType type!"); byte[] data = get(key); if (data == null) diff --git a/src/main/java/net/minestom/server/storage/StorageManager.java b/src/main/java/net/minestom/server/storage/StorageManager.java index 29bb4a6e5..d0431b908 100644 --- a/src/main/java/net/minestom/server/storage/StorageManager.java +++ b/src/main/java/net/minestom/server/storage/StorageManager.java @@ -1,5 +1,6 @@ package net.minestom.server.storage; +import net.minestom.server.utils.validate.Check; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,8 +42,8 @@ public class StorageManager { * @throws NullPointerException if no default StorageSystem is defined {@link #defineDefaultStorageSystem(Supplier)} */ public StorageFolder getFolder(String folderPath) { - if (defaultStorageSystemSupplier == null) - throw new NullPointerException("You need to either define a default storage system or specify your storage system for this specific folder"); + Check.notNull(defaultStorageSystemSupplier, + "You need to either define a default storage system or specify your storage system for this specific folder"); StorageSystem storageSystem = defaultStorageSystemSupplier.get(); return getFolder(folderPath, storageSystem); diff --git a/src/main/java/net/minestom/server/utils/consumer/StringConsumer.java b/src/main/java/net/minestom/server/utils/consumer/StringConsumer.java deleted file mode 100644 index 14890781b..000000000 --- a/src/main/java/net/minestom/server/utils/consumer/StringConsumer.java +++ /dev/null @@ -1,7 +0,0 @@ -package net.minestom.server.utils.consumer; - -public interface StringConsumer { - - void accept(String string, int length); - -} diff --git a/src/main/java/net/minestom/server/utils/inventory/PlayerInventoryUtils.java b/src/main/java/net/minestom/server/utils/inventory/PlayerInventoryUtils.java index 1d2ef0564..a795cc953 100644 --- a/src/main/java/net/minestom/server/utils/inventory/PlayerInventoryUtils.java +++ b/src/main/java/net/minestom/server/utils/inventory/PlayerInventoryUtils.java @@ -21,6 +21,7 @@ public class PlayerInventoryUtils { * * @param slot the packet slot * @param offset the slot count separating the up part of the inventory to the bottom part (armor/craft in PlayerInventory, inventory slots in others) + * the offset for the player inventory is {@link #OFFSET} * @return a packet which can be use internally with Minestom */ public static int convertSlot(int slot, int offset) { diff --git a/src/main/java/net/minestom/server/utils/item/ItemStackUtils.java b/src/main/java/net/minestom/server/utils/item/ItemStackUtils.java new file mode 100644 index 000000000..ec1fb3e4f --- /dev/null +++ b/src/main/java/net/minestom/server/utils/item/ItemStackUtils.java @@ -0,0 +1,15 @@ +package net.minestom.server.utils.item; + +import net.minestom.server.item.ItemStack; + +public class ItemStackUtils { + + /** + * @param itemStack the ItemStack to return if not null + * @return {@code itemStack} if not null, {@link ItemStack#getAirItem()} otherwise + */ + public static ItemStack notNull(ItemStack itemStack) { + return itemStack == null ? ItemStack.getAirItem() : itemStack; + } + +} diff --git a/src/main/java/net/minestom/server/utils/validate/Check.java b/src/main/java/net/minestom/server/utils/validate/Check.java new file mode 100644 index 000000000..de641f427 --- /dev/null +++ b/src/main/java/net/minestom/server/utils/validate/Check.java @@ -0,0 +1,22 @@ +package net.minestom.server.utils.validate; + +import java.util.Objects; + +public class Check { + + public static void notNull(Object object, String reason) { + if (Objects.isNull(object)) + throw new NullPointerException(reason); + } + + public static void argCondition(boolean condition, String reason) { + if (condition) + throw new IllegalArgumentException(reason); + } + + public static void stateCondition(boolean condition, String reason) { + if (condition) + throw new IllegalStateException(reason); + } + +}