From 2b1e1040a884e0d97189d0c118aad840821f3b04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=80=E5=93=A5?= <3523206925@qq.com> Date: Sat, 5 Aug 2023 16:16:30 +0800 Subject: [PATCH] refactor: code style clean up & add qodana_code_quality.yml --- .github/workflows/qodana_code_quality.yml | 19 +++++++++++++++ .../java/cn/allay/api/client/BaseClient.java | 1 + .../main/java/cn/allay/api/client/Client.java | 1 + .../java/cn/allay/api/client/FakeClient.java | 2 +- .../client/{ => data}/AdventureSettings.java | 3 ++- .../movement/ClientMovementValidator.java | 10 ++++++++ .../java/cn/allay/api/client/skin/Skin.java | 4 +++- .../api/datastruct/aabbtree/AABBTree.java | 5 ++-- .../datastruct/aabbtree/CollisionPair.java | 11 ++------- .../nb/Int2ObjectNonBlockingMap.java | 7 +++--- .../nb/Long2ObjectNonBlockingMap.java | 7 +++--- .../impl/base/EntityBaseComponent.java | 5 ++++ .../impl/base/EntityBaseComponentImpl.java | 2 +- .../EntityContainerViewerComponentImpl.java | 2 +- .../allay/api/entity/impl/EntityPlayer.java | 19 ++++++++++++++- .../impl/base/ItemBaseComponent.java | 2 +- .../impl/base/ItemBaseComponentImpl.java | 4 ++-- .../api/item/type/ItemStackInitInfo.java | 2 +- .../world/entity/EntityPhysicsService.java | 6 +++-- .../cn/allay/api/world/palette/Palette.java | 23 ++++++++++--------- .../api/world/palette/bitarray/BitArray.java | 6 ++--- .../palette/bitarray/PaddedBitArray.java | 6 +---- .../world/palette/bitarray/Pow2BitArray.java | 6 +---- .../palette/bitarray/SingletonBitArray.java | 6 ++--- .../VanillaBlockBehaviorInterfaceGen.java | 2 +- .../cn/allay/codegen/VanillaItemClassGen.java | 4 +--- .../server/ThroughList2ArrayJMHTest.java | 8 +++---- .../cn/allay/server/client/AllayClient.java | 4 +--- .../item/type/AllayCreativeItemRegistry.java | 2 +- .../java/cn/allay/server/utils/GameLoop.java | 4 +++- .../cn/allay/server/world/AllayWorld.java | 10 ++++---- .../allay/server/world/chunk/HeightMap.java | 2 +- .../entity/AllayEntityPhysicsService.java | 11 +++++++++ .../world/storage/anvil/AnvilRegionFile.java | 17 ++++++-------- 34 files changed, 136 insertions(+), 87 deletions(-) create mode 100644 .github/workflows/qodana_code_quality.yml rename Allay-API/src/main/java/cn/allay/api/client/{ => data}/AdventureSettings.java (98%) create mode 100644 Allay-API/src/main/java/cn/allay/api/client/movement/ClientMovementValidator.java create mode 100644 Allay-Server/src/main/java/cn/allay/server/world/entity/AllayEntityPhysicsService.java diff --git a/.github/workflows/qodana_code_quality.yml b/.github/workflows/qodana_code_quality.yml new file mode 100644 index 000000000..5d0c8363c --- /dev/null +++ b/.github/workflows/qodana_code_quality.yml @@ -0,0 +1,19 @@ +name: Qodana +on: + workflow_dispatch: + pull_request: + push: + branches: + - master + +jobs: + qodana: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: 'Qodana Scan' + uses: JetBrains/qodana-action@v2023.2 + env: + QODANA_TOKEN: ${{ secrets.QODANA_TOKEN }} \ No newline at end of file diff --git a/Allay-API/src/main/java/cn/allay/api/client/BaseClient.java b/Allay-API/src/main/java/cn/allay/api/client/BaseClient.java index cfa9f68ab..479d6d617 100644 --- a/Allay-API/src/main/java/cn/allay/api/client/BaseClient.java +++ b/Allay-API/src/main/java/cn/allay/api/client/BaseClient.java @@ -1,5 +1,6 @@ package cn.allay.api.client; +import cn.allay.api.client.data.AdventureSettings; import cn.allay.api.client.data.LoginData; import cn.allay.api.client.skin.Skin; import cn.allay.api.entity.impl.EntityPlayer; diff --git a/Allay-API/src/main/java/cn/allay/api/client/Client.java b/Allay-API/src/main/java/cn/allay/api/client/Client.java index 77df07d99..ff9f0869c 100644 --- a/Allay-API/src/main/java/cn/allay/api/client/Client.java +++ b/Allay-API/src/main/java/cn/allay/api/client/Client.java @@ -1,5 +1,6 @@ package cn.allay.api.client; +import cn.allay.api.client.data.AdventureSettings; import cn.allay.api.client.data.LoginData; import cn.allay.api.client.info.DeviceInfo; import cn.allay.api.client.skin.Skin; diff --git a/Allay-API/src/main/java/cn/allay/api/client/FakeClient.java b/Allay-API/src/main/java/cn/allay/api/client/FakeClient.java index 01de8250a..62ec209d3 100644 --- a/Allay-API/src/main/java/cn/allay/api/client/FakeClient.java +++ b/Allay-API/src/main/java/cn/allay/api/client/FakeClient.java @@ -29,11 +29,11 @@ * * @author daoge_cmd */ +@Getter public class FakeClient extends BaseClient { protected static final ContainerActionProcessorHolder EMPTY_CONTAINER_ACTION_PROCESSOR_HOLDER = new SimpleContainerActionProcessorHolder(); - @Getter @Setter protected boolean loaderActive; diff --git a/Allay-API/src/main/java/cn/allay/api/client/AdventureSettings.java b/Allay-API/src/main/java/cn/allay/api/client/data/AdventureSettings.java similarity index 98% rename from Allay-API/src/main/java/cn/allay/api/client/AdventureSettings.java rename to Allay-API/src/main/java/cn/allay/api/client/data/AdventureSettings.java index 16b15b17c..11a8962ec 100644 --- a/Allay-API/src/main/java/cn/allay/api/client/AdventureSettings.java +++ b/Allay-API/src/main/java/cn/allay/api/client/data/AdventureSettings.java @@ -1,5 +1,6 @@ -package cn.allay.api.client; +package cn.allay.api.client.data; +import cn.allay.api.client.Client; import lombok.Getter; import org.cloudburstmc.protocol.bedrock.data.Ability; import org.cloudburstmc.protocol.bedrock.data.AbilityLayer; diff --git a/Allay-API/src/main/java/cn/allay/api/client/movement/ClientMovementValidator.java b/Allay-API/src/main/java/cn/allay/api/client/movement/ClientMovementValidator.java new file mode 100644 index 000000000..cb3c791d2 --- /dev/null +++ b/Allay-API/src/main/java/cn/allay/api/client/movement/ClientMovementValidator.java @@ -0,0 +1,10 @@ +package cn.allay.api.client.movement; + +/** + * Allay Project 2023/8/5 + * + * @author daoge_cmd + */ +public interface ClientMovementValidator { + boolean validate() +} diff --git a/Allay-API/src/main/java/cn/allay/api/client/skin/Skin.java b/Allay-API/src/main/java/cn/allay/api/client/skin/Skin.java index e383a57c4..fc03a783e 100644 --- a/Allay-API/src/main/java/cn/allay/api/client/skin/Skin.java +++ b/Allay-API/src/main/java/cn/allay/api/client/skin/Skin.java @@ -5,6 +5,7 @@ import lombok.Getter; import lombok.Setter; import lombok.ToString; +import lombok.extern.slf4j.Slf4j; import org.cloudburstmc.protocol.bedrock.data.skin.*; import org.jose4j.json.internal.json_simple.JSONObject; import org.jose4j.json.internal.json_simple.JSONValue; @@ -28,6 +29,7 @@ @ToString @Getter @Setter +@Slf4j public class Skin { public static final int SINGLE_SKIN_SIZE = 8192; @@ -102,7 +104,7 @@ public static Skin fromInputStream(InputStream inputStream) { try { skinData = ImageIO.read(inputStream); } catch (IOException e) { - e.printStackTrace(); + log.error("Error while reading skin from input stream", e); } if (skinData != null) { skin.setSkinData(skinData); diff --git a/Allay-API/src/main/java/cn/allay/api/datastruct/aabbtree/AABBTree.java b/Allay-API/src/main/java/cn/allay/api/datastruct/aabbtree/AABBTree.java index f8506d224..26f80744b 100644 --- a/Allay-API/src/main/java/cn/allay/api/datastruct/aabbtree/AABBTree.java +++ b/Allay-API/src/main/java/cn/allay/api/datastruct/aabbtree/AABBTree.java @@ -260,7 +260,7 @@ private void detectCollisionPairsWithNode(AABBTreeNode nodeToTest, CollisionF AABBf overlapWith = nodeToTest.getAABB(); while (!stack.isEmpty()) { - Integer nodeIndex = stack.pop(); + int nodeIndex = stack.pop(); if (nodeIndex == AABBTreeNode.INVALID_NODE_INDEX) { continue; } @@ -377,8 +377,7 @@ public void detectCollisionPairs(CollisionFilter filter, List> alreadyTested = new HashSet<>(); - for (int i = 0; i < nodes.size(); i++) { - AABBTreeNode testedNode = nodes.get(i); + for (AABBTreeNode testedNode : nodes) { if (!testedNode.isLeaf()) { continue; } diff --git a/Allay-API/src/main/java/cn/allay/api/datastruct/aabbtree/CollisionPair.java b/Allay-API/src/main/java/cn/allay/api/datastruct/aabbtree/CollisionPair.java index c711f56fe..0e7ea5f65 100644 --- a/Allay-API/src/main/java/cn/allay/api/datastruct/aabbtree/CollisionPair.java +++ b/Allay-API/src/main/java/cn/allay/api/datastruct/aabbtree/CollisionPair.java @@ -5,16 +5,9 @@ import java.util.Objects; @Getter -public final class CollisionPair { - private final T objectA; - private final T objectB; +public record CollisionPair(T objectA, T objectB) { - public CollisionPair(T objectA, T objectB) { - this.objectA = objectA; - this.objectB = objectB; - } - - @Override + @Override public boolean equals(Object o) { if (this == o) { return true; diff --git a/Allay-API/src/main/java/cn/allay/api/datastruct/collections/nb/Int2ObjectNonBlockingMap.java b/Allay-API/src/main/java/cn/allay/api/datastruct/collections/nb/Int2ObjectNonBlockingMap.java index 5a92717ef..6355bf6f9 100644 --- a/Allay-API/src/main/java/cn/allay/api/datastruct/collections/nb/Int2ObjectNonBlockingMap.java +++ b/Allay-API/src/main/java/cn/allay/api/datastruct/collections/nb/Int2ObjectNonBlockingMap.java @@ -2,6 +2,7 @@ import it.unimi.dsi.fastutil.ints.AbstractIntSet; import it.unimi.dsi.fastutil.ints.IntIterator; +import org.jetbrains.annotations.NotNull; import java.io.IOException; import java.io.Serial; @@ -431,7 +432,7 @@ public TypeV remove(Object key) { /** * Auto-boxing version of {@link #remove(int, Object)}. */ - public boolean remove(Object key, Object Val) { + public boolean remove(@NotNull Object key, Object Val) { return (key instanceof Integer) && remove(((Integer) key).intValue(), Val); } @@ -803,7 +804,7 @@ private static final class CHM implements Serializable { static private final AtomicLongFieldUpdater _copyDoneUpdater = AtomicLongFieldUpdater.newUpdater(CHM.class, "_copyDone"); // Back-pointer to top-level structure - final Int2ObjectNonBlockingMap _nbhml; + final Int2ObjectNonBlockingMap _nbhml; final int[] _keys; final Object[] _vals; // --- @@ -844,7 +845,7 @@ private static final class CHM implements Serializable { private ConcurrentAutoIntTable _slots; // Simple constructor - CHM(final Int2ObjectNonBlockingMap nbhml, ConcurrentAutoIntTable size, final int logsize) { + CHM(final Int2ObjectNonBlockingMap nbhml, ConcurrentAutoIntTable size, final int logsize) { _nbhml = nbhml; _size = size; _slots = new ConcurrentAutoIntTable(); diff --git a/Allay-API/src/main/java/cn/allay/api/datastruct/collections/nb/Long2ObjectNonBlockingMap.java b/Allay-API/src/main/java/cn/allay/api/datastruct/collections/nb/Long2ObjectNonBlockingMap.java index 994d9a364..26a282ded 100644 --- a/Allay-API/src/main/java/cn/allay/api/datastruct/collections/nb/Long2ObjectNonBlockingMap.java +++ b/Allay-API/src/main/java/cn/allay/api/datastruct/collections/nb/Long2ObjectNonBlockingMap.java @@ -3,6 +3,7 @@ import it.unimi.dsi.fastutil.longs.AbstractLongSet; import it.unimi.dsi.fastutil.longs.LongIterator; import it.unimi.dsi.fastutil.longs.LongSet; +import org.jetbrains.annotations.NotNull; import java.io.IOException; import java.io.Serial; @@ -433,7 +434,7 @@ public TypeV remove(Object key) { /** * Auto-boxing version of {@link #remove(long, Object)}. */ - public boolean remove(Object key, Object Val) { + public boolean remove(@NotNull Object key, Object Val) { return (key instanceof Long) && remove(((Long) key).longValue(), Val); } @@ -808,7 +809,7 @@ private static final class CHM implements Serializable { static private final AtomicLongFieldUpdater _copyDoneUpdater = AtomicLongFieldUpdater.newUpdater(CHM.class, "_copyDone"); // Back-pointer to top-level structure - final Long2ObjectNonBlockingMap _nbhml; + final Long2ObjectNonBlockingMap _nbhml; final long[] _keys; final Object[] _vals; // --- @@ -849,7 +850,7 @@ private static final class CHM implements Serializable { private ConcurrentAutoLongTable _slots; // Simple constructor - CHM(final Long2ObjectNonBlockingMap nbhml, ConcurrentAutoLongTable size, final int logsize) { + CHM(final Long2ObjectNonBlockingMap nbhml, ConcurrentAutoLongTable size, final int logsize) { _nbhml = nbhml; _size = size; _slots = new ConcurrentAutoLongTable(); diff --git a/Allay-API/src/main/java/cn/allay/api/entity/component/impl/base/EntityBaseComponent.java b/Allay-API/src/main/java/cn/allay/api/entity/component/impl/base/EntityBaseComponent.java index 9e1c466c7..3572fbd2c 100644 --- a/Allay-API/src/main/java/cn/allay/api/entity/component/impl/base/EntityBaseComponent.java +++ b/Allay-API/src/main/java/cn/allay/api/entity/component/impl/base/EntityBaseComponent.java @@ -80,4 +80,9 @@ public interface EntityBaseComponent { @Inject void sendPacketToViewersImmediately(BedrockPacket packet); + + @Inject + default double getBaseOffset() { + return 0; + } } diff --git a/Allay-API/src/main/java/cn/allay/api/entity/component/impl/base/EntityBaseComponentImpl.java b/Allay-API/src/main/java/cn/allay/api/entity/component/impl/base/EntityBaseComponentImpl.java index 13a5d295f..5551b562c 100644 --- a/Allay-API/src/main/java/cn/allay/api/entity/component/impl/base/EntityBaseComponentImpl.java +++ b/Allay-API/src/main/java/cn/allay/api/entity/component/impl/base/EntityBaseComponentImpl.java @@ -253,7 +253,7 @@ public BedrockPacket createSpawnPacket() { addEntityPacket.setRuntimeEntityId(uniqueId); addEntityPacket.setUniqueEntityId(uniqueId); addEntityPacket.setIdentifier(entityType.getIdentifier().toString()); - addEntityPacket.setPosition(Vector3f.from(location.x(), location.y(), location.z())); + addEntityPacket.setPosition(Vector3f.from(location.x(), location.y() + getBaseOffset(), location.z())); addEntityPacket.setMotion(Vector3f.from(motion.x(), motion.y(), motion.z())); addEntityPacket.setRotation(Vector2f.from(location.pitch(), location.yaw())); addEntityPacket.getMetadata().putAll(metadata.getEntityDataMap()); diff --git a/Allay-API/src/main/java/cn/allay/api/entity/component/impl/container/EntityContainerViewerComponentImpl.java b/Allay-API/src/main/java/cn/allay/api/entity/component/impl/container/EntityContainerViewerComponentImpl.java index ba5e71eef..1faafd08b 100644 --- a/Allay-API/src/main/java/cn/allay/api/entity/component/impl/container/EntityContainerViewerComponentImpl.java +++ b/Allay-API/src/main/java/cn/allay/api/entity/component/impl/container/EntityContainerViewerComponentImpl.java @@ -108,7 +108,7 @@ public void onSlotChange(Container container, int slot, ItemStack current) { @Nullable @Impl public T getOpenedContainer(FullContainerType type) { - return (T) id2ContainerBiMap.get(type); + return (T) type2ContainerBiMap.get(type); } @Override diff --git a/Allay-API/src/main/java/cn/allay/api/entity/impl/EntityPlayer.java b/Allay-API/src/main/java/cn/allay/api/entity/impl/EntityPlayer.java index f38ff3f1f..f98874663 100644 --- a/Allay-API/src/main/java/cn/allay/api/entity/impl/EntityPlayer.java +++ b/Allay-API/src/main/java/cn/allay/api/entity/impl/EntityPlayer.java @@ -96,6 +96,7 @@ class EntityPlayerBaseComponentImpl extends EntityBaseComponentImpl info, Function aabbGetter) { super(info, aabbGetter); @@ -127,7 +128,7 @@ public BedrockPacket createSpawnPacket() { addPlayerPacket.setUuid(client.getLoginData().getUuid()); addPlayerPacket.setUsername(client.getName()); addPlayerPacket.setPlatformChatId(client.getLoginData().getDeviceInfo().getDeviceId()); - addPlayerPacket.setPosition(Vector3f.from(location.x(), location.y(), location.z())); + addPlayerPacket.setPosition(Vector3f.from(location.x(), location.y() + getBaseOffset(), location.z())); addPlayerPacket.setMotion(Vector3f.from(motion.x(), motion.y(), motion.z())); addPlayerPacket.setRotation(Vector3f.from(location.pitch(), location.yaw(), location.headYaw())); addPlayerPacket.setGameType(client.getGameType()); @@ -190,6 +191,22 @@ public void setGliding(boolean gliding) { public boolean isGliding() { return gliding; } + + @Override + public void setCrawling(boolean crawling) { + this.crawling = crawling; + } + + @Override + public boolean isCrawling() { + return crawling; + } + + @Override + @Impl + public double getBaseOffset() { + return 1.62; + } } } diff --git a/Allay-API/src/main/java/cn/allay/api/item/component/impl/base/ItemBaseComponent.java b/Allay-API/src/main/java/cn/allay/api/item/component/impl/base/ItemBaseComponent.java index 975a0d233..4ec1eb843 100644 --- a/Allay-API/src/main/java/cn/allay/api/item/component/impl/base/ItemBaseComponent.java +++ b/Allay-API/src/main/java/cn/allay/api/item/component/impl/base/ItemBaseComponent.java @@ -35,7 +35,7 @@ public interface ItemBaseComponent { NbtMap getNbt(); @Inject - void setNbt(@Nullable NbtMap nbt); + void setNbt(@Nullable NbtMap nbt); @Nullable @Inject diff --git a/Allay-API/src/main/java/cn/allay/api/item/component/impl/base/ItemBaseComponentImpl.java b/Allay-API/src/main/java/cn/allay/api/item/component/impl/base/ItemBaseComponentImpl.java index 41f2e6925..a7da6e0cc 100644 --- a/Allay-API/src/main/java/cn/allay/api/item/component/impl/base/ItemBaseComponentImpl.java +++ b/Allay-API/src/main/java/cn/allay/api/item/component/impl/base/ItemBaseComponentImpl.java @@ -92,7 +92,7 @@ public NbtMap getNbt() { @Override @Impl - public void setNbt(NbtMap nbt) { + public void setNbt(@Nullable NbtMap nbt) { this.nbt = nbt; } @@ -145,7 +145,7 @@ public void setStackNetworkId(int newStackNetworkId) { @Override @Impl public ItemStack copy(boolean newStackNetworkId) { - return itemType.createItemStack(new ItemStackInitInfo.Simple(count, damage, nbt, blockState, stackNetworkId, newStackNetworkId)); + return itemType.createItemStack(new ItemStackInitInfo.Simple<>(count, damage, nbt, blockState, stackNetworkId, newStackNetworkId)); } @Override diff --git a/Allay-API/src/main/java/cn/allay/api/item/type/ItemStackInitInfo.java b/Allay-API/src/main/java/cn/allay/api/item/type/ItemStackInitInfo.java index c38c5e65b..d33de8cec 100644 --- a/Allay-API/src/main/java/cn/allay/api/item/type/ItemStackInitInfo.java +++ b/Allay-API/src/main/java/cn/allay/api/item/type/ItemStackInitInfo.java @@ -45,7 +45,7 @@ final class Simple implements ItemStackInitInfo { @Setter private ItemType itemType; - public Simple(int count, int damage, NbtMap nbt, BlockState blockState, Integer stackNetworkId, boolean autoAssignStackNetworkId) { + public Simple(int count, int damage, @Nullable NbtMap nbt, @Nullable BlockState blockState, @Nullable Integer stackNetworkId, boolean autoAssignStackNetworkId) { this.count = count; this.damage = damage; this.nbt = nbt; diff --git a/Allay-API/src/main/java/cn/allay/api/world/entity/EntityPhysicsService.java b/Allay-API/src/main/java/cn/allay/api/world/entity/EntityPhysicsService.java index 348a9a79f..f3b2d124c 100644 --- a/Allay-API/src/main/java/cn/allay/api/world/entity/EntityPhysicsService.java +++ b/Allay-API/src/main/java/cn/allay/api/world/entity/EntityPhysicsService.java @@ -1,6 +1,8 @@ package cn.allay.api.world.entity; import cn.allay.api.entity.Entity; +import cn.allay.api.math.Location3dc; +import org.joml.Vector3dc; import org.joml.primitives.AABBdc; import java.util.Collections; @@ -23,6 +25,8 @@ public interface EntityPhysicsService { void containEntity(Entity entity); + void offerNewLocation(Entity entity, Location3dc location); + default List getCollidingEntities(Entity entity) { if (entity.hasCollision()) return getCollidingEntities(entity.getAABB()); @@ -30,6 +34,4 @@ default List getCollidingEntities(Entity entity) { } List getCollidingEntities(AABBdc aabb); - - } diff --git a/Allay-API/src/main/java/cn/allay/api/world/palette/Palette.java b/Allay-API/src/main/java/cn/allay/api/world/palette/Palette.java index db58f9b9a..784cdad56 100644 --- a/Allay-API/src/main/java/cn/allay/api/world/palette/Palette.java +++ b/Allay-API/src/main/java/cn/allay/api/world/palette/Palette.java @@ -85,9 +85,9 @@ public void readFromNetwork(ByteBuf byteBuf, RuntimeDataDeserializer deserial } public void writeToStoragePersistent(ByteBuf byteBuf, PersistentDataSerializer serializer) { - byteBuf.writeByte(Palette.getPaletteHeader(this.bitArray.getVersion(), false)); + byteBuf.writeByte(Palette.getPaletteHeader(this.bitArray.version(), false)); - for (int word : this.bitArray.getWords()) byteBuf.writeIntLE(word); + for (int word : this.bitArray.words()) byteBuf.writeIntLE(word); byteBuf.writeIntLE(this.palette.size()); @@ -103,8 +103,8 @@ public void writeToStorageRuntime(ByteBuf byteBuf, RuntimeDataSerializer seri if (writeLast(byteBuf, last)) return; if (writeEmpty(byteBuf, serializer)) return; - byteBuf.writeByte(Palette.getPaletteHeader(this.bitArray.getVersion(), true)); - for (int word : this.bitArray.getWords()) byteBuf.writeIntLE(word); + byteBuf.writeByte(Palette.getPaletteHeader(this.bitArray.version(), true)); + for (int word : this.bitArray.words()) byteBuf.writeIntLE(word); byteBuf.writeIntLE(this.palette.size()); for (V value : this.palette) byteBuf.writeIntLE(serializer.serialize(value)); } @@ -175,7 +175,7 @@ public int paletteIndexFor(V value) { index = this.palette.size(); this.palette.add(value); - final BitArrayVersion version = this.bitArray.getVersion(); + final BitArrayVersion version = this.bitArray.version(); if (index > version.maxEntryValue) { final BitArrayVersion next = version.next; if (next != null) this.onResize(next); @@ -185,11 +185,12 @@ public int paletteIndexFor(V value) { } public boolean isEmpty() { - boolean result = true; - if (this.palette.size() > 1) result = false; - for (int word : this.bitArray.getWords()) - if (Integer.toUnsignedLong(word) != 0L) + boolean result = this.palette.size() <= 1; + for (int word : this.bitArray.words()) + if (Integer.toUnsignedLong(word) != 0L) { result = false; + break; + } return result; } @@ -217,9 +218,9 @@ private boolean writeLast(ByteBuf byteBuf, Palette last) { } private void writeWords(ByteBuf byteBuf, RuntimeDataSerializer serializer) { - byteBuf.writeByte(getPaletteHeader(this.bitArray.getVersion(), true)); + byteBuf.writeByte(getPaletteHeader(this.bitArray.version(), true)); - for (int word : this.bitArray.getWords()) byteBuf.writeIntLE(word); + for (int word : this.bitArray.words()) byteBuf.writeIntLE(word); this.bitArray.writeSizeToNetwork(byteBuf, this.palette.size()); for (V value : this.palette) VarInts.writeInt(byteBuf, serializer.serialize(value)); diff --git a/Allay-API/src/main/java/cn/allay/api/world/palette/bitarray/BitArray.java b/Allay-API/src/main/java/cn/allay/api/world/palette/bitarray/BitArray.java index 8ca1df59b..8a3b2ccd8 100644 --- a/Allay-API/src/main/java/cn/allay/api/world/palette/bitarray/BitArray.java +++ b/Allay-API/src/main/java/cn/allay/api/world/palette/bitarray/BitArray.java @@ -22,11 +22,11 @@ default int readSizeFromNetwork(ByteBuf buffer) { return VarInts.readInt(buffer); } - int getSize(); + int size(); - int[] getWords(); + int[] words(); - BitArrayVersion getVersion(); + BitArrayVersion version(); BitArray copy(); diff --git a/Allay-API/src/main/java/cn/allay/api/world/palette/bitarray/PaddedBitArray.java b/Allay-API/src/main/java/cn/allay/api/world/palette/bitarray/PaddedBitArray.java index 8cf976546..a74d04972 100644 --- a/Allay-API/src/main/java/cn/allay/api/world/palette/bitarray/PaddedBitArray.java +++ b/Allay-API/src/main/java/cn/allay/api/world/palette/bitarray/PaddedBitArray.java @@ -11,11 +11,7 @@ * @author JukeboxMC | daoge_cmd */ @Getter -public final class PaddedBitArray implements BitArray { - - private final int[] words; - private final BitArrayVersion version; - private final int size; +public record PaddedBitArray(BitArrayVersion version, int size, int[] words) implements BitArray { public PaddedBitArray(BitArrayVersion version, int size, int[] words) { this.size = size; diff --git a/Allay-API/src/main/java/cn/allay/api/world/palette/bitarray/Pow2BitArray.java b/Allay-API/src/main/java/cn/allay/api/world/palette/bitarray/Pow2BitArray.java index 590eb61ec..7832f4bc9 100644 --- a/Allay-API/src/main/java/cn/allay/api/world/palette/bitarray/Pow2BitArray.java +++ b/Allay-API/src/main/java/cn/allay/api/world/palette/bitarray/Pow2BitArray.java @@ -11,11 +11,7 @@ * @author JukeboxMC | daoge_cmd */ @Getter -public final class Pow2BitArray implements BitArray { - - private final int[] words; - private final BitArrayVersion version; - private final int size; +public record Pow2BitArray(BitArrayVersion version, int size, int[] words) implements BitArray { public Pow2BitArray(BitArrayVersion version, int size, int[] words) { this.size = size; diff --git a/Allay-API/src/main/java/cn/allay/api/world/palette/bitarray/SingletonBitArray.java b/Allay-API/src/main/java/cn/allay/api/world/palette/bitarray/SingletonBitArray.java index 45cc7dcae..d46eb36b2 100644 --- a/Allay-API/src/main/java/cn/allay/api/world/palette/bitarray/SingletonBitArray.java +++ b/Allay-API/src/main/java/cn/allay/api/world/palette/bitarray/SingletonBitArray.java @@ -25,17 +25,17 @@ public void writeSizeToNetwork(ByteBuf buffer, int size) { } @Override - public int getSize() { + public int size() { return 1; } @Override - public int[] getWords() { + public int[] words() { return EMPTY_ARRAY; } @Override - public BitArrayVersion getVersion() { + public BitArrayVersion version() { return BitArrayVersion.V0; } diff --git a/Allay-CodeGen/src/main/java/cn/allay/codegen/VanillaBlockBehaviorInterfaceGen.java b/Allay-CodeGen/src/main/java/cn/allay/codegen/VanillaBlockBehaviorInterfaceGen.java index 5b8ee7a45..c65ab8a6d 100644 --- a/Allay-CodeGen/src/main/java/cn/allay/codegen/VanillaBlockBehaviorInterfaceGen.java +++ b/Allay-CodeGen/src/main/java/cn/allay/codegen/VanillaBlockBehaviorInterfaceGen.java @@ -78,7 +78,7 @@ private static void generateBlockType(VanillaBlockId vanillaBlockId, String clas .add(" .vanillaBlock($T.$N, true)\n", VANILLA_BLOCK_ID_CLASS_NAME, vanillaBlockId.name()); var blockPaletteData = MAPPED_BLOCK_PALETTE_NBT.get(vanillaBlockId.getIdentifier().toString()); var states = blockPaletteData.getCompound("states"); - if (states.size() != 0) { + if (!states.isEmpty()) { initializer.add(" .setProperties("); AtomicInteger count = new AtomicInteger(); states.forEach((name, value) -> { diff --git a/Allay-CodeGen/src/main/java/cn/allay/codegen/VanillaItemClassGen.java b/Allay-CodeGen/src/main/java/cn/allay/codegen/VanillaItemClassGen.java index 039a948d8..7678889ea 100644 --- a/Allay-CodeGen/src/main/java/cn/allay/codegen/VanillaItemClassGen.java +++ b/Allay-CodeGen/src/main/java/cn/allay/codegen/VanillaItemClassGen.java @@ -31,9 +31,7 @@ public static void main(String[] args) { static { try { NbtMap reader = (NbtMap) NbtUtils.createGZIPReader(Files.newInputStream(ITEM_DATA_FILE_PATH)).readTag(); - reader.getList("item", NbtType.COMPOUND).forEach(item -> { - MAPPED_ITEM_DATA.put(item.getString("name"), item); - }); + reader.getList("item", NbtType.COMPOUND).forEach(item -> MAPPED_ITEM_DATA.put(item.getString("name"), item)); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/Allay-Server/src/jmh/java/cn/allay/server/ThroughList2ArrayJMHTest.java b/Allay-Server/src/jmh/java/cn/allay/server/ThroughList2ArrayJMHTest.java index 91b6bad60..797035aa9 100644 --- a/Allay-Server/src/jmh/java/cn/allay/server/ThroughList2ArrayJMHTest.java +++ b/Allay-Server/src/jmh/java/cn/allay/server/ThroughList2ArrayJMHTest.java @@ -49,15 +49,15 @@ public void listForEach(Blackhole blackhole) { @Benchmark public void arrayForI(Blackhole blackhole) { - for (int i = 0; i < data1.length; i++) { - blackhole.consume(data1[i]); + for (BlockPropertyType.BlockPropertyValue propertyValue : data1) { + blackhole.consume(propertyValue); } } @Benchmark public void listForI(Blackhole blackhole) { - for (int i = 0; i < data2.size(); i++) { - blackhole.consume(data2.get(i)); + for (BlockPropertyType.BlockPropertyValue propertyValue : data2) { + blackhole.consume(propertyValue); } } } diff --git a/Allay-Server/src/main/java/cn/allay/server/client/AllayClient.java b/Allay-Server/src/main/java/cn/allay/server/client/AllayClient.java index 2f0e684e7..f3c048648 100644 --- a/Allay-Server/src/main/java/cn/allay/server/client/AllayClient.java +++ b/Allay-Server/src/main/java/cn/allay/server/client/AllayClient.java @@ -9,14 +9,13 @@ import cn.allay.api.data.VanillaEntityTypes; import cn.allay.api.entity.attribute.Attribute; import cn.allay.api.entity.impl.EntityPlayer; -import cn.allay.api.entity.type.EntityInitInfo; import cn.allay.api.entity.type.EntityTypeRegistry; import cn.allay.api.item.type.CreativeItemRegistry; import cn.allay.api.item.type.ItemTypeRegistry; import cn.allay.api.math.Location3d; import cn.allay.api.math.Location3dc; import cn.allay.api.math.Position3ic; -import cn.allay.api.client.AdventureSettings; +import cn.allay.api.client.data.AdventureSettings; import cn.allay.api.client.data.LoginData; import cn.allay.api.server.Server; import cn.allay.api.world.biome.BiomeTypeRegistry; @@ -43,7 +42,6 @@ import org.cloudburstmc.protocol.common.util.OptionalBoolean; import javax.annotation.Nullable; -import javax.crypto.SecretKey; import java.util.LinkedList; import java.util.List; import java.util.Set; diff --git a/Allay-Server/src/main/java/cn/allay/server/item/type/AllayCreativeItemRegistry.java b/Allay-Server/src/main/java/cn/allay/server/item/type/AllayCreativeItemRegistry.java index 8b261e305..0465ca76d 100644 --- a/Allay-Server/src/main/java/cn/allay/server/item/type/AllayCreativeItemRegistry.java +++ b/Allay-Server/src/main/java/cn/allay/server/item/type/AllayCreativeItemRegistry.java @@ -74,7 +74,7 @@ public Map load(Void input) { var blockStateHash = obj.getInt("blockStateHash"); var blockState = BlockStateHashPalette.getRegistry().get(blockStateHash); var tag = obj.getCompound("tag", null); - var itemStack = itemType.createItemStack(new ItemStackInitInfo.Simple(count, damage, tag, blockState, index + 1, false)); + var itemStack = itemType.createItemStack(new ItemStackInitInfo.Simple<>(count, damage, tag, blockState, index + 1, false)); map.put(index, itemStack); }); log.info("Loaded creative item registry successfully"); diff --git a/Allay-Server/src/main/java/cn/allay/server/utils/GameLoop.java b/Allay-Server/src/main/java/cn/allay/server/utils/GameLoop.java index 31f8efca3..11dbbb6e7 100644 --- a/Allay-Server/src/main/java/cn/allay/server/utils/GameLoop.java +++ b/Allay-Server/src/main/java/cn/allay/server/utils/GameLoop.java @@ -1,6 +1,7 @@ package cn.allay.server.utils; import lombok.Getter; +import lombok.extern.slf4j.Slf4j; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -11,6 +12,7 @@ * * @author daoge_cmd */ +@Slf4j public final class GameLoop { private final AtomicBoolean isRunning = new AtomicBoolean(true); @@ -58,7 +60,7 @@ public void startLoop() { try { Thread.sleep(Math.max(TimeUnit.NANOSECONDS.toMillis(nanoSleepTime), 0)); } catch (InterruptedException exception) { - exception.printStackTrace(); + log.error("GameLoop interrupted", exception); onStop.run(); return; } diff --git a/Allay-Server/src/main/java/cn/allay/server/world/AllayWorld.java b/Allay-Server/src/main/java/cn/allay/server/world/AllayWorld.java index 9a264bb3a..7275d76e5 100644 --- a/Allay-Server/src/main/java/cn/allay/server/world/AllayWorld.java +++ b/Allay-Server/src/main/java/cn/allay/server/world/AllayWorld.java @@ -79,12 +79,10 @@ public void startTick() { } worldMainThread = Thread.ofPlatform() .name("Allay World Main Thread") - .start(() -> { - GameLoop.builder() - .onTick(gameLoop -> tick()) - .build() - .startLoop(); - }); + .start(() -> GameLoop.builder() + .onTick(gameLoop -> tick()) + .build() + .startLoop()); } @Override diff --git a/Allay-Server/src/main/java/cn/allay/server/world/chunk/HeightMap.java b/Allay-Server/src/main/java/cn/allay/server/world/chunk/HeightMap.java index ba2829eb1..538d15879 100644 --- a/Allay-Server/src/main/java/cn/allay/server/world/chunk/HeightMap.java +++ b/Allay-Server/src/main/java/cn/allay/server/world/chunk/HeightMap.java @@ -38,7 +38,7 @@ public static int[] unpack(long[] longs, int lengthInBits) { int intPerLong = (int) Math.floor(64.0 / lengthInBits); int intCount = (int) Math.ceil((double) longs.length * intPerLong); int[] ints = new int[intCount]; - int intPerLongCeil = (int) Math.ceil(intPerLong); + int intPerLongCeil = (int) (double) intPerLong; long mask = (1L << lengthInBits) - 1L; for (int i = 0; i < ints.length; i++) { int longIndex = i / intPerLongCeil; diff --git a/Allay-Server/src/main/java/cn/allay/server/world/entity/AllayEntityPhysicsService.java b/Allay-Server/src/main/java/cn/allay/server/world/entity/AllayEntityPhysicsService.java new file mode 100644 index 000000000..e10940ea4 --- /dev/null +++ b/Allay-Server/src/main/java/cn/allay/server/world/entity/AllayEntityPhysicsService.java @@ -0,0 +1,11 @@ +package cn.allay.server.world.entity; + +import cn.allay.api.world.entity.EntityPhysicsService; + +/** + * Allay Project 2023/8/5 + * + * @author daoge_cmd + */ +public class AllayEntityPhysicsService implements EntityPhysicsService { +} diff --git a/Allay-Server/src/main/java/cn/allay/server/world/storage/anvil/AnvilRegionFile.java b/Allay-Server/src/main/java/cn/allay/server/world/storage/anvil/AnvilRegionFile.java index 214352947..f11e1d080 100644 --- a/Allay-Server/src/main/java/cn/allay/server/world/storage/anvil/AnvilRegionFile.java +++ b/Allay-Server/src/main/java/cn/allay/server/world/storage/anvil/AnvilRegionFile.java @@ -119,16 +119,13 @@ public synchronized NbtMap readChunkData(int chunkX, int chunkZ) throws IOExcept byte[] input = new byte[chunk.readableBytes()]; byte[] output; chunk.readBytes(input); - switch (compressionType) { - case GZIP_COMPRESSION: - output = ZlibProviderType.LibDeflateThreadLocal.of(CompressionType.GZIP, 6).inflate(input, CHUNK_SIZE_LIMIT); - break; - case ZLIB_COMPRESSION: - output = ZlibProviderType.LibDeflateThreadLocal.of(CompressionType.ZLIB, 6).inflate(input, CHUNK_SIZE_LIMIT); - break; - default: - throw new IllegalArgumentException("Unknown compression type: " + compressionType); - } + output = switch (compressionType) { + case GZIP_COMPRESSION -> + ZlibProviderType.LibDeflateThreadLocal.of(CompressionType.GZIP, 6).inflate(input, CHUNK_SIZE_LIMIT); + case ZLIB_COMPRESSION -> + ZlibProviderType.LibDeflateThreadLocal.of(CompressionType.ZLIB, 6).inflate(input, CHUNK_SIZE_LIMIT); + default -> throw new IllegalArgumentException("Unknown compression type: " + compressionType); + }; NBTInputStream reader = NbtUtils.createReader(new BufferedInputStream(new ByteArrayInputStream(output))); return (NbtMap) reader.readTag(); }