mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-19 06:32:03 +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.MinecraftServer;
|
||||||
import net.minestom.server.utils.time.UpdateOption;
|
import net.minestom.server.utils.time.UpdateOption;
|
||||||
|
import net.minestom.server.utils.validate.Check;
|
||||||
|
|
||||||
import java.lang.management.ManagementFactory;
|
import java.lang.management.ManagementFactory;
|
||||||
import java.lang.management.ThreadInfo;
|
import java.lang.management.ThreadInfo;
|
||||||
@ -37,8 +38,7 @@ public class BenchmarkManager {
|
|||||||
private long time;
|
private long time;
|
||||||
|
|
||||||
public void enable(UpdateOption updateOption) {
|
public void enable(UpdateOption updateOption) {
|
||||||
if (enabled)
|
Check.stateCondition(enabled, "A benchmark is already running, please disable it first.");
|
||||||
throw new IllegalStateException("A benchmark is already running, please disable it first.");
|
|
||||||
|
|
||||||
this.updateOption = updateOption;
|
this.updateOption = updateOption;
|
||||||
time = updateOption.getTimeUnit().toMilliseconds(updateOption.getValue());
|
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.event.player.PlayerCommandEvent;
|
||||||
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
||||||
import net.minestom.server.utils.ArrayUtils;
|
import net.minestom.server.utils.ArrayUtils;
|
||||||
|
import net.minestom.server.utils.validate.Check;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -29,10 +30,8 @@ public class CommandManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean execute(Player source, String command) {
|
public boolean execute(Player source, String command) {
|
||||||
if (source == null)
|
Check.notNull(source, "Source cannot be null");
|
||||||
throw new NullPointerException("Source cannot be null");
|
Check.notNull(command, "Command string cannot be null");
|
||||||
if (command == null)
|
|
||||||
throw new NullPointerException("Command string cannot be null");
|
|
||||||
|
|
||||||
PlayerCommandEvent playerCommandEvent = new PlayerCommandEvent(source, command);
|
PlayerCommandEvent playerCommandEvent = new PlayerCommandEvent(source, command);
|
||||||
source.callEvent(PlayerCommandEvent.class, playerCommandEvent);
|
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.Vector;
|
||||||
import net.minestom.server.utils.*;
|
import net.minestom.server.utils.*;
|
||||||
import net.minestom.server.utils.time.TimeUnit;
|
import net.minestom.server.utils.time.TimeUnit;
|
||||||
|
import net.minestom.server.utils.validate.Check;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
@ -172,8 +173,7 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void teleport(Position position, Runnable callback) {
|
public void teleport(Position position, Runnable callback) {
|
||||||
if (instance == null)
|
Check.stateCondition(instance == null, "You need to use Entity#setInstance before teleporting an entity!");
|
||||||
throw new IllegalStateException("You need to use Entity#setInstance before teleporting an entity!");
|
|
||||||
|
|
||||||
Runnable runnable = () -> {
|
Runnable runnable = () -> {
|
||||||
refreshPosition(position.getX(), position.getY(), position.getZ());
|
refreshPosition(position.getX(), position.getY(), position.getZ());
|
||||||
@ -429,11 +429,9 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setInstance(Instance instance) {
|
public void setInstance(Instance instance) {
|
||||||
if (instance == null)
|
Check.notNull(instance, "instance cannot be null!");
|
||||||
throw new IllegalArgumentException("instance cannot be null!");
|
Check.stateCondition(!MinecraftServer.getInstanceManager().getInstances().contains(instance),
|
||||||
|
"Instances need to be registered with InstanceManager#createInstanceContainer or InstanceManager#createSharedInstance");
|
||||||
if (!MinecraftServer.getInstanceManager().getInstances().contains(instance))
|
|
||||||
throw new IllegalStateException("Instances need to be registered with InstanceManager#createInstanceContainer or InstanceManager#createSharedInstance");
|
|
||||||
|
|
||||||
if (this.instance != null) {
|
if (this.instance != null) {
|
||||||
this.instance.removeEntity(this);
|
this.instance.removeEntity(this);
|
||||||
@ -472,8 +470,8 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addPassenger(Entity entity) {
|
public void addPassenger(Entity entity) {
|
||||||
if (instance == null)
|
Check.stateCondition(instance == null, "You need to set an instance using Entity#setInstance");
|
||||||
throw new IllegalStateException("You need to set an instance using Entity#setInstance");
|
|
||||||
if (entity.getVehicle() != null) {
|
if (entity.getVehicle() != null) {
|
||||||
entity.getVehicle().removePassenger(entity);
|
entity.getVehicle().removePassenger(entity);
|
||||||
}
|
}
|
||||||
@ -485,8 +483,8 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void removePassenger(Entity entity) {
|
public void removePassenger(Entity entity) {
|
||||||
if (instance == null)
|
Check.stateCondition(instance == null, "You need to set an instance using Entity#setInstance");
|
||||||
throw new IllegalStateException("You need to set an instance using Entity#setInstance");
|
|
||||||
if (!passengers.contains(entity))
|
if (!passengers.contains(entity))
|
||||||
return;
|
return;
|
||||||
this.passengers.remove(entity);
|
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.ChunkUtils;
|
||||||
import net.minestom.server.utils.Position;
|
import net.minestom.server.utils.Position;
|
||||||
import net.minestom.server.utils.Vector;
|
import net.minestom.server.utils.Vector;
|
||||||
|
import net.minestom.server.utils.item.ItemStackUtils;
|
||||||
import net.minestom.server.utils.time.TimeUnit;
|
import net.minestom.server.utils.time.TimeUnit;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
@ -34,6 +35,14 @@ public abstract class EntityCreature extends LivingEntity {
|
|||||||
|
|
||||||
public EntityCreature(EntityType entityType, Position spawnPosition) {
|
public EntityCreature(EntityType entityType, Position spawnPosition) {
|
||||||
super(entityType.getId(), 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
|
@Override
|
||||||
@ -150,7 +159,7 @@ public abstract class EntityCreature extends LivingEntity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setItemInMainHand(ItemStack itemStack) {
|
public void setItemInMainHand(ItemStack itemStack) {
|
||||||
this.mainHandItem = itemStack;
|
this.mainHandItem = ItemStackUtils.notNull(itemStack);
|
||||||
syncEquipment(EntityEquipmentPacket.Slot.MAIN_HAND);
|
syncEquipment(EntityEquipmentPacket.Slot.MAIN_HAND);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,7 +170,7 @@ public abstract class EntityCreature extends LivingEntity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setItemInOffHand(ItemStack itemStack) {
|
public void setItemInOffHand(ItemStack itemStack) {
|
||||||
this.offHandItem = itemStack;
|
this.offHandItem = ItemStackUtils.notNull(itemStack);
|
||||||
syncEquipment(EntityEquipmentPacket.Slot.OFF_HAND);
|
syncEquipment(EntityEquipmentPacket.Slot.OFF_HAND);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -264,6 +273,8 @@ public abstract class EntityCreature extends LivingEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private ItemStack getEquipmentItem(ItemStack itemStack, ArmorEquipEvent.ArmorSlot armorSlot) {
|
private ItemStack getEquipmentItem(ItemStack itemStack, ArmorEquipEvent.ArmorSlot armorSlot) {
|
||||||
|
itemStack = ItemStackUtils.notNull(itemStack);
|
||||||
|
|
||||||
ArmorEquipEvent armorEquipEvent = new ArmorEquipEvent(this, itemStack, armorSlot);
|
ArmorEquipEvent armorEquipEvent = new ArmorEquipEvent(this, itemStack, armorSlot);
|
||||||
callEvent(ArmorEquipEvent.class, armorEquipEvent);
|
callEvent(ArmorEquipEvent.class, armorEquipEvent);
|
||||||
return armorEquipEvent.getArmorItem();
|
return armorEquipEvent.getArmorItem();
|
||||||
|
@ -6,6 +6,7 @@ import net.minestom.server.instance.Chunk;
|
|||||||
import net.minestom.server.instance.Instance;
|
import net.minestom.server.instance.Instance;
|
||||||
import net.minestom.server.instance.InstanceManager;
|
import net.minestom.server.instance.InstanceManager;
|
||||||
import net.minestom.server.utils.thread.MinestomThread;
|
import net.minestom.server.utils.thread.MinestomThread;
|
||||||
|
import net.minestom.server.utils.validate.Check;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
@ -162,8 +163,8 @@ public class EntityManager {
|
|||||||
PlayerLoginEvent loginEvent = new PlayerLoginEvent();
|
PlayerLoginEvent loginEvent = new PlayerLoginEvent();
|
||||||
playerCache.callEvent(PlayerLoginEvent.class, loginEvent);
|
playerCache.callEvent(PlayerLoginEvent.class, loginEvent);
|
||||||
Instance spawningInstance = loginEvent.getSpawningInstance();
|
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);
|
playerCache.setInstance(spawningInstance);
|
||||||
});
|
});
|
||||||
|
@ -272,8 +272,6 @@ public abstract class LivingEntity extends Entity implements EquipmentHandler {
|
|||||||
|
|
||||||
protected EntityEquipmentPacket getEquipmentPacket(EntityEquipmentPacket.Slot slot) {
|
protected EntityEquipmentPacket getEquipmentPacket(EntityEquipmentPacket.Slot slot) {
|
||||||
ItemStack itemStack = getEquipment(slot);
|
ItemStack itemStack = getEquipment(slot);
|
||||||
if (itemStack == null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
EntityEquipmentPacket equipmentPacket = new EntityEquipmentPacket();
|
EntityEquipmentPacket equipmentPacket = new EntityEquipmentPacket();
|
||||||
equipmentPacket.entityId = getEntityId();
|
equipmentPacket.entityId = getEntityId();
|
||||||
|
@ -32,6 +32,7 @@ import net.minestom.server.sound.Sound;
|
|||||||
import net.minestom.server.sound.SoundCategory;
|
import net.minestom.server.sound.SoundCategory;
|
||||||
import net.minestom.server.stat.PlayerStatistic;
|
import net.minestom.server.stat.PlayerStatistic;
|
||||||
import net.minestom.server.utils.*;
|
import net.minestom.server.utils.*;
|
||||||
|
import net.minestom.server.utils.validate.Check;
|
||||||
import net.minestom.server.world.Dimension;
|
import net.minestom.server.world.Dimension;
|
||||||
import net.minestom.server.world.LevelType;
|
import net.minestom.server.world.LevelType;
|
||||||
|
|
||||||
@ -159,7 +160,7 @@ public class Player extends LivingEntity {
|
|||||||
if (targetCustomBlock != null) {
|
if (targetCustomBlock != null) {
|
||||||
int animationCount = 10;
|
int animationCount = 10;
|
||||||
long since = System.currentTimeMillis() - targetBlockTime;
|
long since = System.currentTimeMillis() - targetBlockTime;
|
||||||
byte stage = (byte) (since / (blockBreakTime / animationCount));
|
byte stage = (byte) (since / (blockBreakTime / animationCount) - 1);
|
||||||
if (stage != targetLastStage) {
|
if (stage != targetLastStage) {
|
||||||
sendBlockBreakAnimation(targetBlockPosition, stage);
|
sendBlockBreakAnimation(targetBlockPosition, stage);
|
||||||
}
|
}
|
||||||
@ -374,10 +375,8 @@ public class Player extends LivingEntity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setInstance(Instance instance) {
|
public void setInstance(Instance instance) {
|
||||||
if (instance == null)
|
Check.notNull(instance, "instance cannot be null!");
|
||||||
throw new IllegalArgumentException("instance cannot be null!");
|
Check.argCondition(this.instance == instance, "Instance should be different than the current one");
|
||||||
if (this.instance == instance)
|
|
||||||
throw new IllegalArgumentException("Instance should be different than the current one");
|
|
||||||
|
|
||||||
boolean firstSpawn = this.instance == null; // TODO: Handle player reconnections, must be false in that case too
|
boolean firstSpawn = this.instance == null; // TODO: Handle player reconnections, must be false in that case too
|
||||||
for (Chunk viewableChunk : viewableChunks) {
|
for (Chunk viewableChunk : viewableChunks) {
|
||||||
@ -596,10 +595,16 @@ public class Player extends LivingEntity {
|
|||||||
sendUpdateHealthPacket();
|
sendUpdateHealthPacket();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return true if the player is currently eating, false otherwise
|
||||||
|
*/
|
||||||
public boolean isEating() {
|
public boolean isEating() {
|
||||||
return isEating;
|
return isEating;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the default eating time for the player
|
||||||
|
*/
|
||||||
public long getDefaultEatingTime() {
|
public long getDefaultEatingTime() {
|
||||||
return defaultEatingTime;
|
return defaultEatingTime;
|
||||||
}
|
}
|
||||||
@ -690,8 +695,8 @@ public class Player extends LivingEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setExp(float exp) {
|
public void setExp(float exp) {
|
||||||
if (!MathUtils.isBetween(exp, 0, 1))
|
Check.argCondition(!MathUtils.isBetween(exp, 0, 1), "Exp should be between 0 and 1");
|
||||||
throw new IllegalArgumentException("Exp should be between 0 and 1");
|
|
||||||
this.exp = exp;
|
this.exp = exp;
|
||||||
sendExperienceUpdatePacket();
|
sendExperienceUpdatePacket();
|
||||||
}
|
}
|
||||||
@ -811,10 +816,8 @@ public class Player extends LivingEntity {
|
|||||||
|
|
||||||
// Require sending chunk data after
|
// Require sending chunk data after
|
||||||
public void sendDimension(Dimension dimension) {
|
public void sendDimension(Dimension dimension) {
|
||||||
if (dimension == null)
|
Check.notNull(dimension, "Dimension cannot be null!");
|
||||||
throw new IllegalArgumentException("Dimension cannot be null!");
|
Check.argCondition(dimension.equals(getDimension()), "The dimension need to be different than the current one!");
|
||||||
if (dimension.equals(getDimension()))
|
|
||||||
throw new IllegalArgumentException("The dimension need to be different than the current one!");
|
|
||||||
|
|
||||||
refreshDimension(dimension);
|
refreshDimension(dimension);
|
||||||
RespawnPacket respawnPacket = new RespawnPacket();
|
RespawnPacket respawnPacket = new RespawnPacket();
|
||||||
@ -847,15 +850,28 @@ public class Player extends LivingEntity {
|
|||||||
refreshGameMode(gameMode);
|
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) {
|
public void setHeldItemSlot(short slot) {
|
||||||
if (slot < 0 || slot > 8)
|
Check.argCondition(!MathUtils.isBetween(slot, 0, 8), "Slot has to be between 0 and 8");
|
||||||
throw new IllegalArgumentException("Slot has to be between 0 and 8");
|
|
||||||
HeldItemChangePacket heldItemChangePacket = new HeldItemChangePacket();
|
HeldItemChangePacket heldItemChangePacket = new HeldItemChangePacket();
|
||||||
heldItemChangePacket.slot = slot;
|
heldItemChangePacket.slot = slot;
|
||||||
playerConnection.sendPacket(heldItemChangePacket);
|
playerConnection.sendPacket(heldItemChangePacket);
|
||||||
refreshHeldSlot(slot);
|
refreshHeldSlot(slot);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the current held slot for the player
|
||||||
|
*/
|
||||||
|
public short getHeldSlot() {
|
||||||
|
return heldSlot;
|
||||||
|
}
|
||||||
|
|
||||||
public void setTeam(Team team) {
|
public void setTeam(Team team) {
|
||||||
if (this.team == team)
|
if (this.team == team)
|
||||||
return;
|
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() {
|
public Inventory getOpenInventory() {
|
||||||
return openInventory;
|
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() {
|
public CustomBlock getCustomBlockTarget() {
|
||||||
return targetCustomBlock;
|
return targetCustomBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return an unmodifiable {@link Set} containing all the current player viewable boss bars
|
||||||
|
*/
|
||||||
public Set<BossBar> getBossBars() {
|
public Set<BossBar> getBossBars() {
|
||||||
return Collections.unmodifiableSet(bossBars);
|
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)
|
* @return true if the inventory has been opened/sent to the player, false otherwise (cancelled by event)
|
||||||
*/
|
*/
|
||||||
public boolean openInventory(Inventory inventory) {
|
public boolean openInventory(Inventory inventory) {
|
||||||
if (inventory == null)
|
Check.notNull(inventory, "Inventory cannot be null, use Player#closeInventory() to close current");
|
||||||
throw new IllegalArgumentException("Inventory cannot be null, use Player#closeInventory() to close current");
|
|
||||||
|
|
||||||
InventoryOpenEvent inventoryOpenEvent = new InventoryOpenEvent(this, inventory);
|
InventoryOpenEvent inventoryOpenEvent = new InventoryOpenEvent(this, inventory);
|
||||||
|
|
||||||
@ -970,10 +992,16 @@ public class Player extends LivingEntity {
|
|||||||
inventory.update();
|
inventory.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return an unmodifiable {@link Set} containing all the chunks that the player sees
|
||||||
|
*/
|
||||||
public Set<Chunk> getViewableChunks() {
|
public Set<Chunk> getViewableChunks() {
|
||||||
return Collections.unmodifiableSet(viewableChunks);
|
return Collections.unmodifiableSet(viewableChunks);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove all the boss bars that the player has
|
||||||
|
*/
|
||||||
public void clearBossBars() {
|
public void clearBossBars() {
|
||||||
this.bossBars.forEach(bossBar -> bossBar.removeViewer(this));
|
this.bossBars.forEach(bossBar -> bossBar.removeViewer(this));
|
||||||
}
|
}
|
||||||
@ -996,8 +1024,7 @@ public class Player extends LivingEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setPermissionLevel(int permissionLevel) {
|
public void setPermissionLevel(int permissionLevel) {
|
||||||
if (!MathUtils.isBetween(permissionLevel, 0, 4))
|
Check.argCondition(!MathUtils.isBetween(permissionLevel, 0, 4), "permissionLevel has to be between 0 and 4");
|
||||||
throw new IllegalArgumentException("permissionLevel has to be between 0 and 4");
|
|
||||||
|
|
||||||
this.permissionLevel = permissionLevel;
|
this.permissionLevel = permissionLevel;
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ public class InventoryClickEvent extends Event {
|
|||||||
/**
|
/**
|
||||||
* Can be null if the clicked inventory is the player one
|
* 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() {
|
public Inventory getInventory() {
|
||||||
return inventory;
|
return inventory;
|
||||||
|
@ -3,6 +3,7 @@ package net.minestom.server.event.inventory;
|
|||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.event.CancellableEvent;
|
import net.minestom.server.event.CancellableEvent;
|
||||||
import net.minestom.server.inventory.Inventory;
|
import net.minestom.server.inventory.Inventory;
|
||||||
|
import net.minestom.server.utils.validate.Check;
|
||||||
|
|
||||||
public class InventoryOpenEvent extends CancellableEvent {
|
public class InventoryOpenEvent extends CancellableEvent {
|
||||||
|
|
||||||
@ -23,9 +24,7 @@ public class InventoryOpenEvent extends CancellableEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setInventory(Inventory inventory) {
|
public void setInventory(Inventory inventory) {
|
||||||
if (inventory == null)
|
Check.notNull(inventory, "Inventory cannot be null!");
|
||||||
throw new NullPointerException("Inventory cannot be null!");
|
|
||||||
|
|
||||||
this.inventory = inventory;
|
this.inventory = inventory;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import net.minestom.server.event.CancellableEvent;
|
|||||||
import net.minestom.server.inventory.Inventory;
|
import net.minestom.server.inventory.Inventory;
|
||||||
import net.minestom.server.inventory.click.ClickType;
|
import net.minestom.server.inventory.click.ClickType;
|
||||||
import net.minestom.server.item.ItemStack;
|
import net.minestom.server.item.ItemStack;
|
||||||
|
import net.minestom.server.utils.item.ItemStackUtils;
|
||||||
|
|
||||||
public class InventoryPreClickEvent extends CancellableEvent {
|
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
|
* 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() {
|
public Inventory getInventory() {
|
||||||
return inventory;
|
return inventory;
|
||||||
@ -43,7 +44,7 @@ public class InventoryPreClickEvent extends CancellableEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setClickedItem(ItemStack clickedItem) {
|
public void setClickedItem(ItemStack clickedItem) {
|
||||||
this.clickedItem = clickedItem;
|
this.clickedItem = ItemStackUtils.notNull(clickedItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ItemStack getCursorItem() {
|
public ItemStack getCursorItem() {
|
||||||
@ -51,6 +52,6 @@ public class InventoryPreClickEvent extends CancellableEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setCursorItem(ItemStack cursorItem) {
|
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.entity.LivingEntity;
|
||||||
import net.minestom.server.event.Event;
|
import net.minestom.server.event.Event;
|
||||||
import net.minestom.server.item.ItemStack;
|
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 {
|
public class ArmorEquipEvent extends Event {
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ public class ArmorEquipEvent extends Event {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setArmorItem(ItemStack armorItem) {
|
public void setArmorItem(ItemStack armorItem) {
|
||||||
this.armorItem = armorItem;
|
this.armorItem = ItemStackUtils.notNull(armorItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArmorSlot getArmorSlot() {
|
public ArmorSlot getArmorSlot() {
|
||||||
@ -37,21 +37,6 @@ public class ArmorEquipEvent extends Event {
|
|||||||
HELMET,
|
HELMET,
|
||||||
CHESTPLATE,
|
CHESTPLATE,
|
||||||
LEGGINGS,
|
LEGGINGS,
|
||||||
BOOTS;
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ import net.minestom.server.utils.PacketUtils;
|
|||||||
import net.minestom.server.utils.SerializerUtils;
|
import net.minestom.server.utils.SerializerUtils;
|
||||||
import net.minestom.server.utils.time.CooldownUtils;
|
import net.minestom.server.utils.time.CooldownUtils;
|
||||||
import net.minestom.server.utils.time.UpdateOption;
|
import net.minestom.server.utils.time.UpdateOption;
|
||||||
|
import net.minestom.server.utils.validate.Check;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.DataOutputStream;
|
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) {
|
public void UNSAFE_setCustomBlock(int x, int y, int z, short visualBlockId, short customBlockId, Data data) {
|
||||||
CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
|
CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
|
||||||
if (customBlock == null)
|
Check.notNull(customBlock, "The custom block " + customBlockId + " does not exist or isn't registered");
|
||||||
throw new IllegalArgumentException("The custom block " + customBlockId + " does not exist or isn't registered");
|
|
||||||
|
|
||||||
UNSAFE_setCustomBlock(x, y, z, visualBlockId, customBlock, data);
|
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.MinecraftServer;
|
||||||
import net.minestom.server.storage.StorageFolder;
|
import net.minestom.server.storage.StorageFolder;
|
||||||
import net.minestom.server.utils.thread.MinestomThread;
|
import net.minestom.server.utils.thread.MinestomThread;
|
||||||
|
import net.minestom.server.utils.validate.Check;
|
||||||
import net.minestom.server.world.Dimension;
|
import net.minestom.server.world.Dimension;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -42,8 +43,7 @@ public class InstanceManager {
|
|||||||
|
|
||||||
public SharedInstance createSharedInstance(SharedInstance sharedInstance) {
|
public SharedInstance createSharedInstance(SharedInstance sharedInstance) {
|
||||||
InstanceContainer instanceContainer = sharedInstance.getInstanceContainer();
|
InstanceContainer instanceContainer = sharedInstance.getInstanceContainer();
|
||||||
if (instanceContainer == null)
|
Check.notNull(instanceContainer, "SharedInstance needs to have an InstanceContainer to be created!");
|
||||||
throw new NullPointerException("SharedInstance needs to have an InstanceContainer to be created!");
|
|
||||||
|
|
||||||
instanceContainer.addSharedInstance(sharedInstance);
|
instanceContainer.addSharedInstance(sharedInstance);
|
||||||
this.instances.add(sharedInstance);
|
this.instances.add(sharedInstance);
|
||||||
@ -51,8 +51,7 @@ public class InstanceManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public SharedInstance createSharedInstance(InstanceContainer instanceContainer) {
|
public SharedInstance createSharedInstance(InstanceContainer instanceContainer) {
|
||||||
if (instanceContainer == null)
|
Check.notNull(instanceContainer, "Instance container cannot be null when creating a SharedInstance!");
|
||||||
throw new IllegalArgumentException("Instance container cannot be null when creating a SharedInstance!");
|
|
||||||
|
|
||||||
SharedInstance sharedInstance = new SharedInstance(UUID.randomUUID(), instanceContainer);
|
SharedInstance sharedInstance = new SharedInstance(UUID.randomUUID(), instanceContainer);
|
||||||
return createSharedInstance(sharedInstance);
|
return createSharedInstance(sharedInstance);
|
||||||
|
@ -70,7 +70,7 @@ public interface EquipmentHandler {
|
|||||||
case BOOTS:
|
case BOOTS:
|
||||||
return getBoots();
|
return getBoots();
|
||||||
default:
|
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.SetSlotPacket;
|
||||||
import net.minestom.server.network.packet.server.play.WindowItemsPacket;
|
import net.minestom.server.network.packet.server.play.WindowItemsPacket;
|
||||||
import net.minestom.server.network.packet.server.play.WindowPropertyPacket;
|
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.inventory.PlayerInventoryUtils;
|
||||||
|
import net.minestom.server.utils.item.ItemStackUtils;
|
||||||
|
import net.minestom.server.utils.validate.Check;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -77,8 +80,8 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setItemStack(int slot, ItemStack itemStack) {
|
public void setItemStack(int slot, ItemStack itemStack) {
|
||||||
if (slot < 0 || slot > getSize())
|
Check.argCondition(!MathUtils.isBetween(slot, 0, getSize()),
|
||||||
throw new IllegalArgumentException(inventoryType.toString() + " does not have slot " + slot);
|
inventoryType.toString() + " does not have slot " + slot);
|
||||||
|
|
||||||
safeItemInsert(slot, itemStack);
|
safeItemInsert(slot, itemStack);
|
||||||
}
|
}
|
||||||
@ -160,7 +163,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
|
|
||||||
private void safeItemInsert(int slot, ItemStack itemStack) {
|
private void safeItemInsert(int slot, ItemStack itemStack) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
itemStack = itemStack == null ? ItemStack.getAirItem() : itemStack;
|
itemStack = ItemStackUtils.notNull(itemStack);
|
||||||
this.itemStacks[slot] = itemStack;
|
this.itemStacks[slot] = itemStack;
|
||||||
SetSlotPacket setSlotPacket = new SetSlotPacket();
|
SetSlotPacket setSlotPacket = new SetSlotPacket();
|
||||||
setSlotPacket.windowId = getWindowId();
|
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.packet.server.play.WindowItemsPacket;
|
||||||
import net.minestom.server.network.player.PlayerConnection;
|
import net.minestom.server.network.player.PlayerConnection;
|
||||||
import net.minestom.server.utils.MathUtils;
|
import net.minestom.server.utils.MathUtils;
|
||||||
|
import net.minestom.server.utils.item.ItemStackUtils;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -181,12 +182,12 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setCursorItem(ItemStack cursorItem) {
|
public void setCursorItem(ItemStack cursorItem) {
|
||||||
this.cursorItem = cursorItem;
|
this.cursorItem = ItemStackUtils.notNull(cursorItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void safeItemInsert(int slot, ItemStack itemStack) {
|
private void safeItemInsert(int slot, ItemStack itemStack) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
itemStack = itemStack == null ? ItemStack.getAirItem() : itemStack;
|
itemStack = ItemStackUtils.notNull(itemStack);
|
||||||
|
|
||||||
EntityEquipmentPacket.Slot equipmentSlot;
|
EntityEquipmentPacket.Slot equipmentSlot;
|
||||||
|
|
||||||
@ -208,7 +209,8 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (armorEquipEvent != null) {
|
if (armorEquipEvent != null) {
|
||||||
equipmentSlot = armorEquipEvent.getArmorSlot().toEquipmentPacketSlot();
|
ArmorEquipEvent.ArmorSlot armorSlot = armorEquipEvent.getArmorSlot();
|
||||||
|
equipmentSlot = EntityEquipmentPacket.Slot.fromArmorSlot(armorSlot);
|
||||||
player.callEvent(ArmorEquipEvent.class, armorEquipEvent);
|
player.callEvent(ArmorEquipEvent.class, armorEquipEvent);
|
||||||
itemStack = armorEquipEvent.getArmorItem();
|
itemStack = armorEquipEvent.getArmorItem();
|
||||||
} else {
|
} else {
|
||||||
@ -216,13 +218,11 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (itemStack != null) {
|
this.items[slot] = itemStack;
|
||||||
this.items[slot] = itemStack;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Refresh slot
|
// Refresh slot
|
||||||
refreshSlot(slot);
|
update();
|
||||||
//update(); in case of problems
|
//refreshSlot(slot); seems to break things concerning +64 stacks
|
||||||
|
|
||||||
// Sync equipment
|
// Sync equipment
|
||||||
if (equipmentSlot != null) {
|
if (equipmentSlot != null) {
|
||||||
|
@ -3,6 +3,7 @@ package net.minestom.server.item;
|
|||||||
import net.minestom.server.data.Data;
|
import net.minestom.server.data.Data;
|
||||||
import net.minestom.server.data.DataContainer;
|
import net.minestom.server.data.DataContainer;
|
||||||
import net.minestom.server.item.rule.VanillaStackingRule;
|
import net.minestom.server.item.rule.VanillaStackingRule;
|
||||||
|
import net.minestom.server.utils.validate.Check;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@ -208,11 +209,9 @@ public class ItemStack implements DataContainer {
|
|||||||
return stackingRule;
|
return stackingRule;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setStackingRule(StackingRule stackingRule) {
|
public static void setDefaultStackingRule(StackingRule defaultStackingRule) {
|
||||||
if (stackingRule == null)
|
Check.notNull(defaultStackingRule, "StackingRule cannot be null!");
|
||||||
throw new NullPointerException("StackingRule cannot be null!");
|
ItemStack.defaultStackingRule = defaultStackingRule;
|
||||||
|
|
||||||
this.stackingRule = stackingRule;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -229,11 +228,9 @@ public class ItemStack implements DataContainer {
|
|||||||
return defaultStackingRule;
|
return defaultStackingRule;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setDefaultStackingRule(StackingRule defaultStackingRule) {
|
public void setStackingRule(StackingRule stackingRule) {
|
||||||
if (defaultStackingRule == null)
|
Check.notNull(stackingRule, "StackingRule cannot be null!");
|
||||||
throw new NullPointerException("StackingRule cannot be null!");
|
this.stackingRule = stackingRule;
|
||||||
|
|
||||||
ItemStack.defaultStackingRule = defaultStackingRule;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte getBitModifier(ItemFlag hideFlag) {
|
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.PlayerChatEvent;
|
||||||
import net.minestom.server.event.player.PlayerCommandEvent;
|
import net.minestom.server.event.player.PlayerCommandEvent;
|
||||||
import net.minestom.server.network.packet.client.play.ClientChatMessagePacket;
|
import net.minestom.server.network.packet.client.play.ClientChatMessagePacket;
|
||||||
|
import net.minestom.server.utils.validate.Check;
|
||||||
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
@ -55,8 +56,7 @@ public class ChatMessageListener {
|
|||||||
player.callCancellableEvent(PlayerChatEvent.class, playerChatEvent, () -> {
|
player.callCancellableEvent(PlayerChatEvent.class, playerChatEvent, () -> {
|
||||||
|
|
||||||
Function<PlayerChatEvent, TextComponent> formatFunction = playerChatEvent.getChatFormatFunction();
|
Function<PlayerChatEvent, TextComponent> formatFunction = playerChatEvent.getChatFormatFunction();
|
||||||
if (formatFunction == null)
|
Check.notNull(formatFunction, "PlayerChatEvent#getChatFormatFunction cannot be null!");
|
||||||
throw new NullPointerException("PlayerChatEvent#chatFormat cannot be null!");
|
|
||||||
|
|
||||||
TextComponent textObject = formatFunction.apply(playerChatEvent);
|
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.PacketWriter;
|
||||||
import net.minestom.server.network.packet.server.ServerPacket;
|
import net.minestom.server.network.packet.server.ServerPacket;
|
||||||
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
|
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
|
||||||
|
import net.minestom.server.utils.validate.Check;
|
||||||
|
|
||||||
public class DeclareRecipesPacket implements ServerPacket {
|
public class DeclareRecipesPacket implements ServerPacket {
|
||||||
|
|
||||||
@ -11,8 +12,8 @@ public class DeclareRecipesPacket implements ServerPacket {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(PacketWriter writer) {
|
public void write(PacketWriter writer) {
|
||||||
if (recipes == null)
|
Check.notNull(recipes, "Recipes cannot be null!");
|
||||||
throw new NullPointerException("Recipes cannot be null!");
|
|
||||||
writer.writeVarInt(recipes.length);
|
writer.writeVarInt(recipes.length);
|
||||||
for (Recipe recipe : recipes) {
|
for (Recipe recipe : recipes) {
|
||||||
recipe.write(writer);
|
recipe.write(writer);
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package net.minestom.server.network.packet.server.play;
|
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.item.ItemStack;
|
||||||
import net.minestom.server.network.packet.PacketWriter;
|
import net.minestom.server.network.packet.PacketWriter;
|
||||||
import net.minestom.server.network.packet.server.ServerPacket;
|
import net.minestom.server.network.packet.server.ServerPacket;
|
||||||
@ -29,7 +30,23 @@ public class EntityEquipmentPacket implements ServerPacket {
|
|||||||
BOOTS,
|
BOOTS,
|
||||||
LEGGINGS,
|
LEGGINGS,
|
||||||
CHESTPLATE,
|
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.ScoreboardObjectivePacket;
|
||||||
import net.minestom.server.network.packet.server.play.UpdateScorePacket;
|
import net.minestom.server.network.packet.server.play.UpdateScorePacket;
|
||||||
import net.minestom.server.network.player.PlayerConnection;
|
import net.minestom.server.network.player.PlayerConnection;
|
||||||
|
import net.minestom.server.utils.validate.Check;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
@ -60,15 +61,13 @@ public class Sidebar implements Viewable {
|
|||||||
|
|
||||||
public void createLine(ScoreboardLine scoreboardLine) {
|
public void createLine(ScoreboardLine scoreboardLine) {
|
||||||
synchronized (lines) {
|
synchronized (lines) {
|
||||||
if (lines.size() >= MAX_LINES_COUNT)
|
Check.stateCondition(lines.size() >= MAX_LINES_COUNT, "You cannot have more than " + MAX_LINES_COUNT + " lines");
|
||||||
throw new IllegalStateException("You cannot have more than " + MAX_LINES_COUNT + " lines");
|
Check.argCondition(lines.contains(scoreboardLine), "You cannot add two times the same ScoreboardLine");
|
||||||
if (lines.contains(scoreboardLine))
|
|
||||||
throw new IllegalArgumentException("You cannot add two times the same ScoreboardLine");
|
|
||||||
|
|
||||||
// Check ID duplication
|
// Check ID duplication
|
||||||
for (ScoreboardLine line : lines) {
|
for (ScoreboardLine line : lines) {
|
||||||
if (line.id.equals(scoreboardLine.id))
|
Check.argCondition(line.id.equals(scoreboardLine.id),
|
||||||
throw new IllegalArgumentException("You cannot add two ScoreboardLine with the same id");
|
"You cannot add two ScoreboardLine with the same id");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup line
|
// 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.PacketReader;
|
||||||
import net.minestom.server.network.packet.PacketWriter;
|
import net.minestom.server.network.packet.PacketWriter;
|
||||||
import net.minestom.server.reader.DataReader;
|
import net.minestom.server.reader.DataReader;
|
||||||
|
import net.minestom.server.utils.validate.Check;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -50,8 +51,7 @@ public class StorageFolder {
|
|||||||
|
|
||||||
public <T> void set(String key, T object, Class<T> type) {
|
public <T> void set(String key, T object, Class<T> type) {
|
||||||
DataType<T> dataType = DATA_MANAGER.getDataType(type);
|
DataType<T> dataType = DATA_MANAGER.getDataType(type);
|
||||||
if (dataType == null)
|
Check.notNull(dataType, "You can only save registered DataType type!");
|
||||||
throw new NullPointerException("You can only save registered DataType type!");
|
|
||||||
|
|
||||||
PacketWriter packetWriter = new PacketWriter();
|
PacketWriter packetWriter = new PacketWriter();
|
||||||
dataType.encode(packetWriter, object); // Encode
|
dataType.encode(packetWriter, object); // Encode
|
||||||
@ -62,8 +62,7 @@ public class StorageFolder {
|
|||||||
|
|
||||||
public <T> T get(String key, Class<T> type) {
|
public <T> T get(String key, Class<T> type) {
|
||||||
DataType<T> dataType = DATA_MANAGER.getDataType(type);
|
DataType<T> dataType = DATA_MANAGER.getDataType(type);
|
||||||
if (dataType == null)
|
Check.notNull(dataType, "You can only save registered DataType type!");
|
||||||
throw new NullPointerException("You can only save registered DataType type!");
|
|
||||||
|
|
||||||
byte[] data = get(key);
|
byte[] data = get(key);
|
||||||
if (data == null)
|
if (data == null)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package net.minestom.server.storage;
|
package net.minestom.server.storage;
|
||||||
|
|
||||||
|
import net.minestom.server.utils.validate.Check;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -41,8 +42,8 @@ public class StorageManager {
|
|||||||
* @throws NullPointerException if no default StorageSystem is defined {@link #defineDefaultStorageSystem(Supplier)}
|
* @throws NullPointerException if no default StorageSystem is defined {@link #defineDefaultStorageSystem(Supplier)}
|
||||||
*/
|
*/
|
||||||
public StorageFolder getFolder(String folderPath) {
|
public StorageFolder getFolder(String folderPath) {
|
||||||
if (defaultStorageSystemSupplier == null)
|
Check.notNull(defaultStorageSystemSupplier,
|
||||||
throw new NullPointerException("You need to either define a default storage system or specify your storage system for this specific folder");
|
"You need to either define a default storage system or specify your storage system for this specific folder");
|
||||||
|
|
||||||
StorageSystem storageSystem = defaultStorageSystemSupplier.get();
|
StorageSystem storageSystem = defaultStorageSystemSupplier.get();
|
||||||
return getFolder(folderPath, storageSystem);
|
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 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)
|
* @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
|
* @return a packet which can be use internally with Minestom
|
||||||
*/
|
*/
|
||||||
public static int convertSlot(int slot, int offset) {
|
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