From 4420e4ceae86677471473c063bffbab9df7be40a Mon Sep 17 00:00:00 2001 From: XiaoMoMi Date: Fri, 26 Dec 2025 04:58:18 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E4=BD=BF=E7=94=A8adaptor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bukkit/api/BukkitAdaptors.java | 4 +++- .../bukkit/api/CraftEngineBlocks.java | 2 +- .../bukkit/api/CraftEngineFurniture.java | 5 ++-- .../bukkit/block/BlockEventListener.java | 6 ++--- .../AbstractCanSurviveBlockBehavior.java | 3 ++- .../block/behavior/CropBlockBehavior.java | 3 ++- .../block/behavior/DoorBlockBehavior.java | 5 ++-- .../behavior/DoubleHighBlockBehavior.java | 2 +- .../block/behavior/FallingBlockBehavior.java | 4 ++-- .../behavior/FenceGateBlockBehavior.java | 5 ++-- .../block/behavior/TrapDoorBlockBehavior.java | 5 ++-- .../bukkit/entity/BukkitEntity.java | 3 ++- .../bukkit/loot/BukkitVanillaLootManager.java | 2 +- .../plugin/injector/BlockStateGenerator.java | 3 ++- .../plugin/user/BukkitServerPlayer.java | 3 ++- .../bukkit/util/InteractUtils.java | 3 ++- .../bukkit/util/LocationUtils.java | 3 ++- .../bukkit/world/BukkitExistingBlock.java | 3 ++- .../craftengine/bukkit/world/BukkitWorld.java | 23 ++++++++++++++++--- .../bukkit/world/BukkitWorldManager.java | 15 ++++++------ core/build.gradle.kts | 2 +- .../hitbox/FurnitureHitBoxTypes.java | 1 - .../craftengine/core/util/RandomUtils.java | 4 ++-- .../craftengine/core/world/WorldPosition.java | 9 ++++++++ 24 files changed, 79 insertions(+), 39 deletions(-) diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/BukkitAdaptors.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/BukkitAdaptors.java index 849222866..2d3df3f32 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/BukkitAdaptors.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/BukkitAdaptors.java @@ -6,7 +6,9 @@ import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine; import net.momirealms.craftengine.bukkit.plugin.user.BukkitServerPlayer; import net.momirealms.craftengine.bukkit.world.BukkitExistingBlock; import net.momirealms.craftengine.bukkit.world.BukkitWorld; +import net.momirealms.craftengine.bukkit.world.BukkitWorldManager; import net.momirealms.craftengine.core.item.Item; +import net.momirealms.craftengine.core.world.CEWorld; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.entity.Entity; @@ -36,7 +38,7 @@ public final class BukkitAdaptors { * This enables CraftEngine world operations on Bukkit world instances. * * @param world the Bukkit World to adapt, must not be null - * @return a non-null BukkitWorld instance wrapping the provided world + * @return the BukkitWorld instance wrapping the provided world */ @NotNull public static BukkitWorld adapt(@NotNull final World world) { diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/CraftEngineBlocks.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/CraftEngineBlocks.java index d769ab88f..8eb56a10e 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/CraftEngineBlocks.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/CraftEngineBlocks.java @@ -198,7 +198,7 @@ public final class CraftEngineBlocks { boolean sendLevelEvent) { ImmutableBlockState state = getCustomBlockState(block); if (state == null || state.isEmpty()) return false; - World world = new BukkitWorld(block.getWorld()); + World world = BukkitAdaptors.adapt(block.getWorld()); Location location = block.getLocation(); WorldPosition position = new WorldPosition(world, location.getBlockX() + 0.5, location.getBlockY() + 0.5, location.getBlockZ() + 0.5); if (dropLoot) { diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/CraftEngineFurniture.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/CraftEngineFurniture.java index 95b86d527..f30ce18e7 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/CraftEngineFurniture.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/CraftEngineFurniture.java @@ -7,7 +7,6 @@ import net.momirealms.craftengine.bukkit.nms.CollisionEntity; import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine; import net.momirealms.craftengine.bukkit.plugin.user.BukkitServerPlayer; -import net.momirealms.craftengine.bukkit.world.BukkitWorld; import net.momirealms.craftengine.core.entity.furniture.AnchorType; import net.momirealms.craftengine.core.entity.furniture.CustomFurniture; import net.momirealms.craftengine.core.entity.furniture.Furniture; @@ -73,6 +72,7 @@ public final class CraftEngineFurniture { @Nullable public static BukkitFurniture rayTrace(Player player, double maxDistance) { BukkitServerPlayer serverPlayer = BukkitAdaptors.adapt(player); + if (serverPlayer == null) return null; Location eyeLocation = serverPlayer.getEyeLocation(); RayTraceResult result = player.getWorld().rayTrace(eyeLocation, eyeLocation.getDirection(), maxDistance, FluidCollisionMode.NEVER, true, 0d, CraftEngineFurniture::isCollisionEntity); if (result == null) @@ -92,6 +92,7 @@ public final class CraftEngineFurniture { @Nullable public static BukkitFurniture rayTrace(Player player) { BukkitServerPlayer serverPlayer = BukkitAdaptors.adapt(player); + if (serverPlayer == null) return null; Location eyeLocation = serverPlayer.getEyeLocation(); RayTraceResult result = player.getWorld().rayTrace(eyeLocation, eyeLocation.getDirection(), serverPlayer.getCachedInteractionRange(), FluidCollisionMode.NEVER, true, 0d, CraftEngineFurniture::isCollisionEntity); if (result == null) @@ -433,7 +434,7 @@ public final class CraftEngineFurniture { Location location = ((BukkitFurniture) furniture).getDropLocation(); furniture.destroy(); LootTable lootTable = (LootTable) furniture.config.lootTable(); - World world = new BukkitWorld(location.getWorld()); + World world = BukkitAdaptors.adapt(location.getWorld()); WorldPosition position = new WorldPosition(world, location.getX(), location.getY(), location.getZ()); if (dropLoot && lootTable != null) { ContextHolder.Builder builder = ContextHolder.builder() diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BlockEventListener.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BlockEventListener.java index 575ad2041..0853b586f 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BlockEventListener.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/BlockEventListener.java @@ -107,7 +107,7 @@ public final class BlockEventListener implements Listener { Player player = event.getPlayer(); Location location = block.getLocation(); BukkitServerPlayer serverPlayer = BukkitAdaptors.adapt(player); - net.momirealms.craftengine.core.world.World world = new BukkitWorld(player.getWorld()); + net.momirealms.craftengine.core.world.World world = BukkitAdaptors.adapt(player.getWorld()); WorldPosition position = new WorldPosition(world, location.getBlockX() + 0.5, location.getBlockY() + 0.5, location.getBlockZ() + 0.5); Item itemInHand = serverPlayer.getItemInHand(InteractionHand.MAIN_HAND); @@ -224,7 +224,7 @@ public final class BlockEventListener implements Listener { event.setExpToDrop(0); } Location location = block.getLocation(); - net.momirealms.craftengine.core.world.World world = new BukkitWorld(location.getWorld()); + net.momirealms.craftengine.core.world.World world = BukkitAdaptors.adapt(location.getWorld()); WorldPosition position = new WorldPosition(world, location.getBlockX() + 0.5, location.getBlockY() + 0.5, location.getBlockZ() + 0.5); ContextHolder.Builder builder = ContextHolder.builder() .withParameter(DirectContextParameters.POSITION, position) @@ -255,7 +255,7 @@ public final class BlockEventListener implements Listener { Cancellable cancellable = Cancellable.of(event::isCancelled, event::setCancelled); state.owner().value().execute(PlayerOptionalContext.of(BukkitAdaptors.adapt(player), ContextHolder.builder() .withParameter(DirectContextParameters.EVENT, cancellable) - .withParameter(DirectContextParameters.POSITION, new WorldPosition(new BukkitWorld(event.getWorld()), LocationUtils.toVec3d(location))) + .withParameter(DirectContextParameters.POSITION, new WorldPosition(BukkitAdaptors.adapt(event.getWorld()), LocationUtils.toVec3d(location))) .withParameter(DirectContextParameters.BLOCK, new BukkitExistingBlock(block)) .withParameter(DirectContextParameters.CUSTOM_BLOCK_STATE, state) ), EventTrigger.STEP); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/AbstractCanSurviveBlockBehavior.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/AbstractCanSurviveBlockBehavior.java index 6e0bb2680..5bec5aff5 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/AbstractCanSurviveBlockBehavior.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/AbstractCanSurviveBlockBehavior.java @@ -1,5 +1,6 @@ package net.momirealms.craftengine.bukkit.block.behavior; +import net.momirealms.craftengine.bukkit.api.BukkitAdaptors; import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBlocks; import net.momirealms.craftengine.bukkit.util.BlockStateUtils; @@ -31,7 +32,7 @@ public abstract class AbstractCanSurviveBlockBehavior extends BukkitBlockBehavio if (!canSurvive(thisBlock, args, () -> true)) { BlockStateUtils.getOptionalCustomBlockState(blockState).ifPresent(customState -> { if (!customState.isEmpty() && customState.owner().value() == this.customBlock) { - net.momirealms.craftengine.core.world.World world = new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(level)); + net.momirealms.craftengine.core.world.World world = BukkitAdaptors.adapt(FastNMS.INSTANCE.method$Level$getCraftWorld(level)); WorldPosition position = new WorldPosition(world, Vec3d.atCenterOf(LocationUtils.fromBlockPos(blockPos))); world.playBlockSound(position, customState.settings().sounds().breakSound()); FastNMS.INSTANCE.method$LevelWriter$destroyBlock(level, blockPos, true); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/CropBlockBehavior.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/CropBlockBehavior.java index 066a13178..656c3f6d8 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/CropBlockBehavior.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/CropBlockBehavior.java @@ -1,5 +1,6 @@ package net.momirealms.craftengine.bukkit.block.behavior; +import net.momirealms.craftengine.bukkit.api.BukkitAdaptors; import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections; import net.momirealms.craftengine.bukkit.util.BlockStateUtils; @@ -175,7 +176,7 @@ public class CropBlockBehavior extends BukkitBlockBehavior { int i = this.getAge(customState) + this.boneMealBonus.getInt( SimpleContext.of(ContextHolder.builder() .withParameter(DirectContextParameters.CUSTOM_BLOCK_STATE, customState) - .withParameter(DirectContextParameters.POSITION, new WorldPosition(new BukkitWorld(world), Vec3d.atCenterOf(new Vec3i(x, y, z)))) + .withParameter(DirectContextParameters.POSITION, new WorldPosition(BukkitAdaptors.adapt(world), Vec3d.atCenterOf(new Vec3i(x, y, z)))) .build()) ); int maxAge = this.ageProperty.max; diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/DoorBlockBehavior.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/DoorBlockBehavior.java index b0dcadff4..eead27298 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/DoorBlockBehavior.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/DoorBlockBehavior.java @@ -1,5 +1,6 @@ package net.momirealms.craftengine.bukkit.block.behavior; +import net.momirealms.craftengine.bukkit.api.BukkitAdaptors; import net.momirealms.craftengine.bukkit.block.BukkitBlockManager; import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine; @@ -261,7 +262,7 @@ public class DoorBlockBehavior extends AbstractCanSurviveBlockBehavior implement world.sendGameEvent(player == null ? null : (org.bukkit.entity.Player) player.platformPlayer(), isOpen ? GameEvent.BLOCK_OPEN : GameEvent.BLOCK_CLOSE, new Vector(pos.x(), pos.y(), pos.z())); SoundData soundData = isOpen ? this.openSound : this.closeSound; if (soundData != null) { - new BukkitWorld(world).playBlockSound( + BukkitAdaptors.adapt(world).playBlockSound( new Vec3d(pos.x() + 0.5, pos.y() + 0.5, pos.z() + 0.5), soundData ); @@ -313,7 +314,7 @@ public class DoorBlockBehavior extends AbstractCanSurviveBlockBehavior implement world.sendGameEvent(null, flag ? GameEvent.BLOCK_OPEN : GameEvent.BLOCK_CLOSE, new Vector(bukkitBlock.getX(), bukkitBlock.getY(), bukkitBlock.getZ())); SoundData soundData = flag ? this.openSound : this.closeSound; if (soundData != null) { - new BukkitWorld(world).playBlockSound( + BukkitAdaptors.adapt(world).playBlockSound( new Vec3d(FastNMS.INSTANCE.field$Vec3i$x(blockPos) + 0.5, FastNMS.INSTANCE.field$Vec3i$y(blockPos) + 0.5, FastNMS.INSTANCE.field$Vec3i$z(blockPos) + 0.5), soundData ); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/DoubleHighBlockBehavior.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/DoubleHighBlockBehavior.java index a384ceb20..069e156f0 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/DoubleHighBlockBehavior.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/DoubleHighBlockBehavior.java @@ -56,7 +56,7 @@ public class DoubleHighBlockBehavior extends AbstractCanSurviveBlockBehavior { return MBlocks.AIR$defaultState; } else if (half == DoubleBlockHalf.LOWER && direction == CoreReflections.instance$Direction$DOWN && !canSurvive(thisBlock, blockState, level, blockPos)) { BlockPos pos = LocationUtils.fromBlockPos(blockPos); - World world = new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(level)); + World world = BukkitAdaptors.adapt(FastNMS.INSTANCE.method$Level$getCraftWorld(level)); WorldPosition position = new WorldPosition(world, Vec3d.atCenterOf(pos)); world.playBlockSound(position, customState.settings().sounds().breakSound()); FastNMS.INSTANCE.method$LevelAccessor$levelEvent(level, WorldEvents.BLOCK_BREAK_EFFECT, blockPos, customState.customBlockState().registryId()); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/FallingBlockBehavior.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/FallingBlockBehavior.java index a184c7369..83b4297fc 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/FallingBlockBehavior.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/FallingBlockBehavior.java @@ -87,7 +87,7 @@ public class FallingBlockBehavior extends BukkitBlockBehavior { Object blockState = CoreReflections.field$FallingBlockEntity$blockState.get(fallingBlockEntity); Optional optionalCustomState = BlockStateUtils.getOptionalCustomBlockState(blockState); if (optionalCustomState.isEmpty()) return; - net.momirealms.craftengine.core.world.World world = new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(level)); + net.momirealms.craftengine.core.world.World world = BukkitAdaptors.adapt(FastNMS.INSTANCE.method$Level$getCraftWorld(level)); WorldPosition position = new WorldPosition(world, CoreReflections.field$Entity$xo.getDouble(fallingBlockEntity), CoreReflections.field$Entity$yo.getDouble(fallingBlockEntity), CoreReflections.field$Entity$zo.getDouble(fallingBlockEntity)); if (this.destroySound != null) { world.playBlockSound(position, this.destroySound); @@ -106,7 +106,7 @@ public class FallingBlockBehavior extends BukkitBlockBehavior { ImmutableBlockState immutableBlockState = BukkitBlockManager.instance().getImmutableBlockState(stateId); if (immutableBlockState == null || immutableBlockState.isEmpty()) return; if (!entity.getEntityData(BaseEntityData.Silent)) { - net.momirealms.craftengine.core.world.World world = new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(level)); + net.momirealms.craftengine.core.world.World world = BukkitAdaptors.adapt(FastNMS.INSTANCE.method$Level$getCraftWorld(level)); if (this.landSound != null) { world.playBlockSound(Vec3d.atCenterOf(LocationUtils.fromBlockPos(pos)), this.landSound); } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/FenceGateBlockBehavior.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/FenceGateBlockBehavior.java index 697b84624..3b7d39e84 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/FenceGateBlockBehavior.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/FenceGateBlockBehavior.java @@ -1,5 +1,6 @@ package net.momirealms.craftengine.bukkit.block.behavior; +import net.momirealms.craftengine.bukkit.api.BukkitAdaptors; import net.momirealms.craftengine.bukkit.item.BukkitItemManager; import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections; @@ -169,7 +170,7 @@ public class FenceGateBlockBehavior extends BukkitBlockBehavior implements IsPat if (this.canOpenByWindCharge && FastNMS.INSTANCE.method$Explosion$canTriggerBlocks(args[3])) { Optional optionalCustomState = BlockStateUtils.getOptionalCustomBlockState(args[0]); if (optionalCustomState.isEmpty()) return; - this.toggle(optionalCustomState.get(), new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(args[1])), LocationUtils.fromBlockPos(args[2]), null); + this.toggle(optionalCustomState.get(), BukkitAdaptors.adapt(FastNMS.INSTANCE.method$Level$getCraftWorld(args[1])), LocationUtils.fromBlockPos(args[2]), null); } } @@ -196,7 +197,7 @@ public class FenceGateBlockBehavior extends BukkitBlockBehavior implements IsPat hasSignal = event.getNewCurrent() > 0; } - World world = new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(level)); + World world = BukkitAdaptors.adapt(FastNMS.INSTANCE.method$Level$getCraftWorld(level)); boolean changed = isOpen(customState) != hasSignal; if (hasSignal && changed) { Object abovePos = LocationUtils.above(blockPos); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/TrapDoorBlockBehavior.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/TrapDoorBlockBehavior.java index 4339bf882..6d9c8b4b0 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/TrapDoorBlockBehavior.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/behavior/TrapDoorBlockBehavior.java @@ -1,5 +1,6 @@ package net.momirealms.craftengine.bukkit.block.behavior; +import net.momirealms.craftengine.bukkit.api.BukkitAdaptors; import net.momirealms.craftengine.bukkit.item.BukkitItemManager; import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections; @@ -144,7 +145,7 @@ public class TrapDoorBlockBehavior extends BukkitBlockBehavior implements IsPath if (this.canOpenByWindCharge && FastNMS.INSTANCE.method$Explosion$canTriggerBlocks(args[3])) { Optional optionalCustomState = BlockStateUtils.getOptionalCustomBlockState(args[0]); if (optionalCustomState.isEmpty()) return; - this.toggle(optionalCustomState.get(), new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(args[1])), LocationUtils.fromBlockPos(args[2]), null); + this.toggle(optionalCustomState.get(), BukkitAdaptors.adapt(FastNMS.INSTANCE.method$Level$getCraftWorld(args[1])), LocationUtils.fromBlockPos(args[2]), null); } } @@ -171,7 +172,7 @@ public class TrapDoorBlockBehavior extends BukkitBlockBehavior implements IsPath hasSignal = event.getNewCurrent() > 0; } - World world = new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(level)); + World world = BukkitAdaptors.adapt(FastNMS.INSTANCE.method$Level$getCraftWorld(level)); boolean changed = customState.get(this.openProperty) != hasSignal; if (hasSignal && changed) { Object abovePos = LocationUtils.above(blockPos); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/BukkitEntity.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/BukkitEntity.java index efda7a2c5..7059606c8 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/BukkitEntity.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/BukkitEntity.java @@ -1,5 +1,6 @@ package net.momirealms.craftengine.bukkit.entity; +import net.momirealms.craftengine.bukkit.api.BukkitAdaptors; import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.util.EntityUtils; import net.momirealms.craftengine.bukkit.util.LocationUtils; @@ -62,7 +63,7 @@ public class BukkitEntity extends AbstractEntity { @Override public World world() { - return new BukkitWorld(platformEntity().getWorld()); + return BukkitAdaptors.adapt(platformEntity().getWorld()); } @Override diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/loot/BukkitVanillaLootManager.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/loot/BukkitVanillaLootManager.java index 7836a0812..510659653 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/loot/BukkitVanillaLootManager.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/loot/BukkitVanillaLootManager.java @@ -66,7 +66,7 @@ public class BukkitVanillaLootManager extends AbstractVanillaLootManager impleme event.setDroppedExp(0); } Location location = entity.getLocation(); - net.momirealms.craftengine.core.world.World world = new BukkitWorld(entity.getWorld()); + net.momirealms.craftengine.core.world.World world = BukkitAdaptors.adapt(entity.getWorld()); WorldPosition position = new WorldPosition(world, location.getX(), location.getY(), location.getZ()); ContextHolder.Builder builder = ContextHolder.builder() .withParameter(DirectContextParameters.POSITION, position); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/injector/BlockStateGenerator.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/injector/BlockStateGenerator.java index 2c2e4d036..6b4057a02 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/injector/BlockStateGenerator.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/injector/BlockStateGenerator.java @@ -14,6 +14,7 @@ import net.bytebuddy.implementation.bind.annotation.AllArguments; import net.bytebuddy.implementation.bind.annotation.RuntimeType; import net.bytebuddy.implementation.bind.annotation.This; import net.bytebuddy.matcher.ElementMatchers; +import net.momirealms.craftengine.bukkit.api.BukkitAdaptors; import net.momirealms.craftengine.bukkit.item.BukkitItemManager; import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine; @@ -117,7 +118,7 @@ public final class BlockStateGenerator { } Object serverLevel = FastNMS.INSTANCE.method$LootParams$Builder$getLevel(builder); - World world = new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(serverLevel)); + World world = BukkitAdaptors.adapt(FastNMS.INSTANCE.method$Level$getCraftWorld(serverLevel)); ContextHolder.Builder lootBuilder = new ContextHolder.Builder() .withParameter(DirectContextParameters.POSITION, new WorldPosition(world, FastNMS.INSTANCE.field$Vec3$x(vec3), FastNMS.INSTANCE.field$Vec3$y(vec3), FastNMS.INSTANCE.field$Vec3$z(vec3))); if (!item.isEmpty()) { diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/user/BukkitServerPlayer.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/user/BukkitServerPlayer.java index f05b2ea0c..228478b8c 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/user/BukkitServerPlayer.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/user/BukkitServerPlayer.java @@ -6,6 +6,7 @@ import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelHandler; import net.kyori.adventure.text.Component; +import net.momirealms.craftengine.bukkit.api.BukkitAdaptors; import net.momirealms.craftengine.bukkit.api.CraftEngineBlocks; import net.momirealms.craftengine.bukkit.api.CraftEngineFurniture; import net.momirealms.craftengine.bukkit.block.entity.BlockEntityHolder; @@ -1093,7 +1094,7 @@ public class BukkitServerPlayer extends Player { @Override public World world() { - return new BukkitWorld(platformPlayer().getWorld()); + return BukkitAdaptors.adapt(platformPlayer().getWorld()); } @Override diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/InteractUtils.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/InteractUtils.java index ca2d08ad8..68bcce69a 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/InteractUtils.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/InteractUtils.java @@ -1,6 +1,7 @@ package net.momirealms.craftengine.bukkit.util; import io.papermc.paper.entity.Shearable; +import net.momirealms.craftengine.bukkit.api.BukkitAdaptors; import net.momirealms.craftengine.bukkit.item.BukkitItemManager; import net.momirealms.craftengine.bukkit.item.behavior.BlockItemBehavior; import net.momirealms.craftengine.bukkit.item.behavior.FlintAndSteelItemBehavior; @@ -195,7 +196,7 @@ public final class InteractUtils { && redstoneWire.getFace(BlockFace.WEST).equals(RedstoneWire.Connection.NONE); if (isCross || isDot) { BlockPos blockPos = result.getBlockPos(); - BukkitWorld bukkitWorld = new BukkitWorld(player.getWorld()); + BukkitWorld bukkitWorld = BukkitAdaptors.adapt(player.getWorld()); World world = bukkitWorld.platformWorld(); Direction[] directions = {Direction.EAST, Direction.WEST, Direction.SOUTH, Direction.NORTH}; diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/LocationUtils.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/LocationUtils.java index 83b564ac9..c1b662f16 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/LocationUtils.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/util/LocationUtils.java @@ -1,5 +1,6 @@ package net.momirealms.craftengine.bukkit.util; +import net.momirealms.craftengine.bukkit.api.BukkitAdaptors; import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.world.BukkitWorld; import net.momirealms.craftengine.core.world.BlockPos; @@ -18,7 +19,7 @@ public final class LocationUtils { } public static WorldPosition toWorldPosition(Location location) { - return new WorldPosition(new BukkitWorld(location.getWorld()), location.getX(), location.getY(), location.getZ(), location.getPitch(), location.getYaw()); + return new WorldPosition(BukkitAdaptors.adapt(location.getWorld()), location.getX(), location.getY(), location.getZ(), location.getPitch(), location.getYaw()); } public static Object toVec(Vec3d vec) { diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/world/BukkitExistingBlock.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/world/BukkitExistingBlock.java index efc3df2f7..509d57607 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/world/BukkitExistingBlock.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/world/BukkitExistingBlock.java @@ -1,5 +1,6 @@ package net.momirealms.craftengine.bukkit.world; +import net.momirealms.craftengine.bukkit.api.BukkitAdaptors; import net.momirealms.craftengine.bukkit.api.CraftEngineBlocks; import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections; @@ -90,7 +91,7 @@ public class BukkitExistingBlock implements ExistingBlock { @Override public World world() { - return new BukkitWorld(this.block.getWorld()); + return BukkitAdaptors.adapt(this.block.getWorld()); } @Override diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/world/BukkitWorld.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/world/BukkitWorld.java index 66df48ee3..3388b98a2 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/world/BukkitWorld.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/world/BukkitWorld.java @@ -31,15 +31,18 @@ import java.util.UUID; public class BukkitWorld implements World { private final WeakReference world; + private final UUID uuid; + private CEWorld ceWorld; private WorldHeight worldHeight; - public BukkitWorld(org.bukkit.World world) { + public BukkitWorld(@NotNull org.bukkit.World world) { this.world = new WeakReference<>(world); + this.uuid = world.getUID(); } @Override public org.bukkit.World platformWorld() { - return world.get(); + return this.world.get(); } @Override @@ -139,7 +142,10 @@ public class BukkitWorld implements World { @Override public CEWorld storageWorld() { - return BukkitWorldManager.instance().getWorld(uuid()); + if (this.ceWorld == null) { + this.ceWorld = BukkitWorldManager.instance().getWorld(uuid()); + } + return this.ceWorld; } @Override @@ -156,4 +162,15 @@ public class BukkitWorld implements World { } return tracked; } + + @Override + public boolean equals(Object o) { + if (!(o instanceof BukkitWorld that)) return false; + return this.uuid.equals(that.uuid()); + } + + @Override + public int hashCode() { + return this.uuid.hashCode(); + } } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/world/BukkitWorldManager.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/world/BukkitWorldManager.java index d5c5fc0b5..84d5788ed 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/world/BukkitWorldManager.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/world/BukkitWorldManager.java @@ -1,5 +1,6 @@ package net.momirealms.craftengine.bukkit.world; +import net.momirealms.craftengine.bukkit.api.BukkitAdaptors; import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine; import net.momirealms.craftengine.bukkit.plugin.injector.WorldStorageInjector; @@ -77,7 +78,7 @@ public class BukkitWorldManager implements WorldManager, Listener { } else { World bukkitWorld = Bukkit.getWorld(uuid); if (bukkitWorld != null) { - world = this.loadWorld(new BukkitWorld(bukkitWorld)); + world = this.loadWorld(wrap(bukkitWorld)); } } return world; @@ -95,7 +96,7 @@ public class BukkitWorldManager implements WorldManager, Listener { public void delayedInit() { // 此时大概率为空,暂且保留代码 for (World world : Bukkit.getWorlds()) { - BukkitWorld wrappedWorld = new BukkitWorld(world); + BukkitWorld wrappedWorld = wrap(world); try { CEWorld ceWorld = this.worlds.computeIfAbsent(world.getUID(), k -> new BukkitCEWorld(wrappedWorld, this.storageAdaptor)); injectChunkGenerator(ceWorld); @@ -144,7 +145,7 @@ public class BukkitWorldManager implements WorldManager, Listener { World world = event.getWorld(); UUID uuid = world.getUID(); if (this.worlds.containsKey(uuid)) return; - CEWorld ceWorld = new BukkitCEWorld(new BukkitWorld(world), this.storageAdaptor); + CEWorld ceWorld = new BukkitCEWorld(wrap(world), this.storageAdaptor); this.worlds.put(uuid, ceWorld); this.resetWorldArray(); this.injectChunkGenerator(ceWorld); @@ -165,7 +166,7 @@ public class BukkitWorldManager implements WorldManager, Listener { } ceWorld.setTicking(true); } else { - this.loadWorld(new BukkitWorld(world)); + this.loadWorld(wrap(world)); } } @@ -217,7 +218,7 @@ public class BukkitWorldManager implements WorldManager, Listener { @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) public void onWorldUnload(WorldUnloadEvent event) { - unloadWorld(new BukkitWorld(event.getWorld())); + unloadWorld(wrap(event.getWorld())); } @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) @@ -255,9 +256,9 @@ public class BukkitWorldManager implements WorldManager, Listener { } @Override - public net.momirealms.craftengine.core.world.World wrap(T world) { + public BukkitWorld wrap(T world) { if (world instanceof World w) { - return new BukkitWorld(w); + return BukkitAdaptors.adapt(w); } else { throw new IllegalArgumentException(world.getClass() + " is not a Bukkit World"); } diff --git a/core/build.gradle.kts b/core/build.gradle.kts index 91872ec24..66049e602 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -12,7 +12,7 @@ repositories { dependencies { // JOML - compileOnly("org.joml:joml:1.10.8") + compileOnly("org.joml:joml:${rootProject.properties["joml_version"]}") // YAML compileOnly(files("${rootProject.rootDir}/libs/boosted-yaml-${rootProject.properties["boosted_yaml_version"]}.jar")) compileOnly("org.yaml:snakeyaml:${rootProject.properties["snake_yaml_version"]}") diff --git a/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/hitbox/FurnitureHitBoxTypes.java b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/hitbox/FurnitureHitBoxTypes.java index 61fc5b871..a6ea6c23b 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/hitbox/FurnitureHitBoxTypes.java +++ b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/hitbox/FurnitureHitBoxTypes.java @@ -14,7 +14,6 @@ public class FurnitureHitBoxTypes { public static final Key INTERACTION = Key.of("minecraft:interaction"); public static final Key SHULKER = Key.of("minecraft:shulker"); public static final Key HAPPY_GHAST = Key.of("minecraft:happy_ghast"); - public static final Key VIRTUAL = Key.of("minecraft:virtual"); public static final Key CUSTOM = Key.of("minecraft:custom"); public static void register(Key key, FurnitureHitBoxConfigFactory factory) { diff --git a/core/src/main/java/net/momirealms/craftengine/core/util/RandomUtils.java b/core/src/main/java/net/momirealms/craftengine/core/util/RandomUtils.java index 7904d1776..951d290ea 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/util/RandomUtils.java +++ b/core/src/main/java/net/momirealms/craftengine/core/util/RandomUtils.java @@ -14,8 +14,8 @@ public final class RandomUtils { return min + (max - min) * ThreadLocalRandom.current().nextFloat(); } - public static int generateRandomInt(int min, int max) { - return min >= max ? min : ThreadLocalRandom.current().nextInt(max - min) + min; + public static int generateRandomInt(int minInclusive, int maxExclusive) { + return minInclusive >= maxExclusive ? minInclusive : ThreadLocalRandom.current().nextInt(maxExclusive - minInclusive) + minInclusive; } public static boolean generateRandomBoolean() { diff --git a/core/src/main/java/net/momirealms/craftengine/core/world/WorldPosition.java b/core/src/main/java/net/momirealms/craftengine/core/world/WorldPosition.java index fb0b9dd02..54adfbd75 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/world/WorldPosition.java +++ b/core/src/main/java/net/momirealms/craftengine/core/world/WorldPosition.java @@ -19,6 +19,15 @@ public class WorldPosition implements Position { this.yRot = 0f; } + public WorldPosition(World world, Vec3i position) { + this.x = position.x(); + this.y = position.y(); + this.z = position.z(); + this.world = world; + this.xRot = 0f; + this.yRot = 0f; + } + public WorldPosition(World world, Position position, float xRot, float yRot) { this.x = position.x(); this.y = position.y();