From 9d033c6a48fdf4933cdf20c5beaf50c0f729bb06 Mon Sep 17 00:00:00 2001 From: Hexeption Date: Mon, 19 Aug 2019 18:13:11 +0100 Subject: [PATCH] Added Magma command and Added a start to the API --- .../fml/common/Loader.java.patch | 30 +++++++ .../FMLHandshakeServerState.java.patch | 21 +++++ .../handshake/NetworkDispatcher.java.patch | 11 +++ .../magmafoundation/magma/api/PlayerAPI.java | 83 +++++++++++++++++++ .../magmafoundation/magma/api/ServerAPI.java | 69 +++++++++++++++ .../magma/commands/MagmaCommand.java | 78 +++++++++++++++++ .../magma/configuration/MagmaConfig.java | 2 + 7 files changed, 294 insertions(+) create mode 100644 patches/net/minecraftforge/fml/common/Loader.java.patch create mode 100644 patches/net/minecraftforge/fml/common/network/handshake/FMLHandshakeServerState.java.patch create mode 100644 patches/net/minecraftforge/fml/common/network/handshake/NetworkDispatcher.java.patch create mode 100644 src/main/java/org/magmafoundation/magma/api/PlayerAPI.java create mode 100644 src/main/java/org/magmafoundation/magma/api/ServerAPI.java create mode 100644 src/main/java/org/magmafoundation/magma/commands/MagmaCommand.java diff --git a/patches/net/minecraftforge/fml/common/Loader.java.patch b/patches/net/minecraftforge/fml/common/Loader.java.patch new file mode 100644 index 0000000..9b4863e --- /dev/null +++ b/patches/net/minecraftforge/fml/common/Loader.java.patch @@ -0,0 +1,30 @@ +--- ../src-base/minecraft/net/minecraftforge/fml/common/Loader.java ++++ ../src-work/minecraft/net/minecraftforge/fml/common/Loader.java +@@ -97,6 +97,7 @@ + import com.google.gson.JsonElement; + import com.google.gson.JsonObject; + import com.google.gson.JsonParser; ++import org.magmafoundation.magma.api.ServerAPI; + + import javax.annotation.Nullable; + +@@ -757,6 +758,7 @@ + modController.distributeStateMessage(LoaderState.AVAILABLE); + GameData.freezeData(); + FMLLog.log.info("Forge Mod Loader has successfully loaded {} mod{}", mods.size(), mods.size() == 1 ? "" : "s"); ++ ServerAPI.mods.put("mods", mods.size()); + progressBar.step("Completing Minecraft initialization"); + } + +@@ -904,8 +906,10 @@ + } + } + +- if (difference.size() > 0) ++ if (difference.size() > 0){ ++ ServerAPI.modList.addAll(difference); + FMLLog.log.info("Attempting connection with missing mods {} at {}", difference, side); ++ } + return true; + } + diff --git a/patches/net/minecraftforge/fml/common/network/handshake/FMLHandshakeServerState.java.patch b/patches/net/minecraftforge/fml/common/network/handshake/FMLHandshakeServerState.java.patch new file mode 100644 index 0000000..1d30170 --- /dev/null +++ b/patches/net/minecraftforge/fml/common/network/handshake/FMLHandshakeServerState.java.patch @@ -0,0 +1,21 @@ +--- ../src-base/minecraft/net/minecraftforge/fml/common/network/handshake/FMLHandshakeServerState.java ++++ ../src-work/minecraft/net/minecraftforge/fml/common/network/handshake/FMLHandshakeServerState.java +@@ -36,6 +36,7 @@ + import net.minecraftforge.fml.relauncher.Side; + import net.minecraftforge.registries.ForgeRegistry; + import net.minecraftforge.registries.RegistryManager; ++import org.magmafoundation.magma.api.PlayerAPI; + + enum FMLHandshakeServerState implements IHandshakeState + { +@@ -67,6 +68,10 @@ + NetworkDispatcher dispatcher = ctx.channel().attr(NetworkDispatcher.FML_DISPATCHER).get(); + dispatcher.setModList(client.modList()); + FMLLog.log.info("Client attempting to join with {} mods : {}", client.modListSize(), client.modListAsString()); ++ if(client.modListSize() > 0) { ++ PlayerAPI.mods.put(dispatcher.player, client.modListSize()); ++ PlayerAPI.modList.put(dispatcher.player, client.modListAsString()); ++ } + String modRejections = FMLNetworkHandler.checkModList(client, Side.CLIENT); + if (modRejections != null) + { diff --git a/patches/net/minecraftforge/fml/common/network/handshake/NetworkDispatcher.java.patch b/patches/net/minecraftforge/fml/common/network/handshake/NetworkDispatcher.java.patch new file mode 100644 index 0000000..3e14386 --- /dev/null +++ b/patches/net/minecraftforge/fml/common/network/handshake/NetworkDispatcher.java.patch @@ -0,0 +1,11 @@ +--- ../src-base/minecraft/net/minecraftforge/fml/common/network/handshake/NetworkDispatcher.java ++++ ../src-work/minecraft/net/minecraftforge/fml/common/network/handshake/NetworkDispatcher.java +@@ -103,7 +103,7 @@ + public static final AttributeKey> FML_GAMEDATA_SNAPSHOT = AttributeKey.valueOf("fml:gameDataSnapshot"); + public final NetworkManager manager; + private final PlayerList scm; +- private EntityPlayerMP player; ++ public EntityPlayerMP player; // Kettle - private -> public + private ConnectionState state; + private ConnectionType connectionType; + private final Side side; diff --git a/src/main/java/org/magmafoundation/magma/api/PlayerAPI.java b/src/main/java/org/magmafoundation/magma/api/PlayerAPI.java new file mode 100644 index 0000000..90af1fd --- /dev/null +++ b/src/main/java/org/magmafoundation/magma/api/PlayerAPI.java @@ -0,0 +1,83 @@ +package org.magmafoundation.magma.api; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import org.bukkit.craftbukkit.entity.CraftPlayer; +import org.bukkit.entity.Player; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * PlayerAPI + * + * @author Hexeption admin@hexeption.co.uk + * @since 19/08/2019 - 04:57 pm + */ +public class PlayerAPI { + + public static Map mods = new ConcurrentHashMap<>(); + public static Map modList = new ConcurrentHashMap<>(); + + /** + * Gets the NMS Player. + * + * @param player - A Bukkit player. + * @return NMS player. + */ + public static EntityPlayerMP getNMSPlayer(Player player) { + return ((CraftPlayer) player).getHandle(); + } + + /** + * Gets the CraftBukkit Player. + * + * @param playerMP - NMS MpPlayer. + * @return player - Bukkit Player + */ + public static Player getCBPlayer(EntityPlayerMP playerMP) { + return playerMP.getBukkitEntity().getPlayer(); + } + + /** + * Check is the player has access to Op. + * + * @param entityPlayer - The player. + * @return boolean - is op or not. + */ + public static boolean isOp(EntityPlayer entityPlayer) { + return ServerAPI.getNMSServer().getPlayerList().canSendCommands(entityPlayer.getGameProfile()); + } + + /** + * Get player mod count. + * + * @param player - The player. + * @return loaded mod count. + */ + public static int getModSize(Player player) { + return mods.get(getNMSPlayer(player)) == null ? 0 : mods.get(getNMSPlayer(player)); + } + + /** + * Gets the list of loaded mods. + * + * @param player - The player + * @return list of loaded mods. + */ + public static String getModlist(Player player) { + return modList.get(getNMSPlayer(player)) == null ? "null" : modList.get(getNMSPlayer(player)); + } + + /** + * Checks if a mod is in the list. + * + * @param player - The player + * @param modid for the mod wanted to check. + * @return boolean - if it's in the list or not. + */ + public static boolean hasMod(Player player, String modid) { + return getModlist(player).contains(modid); + } + +} diff --git a/src/main/java/org/magmafoundation/magma/api/ServerAPI.java b/src/main/java/org/magmafoundation/magma/api/ServerAPI.java new file mode 100644 index 0000000..53f6689 --- /dev/null +++ b/src/main/java/org/magmafoundation/magma/api/ServerAPI.java @@ -0,0 +1,69 @@ +package org.magmafoundation.magma.api; + +import io.netty.util.internal.ConcurrentSet; +import net.minecraft.server.MinecraftServer; +import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.CraftServer; + +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +/** + * ServerAPI + * + * @author Hexeption admin@hexeption.co.uk + * @since 19/08/2019 - 04:47 pm + */ +public class ServerAPI { + + public static Map mods = new ConcurrentHashMap<>(); + public static Set modList = new ConcurrentSet<>(); + + /** + * How many mods are loaded. + * + * @return int - loaded mods. + */ + public static int getModSize() { + return mods.get("mods") == null ? 0 : mods.get("mods"); + } + + /** + * List all loaded mods by name. + * + * @return String - List of mods. + */ + public static String getModList() { + return modList.toString(); + } + + /** + * Checks if a mod is in the list. + * + * @param modid for the mod to check. + * @return boolean - if it's in the list or not. + */ + public static boolean hasMod(String modid) { + return getModList().contains(modid); + } + + /** + * Gets the Minecraft Server instance. + * + * @return MinecraftServer instance. + */ + public static MinecraftServer getNMSServer() { + return MinecraftServer.getServerInstance(); + } + + /** + * Gets the CraftBukkit Server + * + * @return CraftServer instance. + */ + public static CraftServer getCBServer() { + return (CraftServer) Bukkit.getServer(); + } + +} diff --git a/src/main/java/org/magmafoundation/magma/commands/MagmaCommand.java b/src/main/java/org/magmafoundation/magma/commands/MagmaCommand.java new file mode 100644 index 0000000..13ec53d --- /dev/null +++ b/src/main/java/org/magmafoundation/magma/commands/MagmaCommand.java @@ -0,0 +1,78 @@ +package org.magmafoundation.magma.commands; + +import net.minecraft.command.CommandBase; +import net.minecraft.util.text.TextFormatting; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.magmafoundation.magma.api.PlayerAPI; +import org.magmafoundation.magma.api.ServerAPI; + +import java.util.Collections; +import java.util.List; + +/** + * MagmaCommand + * + * @author Hexeption admin@hexeption.co.uk + * @since 19/08/2019 - 04:40 pm + */ +public class MagmaCommand extends Command { + + public MagmaCommand(String name) { + super(name); + + this.description = "Magma commands"; + this.usageMessage = "/magma [mods|playermods]"; + this.setPermission("magma.commands.magma"); + } + + @Override + public List tabComplete(CommandSender sender, String alias, String[] args, Location location) throws IllegalArgumentException { + if (args.length <= 1) { + return CommandBase.getListOfStringsMatchingLastWord(args, "mods", "playermods"); + } + return Collections.emptyList(); + } + + @Override + public boolean execute(CommandSender sender, String commandLabel, String[] args) { + + if(!sender.hasPermission("magma.commands.magma")) { + sender.sendMessage(ChatColor.RED + "You don't got the permission to execute this command!"); + return false; + } + + if(args.length == 0){ + sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); + return false; + } + + switch (args[0].toLowerCase()){ + case "mods": + sender.sendMessage(ChatColor.GREEN + "" + ServerAPI.getModSize() + " " + ServerAPI.getModList()); + break; + case "playermods": + if(args.length == 1){ + sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); + return false; + } + + Player player = Bukkit.getPlayer(args[1].toString()); + if(player != null){ + sender.sendMessage(ChatColor.GREEN + "" + PlayerAPI.getModSize(player) + " " + PlayerAPI.getModlist(player)); + }else{ + sender.sendMessage(ChatColor.RED + "The player [" + args[1] + "] is not online."); + } + break; + default: + sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); + return false; + } + + return true; + } +} diff --git a/src/main/java/org/magmafoundation/magma/configuration/MagmaConfig.java b/src/main/java/org/magmafoundation/magma/configuration/MagmaConfig.java index 4d90c3c..fcbcf1c 100644 --- a/src/main/java/org/magmafoundation/magma/configuration/MagmaConfig.java +++ b/src/main/java/org/magmafoundation/magma/configuration/MagmaConfig.java @@ -2,6 +2,7 @@ import net.minecraft.server.MinecraftServer; import org.bukkit.configuration.file.YamlConfiguration; +import org.magmafoundation.magma.commands.MagmaCommand; import org.magmafoundation.magma.commands.VersionCommand; import org.magmafoundation.magma.configuration.value.Value; @@ -52,6 +53,7 @@ public void init() { @Override protected void addCommands() { + commands.put("magma", new MagmaCommand("magma")); commands.put("version", new VersionCommand("version")); }