Files
src
build.gradle
gradle.properties
settings.gradle
Code
9 791
src/main/java/de/mcsmp/enhancedwither/EnhancedWither.java
package de.mcsmp.enhancedwither;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.minecraft.command.permission.Permission;
import net.minecraft.command.permission.PermissionLevel;
import net.minecraft.entity.boss.WitherEntity;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
public final class EnhancedWither implements ModInitializer {
public static final String MOD_ID = "mcsmp_enhanced_wither";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static final ConfigManager CONFIG = new ConfigManager();
private static final Permission ADMIN_PERMISSION = new Permission.Level(PermissionLevel.ADMINS);
@Override
public void onInitialize() {
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
CONFIG.load(server);
CONFIG.applyToLoadedWithers(server);
});
ServerEntityEvents.ENTITY_LOAD.register((entity, world) -> {
if (entity instanceof WitherEntity wither) {
CONFIG.applyTo(wither);
}
});
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
dispatcher.register(literal("witherconfig")
.requires(EnhancedWither::isAdmin)
.executes(context -> showStatus(context.getSource()))
.then(literal("status")
.executes(context -> showStatus(context.getSource())))
.then(literal("reset")
.executes(context -> {
CONFIG.reset(context.getSource().getServer());
context.getSource().sendFeedback(() -> Text.literal("Enhanced Wither settings reset to vanilla."), false);
return 1;
}))
.then(literal("reload")
.executes(context -> {
CONFIG.reload(context.getSource().getServer());
context.getSource().sendFeedback(() -> Text.literal("Enhanced Wither settings reloaded."), false);
return 1;
}))
.then(literal("save")
.executes(context -> {
CONFIG.save();
context.getSource().sendFeedback(() -> Text.literal("Enhanced Wither settings saved."), false);
return 1;
}))
.then(literal("profile")
.then(argument("name", StringArgumentType.word())
.executes(context -> runSettingCommand(context.getSource(), () ->
CONFIG.setProfile(StringArgumentType.getString(context, "name"), context.getSource().getServer())))))
.then(literal("set")
.then(argument("key", StringArgumentType.word())
.then(argument("value", DoubleArgumentType.doubleArg())
.executes(context -> runSettingCommand(context.getSource(), () ->
CONFIG.setNumeric(
StringArgumentType.getString(context, "key"),
DoubleArgumentType.getDouble(context, "value"),
context.getSource().getServer()
))))))
.then(literal("special")
.then(argument("name", StringArgumentType.word())
.then(argument("enabled", BoolArgumentType.bool())
.executes(context -> runSettingCommand(context.getSource(), () ->
CONFIG.setSpecial(
StringArgumentType.getString(context, "name"),
BoolArgumentType.getBool(context, "enabled"),
context.getSource().getServer()
))))))
)
);
}
private static boolean isAdmin(ServerCommandSource source) {
return source.getPermissions().hasPermission(ADMIN_PERMISSION);
}
private static int showStatus(ServerCommandSource source) {
source.sendFeedback(() -> Text.literal(CONFIG.describe()), false);
return 1;
}
private static int runSettingCommand(ServerCommandSource source, Runnable action) {
try {
action.run();
source.sendFeedback(() -> Text.literal("Enhanced Wither settings updated and applied."), false);
return 1;
} catch (IllegalArgumentException exception) {
source.sendFeedback(() -> Text.literal(exception.getMessage()), false);
return 0;
}
}
}