From 0c9d85076207eb6dece2c98f550d23fe921e3d18 Mon Sep 17 00:00:00 2001 From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com> Date: Sat, 28 Dec 2024 21:52:43 -0800 Subject: [PATCH] 0.0.3 --- README.md | 1 + gradle.properties | 4 +- .../io/wdsj/hybridfix/HybridFixPlugin.java | 1 + .../hybridfix/command/CommandHybridFix.java | 4 +- .../io/wdsj/hybridfix/config/Settings.java | 18 +++ .../entry/bukkit/HybridFixInternalPlugin.java | 142 ++++++++++++++++++ .../bukkit/listener/ExplodeListener.java | 31 ++++ .../mixin/bukkit/plugin/CraftServerMixin.java | 32 ++++ .../bukkit/plugin/DedicatedServerMixin.java | 35 +++++ src/main/resources/mixins.bukkit.plugin.json | 12 ++ 10 files changed, 277 insertions(+), 3 deletions(-) create mode 100644 src/main/java/io/wdsj/hybridfix/entry/bukkit/HybridFixInternalPlugin.java create mode 100644 src/main/java/io/wdsj/hybridfix/entry/bukkit/listener/ExplodeListener.java create mode 100644 src/main/java/io/wdsj/hybridfix/mixin/bukkit/plugin/CraftServerMixin.java create mode 100644 src/main/java/io/wdsj/hybridfix/mixin/bukkit/plugin/DedicatedServerMixin.java create mode 100644 src/main/resources/mixins.bukkit.plugin.json diff --git a/README.md b/README.md index 8ae088e..f7a86c8 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ Provide bugfixes, optimizations and utilities for Forge+Bukkit server environmen - Skip firing event if no listeners registered. - Disable Timings for less performance overhead. - Compatibility first, shouldn't break any mods/plugins. +- Built Bukkit plugin in the mod, offers utilities to server owners. Configuration file is under `${minecraftDir}/config/hybridfix.cfg` diff --git a/gradle.properties b/gradle.properties index 41deefc..c8cd20b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,7 +11,7 @@ generate_javadocs_jar = false # Mod Information # HIGHLY RECOMMEND complying with SemVer for mod_version: https://semver.org/ -mod_version = 0.0.2 +mod_version = 0.0.3 root_package = io.wdsj mod_id = hybridfix mod_name = HybridFix @@ -101,7 +101,7 @@ mixin_booter_version = 10.2 # You should change package root once they are generated generate_mixins_json = true # Delimit configs with spaces. Should only put configs name instead of full file name -mixin_configs = fix.capability bridge.explosion bridge.explosion.mohist bridge.permission fix.capability.mohist perf.eventbus perf.timings.v1 misc.command +mixin_configs = fix.capability bridge.explosion bridge.explosion.mohist bridge.permission fix.capability.mohist perf.eventbus perf.timings.v1 misc.command bukkit.plugin # A refmap is a json that denotes mapping conversions, this json is generated automatically, with the name `mixins.mod_id.refmap.json` # Use the property `mixin_refmap` if you want it to use a different name, only one name is accepted mixin_refmap = mixins.${mod_id}.refmap.json diff --git a/src/main/java/io/wdsj/hybridfix/HybridFixPlugin.java b/src/main/java/io/wdsj/hybridfix/HybridFixPlugin.java index 4a2abf7..0ac5e5b 100644 --- a/src/main/java/io/wdsj/hybridfix/HybridFixPlugin.java +++ b/src/main/java/io/wdsj/hybridfix/HybridFixPlugin.java @@ -35,6 +35,7 @@ public class HybridFixPlugin implements IFMLLoadingPlugin, IEarlyMixinLoader { put("mixins.perf.timings.v1.json", () -> Settings.disableTimings); } put("mixins.misc.command.json", () -> Settings.registerHybridFixCommands); + put("mixins.bukkit.plugin.json", () -> Settings.bukkitPluginConfig.enable); } }); diff --git a/src/main/java/io/wdsj/hybridfix/command/CommandHybridFix.java b/src/main/java/io/wdsj/hybridfix/command/CommandHybridFix.java index dfb3aa9..214dcb7 100644 --- a/src/main/java/io/wdsj/hybridfix/command/CommandHybridFix.java +++ b/src/main/java/io/wdsj/hybridfix/command/CommandHybridFix.java @@ -68,8 +68,10 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args) case "version": sender.sendMessage("This server is running HybridFix version " + HybridFix.VERSION); break; + default: + sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); + return false; } - return true; } diff --git a/src/main/java/io/wdsj/hybridfix/config/Settings.java b/src/main/java/io/wdsj/hybridfix/config/Settings.java index f9ea7ac..59f92bb 100644 --- a/src/main/java/io/wdsj/hybridfix/config/Settings.java +++ b/src/main/java/io/wdsj/hybridfix/config/Settings.java @@ -39,6 +39,24 @@ public class Settings { @Config.RequiresMcRestart public static boolean registerHybridFixCommands = true; + @Config.Comment("Configuration for HybridFix built-in bukkit plugin.") + @Config.RequiresMcRestart + public static BukkitPluginSettings bukkitPluginConfig = new BukkitPluginSettings(); + + public static class BukkitPluginSettings { + @Config.Comment("Enable HybridFix built-in bukkit plugin.(All bukkit plugin features in this section won't work if you disabled this!)") + @Config.RequiresMcRestart + public boolean enable = false; + + @Config.Comment("Enable HybridFix built-in AntiExplode.") + @Config.RequiresMcRestart + public boolean antiExplode = false; + + @Config.Comment("Worlds that AntiExplode should protect.") + @Config.RequiresMcRestart + public String[] antiExplodeWorlds = new String[]{"world"}; + } + static { ConfigAnytime.register(Settings.class); } diff --git a/src/main/java/io/wdsj/hybridfix/entry/bukkit/HybridFixInternalPlugin.java b/src/main/java/io/wdsj/hybridfix/entry/bukkit/HybridFixInternalPlugin.java new file mode 100644 index 0000000..4feaa7a --- /dev/null +++ b/src/main/java/io/wdsj/hybridfix/entry/bukkit/HybridFixInternalPlugin.java @@ -0,0 +1,142 @@ +package io.wdsj.hybridfix.entry.bukkit; + +import io.wdsj.hybridfix.HybridFix; +import org.bukkit.Bukkit; +import org.bukkit.Server; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.generator.ChunkGenerator; +import org.bukkit.plugin.PluginBase; +import org.bukkit.plugin.PluginDescriptionFile; +import org.bukkit.plugin.PluginLoader; +import org.bukkit.plugin.PluginLogger; +import org.bukkit.plugin.java.JavaPluginLoader; + +import java.io.File; +import java.io.InputStream; +import java.util.List; + +public class HybridFixInternalPlugin extends PluginBase { + private boolean enabled = true; + private PluginLoader loader; + private static final HybridFixInternalPlugin INSTANCE = new HybridFixInternalPlugin(); + public static HybridFixInternalPlugin getInstance() { + return INSTANCE; + } + private final String pluginName; + private PluginDescriptionFile pdf; + + public HybridFixInternalPlugin() { + this.pluginName = "HybridFix"; + pdf = new PluginDescriptionFile(pluginName, HybridFix.VERSION, "hybridfix"); + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + @Override + public File getDataFolder() { + throw new UnsupportedOperationException("Not supported."); + } + + @Override + public PluginDescriptionFile getDescription() { + return pdf; + } + + @Override + public FileConfiguration getConfig() { + throw new UnsupportedOperationException("Not supported."); + } + + @Override + public InputStream getResource(String filename) { + throw new UnsupportedOperationException("Not supported."); + } + + @Override + public void saveConfig() { + throw new UnsupportedOperationException("Not supported."); + } + + @Override + public void saveDefaultConfig() { + throw new UnsupportedOperationException("Not supported."); + } + + @Override + public void saveResource(String resourcePath, boolean replace) { + throw new UnsupportedOperationException("Not supported."); + } + + @Override + public void reloadConfig() { + throw new UnsupportedOperationException("Not supported."); + } + + @Override + public PluginLogger getLogger() { + throw new UnsupportedOperationException("Not supported."); + } + + @Override + public PluginLoader getPluginLoader() { + if (loader == null) { + loader = new JavaPluginLoader(Bukkit.getServer()); + } + return loader; + } + + @Override + public Server getServer() { + return Bukkit.getServer(); + } + + @Override + public boolean isEnabled() { + return enabled; + } + + @Override + public void onDisable() { + throw new UnsupportedOperationException("Not supported."); + } + + @Override + public void onLoad() { + throw new UnsupportedOperationException("Not supported."); + } + + @Override + public void onEnable() { + throw new UnsupportedOperationException("Not supported."); + } + + @Override + public boolean isNaggable() { + throw new UnsupportedOperationException("Not supported."); + } + + @Override + public void setNaggable(boolean canNag) { + throw new UnsupportedOperationException("Not supported."); + } + + @Override + public ChunkGenerator getDefaultWorldGenerator(String worldName, String id) { + throw new UnsupportedOperationException("Not supported."); + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + throw new UnsupportedOperationException("Not supported."); + } + + @Override + public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + throw new UnsupportedOperationException("Not supported."); + } + +} diff --git a/src/main/java/io/wdsj/hybridfix/entry/bukkit/listener/ExplodeListener.java b/src/main/java/io/wdsj/hybridfix/entry/bukkit/listener/ExplodeListener.java new file mode 100644 index 0000000..862aff6 --- /dev/null +++ b/src/main/java/io/wdsj/hybridfix/entry/bukkit/listener/ExplodeListener.java @@ -0,0 +1,31 @@ +package io.wdsj.hybridfix.entry.bukkit.listener; + +import io.wdsj.hybridfix.config.Settings; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockExplodeEvent; +import org.bukkit.event.entity.EntityExplodeEvent; + +public class ExplodeListener implements Listener { + @EventHandler + public void onEntityExplode(EntityExplodeEvent event) { + String worldName = event.getEntity().getWorld().getName(); + for (String s : Settings.bukkitPluginConfig.antiExplodeWorlds) { + if (s.equalsIgnoreCase(worldName)) { + event.setCancelled(true); + break; + } + } + } + + @EventHandler + public void onBlockExplode(BlockExplodeEvent event) { + String worldName = event.getBlock().getWorld().getName(); + for (String s : Settings.bukkitPluginConfig.antiExplodeWorlds) { + if (s.equalsIgnoreCase(worldName)) { + event.setCancelled(true); + break; + } + } + } +} diff --git a/src/main/java/io/wdsj/hybridfix/mixin/bukkit/plugin/CraftServerMixin.java b/src/main/java/io/wdsj/hybridfix/mixin/bukkit/plugin/CraftServerMixin.java new file mode 100644 index 0000000..b0c7137 --- /dev/null +++ b/src/main/java/io/wdsj/hybridfix/mixin/bukkit/plugin/CraftServerMixin.java @@ -0,0 +1,32 @@ +package io.wdsj.hybridfix.mixin.bukkit.plugin; + +import io.wdsj.hybridfix.HybridFix; +import io.wdsj.hybridfix.entry.bukkit.HybridFixInternalPlugin; +import org.bukkit.craftbukkit.v1_12_R1.CraftServer; +import org.bukkit.event.HandlerList; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(CraftServer.class) +public abstract class CraftServerMixin { + + @Inject( + method = "disablePlugins", + at = @At( + value = "TAIL" + ), + remap = false + ) + public void onDisablePlugins(CallbackInfo ci) { + hybridFix$disableInternalPlugin(); + } + + @Unique + private void hybridFix$disableInternalPlugin() { + HybridFix.LOGGER.info("[HybridFix] Disabling HybridFix internal plugin v{}", HybridFix.VERSION); + HandlerList.unregisterAll(HybridFixInternalPlugin.getInstance()); + } +} diff --git a/src/main/java/io/wdsj/hybridfix/mixin/bukkit/plugin/DedicatedServerMixin.java b/src/main/java/io/wdsj/hybridfix/mixin/bukkit/plugin/DedicatedServerMixin.java new file mode 100644 index 0000000..e9c3c73 --- /dev/null +++ b/src/main/java/io/wdsj/hybridfix/mixin/bukkit/plugin/DedicatedServerMixin.java @@ -0,0 +1,35 @@ +package io.wdsj.hybridfix.mixin.bukkit.plugin; + +import io.wdsj.hybridfix.HybridFix; +import io.wdsj.hybridfix.config.Settings; +import io.wdsj.hybridfix.entry.bukkit.HybridFixInternalPlugin; +import io.wdsj.hybridfix.entry.bukkit.listener.ExplodeListener; +import net.minecraft.server.dedicated.DedicatedServer; +import org.bukkit.Bukkit; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(DedicatedServer.class) +public abstract class DedicatedServerMixin { + + @Inject( + method = "init", + at = @At( + value = "TAIL" + ) + ) + public void onInit(CallbackInfoReturnable cir) { + hybridFix$register(); + } + + @Unique + private void hybridFix$register() { + HybridFix.LOGGER.info("[HybridFix] Enabling HybridFix internal plugin v{}", HybridFix.VERSION); + if (Settings.bukkitPluginConfig.antiExplode) { + Bukkit.getServer().getPluginManager().registerEvents(new ExplodeListener(), HybridFixInternalPlugin.getInstance()); + } + } +} diff --git a/src/main/resources/mixins.bukkit.plugin.json b/src/main/resources/mixins.bukkit.plugin.json new file mode 100644 index 0000000..7bd01a8 --- /dev/null +++ b/src/main/resources/mixins.bukkit.plugin.json @@ -0,0 +1,12 @@ +{ + "package": "io.wdsj.hybridfix.mixin.bukkit.plugin", + "required": true, + "refmap": "${mixin_refmap}", + "target": "@env(DEFAULT)", + "minVersion": "0.8", + "compatibilityLevel": "JAVA_8", + "mixins": [ + "DedicatedServerMixin", + "CraftServerMixin" + ] +} \ No newline at end of file