Skip to content

Commit

Permalink
✅ 增强规则&BUG修复
Browse files Browse the repository at this point in the history
  • Loading branch information
1024-byteeeee committed Sep 9, 2024
1 parent 1d8d1f2 commit 5c5ccb2
Show file tree
Hide file tree
Showing 13 changed files with 377 additions and 84 deletions.
22 changes: 13 additions & 9 deletions src/main/java/club/mcams/carpet/AmsServerSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@

package club.mcams.carpet;

import club.mcams.carpet.validators.rule.fancyFakePlayerName.FancyFakePlayerNameRuleObserver;
import club.mcams.carpet.observers.rule.fancyFakePlayerName.FancyFakePlayerNameRuleObserver;
import top.byteeeee.annotationtoolbox.annotation.GameVersion;

//#if MC>=12002
//$$ import club.mcams.carpet.validators.rule.stackableDiscount.StackableDiscountRuleObserver;
//$$ import club.mcams.carpet.observers.rule.stackableDiscount.StackableDiscountRuleObserver;
//#endif
import club.mcams.carpet.validators.rule.largeShulkerBox.LargeShulkerBoxRuleObserver;
import club.mcams.carpet.observers.rule.largeShulkerBox.LargeShulkerBoxRuleObserver;

import club.mcams.carpet.validators.rule.maxPlayerBlockInteractionRange.MaxPlayerBlockInteractionRangeValidator;
import club.mcams.carpet.validators.rule.maxPlayerEntityInteractionRange.MaxPlayerEntityInteractionRangeValidator;
import club.mcams.carpet.validators.rule.blockChunkLoaderTimeController.MaxTimeValidator;
Expand Down Expand Up @@ -79,8 +80,12 @@ public class AmsServerSettings {
@Rule(categories = {AMS, FEATURE, SURVIVAL})
public static boolean sharedVillagerDiscounts = false;

@Rule(categories = {AMS, FEATURE})
public static boolean fakePeace = false;
@Rule(
options = {"false", "true", "minecraft:overworld", "minecraft:the_end", "minecraft:the_nether", "minecraft:the_end,minecraft:the_nether"},
categories = {AMS, FEATURE},
strict = false
)
public static String fakePeace = "false";

@Rule(categories = {AMS, FEATURE, SURVIVAL})
public static boolean extinguishedCampfire = false;
Expand Down Expand Up @@ -228,11 +233,10 @@ public class AmsServerSettings {
public static double maxClientInteractionReachDistance = -1.0D;

@Rule(
options = {"VANILLA", "minecraft:bedrock", "minecraft:bedrock,minecraft:obsidian"},
categories = {AMS, FEATURE},
strict = false
options = {"0", "1", "2", "3", "4", "ops", "true", "false"},
categories = {AMS, FEATURE, COMMAND}
)
public static String customMovableBlock = "VANILLA";
public static String commandCustomMovableBlock = "false";

@Rule(categories = {AMS, FEATURE})
public static boolean easyMaxLevelBeacon = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package club.mcams.carpet.commands;

import club.mcams.carpet.commands.rule.commandCustomCommandPermissionLevel.CustomCommandPermissionLevelRegistry;
import club.mcams.carpet.commands.rule.commandCustomMovableBlock.CustomMovableBlockRegistry;
import club.mcams.carpet.commands.rule.commandGetPlayerSkull.GetPlayerSkullCommandRegistry;
import club.mcams.carpet.commands.rule.commandGetSaveSize.GetSaveSizeCommandRegistry;
import club.mcams.carpet.commands.rule.commandGetSystemInfo.GetSystemInfoCommandRegistry;
Expand Down Expand Up @@ -78,5 +79,12 @@ public static void registerCommands(
CustomCommandPermissionLevelRegistry.register(dispatcher);

GetPlayerSkullCommandRegistry.register(dispatcher);

CustomMovableBlockRegistry.register(
dispatcher
//#if MC>=11900
//$$ , commandBuildContext
//#endif
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* This file is part of the Carpet AMS Addition project, licensed under the
* GNU Lesser General Public License v3.0
*
* Copyright (C) 2024 A Minecraft Server and contributors
*
* Carpet AMS Addition is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Carpet AMS Addition 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Carpet AMS Addition. If not, see <https://www.gnu.org/licenses/>.
*/

package club.mcams.carpet.commands.rule.commandCustomMovableBlock;

import club.mcams.carpet.AmsServerSettings;
import club.mcams.carpet.translations.Translator;
import club.mcams.carpet.utils.CommandHelper;
import club.mcams.carpet.config.rule.commandCustomMovableBlock.CustomMovableBlockConfig;

import club.mcams.carpet.utils.Messenger;
import club.mcams.carpet.utils.RegexTools;
import com.mojang.brigadier.CommandDispatcher;

import net.minecraft.block.BlockState;
import net.minecraft.command.argument.BlockStateArgumentType;
import net.minecraft.entity.player.PlayerEntity;
//#if MC>=11900
//$$ import net.minecraft.command.CommandRegistryAccess;
//#endif
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Style;
import net.minecraft.util.Formatting;

import java.util.ArrayList;
import java.util.List;

import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;

public class CustomMovableBlockRegistry {
private static final Translator translator = new Translator("command.customMovableBlock");
private static final String MSG_HEAD = "<customMovableBlock> ";
public static final List<String> CUSTOM_MOVABLE_BLOCKS = new ArrayList<>();

//#if MC<11900
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
//#else
//$$ public static void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess commandRegistryAccess) {
//#endif
dispatcher.register(
CommandManager.literal("customMovableBlock")
.requires(source -> CommandHelper.canUseCommand(source, AmsServerSettings.commandCustomMovableBlock))
.then(literal("add")
//#if MC<11900
.then(argument("block", BlockStateArgumentType.blockState())
//#else
//$$ .then(argument("block", BlockStateArgumentType.blockState(commandRegistryAccess))
//#endif
.executes(context -> add(
context.getSource().getServer(),
context.getSource().getPlayer(),
BlockStateArgumentType.getBlockState(context, "block").getBlockState()
))))
.then(literal("remove")
//#if MC<11900
.then(argument("block", BlockStateArgumentType.blockState())
//#else
//$$ .then(argument("block", BlockStateArgumentType.blockState(commandRegistryAccess))
//#endif
.executes(context -> remove(
context.getSource().getServer(),
context.getSource().getPlayer(),
BlockStateArgumentType.getBlockState(context, "block").getBlockState())
)))
.then(literal("removeAll")
.executes(context -> removeAll(context.getSource().getServer(), context.getSource().getPlayer())))
.then(literal("list")
.executes(context -> list(context.getSource().getPlayer())))
.then(literal("help")
.executes(context -> help(context.getSource().getPlayer())))
);
}

private static int add(MinecraftServer server, PlayerEntity player, BlockState blockState) {
if (!CUSTOM_MOVABLE_BLOCKS.contains(getBlockName(blockState))) {
CUSTOM_MOVABLE_BLOCKS.add(getBlockName(blockState));
saveToJson(server);
player.sendMessage(Messenger.s(MSG_HEAD + "+ " + getBlockName(blockState)).formatted(Formatting.GREEN), false);
} else {
player.sendMessage(
Messenger.s(
MSG_HEAD + getBlockName(blockState) + translator.tr("already_exists").getString()
).formatted(Formatting.YELLOW), false
);
}
return 1;
}

private static int remove(MinecraftServer server, PlayerEntity player, BlockState blockState) {
if (CUSTOM_MOVABLE_BLOCKS.contains(getBlockName(blockState))) {
CUSTOM_MOVABLE_BLOCKS.remove(getBlockName(blockState));
saveToJson(server);
player.sendMessage(Messenger.s(MSG_HEAD + "- " + getBlockName(blockState)).formatted(Formatting.RED), false);
} else {
player.sendMessage(Messenger.s(MSG_HEAD + getBlockName(blockState) + translator.tr("not_found").getString()).formatted(Formatting.RED), false);
}
return 1;
}

private static int removeAll(MinecraftServer server, PlayerEntity player) {
CUSTOM_MOVABLE_BLOCKS.clear();
saveToJson(server);
player.sendMessage(Messenger.s(MSG_HEAD + translator.tr("removeAll").getString()).formatted(Formatting.RED), false);
return 1;
}

private static int list(ServerPlayerEntity player) {
player.sendMessage(
Messenger.s(
translator.tr("list").getString() + "\n-------------------------------").formatted(Formatting.GREEN),
false
);
for (String blockName : CUSTOM_MOVABLE_BLOCKS) {
player.sendMessage(Messenger.s(blockName).formatted(Formatting.GREEN), false);
}
return 1;
}

private static int help(ServerPlayerEntity player) {
String setHelpText = translator.tr("help.set").getString();
String removeHelpText = translator.tr("help.remove").getString();
String removeAllHelpText = translator.tr("help.removeAll").getString();
String listHelpText = translator.tr("help.list").getString();
player.sendMessage(
Messenger.s(
"\n" +
setHelpText + "\n" +
removeHelpText + "\n" +
removeAllHelpText + "\n" +
listHelpText
).setStyle(Style.EMPTY.withColor(Formatting.GRAY)),
false
);
return 1;
}

private static void saveToJson(MinecraftServer server) {
String CONFIG_FILE_PATH = CustomMovableBlockConfig.getPath(server);
CustomMovableBlockConfig.saveToJson(CUSTOM_MOVABLE_BLOCKS, CONFIG_FILE_PATH);
}

private static String getBlockName(BlockState blockState) {
return RegexTools.getBlockRegisterName(blockState.getBlock().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import club.mcams.carpet.utils.CommandHelper;
import club.mcams.carpet.utils.compat.DimensionWrapper;

//#if MC>=12102
//$$ import java.util.Set;
//#endif

import com.mojang.brigadier.CommandDispatcher;

import net.minecraft.command.argument.BlockPosArgumentType;
Expand Down Expand Up @@ -57,7 +61,11 @@ private static int executeTeleport(ServerPlayerEntity player, ServerWorld target
int x = destinationPos.getX();
int y = destinationPos.getY();
int z = destinationPos.getZ();
//#if MC>=12102
//$$ player.teleport(targetDimension, x, y, z, Set.of(), player.getPitch(1), 1, false);
//#else
player.teleport(targetDimension, x, y, z, player.getYaw(1), player.getPitch(1));
//#endif
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,31 @@

import club.mcams.carpet.commands.rule.commandCustomBlockBlastResistance.CustomBlockBlastResistanceCommandRegistry;
import club.mcams.carpet.commands.rule.commandCustomCommandPermissionLevel.CustomCommandPermissionLevelRegistry;
import club.mcams.carpet.commands.rule.commandCustomMovableBlock.CustomMovableBlockRegistry;
import club.mcams.carpet.commands.rule.commandPlayerLeader.LeaderCommandRegistry;

import club.mcams.carpet.config.rule.amsUpdateSuppressionCrashFix.ForceModeCommandConfig;
import club.mcams.carpet.config.rule.commandCustomBlockBlastResistance.CustomBlockBlastResistanceConfig;
import club.mcams.carpet.config.rule.commandCustomCommandPermissionLevel.CustomCommandPermissionLevelConfig;
import club.mcams.carpet.config.rule.commandCustomMovableBlock.CustomMovableBlockConfig;
import club.mcams.carpet.config.rule.commandLeader.LeaderConfig;

import net.minecraft.server.MinecraftServer;

public class LoadConfigFromJson {
public static void load(MinecraftServer server) {
clearMemory();
ForceModeCommandConfig.loadConfigFromJson(server);
CustomBlockBlastResistanceConfig.loadFromJson(CustomBlockBlastResistanceConfig.getPath(server));
LeaderConfig.loadFromJson(LeaderConfig.getPath(server));
CustomCommandPermissionLevelConfig.loadFromJson(CustomCommandPermissionLevelConfig.getPath(server));
CustomMovableBlockConfig.loadFromJson(CustomMovableBlockConfig.getPath(server));
}

private static void clearMemory() {
CustomBlockBlastResistanceCommandRegistry.CUSTOM_BLOCK_BLAST_RESISTANCE_MAP.clear();
LeaderCommandRegistry.LEADER_LIST.clear();
CustomCommandPermissionLevelRegistry.COMMAND_PERMISSION_MAP.clear();
CustomMovableBlockRegistry.CUSTOM_MOVABLE_BLOCKS.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* This file is part of the Carpet AMS Addition project, licensed under the
* GNU Lesser General Public License v3.0
*
* Copyright (C) 2024 A Minecraft Server and contributors
*
* Carpet AMS Addition is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Carpet AMS Addition 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Carpet AMS Addition. If not, see <https://www.gnu.org/licenses/>.
*/

package club.mcams.carpet.config.rule.commandCustomMovableBlock;

import club.mcams.carpet.AmsServer;
import club.mcams.carpet.commands.rule.commandCustomMovableBlock.CustomMovableBlockRegistry;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import net.minecraft.server.MinecraftServer;
import net.minecraft.util.WorldSavePath;

import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

public class CustomMovableBlockConfig {
@SuppressWarnings("ReadWriteStringCanBeUsed")
public static void loadFromJson(String configFilePath) {
Gson gson = new Gson();
Path path = Paths.get(configFilePath);
CustomMovableBlockRegistry.CUSTOM_MOVABLE_BLOCKS.clear();
if (Files.exists(path)) {
try {
String json = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
Type type = new TypeToken<List<String>>() {}.getType();
List<String> simplifiedMap = gson.fromJson(json, type);
CustomMovableBlockRegistry.CUSTOM_MOVABLE_BLOCKS.addAll(simplifiedMap);
} catch (IOException e) {
AmsServer.LOGGER.warn("Failed to load config", e);
}
}
}

@SuppressWarnings("ReadWriteStringCanBeUsed")
public static void saveToJson(List<String> customBlockMap, String configFilePath) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
List<String> simplifiedMap = new ArrayList<>(customBlockMap);
String json = gson.toJson(simplifiedMap);
try {
Path path = Paths.get(configFilePath);
Files.createDirectories(path.getParent());
Files.write(path, json.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
AmsServer.LOGGER.warn("Failed to save config", e);
}
}

public static String getPath(MinecraftServer server) {
return server.getSavePath(WorldSavePath.ROOT).resolve("carpetamsaddition/custom_movable_block" + ".json").toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public static void loadFromJson(String configFilePath) {
}

@SuppressWarnings("ReadWriteStringCanBeUsed")
public static void saveToJson(Map<String, String> customBlockMap, String configFilePath) {
public static void saveToJson(Map<String, String> leaderMap, String configFilePath) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Map<String, String> simplifiedMap = new HashMap<>(customBlockMap);
Map<String, String> simplifiedMap = new HashMap<>(leaderMap);
String json = gson.toJson(simplifiedMap);
try {
Path path = Paths.get(configFilePath);
Expand Down
Loading

0 comments on commit 5c5ccb2

Please sign in to comment.