mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-01 05:58:00 +01:00
Cleanup
This commit is contained in:
parent
28e6ef9b87
commit
97bd1ecdb1
@ -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());
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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();
|
||||
|
@ -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);
|
||||
});
|
||||
|
@ -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();
|
||||
|
@ -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<BossBar> 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<Chunk> 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;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -70,7 +70,7 @@ public interface EquipmentHandler {
|
||||
case BOOTS:
|
||||
return getBoots();
|
||||
default:
|
||||
throw new NullPointerException("Equipment slot cannot be null");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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<PlayerChatEvent, TextComponent> 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);
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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 <T> void set(String key, T object, Class<T> type) {
|
||||
DataType<T> 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> T get(String key, Class<T> type) {
|
||||
DataType<T> 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)
|
||||
|
@ -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);
|
||||
|
@ -1,7 +0,0 @@
|
||||
package net.minestom.server.utils.consumer;
|
||||
|
||||
public interface StringConsumer {
|
||||
|
||||
void accept(String string, int length);
|
||||
|
||||
}
|
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
22
src/main/java/net/minestom/server/utils/validate/Check.java
Normal file
22
src/main/java/net/minestom/server/utils/validate/Check.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user