dynamic command injection
This commit is contained in:
parent
775363aa57
commit
15645a58f5
@ -1,24 +0,0 @@
|
||||
package cz.marwland.mc.core;
|
||||
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public abstract class Feature implements Listener {
|
||||
private final Plugin plugin;
|
||||
private String name = "";
|
||||
|
||||
public Feature(Plugin plugin, String name) {
|
||||
this.plugin = plugin;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void onEnable() {
|
||||
}
|
||||
|
||||
public void onDisable() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
@ -4,9 +4,9 @@ import java.util.HashMap;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import cz.marwland.mc.core.cmd.First;
|
||||
import cz.marwland.mc.core.config.ConfigManager;
|
||||
import cz.marwland.mc.features.Heil;
|
||||
import cz.marwland.mc.core.features.Feature;
|
||||
import cz.marwland.mc.essentials.features.Heil;
|
||||
|
||||
public class MarwCore extends JavaPlugin {
|
||||
|
||||
@ -15,19 +15,20 @@ public class MarwCore extends JavaPlugin {
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
getCommand("first").setExecutor(new First(this));
|
||||
getServer().getPluginManager().registerEvents(new EventListener(this), this);
|
||||
getLogger().info("Enabled!");
|
||||
//getServer().getPluginManager().registerEvents(new EventListener(this), this);
|
||||
|
||||
configManager = new ConfigManager(this);
|
||||
configManager.registerConfig("borders.yml");
|
||||
configManager.load();
|
||||
|
||||
this.addFeature(new Heil(this));
|
||||
this.features.forEach((k, v) -> v.onEnable());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
configManager.save();
|
||||
this.features.forEach((k, v) -> v.onDisable());
|
||||
}
|
||||
|
||||
public ConfigManager getConfigManager() {
|
||||
@ -35,11 +36,11 @@ public class MarwCore extends JavaPlugin {
|
||||
}
|
||||
|
||||
public void addFeature(Feature f) {
|
||||
f.getName();
|
||||
getServer().getPluginManager().registerEvents(f, this);
|
||||
this.features.put(f.getName(), f);
|
||||
}
|
||||
|
||||
public Feature getFeature(String name) {
|
||||
return this.features.get(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,29 +0,0 @@
|
||||
package cz.marwland.mc.core.cmd;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import cz.marwland.mc.core.MarwCore;
|
||||
|
||||
public class First implements CommandExecutor {
|
||||
private final MarwCore plugin;
|
||||
|
||||
public First(MarwCore plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
plugin.getLogger().info("Hello?");
|
||||
sender.sendMessage("henlo");
|
||||
if (!(sender instanceof Player))
|
||||
return true;
|
||||
|
||||
Player p = (Player) sender;
|
||||
p.getWorld().getWorldBorder();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
55
src/cz/marwland/mc/core/features/Feature.java
Normal file
55
src/cz/marwland/mc/core/features/Feature.java
Normal file
@ -0,0 +1,55 @@
|
||||
package cz.marwland.mc.core.features;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.bukkit.command.CommandMap;
|
||||
import org.bukkit.command.defaults.BukkitCommand;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public abstract class Feature implements Listener {
|
||||
private final Plugin plugin;
|
||||
private String name = "";
|
||||
private ArrayList<BukkitCommand> commands = new ArrayList<>();
|
||||
private static CommandMap cmap;
|
||||
|
||||
public Feature(Plugin plugin, String name) {
|
||||
this.plugin = plugin;
|
||||
this.name = name;
|
||||
Method commandMap;
|
||||
try {
|
||||
commandMap = plugin.getServer().getClass().getMethod("getCommandMap");
|
||||
cmap = (CommandMap) commandMap.invoke(plugin.getServer());
|
||||
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void onEnable() {
|
||||
if (cmap != null) {
|
||||
for (BukkitCommand cmd : commands) {
|
||||
cmap.register(plugin.getName().toLowerCase(), cmd);
|
||||
}
|
||||
}
|
||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
||||
}
|
||||
|
||||
public void onDisable() {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public ArrayList<BukkitCommand> getCommands() {
|
||||
return this.commands;
|
||||
}
|
||||
|
||||
public void addCommand(BukkitCommand cmd) {
|
||||
this.commands.add(cmd);
|
||||
}
|
||||
|
||||
}
|
35
src/cz/marwland/mc/essentials/features/Heil.java
Normal file
35
src/cz/marwland/mc/essentials/features/Heil.java
Normal file
@ -0,0 +1,35 @@
|
||||
package cz.marwland.mc.essentials.features;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.defaults.BukkitCommand;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.player.PlayerChatEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import cz.marwland.mc.core.features.Feature;
|
||||
|
||||
public class Heil extends Feature {
|
||||
|
||||
public Heil(Plugin plugin) {
|
||||
super(plugin, "heil");
|
||||
// String name, String description, String usageMessage, List<String> aliases)
|
||||
this.addCommand(new BukkitCommand("sieg", "Heil the Führer!", "/sieg", Arrays.asList()) {
|
||||
@Override
|
||||
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
|
||||
sender.sendMessage("xddd");
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerChat(PlayerChatEvent event) {
|
||||
if (event.getMessage().contains("sieg")) {
|
||||
event.setMessage("SIEG HEIL!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package cz.marwland.mc.features;
|
||||
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.player.PlayerChatEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import cz.marwland.mc.core.Feature;
|
||||
|
||||
public class Heil extends Feature {
|
||||
|
||||
public Heil(Plugin plugin) {
|
||||
super(plugin, "heil");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
|
||||
}
|
||||
@EventHandler
|
||||
public void onPlayerChat(PlayerChatEvent event) {
|
||||
if (event.getMessage().contains("sieg")) {
|
||||
event.setMessage("SIEG HEIL!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user