From f35bd05330f71d20b171e290618c6ef6df481f9b Mon Sep 17 00:00:00 2001 From: CoolLoong <1542536763@qq.com> Date: Thu, 7 Mar 2024 22:40:14 +0800 Subject: [PATCH] fix: Migrate player before create player table --- .../java/ms/kevi/plotplugin/PlotPlugin.java | 2 +- .../PlotLevelRegistrationListener.java | 124 +++++++++++------- .../kevi/plotplugin/manager/PlotManager.java | 13 +- .../plotplugin/util/PlotLevelSettings.java | 55 +++----- 4 files changed, 100 insertions(+), 94 deletions(-) diff --git a/src/main/java/ms/kevi/plotplugin/PlotPlugin.java b/src/main/java/ms/kevi/plotplugin/PlotPlugin.java index 06a8481..da20ea0 100644 --- a/src/main/java/ms/kevi/plotplugin/PlotPlugin.java +++ b/src/main/java/ms/kevi/plotplugin/PlotPlugin.java @@ -270,11 +270,11 @@ public void onEnable() { } } + this.playerManager = new PlayerManager(this); if (PlayersMigrator.shouldMigrate(this)) { PlayersMigrator.migratePlayers(this); } - this.playerManager = new PlayerManager(this); server.getScheduler().scheduleDelayedTask(this, this::loadPlayerNames, 1); server.getPluginManager().registerEvents(new PlotListener(this), this); diff --git a/src/main/java/ms/kevi/plotplugin/listener/PlotLevelRegistrationListener.java b/src/main/java/ms/kevi/plotplugin/listener/PlotLevelRegistrationListener.java index 94f0934..4aa513e 100644 --- a/src/main/java/ms/kevi/plotplugin/listener/PlotLevelRegistrationListener.java +++ b/src/main/java/ms/kevi/plotplugin/listener/PlotLevelRegistrationListener.java @@ -17,13 +17,14 @@ package ms.kevi.plotplugin.listener; import cn.nukkit.Player; +import cn.nukkit.block.BlockAir; import cn.nukkit.block.BlockState; import cn.nukkit.event.EventHandler; import cn.nukkit.event.EventPriority; import cn.nukkit.event.Listener; import cn.nukkit.event.player.PlayerChatEvent; -import cn.nukkit.item.Item; import cn.nukkit.level.Level; +import cn.nukkit.registry.Registries; import lombok.RequiredArgsConstructor; import ms.kevi.plotplugin.PlotPlugin; import ms.kevi.plotplugin.lang.TranslationKey; @@ -75,56 +76,87 @@ public void onChat(PlayerChatEvent event) { } catch (NumberFormatException e) { break; } - registration.getLevelSettings().setRoadBiome(biome); } case FIRST_LAYER -> { - if (!event.getMessage().contains(":")) break; - final BlockState blockState = Item.get(event.getMessage()).getBlock().getBlockState(); - registration.getLevelSettings().setFirstLayerBlockId(blockState.getIdentifier()); - registration.getLevelSettings().setFirstLayerBlockMeta(blockState.specialValue()); + try { + final BlockState blockState = Registries.BLOCKSTATE.get(Integer.parseInt(event.getMessage())); + if (blockState == BlockAir.STATE) { + break; + } + registration.getLevelSettings().setFirstLayerBlockHash(blockState.blockStateHash()); + } catch (NumberFormatException ignore) { + } } case MIDDLE_LAYER -> { - if (!event.getMessage().contains(":")) break; - final BlockState blockState = Item.get(event.getMessage()).getBlock().getBlockState(); - registration.getLevelSettings().setMiddleLayerBlockId(blockState.getIdentifier()); - registration.getLevelSettings().setMiddleLayerBlockMeta(blockState.specialValue()); + try { + final BlockState blockState = Registries.BLOCKSTATE.get(Integer.parseInt(event.getMessage())); + if (blockState == BlockAir.STATE) { + break; + } + registration.getLevelSettings().setMiddleLayerBlockHash(blockState.blockStateHash()); + } catch (NumberFormatException ignore) { + } } case LAST_LAYER -> { - if (!event.getMessage().contains(":")) break; - final BlockState blockState = Item.get(event.getMessage()).getBlock().getBlockState(); - registration.getLevelSettings().setLastLayerBlockId(blockState.getIdentifier()); - registration.getLevelSettings().setLastLayerBlockMeta(blockState.specialValue()); + try { + final BlockState blockState = Registries.BLOCKSTATE.get(Integer.parseInt(event.getMessage())); + if (blockState == BlockAir.STATE) { + break; + } + registration.getLevelSettings().setLastLayerBlockHash(blockState.blockStateHash()); + } catch (NumberFormatException ignore) { + } } case ROAD -> { - if (!event.getMessage().contains(":")) break; - final BlockState blockState = Item.get(event.getMessage()).getBlock().getBlockState(); - registration.getLevelSettings().setRoadBlockId(blockState.getIdentifier()); - registration.getLevelSettings().setRoadBlockMeta(blockState.specialValue()); + try { + final BlockState blockState = Registries.BLOCKSTATE.get(Integer.parseInt(event.getMessage())); + if (blockState == BlockAir.STATE) { + break; + } + registration.getLevelSettings().setRoadBlockHash(blockState.blockStateHash()); + } catch (NumberFormatException ignore) { + } } case ROAD_FILLING -> { - if (!event.getMessage().contains(":")) break; - final BlockState blockState = Item.get(event.getMessage()).getBlock().getBlockState(); - registration.getLevelSettings().setRoadFillingBlockId(blockState.getIdentifier()); - registration.getLevelSettings().setRoadFillingBlockMeta(blockState.specialValue()); + try { + final BlockState blockState = Registries.BLOCKSTATE.get(Integer.parseInt(event.getMessage())); + if (blockState == BlockAir.STATE) { + break; + } + registration.getLevelSettings().setRoadFillingBlockHash(blockState.blockStateHash()); + } catch (NumberFormatException ignore) { + } } case WALL_UNOWNED -> { - if (!event.getMessage().contains(":")) break; - final BlockState blockState = Item.get(event.getMessage()).getBlock().getBlockState(); - registration.getLevelSettings().setWallPlotBlockId(blockState.getIdentifier()); - registration.getLevelSettings().setWallPlotBlockMeta(blockState.specialValue()); + try { + final BlockState blockState = Registries.BLOCKSTATE.get(Integer.parseInt(event.getMessage())); + if (blockState == BlockAir.STATE) { + break; + } + registration.getLevelSettings().setWallPlotBlockHash(blockState.blockStateHash()); + } catch (NumberFormatException ignore) { + } } case WALL_CLAIMED -> { - if (!event.getMessage().contains(":")) break; - final BlockState blockState = Item.get(event.getMessage()).getBlock().getBlockState(); - registration.getLevelSettings().setClaimPlotBlockId(blockState.getIdentifier()); - registration.getLevelSettings().setClaimPlotBlockMeta(blockState.specialValue()); + try { + final BlockState blockState = Registries.BLOCKSTATE.get(Integer.parseInt(event.getMessage())); + if (blockState == BlockAir.STATE) { + break; + } + registration.getLevelSettings().setClaimPlotBlockHash(blockState.blockStateHash()); + } catch (NumberFormatException ignore) { + } } case WALL_FILLING -> { - if (!event.getMessage().contains(":")) break; - final BlockState blockState = Item.get(event.getMessage()).getBlock().getBlockState(); - registration.getLevelSettings().setWallFillingBlockId(blockState.getIdentifier()); - registration.getLevelSettings().setWallFillingBlockMeta(blockState.specialValue()); + try { + final BlockState blockState = Registries.BLOCKSTATE.get(Integer.parseInt(event.getMessage())); + if (blockState == BlockAir.STATE) { + break; + } + registration.getLevelSettings().setWallFillingBlockHash(blockState.blockStateHash()); + } catch (NumberFormatException ignore) { + } } case PLOT_SIZE -> { final int number; @@ -187,43 +219,35 @@ public void onChat(PlayerChatEvent event) { registration.getLevelSettings().getRoadBiome())); case FIRST_LAYER -> player.sendMessage(this.plugin.getLanguage().translate(player, TranslationKey.GENERATE_FIRST_LAYER, - registration.getLevelSettings().getFirstLayerBlockId() + ":" + - registration.getLevelSettings().getFirstLayerBlockMeta() + registration.getLevelSettings().getFirstLayerState() )); case MIDDLE_LAYER -> player.sendMessage(this.plugin.getLanguage().translate(player, TranslationKey.GENERATE_MIDDLE_LAYER, - registration.getLevelSettings().getMiddleLayerBlockId() + ":" + - registration.getLevelSettings().getMiddleLayerBlockMeta() + registration.getLevelSettings().getMiddleLayerState() )); case LAST_LAYER -> player.sendMessage(this.plugin.getLanguage().translate(player, TranslationKey.GENERATE_LAST_LAYER, - registration.getLevelSettings().getLastLayerBlockId() + ":" + - registration.getLevelSettings().getLastLayerBlockMeta() + registration.getLevelSettings().getLastLayerState() )); case ROAD -> player.sendMessage(this.plugin.getLanguage().translate(player, TranslationKey.GENERATE_ROAD, - registration.getLevelSettings().getRoadBlockId() + ":" + - registration.getLevelSettings().getRoadBlockMeta() + registration.getLevelSettings().getRoadState() )); case ROAD_FILLING -> player.sendMessage(this.plugin.getLanguage().translate(player, TranslationKey.GENERATE_ROAD_FILLING, - registration.getLevelSettings().getRoadFillingBlockId() + ":" + - registration.getLevelSettings().getRoadFillingBlockMeta() + registration.getLevelSettings().getRoadFillingState() )); case WALL_UNOWNED -> player.sendMessage(this.plugin.getLanguage().translate(player, TranslationKey.GENERATE_WALL_UNOWNED, - registration.getLevelSettings().getWallPlotBlockId() + ":" + - registration.getLevelSettings().getWallPlotBlockMeta() + registration.getLevelSettings().getWallPlotState() )); case WALL_CLAIMED -> player.sendMessage(this.plugin.getLanguage().translate(player, TranslationKey.GENERATE_WALL_CLAIMED, - registration.getLevelSettings().getClaimPlotBlockId() + ":" + - registration.getLevelSettings().getClaimPlotBlockMeta() + registration.getLevelSettings().getClaimPlotState() )); case WALL_FILLING -> player.sendMessage(this.plugin.getLanguage().translate(player, TranslationKey.GENERATE_WALL_FILLING, - registration.getLevelSettings().getWallFillingBlockId() + ":" + - registration.getLevelSettings().getWallFillingBlockMeta() + registration.getLevelSettings().getWallFillingState() )); case PLOT_SIZE -> player.sendMessage(this.plugin.getLanguage().translate(player, TranslationKey.GENERATE_PLOT_SIZE, diff --git a/src/main/java/ms/kevi/plotplugin/manager/PlotManager.java b/src/main/java/ms/kevi/plotplugin/manager/PlotManager.java index 9fd8192..21873ad 100644 --- a/src/main/java/ms/kevi/plotplugin/manager/PlotManager.java +++ b/src/main/java/ms/kevi/plotplugin/manager/PlotManager.java @@ -29,7 +29,6 @@ import cn.nukkit.math.BlockVector3; import cn.nukkit.math.SimpleAxisAlignedBB; import cn.nukkit.math.Vector3; -import cn.nukkit.registry.Registries; import cn.nukkit.utils.Config; import cn.nukkit.utils.ConfigSection; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; @@ -436,9 +435,9 @@ public void unlinkPlotFromAll(Plot centerPlot) { } private void finishPlotUnlinkFromNeighbors(Plot centerPlot) { - final BlockState claimBlock = Registries.BLOCK.getBlockProperties(this.levelSettings.getClaimPlotBlockId()).getBlockState(this.levelSettings.getClaimPlotBlockMeta()); - final BlockState wallBlock = Registries.BLOCK.getBlockProperties(this.levelSettings.getWallPlotBlockId()).getBlockState(this.levelSettings.getWallPlotBlockMeta()); - final BlockState wallFillingBlock = Registries.BLOCK.getBlockProperties(this.levelSettings.getWallFillingBlockId()).getBlockState(this.levelSettings.getWallFillingBlockMeta()); + final BlockState claimBlock = this.levelSettings.getClaimPlotState(); + final BlockState wallBlock = this.levelSettings.getWallPlotState(); + final BlockState wallFillingBlock = this.levelSettings.getWallFillingState(); final Set plots = new HashSet<>(Collections.singleton(centerPlot)); { @@ -463,9 +462,9 @@ private void finishPlotUnlinkFromNeighbors(Plot centerPlot) { } private void finishPlotUnlinkFromAll(Set plots) { - final BlockState claimBlock = Registries.BLOCK.getBlockProperties(this.levelSettings.getClaimPlotBlockId()).getBlockState(this.levelSettings.getClaimPlotBlockMeta()); - final BlockState wallBlock = Registries.BLOCK.getBlockProperties(this.levelSettings.getWallPlotBlockId()).getBlockState(this.levelSettings.getWallPlotBlockMeta()); - final BlockState wallFillingBlock = Registries.BLOCK.getBlockProperties(this.levelSettings.getWallFillingBlockId()).getBlockState(this.levelSettings.getWallFillingBlockMeta()); + final BlockState claimBlock = this.levelSettings.getClaimPlotState(); + final BlockState wallBlock = this.levelSettings.getWallPlotState(); + final BlockState wallFillingBlock = this.levelSettings.getWallFillingState(); for (Plot plot : plots) { for (int iDir = 0; iDir < 4; iDir++) diff --git a/src/main/java/ms/kevi/plotplugin/util/PlotLevelSettings.java b/src/main/java/ms/kevi/plotplugin/util/PlotLevelSettings.java index c4f6fb4..a3a08b8 100644 --- a/src/main/java/ms/kevi/plotplugin/util/PlotLevelSettings.java +++ b/src/main/java/ms/kevi/plotplugin/util/PlotLevelSettings.java @@ -16,8 +16,9 @@ package ms.kevi.plotplugin.util; -import cn.nukkit.block.Block; -import cn.nukkit.block.BlockState; +import cn.nukkit.block.*; +import cn.nukkit.block.property.CommonBlockProperties; +import cn.nukkit.block.property.enums.MinecraftVerticalHalf; import cn.nukkit.level.Level; import cn.nukkit.level.biome.BiomeID; import cn.nukkit.registry.Registries; @@ -35,70 +36,52 @@ @Getter @Setter public class PlotLevelSettings { - private int dimension = Level.DIMENSION_OVERWORLD; - private int plotBiome = BiomeID.PLAINS; private int roadBiome = BiomeID.PLAINS; - private int groundHeight = 64; private int plotSize = 35; private int roadSize = 7; - private String firstLayerBlockId = Block.BEDROCK; - private short firstLayerBlockMeta = 0; - - private String middleLayerBlockId = Block.DIRT; - private short middleLayerBlockMeta = 0; - - private String lastLayerBlockId = Block.GRASS; - private short lastLayerBlockMeta = 0; - - private String wallFillingBlockId = Block.STONE; - private short wallFillingBlockMeta = 0; - - private String wallPlotBlockId = Block.STONE_BLOCK_SLAB; - private short wallPlotBlockMeta = 0; - - private String claimPlotBlockId = Block.STONE_BLOCK_SLAB; - private short claimPlotBlockMeta = 1; - - private String roadBlockId = Block.OAK_PLANKS; - private short roadBlockMeta = 0; - - private String roadFillingBlockId = Block.DIRT; - private short roadFillingBlockMeta = 0; + private int firstLayerBlockHash = BlockBedrock.PROPERTIES.getDefaultState().blockStateHash(); + private int middleLayerBlockHash = BlockDirt.PROPERTIES.getDefaultState().blockStateHash(); + private int lastLayerBlockHash = BlockGrass.PROPERTIES.getDefaultState().blockStateHash(); + private int wallFillingBlockHash = BlockStone.PROPERTIES.getDefaultState().blockStateHash(); + private int wallPlotBlockHash = BlockStoneBlockSlab.PROPERTIES.getBlockState(CommonBlockProperties.MINECRAFT_VERTICAL_HALF.createValue(MinecraftVerticalHalf.BOTTOM)).blockStateHash(); + private int claimPlotBlockHash = BlockStoneBlockSlab2.PROPERTIES.getBlockState(CommonBlockProperties.MINECRAFT_VERTICAL_HALF.createValue(MinecraftVerticalHalf.BOTTOM)).blockStateHash(); + private int roadBlockHash = BlockOakPlanks.PROPERTIES.getDefaultState().blockStateHash(); + private int roadFillingBlockHash = BlockDirt.PROPERTIES.getDefaultState().blockStateHash(); public BlockState getFirstLayerState() { - return Registries.BLOCK.getBlockProperties(firstLayerBlockId).getBlockState(this.firstLayerBlockMeta); + return Registries.BLOCKSTATE.get(firstLayerBlockHash); } public BlockState getMiddleLayerState() { - return Registries.BLOCK.getBlockProperties(middleLayerBlockId).getBlockState(this.middleLayerBlockMeta); + return Registries.BLOCKSTATE.get(middleLayerBlockHash); } public BlockState getLastLayerState() { - return Registries.BLOCK.getBlockProperties(lastLayerBlockId).getBlockState(this.lastLayerBlockMeta); + return Registries.BLOCKSTATE.get(lastLayerBlockHash); } public BlockState getWallFillingState() { - return Registries.BLOCK.getBlockProperties(wallFillingBlockId).getBlockState(this.wallFillingBlockMeta); + return Registries.BLOCKSTATE.get(wallFillingBlockHash); } public BlockState getWallPlotState() { - return Registries.BLOCK.getBlockProperties(wallPlotBlockId).getBlockState(this.wallPlotBlockMeta); + return Registries.BLOCKSTATE.get(wallPlotBlockHash); } public BlockState getClaimPlotState() { - return Registries.BLOCK.getBlockProperties(claimPlotBlockId).getBlockState(this.claimPlotBlockMeta); + return Registries.BLOCKSTATE.get(claimPlotBlockHash); } public BlockState getRoadState() { - return Registries.BLOCK.getBlockProperties(roadBlockId).getBlockState(this.roadBlockMeta); + return Registries.BLOCKSTATE.get(roadBlockHash); } public BlockState getRoadFillingState() { - return Registries.BLOCK.getBlockProperties(roadFillingBlockId).getBlockState(this.roadFillingBlockMeta); + return Registries.BLOCKSTATE.get(roadFillingBlockHash); } public int getTotalSize() {