Update to Java 17 (#462)

This commit is contained in:
TheMode 2021-10-22 01:55:55 +02:00 committed by GitHub
parent 85d9256fa8
commit b308ce5baa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
43 changed files with 200 additions and 475 deletions

View File

@ -11,10 +11,10 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 11
java-version: 17
- name: Run java checkstyle
uses: nikitasavinov/checkstyle-action@0.3.1
with:

View File

@ -11,10 +11,10 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v1
with:
java-version: 11
java-version: 17
- name: Build javadoc
run: gradle javadoc

View File

@ -13,11 +13,11 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Set up JDK 16
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: 16
distribution: 'zulu'
java-version: 17
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Setup gradle cache

View File

@ -3,14 +3,14 @@ import org.gradle.internal.os.OperatingSystem
plugins {
id 'java-library'
id 'maven-publish'
id 'org.jetbrains.kotlin.jvm' version '1.5.21'
id 'org.jetbrains.kotlin.jvm' version '1.5.31'
id 'checkstyle'
}
group 'net.minestom.server'
version '1.0'
sourceCompatibility = 11
sourceCompatibility = 17
project.ext.lwjglVersion = "3.2.3"
switch (OperatingSystem.current()) {

View File

@ -7,7 +7,7 @@ plugins {
group 'net.minestom.server'
version '1.0'
sourceCompatibility = 1.11
sourceCompatibility = 17
application {
mainClass.set("net.minestom.codegen.Generators")

View File

@ -2,7 +2,7 @@
mcVersion = 1.17
asmVersion=9.2
mixinVersion=0.8.3
mixinVersion=0.8.4
hephaistosVersion=v1.1.8
kotlinVersion=1.5.21
kotlinVersion=1.5.31
adventureVersion=4.8.1

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-rc-2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -1,2 +1,5 @@
jdk:
- openjdk11
before_install:
- source "$HOME/.sdkman/bin/sdkman-init.sh"
- sdk update
- sdk install java 17-open
- sdk use java 17-open

View File

@ -148,54 +148,28 @@ public class CollisionUtils {
@NotNull Pos currentPosition, @NotNull Pos newPosition) {
final WorldBorder worldBorder = instance.getWorldBorder();
final WorldBorder.CollisionAxis collisionAxis = worldBorder.getCollisionAxis(newPosition);
switch (collisionAxis) {
case NONE:
// Apply velocity + gravity
return newPosition;
case BOTH:
// Apply Y velocity/gravity
return new Pos(currentPosition.x(), newPosition.y(), currentPosition.z());
case X:
// Apply Y/Z velocity/gravity
return new Pos(currentPosition.x(), newPosition.y(), newPosition.z());
case Z:
// Apply X/Y velocity/gravity
return new Pos(newPosition.x(), newPosition.y(), currentPosition.z());
}
throw new IllegalStateException("Something weird happened...");
return switch (collisionAxis) {
case NONE ->
// Apply velocity + gravity
newPosition;
case BOTH ->
// Apply Y velocity/gravity
new Pos(currentPosition.x(), newPosition.y(), currentPosition.z());
case X ->
// Apply Y/Z velocity/gravity
new Pos(currentPosition.x(), newPosition.y(), newPosition.z());
case Z ->
// Apply X/Y velocity/gravity
new Pos(newPosition.x(), newPosition.y(), currentPosition.z());
};
}
public static class PhysicsResult {
private final Pos newPosition;
private final Vec newVelocity;
private final boolean isOnGround;
public PhysicsResult(Pos newPosition, Vec newVelocity, boolean isOnGround) {
this.newPosition = newPosition;
this.newVelocity = newVelocity;
this.isOnGround = isOnGround;
}
public Pos newPosition() {
return newPosition;
}
public Vec newVelocity() {
return newVelocity;
}
public boolean isOnGround() {
return isOnGround;
}
public record PhysicsResult(Pos newPosition,
Vec newVelocity,
boolean isOnGround) {
}
private static class StepResult {
private final Vec newPosition;
private final boolean foundCollision;
public StepResult(Vec newPosition, boolean foundCollision) {
this.newPosition = newPosition;
this.foundCollision = foundCollision;
}
private record StepResult(Vec newPosition,
boolean foundCollision) {
}
}

View File

@ -15,8 +15,7 @@ import java.util.function.DoubleUnaryOperator;
* Can either be a {@link Pos} or {@link Vec}.
* Interface will become {@code sealed} in the future.
*/
@ApiStatus.NonExtendable
public interface Point {
public sealed interface Point permits Vec, Pos {
/**
* Gets the X coordinate.
@ -176,22 +175,14 @@ public interface Point {
@Contract(pure = true)
default @NotNull Point relative(@NotNull BlockFace face) {
switch (face) {
case BOTTOM:
return sub(0, 1, 0);
case TOP:
return add(0, 1, 0);
case NORTH:
return sub(0, 0, 1);
case SOUTH:
return add(0, 0, 1);
case WEST:
return sub(1, 0, 0);
case EAST:
return add(1, 0, 0);
default: // should never be called
return this;
}
return switch (face) {
case BOTTOM -> sub(0, 1, 0);
case TOP -> add(0, 1, 0);
case NORTH -> sub(0, 0, 1);
case SOUTH -> add(0, 0, 1);
case WEST -> sub(1, 0, 0);
case EAST -> add(1, 0, 0);
};
}
/**

View File

@ -5,7 +5,6 @@ import net.minestom.server.utils.MathUtils;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.function.DoubleUnaryOperator;
/**
@ -13,18 +12,11 @@ import java.util.function.DoubleUnaryOperator;
* <p>
* To become record and primitive.
*/
public final class Pos implements Point {
public record Pos(double x, double y, double z, float yaw, float pitch) implements Point {
public static final Pos ZERO = new Pos(0, 0, 0);
private final double x, y, z;
private final float yaw, pitch;
public Pos(double x, double y, double z, float yaw, float pitch) {
this.x = x;
this.y = y;
this.z = z;
this.yaw = fixYaw(yaw);
this.pitch = pitch;
public Pos {
yaw = fixYaw(yaw);
}
public Pos(double x, double y, double z) {
@ -47,8 +39,7 @@ public final class Pos implements Point {
* @return the converted position
*/
public static @NotNull Pos fromPoint(@NotNull Point point) {
if (point instanceof Pos)
return (Pos) point;
if (point instanceof Pos pos) return pos;
return new Pos(point.x(), point.y(), point.z());
}
@ -293,34 +284,6 @@ public final class Pos implements Point {
return new Vec(x, y, z);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pos pos = (Pos) o;
return Double.compare(pos.x, x) == 0 &&
Double.compare(pos.y, y) == 0 &&
Double.compare(pos.z, z) == 0 &&
Float.compare(pos.yaw, yaw) == 0 &&
Float.compare(pos.pitch, pitch) == 0;
}
@Override
public int hashCode() {
return Objects.hash(x, y, z, yaw, pitch);
}
@Override
public String toString() {
return "Pos{" +
"x=" + x +
", y=" + y +
", z=" + z +
", yaw=" + yaw +
", pitch=" + pitch +
'}';
}
@FunctionalInterface
public interface Operator {
@NotNull Pos apply(double x, double y, double z, float yaw, float pitch);

View File

@ -5,7 +5,6 @@ import net.minestom.server.utils.MathUtils;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.function.DoubleUnaryOperator;
/**
@ -13,27 +12,12 @@ import java.util.function.DoubleUnaryOperator;
* <p>
* To become record and primitive.
*/
public final class Vec implements Point {
public record Vec(double x, double y, double z) implements Point {
public static final Vec ZERO = new Vec(0);
public static final Vec ONE = new Vec(1);
public static final double EPSILON = 0.000001;
private final double x, y, z;
/**
* Creates a new vec with the 3 coordinates set.
*
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
*/
public Vec(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
/**
* Creates a new vec with the [x;z] coordinates set. Y is set to 0.
*
@ -61,8 +45,7 @@ public final class Vec implements Point {
* @return the converted vector
*/
public static @NotNull Vec fromPoint(@NotNull Point point) {
if (point instanceof Vec)
return (Vec) point;
if (point instanceof Vec vec) return vec;
return new Vec(point.x(), point.y(), point.z());
}
@ -502,28 +485,6 @@ public final class Vec implements Point {
return z;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Vec vec = (Vec) o;
return Double.compare(vec.x, x) == 0 && Double.compare(vec.y, y) == 0 && Double.compare(vec.z, z) == 0;
}
@Override
public int hashCode() {
return Objects.hash(x, y, z);
}
@Override
public String toString() {
return "Vec{" +
"x=" + x +
", y=" + y +
", z=" + z +
'}';
}
@FunctionalInterface
public interface Operator {
/**

View File

@ -1257,9 +1257,8 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler
final Chunk newChunk = instance.getChunk(newChunkX, newChunkZ);
Check.notNull(newChunk, "The entity {0} tried to move in an unloaded chunk at {1}", getEntityId(), newPosition);
instance.UNSAFE_switchEntityChunk(this, currentChunk, newChunk);
if (this instanceof Player) {
if (this instanceof Player player) {
// Refresh player view
final Player player = (Player) this;
player.refreshVisibleChunks(newChunk);
player.refreshVisibleEntities(newChunk);
}

View File

@ -3,15 +3,13 @@ package net.minestom.server.entity;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.registry.Registry;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
@ApiStatus.NonExtendable
public interface EntityType extends ProtocolObject, EntityTypes {
public sealed interface EntityType extends ProtocolObject, EntityTypes permits EntityTypeImpl {
/**
* Returns the entity registry.
*

View File

@ -34,20 +34,13 @@ public enum EquipmentSlot {
}
public static EquipmentSlot fromAttributeSlot(@NotNull AttributeSlot attributeSlot) {
switch (attributeSlot) {
case MAINHAND:
return MAIN_HAND;
case OFFHAND:
return OFF_HAND;
case FEET:
return BOOTS;
case LEGS:
return LEGGINGS;
case CHEST:
return CHESTPLATE;
case HEAD:
return HELMET;
}
throw new IllegalStateException("Something weird happened");
return switch (attributeSlot) {
case MAINHAND -> MAIN_HAND;
case OFFHAND -> OFF_HAND;
case FEET -> BOOTS;
case LEGS -> LEGGINGS;
case CHEST -> CHESTPLATE;
case HEAD -> HELMET;
};
}
}

View File

@ -291,49 +291,28 @@ public class Metadata {
}
private static <T> Value<T> getCorrespondingNewEmptyValue(int type) {
switch (type) {
case TYPE_BYTE:
return (Value<T>) Byte((byte) 0);
case TYPE_VARINT:
return (Value<T>) VarInt(0);
case TYPE_FLOAT:
return (Value<T>) Float(0);
case TYPE_STRING:
return (Value<T>) String("");
case TYPE_CHAT:
return (Value<T>) Chat(Component.empty());
case TYPE_OPTCHAT:
return (Value<T>) OptChat(null);
case TYPE_SLOT:
return (Value<T>) Slot(ItemStack.AIR);
case TYPE_BOOLEAN:
return (Value<T>) Boolean(false);
case TYPE_ROTATION:
return (Value<T>) Rotation(Vec.ZERO);
case TYPE_POSITION:
return (Value<T>) Position(Vec.ZERO);
case TYPE_OPTPOSITION:
return (Value<T>) OptPosition(null);
case TYPE_DIRECTION:
return (Value<T>) Direction(Direction.DOWN);
case TYPE_OPTUUID:
return (Value<T>) OptUUID(null);
case TYPE_OPTBLOCKID:
return (Value<T>) OptBlockID(null);
case TYPE_NBT:
return (Value<T>) NBT(new NBTEnd());
case TYPE_PARTICLE:
throw new UnsupportedOperationException();
case TYPE_VILLAGERDATA:
return (Value<T>) VillagerData(0, 0, 0);
case TYPE_OPTVARINT:
return (Value<T>) OptVarInt(null);
case TYPE_POSE:
return (Value<T>) Pose(Entity.Pose.STANDING);
default:
throw new UnsupportedOperationException();
}
return switch (type) {
case TYPE_BYTE -> (Value<T>) Byte((byte) 0);
case TYPE_VARINT -> (Value<T>) VarInt(0);
case TYPE_FLOAT -> (Value<T>) Float(0);
case TYPE_STRING -> (Value<T>) String("");
case TYPE_CHAT -> (Value<T>) Chat(Component.empty());
case TYPE_OPTCHAT -> (Value<T>) OptChat(null);
case TYPE_SLOT -> (Value<T>) Slot(ItemStack.AIR);
case TYPE_BOOLEAN -> (Value<T>) Boolean(false);
case TYPE_ROTATION -> (Value<T>) Rotation(Vec.ZERO);
case TYPE_POSITION -> (Value<T>) Position(Vec.ZERO);
case TYPE_OPTPOSITION -> (Value<T>) OptPosition(null);
case TYPE_DIRECTION -> (Value<T>) Direction(Direction.DOWN);
case TYPE_OPTUUID -> (Value<T>) OptUUID(null);
case TYPE_OPTBLOCKID -> (Value<T>) OptBlockID(null);
case TYPE_NBT -> (Value<T>) NBT(new NBTEnd());
case TYPE_PARTICLE -> throw new UnsupportedOperationException();
case TYPE_VILLAGERDATA -> (Value<T>) VillagerData(0, 0, 0);
case TYPE_OPTVARINT -> (Value<T>) OptVarInt(null);
case TYPE_POSE -> (Value<T>) Pose(Entity.Pose.STANDING);
default -> throw new UnsupportedOperationException();
};
}
private static <T> Value<T> read(int type, BinaryReader reader) {

View File

@ -23,8 +23,7 @@ import java.util.function.Predicate;
*
* @param <T> The event type accepted by this node
*/
@ApiStatus.NonExtendable
public interface EventNode<T extends Event> {
public sealed interface EventNode<T extends Event> permits EventNodeImpl {
/**
* Creates an event node which accepts any event type with no filtering.

View File

@ -15,7 +15,7 @@ import java.util.function.BiConsumer;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
class EventNodeImpl<T extends Event> implements EventNode<T> {
non-sealed class EventNodeImpl<T extends Event> implements EventNode<T> {
private static final Object GLOBAL_CHILD_LOCK = new Object();
private final Map<Class<? extends T>, Handle<T>> handleMap = new ConcurrentHashMap<>();
@ -259,7 +259,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
final Set<Consumer<T>> bindingConsumers = new CopyOnWriteArraySet<>();
}
private static final class Handle<E extends Event> implements ListenerHandle<E> {
static final class Handle<E extends Event> implements ListenerHandle<E> {
private final EventNodeImpl<E> node;
private final Class<E> eventType;
private Consumer<E> listener = null;

View File

@ -12,8 +12,7 @@ import org.jetbrains.annotations.NotNull;
* @param <E> the event type
*/
@ApiStatus.Experimental
@ApiStatus.NonExtendable
public interface ListenerHandle<E extends Event> {
public sealed interface ListenerHandle<E extends Event> permits EventNodeImpl.Handle {
/**
* Calls the given event.
* Will try to fast exit the execution when possible if {@link #hasListener()} return {@code false}.

View File

@ -5,7 +5,10 @@ import net.minestom.server.registry.Registry;
import net.minestom.server.tag.Tag;
import net.minestom.server.tag.TagReadable;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.*;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.util.Collection;
@ -19,8 +22,7 @@ import java.util.function.BiPredicate;
* <p>
* Implementations are expected to be immutable.
*/
@ApiStatus.NonExtendable
public interface Block extends ProtocolObject, TagReadable, Blocks {
public sealed interface Block extends ProtocolObject, TagReadable, Blocks permits BlockImpl {
/**
* Creates a new block with the the property {@code property} sets to {@code value}.

View File

@ -23,20 +23,13 @@ public enum BlockFace {
@NotNull
public BlockFace getOppositeFace() {
switch(this) {
case BOTTOM:
return TOP;
case TOP:
return BOTTOM;
case NORTH:
return SOUTH;
case SOUTH:
return NORTH;
case WEST:
return EAST;
case EAST:
return WEST;
}
return null;
return switch (this) {
case BOTTOM -> TOP;
case TOP -> BOTTOM;
case NORTH -> SOUTH;
case SOUTH -> NORTH;
case WEST -> EAST;
case EAST -> WEST;
};
}
}

View File

@ -82,12 +82,8 @@ public interface BlockHandler {
/**
* Represents an object forwarded to {@link #onPlace(Placement)}.
* <p>
* Will in the future rely on sealed classes (https://openjdk.java.net/jeps/409)
* and record pattern for the implementations (https://openjdk.java.net/jeps/405).
*/
@ApiStatus.NonExtendable
class Placement {
sealed class Placement permits PlayerPlacement {
private final Block block;
private final Instance instance;
private final Point blockPosition;
@ -155,8 +151,7 @@ public interface BlockHandler {
}
}
@ApiStatus.NonExtendable
class Destroy {
sealed class Destroy permits PlayerDestroy {
private final Block block;
private final Instance instance;
private final Point blockPosition;
@ -195,8 +190,7 @@ public interface BlockHandler {
}
}
@ApiStatus.NonExtendable
class Interaction {
final class Interaction {
private final Block block;
private final Instance instance;
private final Point blockPosition;
@ -233,8 +227,7 @@ public interface BlockHandler {
}
}
@ApiStatus.NonExtendable
class Touch {
final class Touch {
private final Block block;
private final Instance instance;
private final Point blockPosition;
@ -265,8 +258,7 @@ public interface BlockHandler {
}
}
@ApiStatus.NonExtendable
class Tick {
final class Tick {
private final Block block;
private final Instance instance;
private final Point blockPosition;

View File

@ -7,7 +7,6 @@ import net.minestom.server.tag.Tag;
import net.minestom.server.tag.TagHandler;
import net.minestom.server.utils.MathUtils;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
@ -21,8 +20,8 @@ import java.util.function.UnaryOperator;
/**
* Represents an inventory where items can be modified/retrieved.
*/
@ApiStatus.NonExtendable
public abstract class AbstractInventory implements InventoryClickHandler, TagHandler {
public sealed abstract class AbstractInventory implements InventoryClickHandler, TagHandler
permits Inventory, PlayerInventory {
private final int size;
protected final ItemStack[] itemStacks;

View File

@ -48,13 +48,10 @@ public interface EquipmentHandler {
* @return the {@link ItemStack} in {@code hand}
*/
default @NotNull ItemStack getItemInHand(@NotNull Player.Hand hand) {
switch (hand) {
case MAIN:
return getItemInMainHand();
case OFF:
return getItemInOffHand();
}
throw new IllegalStateException("Something weird happened");
return switch (hand) {
case MAIN -> getItemInMainHand();
case OFF -> getItemInOffHand();
};
}
/**
@ -65,13 +62,8 @@ public interface EquipmentHandler {
*/
default void setItemInHand(@NotNull Player.Hand hand, @NotNull ItemStack stack) {
switch (hand) {
case MAIN:
setItemInMainHand(stack);
break;
case OFF:
setItemInOffHand(stack);
break;
case MAIN -> setItemInMainHand(stack);
case OFF -> setItemInOffHand(stack);
}
}
@ -138,45 +130,24 @@ public interface EquipmentHandler {
* @return the equipment {@link ItemStack}
*/
default @NotNull ItemStack getEquipment(@NotNull EquipmentSlot slot) {
switch (slot) {
case MAIN_HAND:
return getItemInMainHand();
case OFF_HAND:
return getItemInOffHand();
case HELMET:
return getHelmet();
case CHESTPLATE:
return getChestplate();
case LEGGINGS:
return getLeggings();
case BOOTS:
return getBoots();
}
throw new IllegalStateException("Something weird happened");
return switch (slot) {
case MAIN_HAND -> getItemInMainHand();
case OFF_HAND -> getItemInOffHand();
case HELMET -> getHelmet();
case CHESTPLATE -> getChestplate();
case LEGGINGS -> getLeggings();
case BOOTS -> getBoots();
};
}
default void setEquipment(@NotNull EquipmentSlot slot, @NotNull ItemStack itemStack) {
switch (slot) {
case MAIN_HAND:
setItemInMainHand(itemStack);
break;
case OFF_HAND:
setItemInOffHand(itemStack);
break;
case HELMET:
setHelmet(itemStack);
break;
case CHESTPLATE:
setChestplate(itemStack);
break;
case LEGGINGS:
setLeggings(itemStack);
break;
case BOOTS:
setBoots(itemStack);
break;
default:
throw new IllegalStateException("Something weird happened");
case MAIN_HAND -> setItemInMainHand(itemStack);
case OFF_HAND -> setItemInOffHand(itemStack);
case HELMET -> setHelmet(itemStack);
case CHESTPLATE -> setChestplate(itemStack);
case LEGGINGS -> setLeggings(itemStack);
case BOOTS -> setBoots(itemStack);
}
}

View File

@ -27,7 +27,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
* You can create one with {@link Inventory#Inventory(InventoryType, String)} or by making your own subclass.
* It can then be opened using {@link Player#openInventory(Inventory)}.
*/
public class Inventory extends AbstractInventory implements Viewable {
public non-sealed class Inventory extends AbstractInventory implements Viewable {
// incremented each time an inventory is created (used in the window packets)
private static byte LAST_INVENTORY_ID;

View File

@ -5,7 +5,6 @@ import net.minestom.server.event.EventDispatcher;
import net.minestom.server.event.inventory.InventoryClickEvent;
import net.minestom.server.inventory.click.ClickType;
import net.minestom.server.item.ItemStack;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
/**
@ -14,8 +13,7 @@ import org.jetbrains.annotations.NotNull;
* <p>
* See https://wiki.vg/Protocol#Click_Window for more information.
*/
@ApiStatus.NonExtendable
public interface InventoryClickHandler {
public sealed interface InventoryClickHandler permits AbstractInventory {
/**
* Called when a {@link Player} left click in the inventory. Can also be to drop the cursor item

View File

@ -18,7 +18,7 @@ import static net.minestom.server.utils.inventory.PlayerInventoryUtils.*;
/**
* Represents the inventory of a {@link Player}, retrieved with {@link Player#getInventory()}.
*/
public class PlayerInventory extends AbstractInventory implements EquipmentHandler {
public non-sealed class PlayerInventory extends AbstractInventory implements EquipmentHandler {
public static final int INVENTORY_SIZE = 46;
public static final int INNER_INVENTORY_SIZE = 36;

View File

@ -3,15 +3,13 @@ package net.minestom.server.item;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.registry.Registry;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
@ApiStatus.NonExtendable
public interface Enchantment extends ProtocolObject, Enchantments {
public sealed interface Enchantment extends ProtocolObject, Enchantments permits EnchantmentImpl {
/**
* Returns the enchantment registry.

View File

@ -4,15 +4,13 @@ import net.minestom.server.instance.block.Block;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.registry.Registry;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
@ApiStatus.NonExtendable
public interface Material extends ProtocolObject, Materials {
public sealed interface Material extends ProtocolObject, Materials permits MaterialImpl {
/**
* Returns the material registry.

View File

@ -15,12 +15,8 @@ public class AnimationListener {
PlayerHandAnimationEvent handAnimationEvent = new PlayerHandAnimationEvent(player, hand);
EventDispatcher.callCancellable(handAnimationEvent, () -> {
switch (hand) {
case MAIN:
player.swingMainHand();
break;
case OFF:
player.swingOffHand();
break;
case MAIN -> player.swingMainHand();
case OFF -> player.swingOffHand();
}
});
}

View File

@ -55,11 +55,11 @@ public enum ChatMessageType {
* @return the chat message type
*/
public static @NotNull ChatMessageType fromPacketID(int id) {
switch (id) {
case 0: return FULL;
case 1: return SYSTEM;
case 2: return NONE;
default: throw new IllegalArgumentException("id must be between 0-2 (inclusive)");
}
return switch (id) {
case 0 -> FULL;
case 1 -> SYSTEM;
case 2 -> NONE;
default -> throw new IllegalArgumentException("id must be between 0-2 (inclusive)");
};
}
}

View File

@ -55,11 +55,10 @@ public enum ChatPosition {
* @return the position
*/
public static @NotNull ChatPosition fromMessageType(@NotNull MessageType messageType) {
switch (messageType) {
case CHAT: return CHAT;
case SYSTEM: return SYSTEM_MESSAGE;
}
throw new IllegalArgumentException("Cannot get position from message type!");
return switch (messageType) {
case CHAT -> CHAT;
case SYSTEM -> SYSTEM_MESSAGE;
};
}
/**
@ -69,11 +68,11 @@ public enum ChatPosition {
* @return the chat position
*/
public static @NotNull ChatPosition fromPacketID(byte id) {
switch (id) {
case 0: return CHAT;
case 1: return SYSTEM_MESSAGE;
case 2: return GAME_INFO;
default: throw new IllegalArgumentException("id must be between 0-2 (inclusive)");
}
return switch (id) {
case 0 -> CHAT;
case 1 -> SYSTEM_MESSAGE;
case 2 -> GAME_INFO;
default -> throw new IllegalArgumentException("id must be between 0-2 (inclusive)");
};
}
}

View File

@ -11,26 +11,7 @@ import java.nio.ByteBuffer;
* Can be used if you want to send the exact same buffer to multiple clients without processing it more than once.
*/
@ApiStatus.Internal
public final class FramedPacket {
private final int packetId;
private final ByteBuffer body;
private final ServerPacket packet;
public FramedPacket(int packetId, @NotNull ByteBuffer body, @NotNull ServerPacket packet) {
this.packetId = packetId;
this.body = body;
this.packet = packet;
}
public int packetId() {
return packetId;
}
public @NotNull ByteBuffer body() {
return body;
}
public @NotNull ServerPacket packet() {
return packet;
}
public record FramedPacket(int packetId,
@NotNull ByteBuffer body,
@NotNull ServerPacket packet) {
}

View File

@ -1,11 +1,11 @@
package net.minestom.server.network.packet.client.play;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.network.packet.client.ClientPlayPacket;
import net.minestom.server.utils.Rotation;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Vec;
import org.jetbrains.annotations.NotNull;
public class ClientUpdateStructureBlockPacket extends ClientPlayPacket {
@ -90,32 +90,22 @@ public class ClientUpdateStructureBlockPacket extends ClientPlayPacket {
}
private int toRestrictedRotation(Rotation rotation) {
switch (rotation) {
case NONE:
return 0;
case CLOCKWISE:
return 1;
case FLIPPED:
return 2;
case COUNTER_CLOCKWISE:
return 3;
default:
throw new IllegalArgumentException("ClientUpdateStructurePacket#rotation must be a valid 90-degree rotation.");
}
return switch (rotation) {
case NONE -> 0;
case CLOCKWISE -> 1;
case FLIPPED -> 2;
case COUNTER_CLOCKWISE -> 3;
default -> throw new IllegalArgumentException("ClientUpdateStructurePacket#rotation must be a valid 90-degree rotation.");
};
}
private Rotation fromRestrictedRotation(int rotation) {
switch (rotation) {
case 0:
return Rotation.NONE;
case 1:
return Rotation.CLOCKWISE;
case 2:
return Rotation.FLIPPED;
case 3:
return Rotation.COUNTER_CLOCKWISE;
default:
throw new IllegalArgumentException("ClientUpdateStructurePacket#rotation must be a valid 90-degree rotation.");
}
return switch (rotation) {
case 0 -> Rotation.NONE;
case 1 -> Rotation.CLOCKWISE;
case 2 -> Rotation.FLIPPED;
case 3 -> Rotation.COUNTER_CLOCKWISE;
default -> throw new IllegalArgumentException("ClientUpdateStructurePacket#rotation must be a valid 90-degree rotation.");
};
}
}

View File

@ -44,8 +44,7 @@ public final class Server {
@ApiStatus.Internal
public void init(SocketAddress address) throws IOException {
if (address instanceof InetSocketAddress) {
InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
if (address instanceof InetSocketAddress inetSocketAddress) {
this.address = inetSocketAddress.getHostString();
this.port = inetSocketAddress.getPort();
} // TODO unix domain support

View File

@ -2,14 +2,12 @@ package net.minestom.server.particle;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
@ApiStatus.NonExtendable
public interface Particle extends ProtocolObject, Particles {
public sealed interface Particle extends ProtocolObject, Particles permits ParticleImpl {
static @NotNull Collection<@NotNull Particle> values() {
return ParticleImpl.values();

View File

@ -3,15 +3,13 @@ package net.minestom.server.potion;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.registry.Registry;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
@ApiStatus.NonExtendable
public interface PotionEffect extends ProtocolObject, PotionEffects {
public sealed interface PotionEffect extends ProtocolObject, PotionEffects permits PotionEffectImpl {
@Contract(pure = true)
@NotNull Registry.PotionEffectEntry registry();

View File

@ -2,14 +2,12 @@ package net.minestom.server.potion;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
@ApiStatus.NonExtendable
public interface PotionType extends ProtocolObject, PotionTypes {
public sealed interface PotionType extends ProtocolObject, PotionTypes permits PotionTypeImpl {
static @NotNull Collection<@NotNull PotionType> values() {
return PotionTypeImpl.values();

View File

@ -271,21 +271,11 @@ public final class Registry {
if (armorProperties != null) {
final String slot = armorProperties.getAsJsonObject().get("slot").getAsString();
switch (slot) {
case "feet":
this.equipmentSlot = EquipmentSlot.BOOTS;
break;
case "legs":
this.equipmentSlot = EquipmentSlot.LEGGINGS;
break;
case "chest":
this.equipmentSlot = EquipmentSlot.CHESTPLATE;
break;
case "head":
this.equipmentSlot = EquipmentSlot.HELMET;
break;
default:
this.equipmentSlot = null;
break;
case "feet" -> this.equipmentSlot = EquipmentSlot.BOOTS;
case "legs" -> this.equipmentSlot = EquipmentSlot.LEGGINGS;
case "chest" -> this.equipmentSlot = EquipmentSlot.CHESTPLATE;
case "head" -> this.equipmentSlot = EquipmentSlot.HELMET;
default -> this.equipmentSlot = null;
}
} else {
this.equipmentSlot = null;

View File

@ -4,14 +4,12 @@ import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
@ApiStatus.NonExtendable
public interface SoundEvent extends ProtocolObject, Sound.Type, SoundEvents {
public sealed interface SoundEvent extends ProtocolObject, Sound.Type, SoundEvents permits SoundEventImpl {
static @NotNull Collection<@NotNull SoundEvent> values() {
return SoundEventImpl.values();

View File

@ -2,14 +2,12 @@ package net.minestom.server.statistic;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
@ApiStatus.NonExtendable
public interface StatisticType extends ProtocolObject, StatisticTypes {
public sealed interface StatisticType extends ProtocolObject, StatisticTypes permits StatisticTypeImpl {
static @NotNull Collection<@NotNull StatisticType> values() {
return StatisticTypeImpl.values();

View File

@ -34,23 +34,14 @@ public enum Direction {
return normalZ;
}
@NotNull
public Direction opposite() {
switch (this) {
case UP:
return DOWN;
case DOWN:
return UP;
case EAST:
return WEST;
case WEST:
return EAST;
case NORTH:
return SOUTH;
case SOUTH:
return NORTH;
default:
throw new IllegalArgumentException();
}
public @NotNull Direction opposite() {
return switch (this) {
case UP -> DOWN;
case DOWN -> UP;
case EAST -> WEST;
case WEST -> EAST;
case NORTH -> SOUTH;
case SOUTH -> NORTH;
};
}
}

View File

@ -41,9 +41,10 @@ public final class BinaryBuffer {
}
public void write(ByteBuffer buffer) {
final int size = buffer.remaining();
// TODO jdk 13 put with index
this.nioBuffer.position(writerOffset).put(buffer);
final int start = buffer.position();
final int end = buffer.limit();
final int size = end - start;
this.nioBuffer.put(writerOffset, buffer, start, size);
this.writerOffset += size;
}
@ -107,13 +108,13 @@ public final class BinaryBuffer {
}
public void writeBytes(byte[] bytes) {
this.nioBuffer.position(writerOffset).put(bytes);
this.nioBuffer.put(writerOffset, bytes);
this.writerOffset += bytes.length;
}
public byte[] readBytes(int length) {
byte[] bytes = new byte[length];
this.nioBuffer.position(readerOffset).get(bytes, 0, length);
this.nioBuffer.get(readerOffset, bytes);
this.readerOffset += length;
return bytes;
}
@ -130,7 +131,7 @@ public final class BinaryBuffer {
}
public ByteBuffer asByteBuffer(int reader, int writer) {
return nioBuffer.position(reader).slice().limit(writer);
return nioBuffer.slice(reader, writer);
}
@ApiStatus.Internal
@ -139,7 +140,7 @@ public final class BinaryBuffer {
}
public boolean writeChannel(WritableByteChannel channel) throws IOException {
var writeBuffer = asByteBuffer(readerOffset, writerOffset - readerOffset);
var writeBuffer = nioBuffer.slice(readerOffset, writerOffset - readerOffset);
final int count = channel.write(writeBuffer);
if (count == -1) {
// EOS
@ -150,7 +151,7 @@ public final class BinaryBuffer {
}
public void readChannel(ReadableByteChannel channel) throws IOException {
final int count = channel.read(nioBuffer.position(readerOffset));
final int count = channel.read(nioBuffer.slice(readerOffset, capacity - readerOffset));
if (count == -1) {
// EOS
throw new IOException("Disconnected");
@ -167,28 +168,6 @@ public final class BinaryBuffer {
'}';
}
public static final class Marker {
private final int readerOffset, writerOffset;
private Marker(int readerOffset, int writerOffset) {
this.readerOffset = readerOffset;
this.writerOffset = writerOffset;
}
public int readerOffset() {
return readerOffset;
}
public int writerOffset() {
return writerOffset;
}
@Override
public String toString() {
return "Marker{" +
"readerOffset=" + readerOffset +
", writerOffset=" + writerOffset +
'}';
}
public record Marker(int readerOffset, int writerOffset) {
}
}