This commit is contained in:
themode 2020-10-11 18:57:24 +02:00
parent 5f7261cd6e
commit af1f976e53
17 changed files with 85 additions and 70 deletions

View File

@ -8,7 +8,7 @@ import java.util.HashSet;
import java.util.Set;
/**
* Represent something in the instance which can be showed or hidden to players
* Represent something which can be displayed or hidden to players
*/
public interface Viewable {

View File

@ -38,7 +38,7 @@ public class Notification {
}
/**
* Get the frame type of the notification
* Get the {@link FrameType} of the notification
*
* @return the notification frame type
*/

View File

@ -10,7 +10,11 @@ import java.sql.Date;
import java.util.Collection;
/**
* Used to send one or multiples {@link Notification}
* Used to send one or multiples {@link Notification}.
* <p>
* Works by sending a completed advancement and remove it immediately.
* <p>
* You can simply create a {@link Notification} object and call {@link #send(Notification, Player)}.
*/
public class NotificationCenter {

View File

@ -25,7 +25,7 @@ public interface Data {
@Override
public Set<String> getKeys() {
return Collections.EMPTY_SET;
return Collections.emptySet();
}
@Override

View File

@ -14,7 +14,7 @@ import java.util.UUID;
public final class DataManager {
private Map<Class, DataType> dataTypeMap = new HashMap<>();
private final Map<Class, DataType> dataTypeMap = new HashMap<>();
{
registerType(Byte.class, new ByteData());

View File

@ -3,6 +3,14 @@ package net.minestom.server.data;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
/**
* Represents an object which can be encoded and decoded back.
* <p>
* Used by {@link DataManager} for {@link SerializableDataImpl}
* and by the storage API in {@link net.minestom.server.storage.StorageLocation}.
*
* @param <T> the type of the object
*/
public abstract class DataType<T> {
/**

View File

@ -19,13 +19,13 @@ public class SerializableDataImpl extends DataImpl implements SerializableData {
* Class name -> Class
* Used to cache class instances so we don't load them by name every time
*/
private static ConcurrentHashMap<String, Class> nameToClassMap = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<String, Class> nameToClassMap = new ConcurrentHashMap<>();
/**
* Data key -> Class
* Used to know the type of an element of this data object (for serialization purpose)
*/
private ConcurrentHashMap<String, Class> dataType = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Class> dataType = new ConcurrentHashMap<>();
/**
* Set a value to a specific key

View File

@ -469,9 +469,8 @@ public class Player extends LivingEntity implements CommandSender {
ColoredText.of(getUsername() + " was killed by poor programming.");
chatMessage = RichMessage.of(coloredChatMessage);
}
MinecraftServer.getConnectionManager().getOnlinePlayers().forEach(player -> {
player.sendMessage(chatMessage);
});
MinecraftServer.getConnectionManager().getOnlinePlayers()
.forEach(player -> player.sendMessage(chatMessage));
}
super.kill();
}

View File

@ -46,7 +46,7 @@ public interface EquipmentHandler {
/**
* Get the {@link ItemStack} in the specific hand
*
* @param hand the hand to get the {@link ItemStack} from
* @param hand the Hand to get the {@link ItemStack} from
* @return the {@link ItemStack} in {@code hand}
*/
default ItemStack getItemInHand(Player.Hand hand) {

View File

@ -32,7 +32,7 @@ import java.util.concurrent.atomic.AtomicInteger;
public class Inventory implements InventoryModifier, InventoryClickHandler, Viewable {
// incremented each time an inventory is created (used in the window packets)
private static AtomicInteger lastInventoryId = new AtomicInteger();
private static final AtomicInteger LAST_INVENTORY_ID = new AtomicInteger();
// the id of this inventory
private final byte id;
@ -73,7 +73,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
}
private static byte generateId() {
byte newInventoryId = (byte) lastInventoryId.incrementAndGet();
byte newInventoryId = (byte) LAST_INVENTORY_ID.incrementAndGet();
if (newInventoryId == Byte.MAX_VALUE)
newInventoryId = 1;
return newInventoryId;
@ -366,11 +366,10 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
if (isInWindow) {
setItemStack(slot, clickResult.getClicked());
setCursorPlayerItem(player, clickResult.getCursor());
} else {
playerInventory.setItemStack(slot, offset, clickResult.getClicked());
setCursorPlayerItem(player, clickResult.getCursor());
}
setCursorPlayerItem(player, clickResult.getCursor());
if (!clickResult.isCancel())
callClickEvent(player, this, slot, ClickType.LEFT_CLICK, clicked, cursor);
@ -393,11 +392,10 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
if (isInWindow) {
setItemStack(slot, clickResult.getClicked());
setCursorPlayerItem(player, clickResult.getCursor());
} else {
playerInventory.setItemStack(slot, offset, clickResult.getClicked());
setCursorPlayerItem(player, clickResult.getCursor());
}
setCursorPlayerItem(player, clickResult.getCursor());
if (!clickResult.isCancel())
callClickEvent(player, this, slot, ClickType.RIGHT_CLICK, clicked, cursor);
@ -419,7 +417,7 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
clickResult = clickProcessor.shiftClick(this, player, slot, clicked, cursor,
// Player inventory loop
new InventoryClickLoopHandler(0, PlayerInventory.INVENTORY_SIZE, 1,
i -> PlayerInventoryUtils.convertToPacketSlot(i),
PlayerInventoryUtils::convertToPacketSlot,
index -> isClickInWindow(index) ? getItemStack(index) : playerInventory.getItemStack(index, offset),
(index, itemStack) -> {
if (isClickInWindow(index)) {
@ -511,12 +509,11 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
if (isInWindow) {
if (resultClicked != null)
setItemStack(slot, resultClicked);
setCursorPlayerItem(player, clickResult.getCursor());
} else {
if (resultClicked != null)
playerInventory.setItemStack(slot, offset, resultClicked);
setCursorPlayerItem(player, clickResult.getCursor());
}
setCursorPlayerItem(player, clickResult.getCursor());
return !clickResult.isCancel();
}
@ -567,20 +564,18 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
// Start by looping through the opened inventory
new InventoryClickLoopHandler(0, getSize(), 1,
i -> i,
index -> getItemStack(index),
(index, itemStack) -> setItemStack(index, itemStack)),
this::getItemStack,
this::setItemStack),
// Looping through player inventory
new InventoryClickLoopHandler(0, PlayerInventory.INVENTORY_SIZE - 9, 1,
i -> PlayerInventoryUtils.convertToPacketSlot(i),
PlayerInventoryUtils::convertToPacketSlot,
index -> playerInventory.getItemStack(index, offset),
(index, itemStack) -> playerInventory.setItemStack(index, offset, itemStack)),
// Player hotbar
// Player hot bar
new InventoryClickLoopHandler(0, 9, 1,
i -> PlayerInventoryUtils.convertToPacketSlot(i),
PlayerInventoryUtils::convertToPacketSlot,
index -> playerInventory.getItemStack(index, offset),
(index, itemStack) -> {
playerInventory.setItemStack(index, offset, itemStack);
}));
(index, itemStack) -> playerInventory.setItemStack(index, offset, itemStack)));
if (clickResult == null)
return false;

View File

@ -14,7 +14,7 @@ import net.minestom.server.item.ItemStack;
public interface InventoryClickHandler {
/**
* Called when a player left click in the inventory. Can also be to drop the cursor item
* Called when a {@link Player} left click in the inventory. Can also be to drop the cursor item
*
* @param player the player who clicked
* @param slot the slot number
@ -23,7 +23,7 @@ public interface InventoryClickHandler {
boolean leftClick(Player player, int slot);
/**
* Called when a player right click in the inventory. Can also be to drop the cursor item
* Called when a {@link Player} right click in the inventory. Can also be to drop the cursor item
*
* @param player the player who clicked
* @param slot the slot number
@ -32,7 +32,7 @@ public interface InventoryClickHandler {
boolean rightClick(Player player, int slot);
/**
* Called when a player shift click in the inventory
* Called when a {@link Player} shift click in the inventory
*
* @param player the player who clicked
* @param slot the slot number
@ -41,7 +41,7 @@ public interface InventoryClickHandler {
boolean shiftClick(Player player, int slot); // shift + left/right click have the same behavior
/**
* Called when a player held click in the inventory
* Called when a {@link Player} held click in the inventory
*
* @param player the player who clicked
* @param slot the slot number
@ -53,7 +53,7 @@ public interface InventoryClickHandler {
boolean middleClick(Player player, int slot);
/**
* Called when a player press the drop button
* Called when a {@link Player} press the drop button
*
* @param player the player who clicked
* @param mode
@ -66,7 +66,7 @@ public interface InventoryClickHandler {
boolean dragging(Player player, int slot, int button);
/**
* Called when a player double click in the inventory
* Called when a {@link Player} double click in the inventory
*
* @param player the player who clicked
* @param slot the slot number

View File

@ -1,5 +1,8 @@
package net.minestom.server.inventory;
/**
* Represents a type of {@link Inventory}
*/
public enum InventoryType {
CHEST_1_ROW(0, 9),

View File

@ -27,18 +27,18 @@ import java.util.concurrent.CopyOnWriteArrayList;
import static net.minestom.server.utils.inventory.PlayerInventoryUtils.*;
/**
* Represents the inventory of a {@link Player}
* Represents the inventory of a {@link Player}.
*/
public class PlayerInventory implements InventoryModifier, InventoryClickHandler, EquipmentHandler {
public static final int INVENTORY_SIZE = 46;
private Player player;
private ItemStack[] items = new ItemStack[INVENTORY_SIZE];
private final Player player;
private final ItemStack[] items = new ItemStack[INVENTORY_SIZE];
private ItemStack cursorItem = ItemStack.getAirItem();
private List<InventoryCondition> inventoryConditions = new CopyOnWriteArrayList<>();
private InventoryClickProcessor clickProcessor = new InventoryClickProcessor();
private final List<InventoryCondition> inventoryConditions = new CopyOnWriteArrayList<>();
private final InventoryClickProcessor clickProcessor = new InventoryClickProcessor();
public PlayerInventory(Player player) {
this.player = player;
@ -425,11 +425,11 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
final ItemStack cursor = getCursorItem();
final ItemStack clicked = getItemStack(slot, OFFSET);
final boolean hotbarClick = convertToPacketSlot(slot) < 9;
final boolean hotBarClick = convertToPacketSlot(slot) < 9;
final InventoryClickResult clickResult = clickProcessor.shiftClick(null, player, slot, clicked, cursor,
new InventoryClickLoopHandler(0, items.length, 1,
i -> {
if (hotbarClick) {
if (hotBarClick) {
return i < 9 ? i + 9 : i - 9;
} else {
return convertSlot(i, OFFSET);
@ -507,7 +507,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
new InventoryClickLoopHandler(0, items.length, 1,
i -> i < 9 ? i + 9 : i - 9,
index -> items[index],
(index, itemStack) -> setItemStack(index, itemStack)));
this::setItemStack));
if (clickResult == null)
return false;

View File

@ -3,7 +3,7 @@ package net.minestom.server.permission;
import net.minestom.server.command.CommandSender;
/**
* Basic Permission implementation that only requires the permission to be given to a player to be considered applied
* Basic {@link Permission} implementation that only requires the permission to be given to the {@link CommandSender} to be considered applied
* (eg. no arguments)
*/
public class BasicPermission implements Permission {

View File

@ -18,7 +18,14 @@ import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Represents a sidebar which can contain up to 16 {@link ScoreboardLine}'s
* Represents a sidebar which can contain up to 16 {@link ScoreboardLine}.
* <p>
* In order to use it you need to create a new instance using {@link #Sidebar(String)} and create new lines
* with {@link #createLine(ScoreboardLine)}. You can then add a {@link Player} to the viewing list using {@link #addViewer(Player)}
* and remove him later with {@link #removeViewer(Player)}.
* <p>
* Lines can be modified using their respective identifier using
* {@link #updateLineContent(String, ColoredText)} and {@link #updateLineScore(String, int)}.
*/
public class Sidebar implements Scoreboard {
@ -106,10 +113,10 @@ public class Sidebar implements Scoreboard {
}
/**
* Updates a line content through the given identifier
* Updates a {@link ScoreboardLine} content through the given identifier
*
* @param id The identifier of the line
* @param content The new content for the line
* @param id The identifier of the {@link ScoreboardLine}
* @param content The new content for the {@link ScoreboardLine}
*/
public void updateLineContent(String id, ColoredText content) {
final ScoreboardLine scoreboardLine = getLine(id);
@ -120,10 +127,10 @@ public class Sidebar implements Scoreboard {
}
/**
* Updates the score of a line through the given identifier
* Updates the score of a {@link ScoreboardLine} through the given identifier
*
* @param id The identifier of the team
* @param score The new score for the line
* @param score The new score for the {@link ScoreboardLine}
*/
public void updateLineScore(String id, int score) {
final ScoreboardLine scoreboardLine = getLine(id);
@ -150,7 +157,7 @@ public class Sidebar implements Scoreboard {
/**
* Removes a {@link ScoreboardLine} through the given identifier
*
* @param id The identifier of the line
* @param id the identifier of the {@link ScoreboardLine}
*/
public void removeLine(String id) {
synchronized (lines) {
@ -189,7 +196,7 @@ public class Sidebar implements Scoreboard {
@Override
public boolean removeViewer(Player player) {
boolean result = this.viewers.remove(player);
final boolean result = this.viewers.remove(player);
PlayerConnection playerConnection = player.getPlayerConnection();
ScoreboardObjectivePacket scoreboardObjectivePacket = this.getDestructionObjectivePacket();
playerConnection.sendPacket(scoreboardObjectivePacket);
@ -219,17 +226,17 @@ public class Sidebar implements Scoreboard {
/**
* The identifier is used to modify the line later
*/
private String id;
private final String id;
/**
* The content for the line
*/
private ColoredText content;
private final ColoredText content;
/**
* The score of the line
*/
private int line;
private String teamName;
private final String teamName;
/**
* The name of the score ({@code entityName}) which is essentially an identifier
*/
@ -350,19 +357,19 @@ public class Sidebar implements Scoreboard {
}
/**
* This class is used to create a team for the sidebar
* This class is used to create a team for the {@link Sidebar}
*/
private static class SidebarTeam {
private String teamName;
private final String teamName;
private ColoredText prefix, suffix;
private String entityName;
private final String entityName;
private ColoredText teamDisplayName = ColoredText.of("displaynametest");
private byte friendlyFlags = 0x00;
private TeamsPacket.NameTagVisibility nameTagVisibility = TeamsPacket.NameTagVisibility.NEVER;
private TeamsPacket.CollisionRule collisionRule = TeamsPacket.CollisionRule.NEVER;
private int teamColor = 2;
private final ColoredText teamDisplayName = ColoredText.of("displaynametest");
private final byte friendlyFlags = 0x00;
private final TeamsPacket.NameTagVisibility nameTagVisibility = TeamsPacket.NameTagVisibility.NEVER;
private final TeamsPacket.CollisionRule collisionRule = TeamsPacket.CollisionRule.NEVER;
private final int teamColor = 2;
/**

View File

@ -178,7 +178,7 @@ public class StorageLocation {
}
/**
* Save a specified cached data and remove it from memory
* Save a specified cached {@link SerializableData} and remove it from memory
*
* @param key the specified cached data key
*/
@ -197,18 +197,16 @@ public class StorageLocation {
}
/**
* Save the whole cached data
* Save the all the cached {@link SerializableData}
*/
public void saveCachedData() {
synchronized (cachedData) {
cachedData.forEach((key, data) -> {
set(key, data.getIndexedSerializedData());
});
cachedData.forEach((key, data) -> set(key, data.getIndexedSerializedData()));
}
}
/**
* Save an unique cached data
* Save an unique cached {@link SerializableData}
*
* @param key the data key
*/

View File

@ -10,13 +10,14 @@ import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
public class StorageManager {
public final class StorageManager {
private static final Logger LOGGER = LoggerFactory.getLogger(StorageManager.class);
private Supplier<StorageSystem> defaultStorageSystemSupplier = null;
// Location -> storage location object
private Map<String, StorageLocation> locationMap = new HashMap<>();
private final Map<String, StorageLocation> locationMap = new HashMap<>();
/**
* Used to get an access to the specified location