9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-26 18:39:17 +00:00

Added fix command

This commit is contained in:
XiaoMoMi
2024-09-15 22:33:52 +08:00
parent d29cbff124
commit 49a755eead
18 changed files with 239 additions and 27 deletions

View File

@@ -45,7 +45,8 @@ public class BukkitCommandManager extends AbstractCommandManager<CommandSender>
new DebugWorldsCommand(this),
new DebugInsightCommand(this),
new UnsafeRestoreCommand(this),
new UnsafeDeleteCommand(this)
new UnsafeDeleteCommand(this),
new UnsafeFixCommand(this)
);
private final Index<String, CommandFeature<CommandSender>> INDEX = Index.create(CommandFeature::getFeatureID, FEATURES);

View File

@@ -18,6 +18,8 @@
package net.momirealms.customcrops.bukkit.command.feature;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
import net.momirealms.customcrops.api.core.world.CustomCropsBlockState;
import net.momirealms.customcrops.api.core.world.Pos3;
@@ -25,12 +27,15 @@ import net.momirealms.customcrops.bukkit.command.BukkitCommandFeature;
import net.momirealms.customcrops.common.command.CustomCropsCommandManager;
import net.momirealms.customcrops.common.helper.AdventureHelper;
import net.momirealms.customcrops.common.locale.MessageConstants;
import net.momirealms.customcrops.common.locale.TranslationManager;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.checkerframework.checker.units.qual.C;
import org.incendo.cloud.Command;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.context.CommandContext;
import java.util.Optional;
@@ -62,13 +67,19 @@ public class DebugDataCommand extends BukkitCommandFeature<CommandSender> {
}
BukkitCustomCropsPlugin.getInstance().getWorldManager().getWorld(location.getWorld()).ifPresent(world -> {
Optional<CustomCropsBlockState> state = world.getBlockState(Pos3.from(location));
state.ifPresent(customCropsBlockState ->
handleFeedback(context,
MessageConstants.COMMAND_DEBUG_DATA_SUCCESS_CUSTOM,
Component.text(customCropsBlockState.asString())));
state.ifPresent(customCropsBlockState -> {
String cData = customCropsBlockState.asString();
TranslatableComponent component = MessageConstants.COMMAND_DEBUG_DATA_SUCCESS_CUSTOM
.arguments(Component.text(cData))
.build();
commandManager.feedbackConsumer().accept(context.sender(), component.key(), TranslationManager.render(component).clickEvent(ClickEvent.copyToClipboard(cData)));
});
});
String bData = block.getBlockData().getAsString();
handleFeedback(context, MessageConstants.COMMAND_DEBUG_DATA_SUCCESS_VANILLA, Component.text(bData));
TranslatableComponent component = MessageConstants.COMMAND_DEBUG_DATA_SUCCESS_VANILLA
.arguments(Component.text(bData))
.build();
commandManager.feedbackConsumer().accept(context.sender(), component.key(), TranslationManager.render(component).clickEvent(ClickEvent.copyToClipboard(bData)));
});
}
@@ -76,4 +87,6 @@ public class DebugDataCommand extends BukkitCommandFeature<CommandSender> {
public String getFeatureID() {
return "debug_data";
}
}

View File

@@ -47,14 +47,14 @@ public class UnsafeDeleteCommand extends BukkitCommandFeature<CommandSender> {
Player player = context.sender();
Optional<CustomCropsWorld<?>> optional = BukkitCustomCropsPlugin.getInstance().getWorldManager().getWorld(player.getWorld());
if (optional.isEmpty()) {
handleFeedback(context, MessageConstants.COMMAND_DEBUG_DELETE_FAILURE_WORLD, Component.text(player.getWorld().getName()));
handleFeedback(context, MessageConstants.COMMAND_UNSAFE_DELETE_FAILURE_WORLD, Component.text(player.getWorld().getName()));
return;
}
CustomCropsWorld<?> world = optional.get();
CustomCropsWorldImpl<?> customCropsWorld = (CustomCropsWorldImpl<?>) world;
ChunkPos chunkPos = ChunkPos.fromBukkitChunk(player.getLocation().getChunk());
customCropsWorld.deleteChunk(chunkPos);
handleFeedback(context, MessageConstants.COMMAND_DEBUG_DELETE_SUCCESS);
handleFeedback(context, MessageConstants.COMMAND_UNSAFE_DELETE_SUCCESS);
});
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright (C) <2024> <XiaoMoMi>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.momirealms.customcrops.bukkit.command.feature;
import net.kyori.adventure.text.Component;
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
import net.momirealms.customcrops.api.core.AbstractItemManager;
import net.momirealms.customcrops.api.core.Registries;
import net.momirealms.customcrops.api.core.block.CustomCropsBlock;
import net.momirealms.customcrops.api.core.world.*;
import net.momirealms.customcrops.bukkit.command.BukkitCommandFeature;
import net.momirealms.customcrops.common.command.CustomCropsCommandManager;
import net.momirealms.customcrops.common.locale.MessageConstants;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.incendo.cloud.Command;
import org.incendo.cloud.CommandManager;
import java.util.Optional;
public class UnsafeFixCommand extends BukkitCommandFeature<CommandSender> {
public UnsafeFixCommand(CustomCropsCommandManager<CommandSender> commandManager) {
super(commandManager);
}
@Override
public Command.Builder<? extends CommandSender> assembleCommand(CommandManager<CommandSender> manager, Command.Builder<CommandSender> builder) {
return builder
.senderType(Player.class)
.flag(manager.flagBuilder("silent").build())
.handler(context -> {
Player player = context.sender();
World bukkitWorld = player.getWorld();
Optional<CustomCropsWorld<?>> optional = BukkitCustomCropsPlugin.getInstance().getWorldManager().getWorld(bukkitWorld);
if (optional.isEmpty()) {
handleFeedback(context, MessageConstants.COMMAND_UNSAFE_FIX_FAILURE_WORLD, Component.text(bukkitWorld.getName()));
return;
}
ChunkPos chunkPos = ChunkPos.fromBukkitChunk(player.getLocation().getChunk());
CustomCropsWorld<?> world = optional.get();
CustomCropsChunk chunk = world.getOrCreateChunk(chunkPos);
CustomCropsSection section = chunk.getSection(BlockPos.fromPos3( Pos3.from(player.getLocation())).sectionID());
AbstractItemManager itemManager = BukkitCustomCropsPlugin.getInstance().getItemManager();
Location baseLocation = new Location(bukkitWorld, chunkPos.x() * 16, section.getSectionID() * 16, chunkPos.z() * 16);
int fixedBlocks = 0;
int corruptedBlocks = 0;
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
for (int y = 0; y < 16; y++) {
Location temp = baseLocation.clone().add(x, y, z);
String id = itemManager.anyID(temp);
Pos3 pos3 = Pos3.from(temp);
Optional<CustomCropsBlockState> previousState = chunk.getBlockState(pos3);
if (previousState.isPresent()) {
CustomCropsBlockState state = previousState.get();
if (state.type().isInstance(id)) {
continue;
} else {
corruptedBlocks++;
chunk.removeBlockState(pos3);
}
}
CustomCropsBlock customCropsBlock = Registries.BLOCKS.get(id);
if (customCropsBlock == null) {
continue;
}
CustomCropsBlockState state = customCropsBlock.createBlockState(id);
if (state != null) {
chunk.addBlockState(pos3, state);
fixedBlocks++;
}
}
}
}
handleFeedback(context, MessageConstants.COMMAND_UNSAFE_FIX_SUCCESS, Component.text(fixedBlocks), Component.text(corruptedBlocks));
});
}
@Override
public String getFeatureID() {
return "unsafe_fix";
}
}

View File

@@ -50,14 +50,14 @@ public class UnsafeRestoreCommand extends BukkitCommandFeature<CommandSender> {
World bukkitWorld = player.getWorld();
Optional<CustomCropsWorld<?>> optional = BukkitCustomCropsPlugin.getInstance().getWorldManager().getWorld(bukkitWorld);
if (optional.isEmpty()) {
handleFeedback(context, MessageConstants.COMMAND_DEBUG_RESTORE_FAILURE_WORLD, Component.text(bukkitWorld.getName()));
handleFeedback(context, MessageConstants.COMMAND_UNSAFE_RESTORE_FAILURE_WORLD, Component.text(bukkitWorld.getName()));
return;
}
ChunkPos chunkPos = ChunkPos.fromBukkitChunk(player.getLocation().getChunk());
CustomCropsWorld<?> world = optional.get();
Optional<CustomCropsChunk> chunk = world.getLoadedChunk(chunkPos);
if (chunk.isEmpty()) {
handleFeedback(context, MessageConstants.COMMAND_DEBUG_RESTORE_FAILURE_CHUNK);
handleFeedback(context, MessageConstants.COMMAND_UNSAFE_RESTORE_FAILURE_CHUNK);
return;
}
CustomCropsChunk customCropsChunk = chunk.get();
@@ -76,7 +76,7 @@ public class UnsafeRestoreCommand extends BukkitCommandFeature<CommandSender> {
}
}
}
handleFeedback(context, MessageConstants.COMMAND_DEBUG_RESTORE_SUCCESS, Component.text(restoredBlocks), Component.text(totalBlocks));
handleFeedback(context, MessageConstants.COMMAND_UNSAFE_RESTORE_SUCCESS, Component.text(restoredBlocks), Component.text(totalBlocks));
});
}

View File

@@ -104,4 +104,13 @@ unsafe_delete:
permission: customcrops.command.unsafe.delete
usage:
- /customcrops unsafe delete
- /ccrops unsafe delete
- /ccrops unsafe delete
# A command to fix the CustomCrops data in one section(16x16x16)
# Usage: [COMMAND]
unsafe_fix:
enable: true
permission: customcrops.command.unsafe.fix
usage:
- /customcrops unsafe fix
- /ccrops unsafe fix

View File

@@ -67,9 +67,13 @@ command.unsafe.delete.success: "<white>Deleted block data in this chunk"
command.unsafe.restore.failure.world: "<red>CustomCrops is not enabled in world [<arg:0>]</red>"
command.unsafe.restore.failure.chunk: "<red>This chunk doesn't contain any data</red>"
command.unsafe.restore.success: "<white>Restored (<arg:0>/<arg:1>) blocks</white>"
command.unsafe.fix.failure.world: "<red>CustomCrops is not enabled in world [<arg:0>]</red>"
command.unsafe.fix.success:
- "<white>Fixed <arg:0> blocks</white>"
- "<white>Removed <arg:1> corrupt blocks</white>"
command.debug.data.failure: "<red>No block selected</red>"
command.debug.data.success.vanilla: "<green>Vanilla block data: <hover:show_text:'<yellow>Copy'><click:copy_to_clipboard:'<arg:0>'><arg:0></click>"
command.debug.data.success.custom: "<gold>Custom block data: <hover:show_text:'<yellow>Copy'><click:copy_to_clipboard:'<arg:0>'><arg:0></click>"
command.debug.data.success.vanilla: "<green>Vanilla block data: <hover:show_text:'<yellow>Copy'><arg:0></click>"
command.debug.data.success.custom: "<gold>Custom block data: <hover:show_text:'<yellow>Copy'><arg:0></click>"
command.debug.worlds.failure: "<red>There's no world loaded</red>"
command.debug.worlds.success:
- "<gold>World: <arg:0></gold>"

View File

@@ -66,9 +66,13 @@ command.unsafe.delete.success: "<white>删除了这个区块的数据"
command.unsafe.restore.failure.world: "<red>CustomCrops没有在世界 [<arg:0>] 启用</red>"
command.unsafe.restore.failure.chunk: "<red>这个区块没有任何数据</red>"
command.unsafe.restore.success: "<white>恢复了 (<arg:0>/<arg:1>) 方块</white>"
command.unsafe.fix.failure.world: "<red>CustomCrops没有在世界 [<arg:0>] 启用</red>"
command.unsafe.fix.success:
- "<white>修复了 <arg:0> 个方块数据</white>"
- "<white>移除了 <arg:1> 个损坏数据</white>"
command.debug.data.failure: "<red>未选中任何方块</red>"
command.debug.data.success.vanilla: "<green>原版方块数据: <hover:show_text:'<yellow>复制'><click:copy_to_clipboard:'<arg:0>'><arg:0></click>"
command.debug.data.success.custom: "<gold>自定义方块数据: <hover:show_text:'<yellow>复制'><click:copy_to_clipboard:'<arg:0>'><arg:0></click>"
command.debug.data.success.vanilla: "<green>原版方块数据: <hover:show_text:'<yellow>复制'><arg:0>"
command.debug.data.success.custom: "<gold>自定义方块数据: <hover:show_text:'<yellow>复制'><arg:0>"
command.debug.worlds.failure: "<red>没有农作物世界被加载</red>"
command.debug.worlds.success:
- "<gold>世界: <arg:0></gold>"