├── settings.gradle ├── src └── main │ ├── resources │ ├── config.yml │ └── plugin.yml │ └── java │ └── fr │ └── mercury │ └── combattag │ ├── MercuryTag.java │ ├── utils │ ├── commands │ │ ├── ICommand.java │ │ ├── Completer.java │ │ ├── Command.java │ │ ├── BukkitCompleter.java │ │ ├── CommandArgs.java │ │ ├── BukkitCommand.java │ │ └── CommandFramework.java │ ├── jsons │ │ ├── JsonPersist.java │ │ ├── Saveable.java │ │ └── DiscUtil.java │ ├── Utils.java │ └── ChatUtils.java │ └── engine │ ├── commands │ ├── CombatCommand.java │ └── AddBlockedCommand.java │ ├── CombatManager.java │ └── CombatListener.java ├── libs └── jar_included │ └── gson-2.8.0.jar ├── .gitignore ├── LICENSE ├── README.md ├── gradlew.bat └── gradlew /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = '[Mercury] CombatTag' 2 | 3 | -------------------------------------------------------------------------------- /src/main/resources/config.yml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SwartZCoding/MercuryCombatTag/HEAD/src/main/resources/config.yml -------------------------------------------------------------------------------- /libs/jar_included/gson-2.8.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SwartZCoding/MercuryCombatTag/HEAD/libs/jar_included/gson-2.8.0.jar -------------------------------------------------------------------------------- /src/main/resources/plugin.yml: -------------------------------------------------------------------------------- 1 | name: MercuryCombatTag 2 | author: SwartZ 3 | version: 1.0.0 4 | main: fr.mercury.combattag.MercuryTag -------------------------------------------------------------------------------- /src/main/java/fr/mercury/combattag/MercuryTag.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SwartZCoding/MercuryCombatTag/HEAD/src/main/java/fr/mercury/combattag/MercuryTag.java -------------------------------------------------------------------------------- /src/main/java/fr/mercury/combattag/utils/commands/ICommand.java: -------------------------------------------------------------------------------- 1 | package fr.mercury.combattag.utils.commands; 2 | 3 | public abstract class ICommand { 4 | 5 | public ICommand() {} 6 | public abstract void onCommand(CommandArgs paramCommandArgs); 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/fr/mercury/combattag/utils/jsons/JsonPersist.java: -------------------------------------------------------------------------------- 1 | package fr.mercury.combattag.utils.jsons; 2 | 3 | import java.io.File; 4 | 5 | public abstract interface JsonPersist { 6 | public abstract File getFile(); 7 | public abstract void loadData(); 8 | public abstract void saveData(boolean sync); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/fr/mercury/combattag/utils/Utils.java: -------------------------------------------------------------------------------- 1 | package fr.mercury.combattag.utils; 2 | 3 | import fr.mercury.combattag.MercuryTag; 4 | 5 | import java.io.File; 6 | 7 | public class Utils { 8 | 9 | public static File getFormatedFile(String fileName) { 10 | return new File(MercuryTag.getInstance().getDataFolder(), fileName); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/fr/mercury/combattag/utils/commands/Completer.java: -------------------------------------------------------------------------------- 1 | package fr.mercury.combattag.utils.commands; 2 | 3 | import java.lang.annotation.Retention; 4 | import java.lang.annotation.RetentionPolicy; 5 | import java.lang.annotation.Target; 6 | 7 | @Target({java.lang.annotation.ElementType.METHOD}) 8 | @Retention(RetentionPolicy.RUNTIME) 9 | public @interface Completer 10 | { 11 | String[] name() default {}; 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/fr/mercury/combattag/utils/commands/Command.java: -------------------------------------------------------------------------------- 1 | package fr.mercury.combattag.utils.commands; 2 | 3 | import java.lang.annotation.Retention; 4 | import java.lang.annotation.RetentionPolicy; 5 | import java.lang.annotation.Target; 6 | 7 | @Target({java.lang.annotation.ElementType.METHOD}) 8 | @Retention(RetentionPolicy.RUNTIME) 9 | public @interface Command 10 | { 11 | String permissionNode() default ""; 12 | 13 | String[] name() default {}; 14 | 15 | boolean isConsole() default false; 16 | } 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | *.bin 3 | 4 | *.lock 5 | 6 | *.class 7 | 8 | .gradle/6.5/gc.properties 9 | 10 | .gradle/buildOutputCleanup/cache.properties 11 | 12 | .gradle/vcs-1/gc.properties 13 | 14 | build/libs/\[Mercury\] CombatTag-1.0-SNAPSHOT.jar 15 | 16 | build/resources/main/config.yml 17 | 18 | build/resources/main/plugin.yml 19 | 20 | build/tmp/compileJava/ 21 | 22 | *.MF 23 | 24 | gradle/wrapper/gradle-wrapper.jar 25 | 26 | gradle/wrapper/gradle-wrapper.properties 27 | 28 | libs/paperspigot-1.7.10-R0.1-SNAPSHOT.jar 29 | -------------------------------------------------------------------------------- /src/main/java/fr/mercury/combattag/utils/jsons/Saveable.java: -------------------------------------------------------------------------------- 1 | package fr.mercury.combattag.utils.jsons; 2 | 3 | import fr.mercury.combattag.MercuryTag; 4 | 5 | import java.io.File; 6 | 7 | public abstract class Saveable implements JsonPersist { 8 | 9 | public boolean needDir, needFirstSave; 10 | 11 | public Saveable(MercuryTag plugin, String name) { 12 | this(plugin, name, false, false); 13 | } 14 | 15 | public Saveable(MercuryTag plugin, String name, boolean needDir, boolean needFirstSave) { 16 | this.needDir = needDir; 17 | this.needFirstSave = needFirstSave; 18 | if (this.needDir) { 19 | if (this.needFirstSave) { 20 | saveData(false); 21 | } else { 22 | File directory = getFile(); 23 | if (!directory.exists()) { 24 | try { 25 | directory.mkdir(); 26 | } catch (Exception exception) { 27 | exception.printStackTrace(); 28 | } 29 | } 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/fr/mercury/combattag/utils/ChatUtils.java: -------------------------------------------------------------------------------- 1 | package fr.mercury.combattag.utils; 2 | 3 | import fr.mercury.combattag.MercuryTag; 4 | import net.minecraft.util.org.apache.commons.lang3.StringUtils; 5 | import org.bukkit.command.CommandSender; 6 | import org.bukkit.configuration.file.FileConfiguration; 7 | import org.bukkit.entity.Player; 8 | 9 | public class ChatUtils { 10 | 11 | public static FileConfiguration config = MercuryTag.getInstance().getConfig(); 12 | 13 | public static void sendConsole(String message) { 14 | 15 | MercuryTag.getInstance().getServer().getConsoleSender().sendMessage(message); 16 | } 17 | 18 | public static void sendNoPermission(CommandSender sender) { 19 | 20 | sender.sendMessage(config.getString("GENERAL.NO_PERMISSION")); 21 | } 22 | 23 | public static void sendNoPermission(Player player) { 24 | 25 | player.sendMessage(config.getString("GENERAL.NO_PERMISSION")); 26 | } 27 | 28 | public static void sendBadFormatted(CommandSender sender) { 29 | 30 | sender.sendMessage(config.getString("GENERAL.BAD_FORMAT")); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Anto' (SwartZ_) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/main/java/fr/mercury/combattag/engine/commands/CombatCommand.java: -------------------------------------------------------------------------------- 1 | package fr.mercury.combattag.engine.commands; 2 | 3 | import fr.mercury.combattag.MercuryTag; 4 | import fr.mercury.combattag.engine.CombatManager; 5 | import fr.mercury.combattag.utils.ChatUtils; 6 | import fr.mercury.combattag.utils.commands.Command; 7 | import fr.mercury.combattag.utils.commands.CommandArgs; 8 | import fr.mercury.combattag.utils.commands.ICommand; 9 | import org.bukkit.entity.Player; 10 | 11 | public class CombatCommand extends ICommand { 12 | 13 | private CombatManager combatManager = CombatManager.getInstance(); 14 | 15 | @Override 16 | @Command(name = {"ct", "combat", "ctags", "mercurytag", "mercurytags"}) 17 | public void onCommand(CommandArgs args) { 18 | 19 | if(args.length() == 0) { 20 | 21 | Player player = (Player) args.getSender(); 22 | 23 | if(this.combatManager.isInCombat(player)) 24 | args.getSender().sendMessage(MercuryTag.getInstance().getConfig().getString("COMBAT.COMMAND_COMBAT").replace("%time%", String.valueOf(this.combatManager.getSecondsLeft((Player) args.getSender())))); 25 | else 26 | this.combatManager.remove(player); 27 | 28 | } else 29 | ChatUtils.sendBadFormatted(args.getSender()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MercuryCombatTag 2 | 3 | *MercuryCombatTag is a plugin who disallow your players to disconnect in fight* 4 | 5 | ## Supports 6 | 7 | * CraftBukkit & Spigot 1.7.10 8 | * Spigot 1.7.10 / 1.8.X 9 | * CraftBukkit & Spigot 1.8.X 10 | * CraftBukkit & Spigot 1.9.X 11 | * CraftBukkit & Spigot 1.12.X 12 | * 13 | ## Features 14 | 15 | * Can instantly kill player when they log off in combat 16 | * Disallow teleporting while player is in combat 17 | * Broadcast a PVP kill/death message 18 | * Deny specific commands while player is in combat 19 | * Supports WorldGuard 20 | * Supports Factions _Most mainstream Faction builds are supported_ 21 | 22 | ## Installation 23 | 24 | 1. Obtain the latest version of the plugins. 25 | 2. Place **MercuryCombatTag.jar** into your server's *plugins* folder. 26 | 3. Start the server. _This creates a new file **plugins/MercuryCombatTag/config.yml**_ 27 | 4. Edit the newly created configuration file with desired behavior. 28 | 5. If you made any changes, restart the server. 29 | 30 | ## Permissions 31 | 32 | | **Permission** | **Description** | 33 | | -------------------------| ------------------------------------------ | 34 | | ct.admin | Permit to add / remove commands blacklist | 35 | | custom perm in config | Bypass combat tagging | 36 | 37 | 38 | ## Commands 39 | 40 | | **Commands** | **Description** | 41 | | -------------------------| ---------------------------------------- | 42 | | ct, combat, ctags | Permit to check if you are tags mode | 43 | | lockcommand | Add or remove locked commands | 44 | 45 | ## Contributing 46 | -------------------------------------------------------------------------------- /src/main/java/fr/mercury/combattag/utils/commands/BukkitCompleter.java: -------------------------------------------------------------------------------- 1 | package fr.mercury.combattag.utils.commands; 2 | 3 | import org.bukkit.command.Command; 4 | import org.bukkit.command.CommandSender; 5 | import org.bukkit.command.TabCompleter; 6 | 7 | import java.lang.reflect.InvocationTargetException; 8 | import java.lang.reflect.Method; 9 | import java.util.AbstractMap; 10 | import java.util.HashMap; 11 | import java.util.List; 12 | import java.util.Map; 13 | import java.util.Map.Entry; 14 | 15 | public class BukkitCompleter implements TabCompleter { 16 | 17 | private Map> completers = new HashMap>(); 18 | 19 | public void addCompleter(String label, Method m, Object obj) { 20 | completers.put(label, new AbstractMap.SimpleEntry(m, obj)); 21 | } 22 | 23 | @SuppressWarnings("unchecked") 24 | @Override 25 | public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { 26 | for (int i = args.length; i >= 0; i--) { 27 | StringBuffer buffer = new StringBuffer(); 28 | buffer.append(label.toLowerCase()); 29 | for (int x = 0; x < i; x++) { 30 | if (!args[x].equals("") && !args[x].equals(" ")) { 31 | buffer.append("." + args[x].toLowerCase()); 32 | } 33 | } 34 | String cmdLabel = buffer.toString(); 35 | if (completers.containsKey(cmdLabel)) { 36 | Entry entry = completers.get(cmdLabel); 37 | try { 38 | return (List) entry.getKey().invoke(entry.getValue(), 39 | new CommandArgs(sender, command, label, args, cmdLabel.split("\\.").length - 1)); 40 | } catch (IllegalArgumentException e) { 41 | e.printStackTrace(); 42 | } catch (IllegalAccessException e) { 43 | e.printStackTrace(); 44 | } catch (InvocationTargetException e) { 45 | e.printStackTrace(); 46 | } 47 | } 48 | } 49 | return null; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/fr/mercury/combattag/engine/commands/AddBlockedCommand.java: -------------------------------------------------------------------------------- 1 | package fr.mercury.combattag.engine.commands; 2 | 3 | import fr.mercury.combattag.MercuryTag; 4 | import fr.mercury.combattag.engine.CombatManager; 5 | import fr.mercury.combattag.utils.ChatUtils; 6 | import fr.mercury.combattag.utils.commands.Command; 7 | import fr.mercury.combattag.utils.commands.CommandArgs; 8 | import fr.mercury.combattag.utils.commands.ICommand; 9 | import org.bukkit.configuration.file.FileConfiguration; 10 | import org.bukkit.entity.Player; 11 | 12 | public class AddBlockedCommand extends ICommand { 13 | 14 | private CombatManager combatManager = CombatManager.getInstance(); 15 | private FileConfiguration config = MercuryTag.getInstance().getConfig(); 16 | 17 | @Override 18 | @Command(name = {"lockcommand"}, permissionNode = "ct.admin") 19 | public void onCommand(CommandArgs args) { 20 | 21 | Player player = args.getPlayer(); 22 | 23 | if(args.length() == 1 && args.getArgs(0).equalsIgnoreCase("list")) { 24 | this.combatManager.sendList(args.getSender()); 25 | return; 26 | } 27 | 28 | if(args.length() == 2) { 29 | 30 | if(args.getArgs(0).equalsIgnoreCase("add")) { 31 | if(combatManager.getCommands().contains(args.getArgs(1))) { 32 | player.sendMessage(this.config.getString("BLOCKED_COMMANDS.COMMAND_ALREADY_REGISTER")); 33 | return; 34 | } 35 | combatManager.addCommand(args.getArgs(1)); 36 | player.sendMessage(this.config.getString("BLOCKED_COMMANDS.COMMAND_LOCKED_SUCCESSFULY").replace("%command%", args.getArgs(1))); 37 | 38 | } else if(args.getArgs(0).equalsIgnoreCase("remove")) { 39 | if(!combatManager.getCommands().contains(args.getArgs(1))) { 40 | player.sendMessage(this.config.getString("BLOCKED_COMMANDS.COMMAND_NOT_ALREADY_REGISTER")); 41 | return; 42 | } 43 | combatManager.removeCommand(args.getArgs(1)); 44 | player.sendMessage(this.config.getString("BLOCKED_COMMANDS.COMMAND_LOCKED_SUCCESSFULY").replace("%command%", args.getArgs(1))); 45 | 46 | } 47 | } else { 48 | ChatUtils.sendBadFormatted(player); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/fr/mercury/combattag/utils/commands/CommandArgs.java: -------------------------------------------------------------------------------- 1 | package fr.mercury.combattag.utils.commands; 2 | 3 | import net.minecraft.util.com.google.common.primitives.Doubles; 4 | import net.minecraft.util.com.google.common.primitives.Ints; 5 | import org.apache.commons.lang.StringUtils; 6 | import org.bukkit.Bukkit; 7 | import org.bukkit.command.Command; 8 | import org.bukkit.command.CommandSender; 9 | import org.bukkit.entity.Player; 10 | 11 | public class CommandArgs 12 | { 13 | private CommandSender sender; 14 | private Command command; 15 | private String label; 16 | private String[] args; 17 | 18 | protected CommandArgs(CommandSender sender, Command command, String label, String[] args, int subCommand) 19 | { 20 | String[] modArgs = new String[args.length - subCommand]; 21 | for (int i = 0; i < args.length - subCommand; i++) { 22 | modArgs[i] = args[(i + subCommand)]; 23 | } 24 | 25 | StringBuffer buffer = new StringBuffer(); 26 | buffer.append(label); 27 | for (int x = 0; x < subCommand; x++) { 28 | buffer.append("." + args[x]); 29 | } 30 | String cmdLabel = buffer.toString(); 31 | this.sender = sender; 32 | this.command = command; 33 | this.label = cmdLabel; 34 | this.args = modArgs; 35 | } 36 | 37 | public CommandSender getSender() { 38 | return sender; 39 | } 40 | 41 | public Command getCommand() { 42 | return command; 43 | } 44 | 45 | public String getLabel() { 46 | return label; 47 | } 48 | 49 | public String[] getArgs() { 50 | return args; 51 | } 52 | 53 | public String getArgs(int index) { 54 | return args[index]; 55 | } 56 | 57 | public int length() { 58 | return args.length; 59 | } 60 | 61 | public boolean isPlayer() { 62 | return sender instanceof Player; 63 | } 64 | 65 | public Player getPlayer() { 66 | if ((sender instanceof Player)) { 67 | return (Player)sender; 68 | } 69 | return null; 70 | } 71 | 72 | public Player asPlayer(int index) 73 | { 74 | return Bukkit.getPlayer(getArgs(index)); 75 | } 76 | 77 | public org.bukkit.OfflinePlayer asOfflinePlayer(int index) { 78 | return Bukkit.getOfflinePlayer(getArgs(index)); 79 | } 80 | 81 | public Integer asInteger(int index) { 82 | return Ints.tryParse(getArgs(index)); 83 | } 84 | 85 | public Double asDouble(int index) { 86 | return Doubles.tryParse(getArgs(index)); 87 | } 88 | 89 | public String asString(int start, int end) { 90 | return StringUtils.join(args, ' ', start, end); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/fr/mercury/combattag/utils/commands/BukkitCommand.java: -------------------------------------------------------------------------------- 1 | package fr.mercury.combattag.utils.commands; 2 | 3 | import org.apache.commons.lang.Validate; 4 | import org.bukkit.command.Command; 5 | import org.bukkit.command.*; 6 | import org.bukkit.plugin.Plugin; 7 | 8 | public class BukkitCommand extends Command { 9 | private final Plugin owningPlugin; 10 | private CommandExecutor executor; 11 | protected BukkitCompleter completer; 12 | 13 | protected BukkitCommand(String label, CommandExecutor executor, Plugin owner) { 14 | super(label); 15 | this.executor = executor; 16 | owningPlugin = owner; 17 | usageMessage = ""; 18 | } 19 | 20 | @Override 21 | public boolean execute(CommandSender sender, String commandLabel, String[] args) { 22 | boolean success = false; 23 | 24 | if (!owningPlugin.isEnabled()) { 25 | return false; 26 | } 27 | 28 | if (!testPermission(sender)) { 29 | return true; 30 | } 31 | try { 32 | success = executor.onCommand(sender, this, commandLabel, args); 33 | } catch (Throwable ex) { 34 | throw new CommandException("Unhandled exception executing command '" + commandLabel + "' in plugin " 35 | + owningPlugin.getDescription().getFullName(), ex); 36 | } 37 | 38 | if ((!success) && (usageMessage.length() > 0)) { 39 | for (String line : usageMessage.replace("", commandLabel).split("\n")) { 40 | sender.sendMessage(line); 41 | } 42 | } 43 | 44 | return success; 45 | } 46 | 47 | @Override 48 | public java.util.List tabComplete(CommandSender sender, String alias, String[] args) 49 | throws CommandException, IllegalArgumentException { 50 | Validate.notNull(sender, "Sender cannot be null"); 51 | Validate.notNull(args, "Arguments cannot be null"); 52 | Validate.notNull(alias, "Alias cannot be null"); 53 | 54 | java.util.List completions = null; 55 | try { 56 | if (completer != null) { 57 | completions = completer.onTabComplete(sender, this, alias, args); 58 | } 59 | if ((completions == null) && ((executor instanceof TabCompleter))) { 60 | completions = ((TabCompleter) executor).onTabComplete(sender, this, alias, args); 61 | } 62 | } catch (Throwable ex) { 63 | StringBuilder message = new StringBuilder(); 64 | message.append("Unhandled exception during tab completion for command '/").append(alias).append(' '); 65 | for (String arg : args) { 66 | message.append(arg).append(' '); 67 | } 68 | 69 | message.deleteCharAt(message.length() - 1).append("' in plugin ") 70 | .append(owningPlugin.getDescription().getFullName()); 71 | throw new CommandException(message.toString(), ex); 72 | } 73 | 74 | if (completions == null) { 75 | return super.tabComplete(sender, alias, args); 76 | } 77 | return completions; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/fr/mercury/combattag/utils/jsons/DiscUtil.java: -------------------------------------------------------------------------------- 1 | package fr.mercury.combattag.utils.jsons; 2 | 3 | import org.bukkit.plugin.java.JavaPlugin; 4 | 5 | import java.io.*; 6 | import java.util.HashMap; 7 | import java.util.Timer; 8 | import java.util.TimerTask; 9 | import java.util.concurrent.locks.Lock; 10 | import java.util.concurrent.locks.ReadWriteLock; 11 | import java.util.concurrent.locks.ReentrantReadWriteLock; 12 | 13 | public class DiscUtil { 14 | private static final String UTF8 = "UTF-8"; 15 | 16 | public static byte[] readBytes(File file) throws IOException { 17 | int length = (int) file.length(); 18 | byte[] output = new byte[length]; 19 | InputStream in = new FileInputStream(file); 20 | int offset = 0; 21 | while (offset < length) { 22 | offset += in.read(output, offset, length - offset); 23 | } 24 | in.close(); 25 | return output; 26 | } 27 | 28 | public static void writeBytes(File file, byte[] bytes) throws IOException { 29 | FileOutputStream out = new FileOutputStream(file); 30 | out.write(bytes); 31 | out.close(); 32 | } 33 | 34 | public static void write(File file, String content) throws IOException { 35 | writeBytes(file, utf8(content)); 36 | } 37 | 38 | public static String read(File file) throws IOException { 39 | return utf8(readBytes(file)); 40 | } 41 | 42 | private static HashMap locks = new HashMap<>(); 43 | 44 | public static boolean writeCatch(JavaPlugin owner, final File file, String content, boolean sync) { 45 | final byte[] bytes = utf8(content); 46 | String name = file.getName(); 47 | 48 | final Lock lock; 49 | if (locks.containsKey(name)) { 50 | lock = (Lock) locks.get(name); 51 | } else { 52 | ReadWriteLock rwl = new ReentrantReadWriteLock(); 53 | lock = rwl.writeLock(); 54 | locks.put(name, lock); 55 | } 56 | 57 | if (!sync) { 58 | lock.lock(); 59 | try { 60 | FileOutputStream out = new FileOutputStream(file); 61 | out.write(bytes); 62 | out.close(); 63 | } catch (IOException e) { 64 | e.printStackTrace(); 65 | } finally { 66 | lock.unlock(); 67 | } 68 | } else { 69 | new Timer().schedule(new TimerTask() { 70 | @Override 71 | public void run() { 72 | lock.lock(); 73 | try { 74 | FileOutputStream out = new FileOutputStream(file); 75 | out.write(bytes); 76 | out.close(); 77 | } catch (IOException e) { 78 | e.printStackTrace(); 79 | } finally { 80 | lock.unlock(); 81 | } 82 | } 83 | }, 0); 84 | } 85 | return true; 86 | } 87 | 88 | public static String readCatch(File file) { 89 | try { 90 | return read(file); 91 | } catch (IOException e) { 92 | } 93 | return null; 94 | } 95 | 96 | public static byte[] utf8(String string) { 97 | try { 98 | return string.getBytes(UTF8); 99 | } catch (UnsupportedEncodingException e) { 100 | e.printStackTrace(); 101 | } 102 | return null; 103 | } 104 | 105 | public static String utf8(byte[] bytes) { 106 | try { 107 | return new String(bytes, UTF8); 108 | } catch (UnsupportedEncodingException e) { 109 | e.printStackTrace(); 110 | } 111 | return null; 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto init 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto init 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :init 68 | @rem Get command-line arguments, handling Windows variants 69 | 70 | if not "%OS%" == "Windows_NT" goto win9xME_args 71 | 72 | :win9xME_args 73 | @rem Slurp the command line arguments. 74 | set CMD_LINE_ARGS= 75 | set _SKIP=2 76 | 77 | :win9xME_args_slurp 78 | if "x%~1" == "x" goto execute 79 | 80 | set CMD_LINE_ARGS=%* 81 | 82 | :execute 83 | @rem Setup the command line 84 | 85 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 86 | 87 | 88 | @rem Execute Gradle 89 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 90 | 91 | :end 92 | @rem End local scope for the variables with windows NT shell 93 | if "%ERRORLEVEL%"=="0" goto mainEnd 94 | 95 | :fail 96 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 97 | rem the _cmd.exe /c_ return code! 98 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 99 | exit /b 1 100 | 101 | :mainEnd 102 | if "%OS%"=="Windows_NT" endlocal 103 | 104 | :omega 105 | -------------------------------------------------------------------------------- /src/main/java/fr/mercury/combattag/engine/CombatManager.java: -------------------------------------------------------------------------------- 1 | package fr.mercury.combattag.engine; 2 | 3 | import fr.mercury.combattag.MercuryTag; 4 | import fr.mercury.combattag.engine.commands.AddBlockedCommand; 5 | import fr.mercury.combattag.engine.commands.CombatCommand; 6 | import fr.mercury.combattag.utils.Utils; 7 | import fr.mercury.combattag.utils.jsons.DiscUtil; 8 | import fr.mercury.combattag.utils.jsons.Saveable; 9 | import lombok.Getter; 10 | import net.minecraft.util.com.google.gson.reflect.TypeToken; 11 | import org.bukkit.command.CommandSender; 12 | import org.bukkit.configuration.file.FileConfiguration; 13 | import org.bukkit.entity.Player; 14 | 15 | import java.io.File; 16 | import java.util.Arrays; 17 | import java.util.HashMap; 18 | import java.util.List; 19 | import java.util.Map; 20 | import java.util.concurrent.atomic.AtomicBoolean; 21 | 22 | public class CombatManager extends Saveable { 23 | 24 | private static @Getter CombatManager instance; 25 | private Map combats; 26 | private @Getter List commands; 27 | private FileConfiguration config = MercuryTag.getInstance().getConfig(); 28 | 29 | public CombatManager(MercuryTag plugin) { 30 | super(plugin, "CombatTag"); 31 | 32 | this.instance = this; 33 | this.combats = new HashMap<>(); 34 | this.commands = Arrays.asList("/eat", "/feed", "ping", "/etpyes", "/etpaccept", "/tpyes", "/tpaccept", "/tpno", "/tpdeny", "/ct", "/combattag", "/eat", "/feed", "/near", "/msg", "/r", "/reply", "/f", "/faction", "kit", "/f"); 35 | 36 | plugin.registerCommand(new CombatCommand()); 37 | plugin.registerCommand(new AddBlockedCommand()); 38 | plugin.registerListener(new CombatListener()); 39 | } 40 | 41 | public void setCombat(Player player) { 42 | 43 | if(!this.isInCombat(player) && !player.hasPermission(this.config.getString("COMBAT.PERMISSION_BYPASS_COMBAT"))) { 44 | player.sendMessage(this.config.getString("COMBAT.START_COMBAT").replace("%time%", String.valueOf(this.config.getInt("COMBAT.COOLDOWN")))); 45 | 46 | // TODO Faire un cooldown 47 | 48 | this.combats.remove(player.getName()); 49 | this.combats.put(player.getName(), System.currentTimeMillis()); 50 | return; 51 | } 52 | 53 | this.combats.remove(player.getName()); 54 | 55 | // TODO Faire un cooldown 56 | 57 | this.combats.put(player.getName(), System.currentTimeMillis()); 58 | } 59 | 60 | public void addCommand(String string) { 61 | this.commands.add(string); 62 | } 63 | 64 | public void removeCommand(String string) { 65 | this.commands.remove(string); 66 | } 67 | 68 | public void sendList(CommandSender sender) { 69 | 70 | StringBuilder commands = new StringBuilder(); 71 | 72 | this.commands.forEach(current -> commands.append("§b§l" + current + "§7, ")); 73 | 74 | 75 | this.config.getStringList("BLOCKED_COMMANDS.MESSAGES").forEach(msg -> sender.sendMessage(msg.replace("%list%", commands.toString()))); 76 | } 77 | 78 | public int getSecondsLeft(Player player) { 79 | 80 | return (int) ((this.combats.get(player.getName()) + this.config.getInt("COMBAT.COOLDOWN") * 1000 - System.currentTimeMillis()) / 1000); 81 | } 82 | 83 | public boolean isInCombat(Player player) { 84 | 85 | if(player.hasPermission(this.config.getString("COMBAT.PERMISSION_BYPASS_COMBAT"))) 86 | return false; 87 | 88 | return this.combats.containsKey(player.getName()) && this.combats.get(player.getName()) + this.config.getInt("COMBAT.COOLDOWN") * 1000 > System.currentTimeMillis(); 89 | } 90 | 91 | public void remove(Player player) { 92 | 93 | this.combats.remove(player.getName()); 94 | player.sendMessage(this.config.getString("COMBAT.FINISH_COMBAT")); 95 | } 96 | 97 | /* 98 | @author https://github.com/QuiiBz - "Tom - QuiiBz#7533" 99 | */ 100 | public boolean canExecute(Player player, String command) { 101 | 102 | if(this.isInCombat(player)) { 103 | 104 | AtomicBoolean canExecute = new AtomicBoolean(false); 105 | 106 | this.commands.forEach(current -> { 107 | 108 | if(command.startsWith(current) && !canExecute.get()) 109 | canExecute.set(true); 110 | }); 111 | 112 | return canExecute.get(); 113 | 114 | } else 115 | this.combats.remove(player.getName()); 116 | 117 | return true; 118 | } 119 | 120 | @Override 121 | public File getFile() { 122 | return Utils.getFormatedFile("commands.json"); 123 | } 124 | 125 | @Override 126 | public void loadData() { 127 | String content = DiscUtil.readCatch(getFile()); 128 | if (content != null) { 129 | this.commands = MercuryTag.getInstance().getGson().fromJson(content, new TypeToken>() {}.getType()); 130 | } 131 | } 132 | 133 | @Override 134 | public void saveData(boolean sync) { 135 | DiscUtil.writeCatch(MercuryTag.getInstance(), this.getFile(), MercuryTag.getInstance().getGson().toJson(this.commands), sync); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /src/main/java/fr/mercury/combattag/engine/CombatListener.java: -------------------------------------------------------------------------------- 1 | package fr.mercury.combattag.engine; 2 | 3 | import com.google.common.collect.ImmutableSet; 4 | import fr.mercury.combattag.MercuryTag; 5 | import org.bukkit.Bukkit; 6 | import org.bukkit.configuration.file.FileConfiguration; 7 | import org.bukkit.entity.*; 8 | import org.bukkit.event.EventHandler; 9 | import org.bukkit.event.EventPriority; 10 | import org.bukkit.event.Listener; 11 | import org.bukkit.event.entity.EntityDamageByEntityEvent; 12 | import org.bukkit.event.entity.PlayerDeathEvent; 13 | import org.bukkit.event.entity.PotionSplashEvent; 14 | import org.bukkit.event.entity.ProjectileLaunchEvent; 15 | import org.bukkit.event.player.PlayerCommandPreprocessEvent; 16 | import org.bukkit.event.player.PlayerKickEvent; 17 | import org.bukkit.event.player.PlayerQuitEvent; 18 | import org.bukkit.potion.PotionEffect; 19 | import org.bukkit.potion.PotionEffectType; 20 | import org.bukkit.projectiles.ProjectileSource; 21 | 22 | import java.util.Set; 23 | 24 | public class CombatListener implements Listener { 25 | 26 | private FileConfiguration config = MercuryTag.getInstance().getConfig(); 27 | 28 | private CombatManager combatManager = CombatManager.getInstance(); 29 | private final Set harmfulEffects = ImmutableSet.of( 30 | PotionEffectType.BLINDNESS, 31 | PotionEffectType.CONFUSION, 32 | PotionEffectType.HARM, 33 | PotionEffectType.HUNGER, 34 | PotionEffectType.POISON, 35 | PotionEffectType.SLOW, 36 | PotionEffectType.SLOW_DIGGING, 37 | PotionEffectType.WEAKNESS, 38 | PotionEffectType.WITHER 39 | ); 40 | 41 | @EventHandler 42 | public void onKick(PlayerKickEvent event) { 43 | 44 | Player player = event.getPlayer(); 45 | 46 | if(this.combatManager.isInCombat(player) && this.config.getBoolean("EVENTS.DISABLE_TAG_ON_KICK")) 47 | this.combatManager.remove(player); 48 | } 49 | 50 | @EventHandler(priority = EventPriority.HIGHEST) 51 | public void onDamage(EntityDamageByEntityEvent event) { 52 | 53 | if(event.getEntity() instanceof Player && event.getDamager() instanceof Projectile) { 54 | Player damaged = (Player) event.getEntity(); 55 | Entity damager = event.getDamager(); 56 | 57 | if(damager.getType() == EntityType.ENDER_PEARL) 58 | return; 59 | 60 | if(damager.getType() == EntityType.FISHING_HOOK) 61 | return; 62 | 63 | this.combatManager.setCombat(damaged); 64 | return; 65 | } 66 | 67 | if(event.getEntity() instanceof Player && event.getDamager() instanceof Player && !event.isCancelled()) { 68 | 69 | Player damaged = (Player) event.getEntity(); 70 | Player damager = (Player) event.getDamager(); 71 | 72 | this.combatManager.setCombat(damaged); 73 | this.combatManager.setCombat(damager); 74 | } 75 | } 76 | 77 | @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) 78 | public void tagPlayer(PotionSplashEvent event) { 79 | 80 | ProjectileSource source = event.getEntity().getShooter(); 81 | if (!(source instanceof Player)) return; 82 | 83 | if(!config.getBoolean("EVENTS.TAG_IF_POTION_AFFECT")) return; 84 | 85 | Player attacker = (Player) source; 86 | boolean isHarmful = false; 87 | 88 | for (PotionEffect effect : event.getPotion().getEffects()) { 89 | if (harmfulEffects.contains(effect.getType())) { 90 | isHarmful = true; 91 | break; 92 | } 93 | } 94 | 95 | // Ne rien faire si la potion n'a pas d'effet néfaste. 96 | if (!isHarmful) return; 97 | 98 | // Tag les joueurs qui sont affectés par la potion. 99 | for (LivingEntity entity : event.getAffectedEntities()) { 100 | if (!(entity instanceof Player)) continue; 101 | 102 | Player victim = (Player) entity; 103 | if (victim == attacker) continue; 104 | 105 | this.combatManager.setCombat(((Player) entity).getPlayer()); 106 | } 107 | 108 | this.combatManager.setCombat(attacker); 109 | 110 | } 111 | 112 | @EventHandler 113 | public void onLaunch(ProjectileLaunchEvent event) { 114 | 115 | Projectile entity = event.getEntity(); 116 | 117 | if (entity.getType() != EntityType.ENDER_PEARL) return; 118 | 119 | if (!(entity.getShooter() instanceof Player)) return; 120 | 121 | } 122 | 123 | @EventHandler 124 | public void onCommand(PlayerCommandPreprocessEvent event) { 125 | 126 | Player player = event.getPlayer(); 127 | String command = event.getMessage(); 128 | 129 | if(!this.combatManager.canExecute(player, command)) { 130 | 131 | event.setCancelled(true); 132 | player.sendMessage(this.config.getString("COMBAT.CANT_EXECUTE_COMMAND")); 133 | 134 | } else 135 | event.setCancelled(false); 136 | } 137 | 138 | @EventHandler 139 | public void onLeave(PlayerQuitEvent event) { 140 | 141 | Player player = event.getPlayer(); 142 | 143 | if(this.combatManager.isInCombat(player)) { 144 | 145 | player.setHealth(0); 146 | 147 | if(config.getBoolean("EVENTS.ENABLE_DEATHMESSAGE_ON_QUIT_EVENT")) 148 | Bukkit.broadcastMessage(this.config.getString("EVENTS.BROADCAST_MESSAGE_ON_QUIT").replace("%player%", player.getName())); 149 | } 150 | } 151 | 152 | @EventHandler 153 | public void onDeath(PlayerDeathEvent event) { 154 | 155 | Player player = event.getEntity(); 156 | Player killer = event.getEntity().getKiller(); 157 | 158 | String deathMessage = killer != null ? this.config.getString("EVENTS.DEATHMESSAGE_DUO").replace("%death%", player.getName()).replace("%killer%", killer.getName()) : "§7" + this.config.getString("EVENTS.DEATHMESSAGE_SOLO").replace("%death%", player.getName()); 159 | 160 | event.setDeathMessage(null); 161 | 162 | if(this.config.getBoolean("EVENTS.ENABLE_DEATHMESSAGE")) 163 | Bukkit.broadcastMessage(deathMessage); 164 | 165 | if(this.combatManager.isInCombat(player)) { 166 | 167 | this.combatManager.remove(player); 168 | } 169 | } 170 | 171 | 172 | } 173 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /src/main/java/fr/mercury/combattag/utils/commands/CommandFramework.java: -------------------------------------------------------------------------------- 1 | package fr.mercury.combattag.utils.commands; 2 | 3 | import org.bukkit.ChatColor; 4 | import org.bukkit.command.CommandExecutor; 5 | import org.bukkit.command.CommandMap; 6 | import org.bukkit.command.CommandSender; 7 | import org.bukkit.command.PluginCommand; 8 | import org.bukkit.entity.Player; 9 | import org.bukkit.plugin.Plugin; 10 | import org.bukkit.plugin.SimplePluginManager; 11 | 12 | import java.lang.reflect.Field; 13 | import java.lang.reflect.InvocationTargetException; 14 | import java.lang.reflect.Method; 15 | import java.util.AbstractMap; 16 | import java.util.HashMap; 17 | import java.util.List; 18 | import java.util.Map; 19 | 20 | public class CommandFramework implements CommandExecutor { 21 | private Map> commandMap = new HashMap<>(); 22 | public CommandMap map; 23 | private Plugin plugin; 24 | 25 | public CommandFramework(Plugin plugin) { 26 | this.plugin = plugin; 27 | if ((plugin.getServer().getPluginManager() instanceof SimplePluginManager)) { 28 | SimplePluginManager manager = (SimplePluginManager) plugin.getServer().getPluginManager(); 29 | try { 30 | Field field = SimplePluginManager.class.getDeclaredField("commandMap"); 31 | field.setAccessible(true); 32 | map = ((CommandMap) field.get(manager)); 33 | } catch (IllegalArgumentException e) { 34 | e.printStackTrace(); 35 | } catch (SecurityException e) { 36 | e.printStackTrace(); 37 | } catch (IllegalAccessException e) { 38 | e.printStackTrace(); 39 | } catch (NoSuchFieldException e) { 40 | e.printStackTrace(); 41 | } 42 | } 43 | } 44 | 45 | @Override 46 | public boolean onCommand(CommandSender sender, org.bukkit.command.Command cmd, String label, String[] args) { 47 | return handleCommand(sender, cmd, label, args); 48 | } 49 | 50 | public boolean handleCommand(CommandSender sender, org.bukkit.command.Command cmd, String label, String[] args) { 51 | for (int i = args.length; i >= 0; i--) { 52 | StringBuffer buffer = new StringBuffer(); 53 | buffer.append(label.toLowerCase()); 54 | for (int x = 0; x < i; x++) { 55 | buffer.append("." + args[x].toLowerCase()); 56 | } 57 | String cmdLabel = buffer.toString(); 58 | if (commandMap.containsKey(cmdLabel)) { 59 | Method method = commandMap.get(cmdLabel).getKey(); 60 | Object methodObject = commandMap.get(cmdLabel).getValue(); 61 | Command command = method.getAnnotation(Command.class); 62 | boolean hasPerm = true; 63 | 64 | if ((!command.isConsole()) && (!(sender instanceof Player))) { 65 | sender.sendMessage("Commande uniquement disponible en jeu"); 66 | return true; 67 | } 68 | 69 | if (!command.permissionNode().isEmpty()) { 70 | if ((command.permissionNode().equalsIgnoreCase("op")) && (!sender.isOp())) { 71 | hasPerm = false; 72 | } else if (!sender.hasPermission(command.permissionNode())) { 73 | hasPerm = false; 74 | } 75 | } 76 | 77 | if (!hasPerm) { 78 | sender.sendMessage(ChatColor.RED 79 | + "Vous n'avez pas la permission d'executer cette commande."); 80 | return true; 81 | } 82 | 83 | CommandArgs commandArgs = new CommandArgs(sender, cmd, label, args, cmdLabel.split("\\.").length - 1); 84 | try { 85 | method.invoke(methodObject, new Object[] { commandArgs }); 86 | } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) { 87 | e.printStackTrace(); 88 | } 89 | return true; 90 | } 91 | } 92 | defaultCommand(new CommandArgs(sender, cmd, label, args, 0)); 93 | return true; 94 | } 95 | 96 | public void registerCommands(Object obj) { 97 | for (Method m : obj.getClass().getMethods()) { 98 | if (m.getAnnotation(Command.class) != null) { 99 | Command command = m 100 | .getAnnotation(Command.class); 101 | if ((m.getParameterTypes().length > 1) || (m.getParameterTypes()[0] != CommandArgs.class)) { 102 | System.out.println("Unable to register command " + m.getName() + ". Unexpected method arguments"); 103 | } else { 104 | 105 | for (String alias : command.name()) 106 | registerCommand(command, alias, m, obj); 107 | } 108 | } else if (m.getAnnotation(Completer.class) != null) { 109 | Completer comp = m.getAnnotation(Completer.class); 110 | if ((m.getParameterTypes().length > 1) || (m.getParameterTypes().length == 0) 111 | || (m.getParameterTypes()[0] != CommandArgs.class)) { 112 | System.out.println( 113 | "Unable to register tab completer " + m.getName() + ". Unexpected method arguments"); 114 | 115 | } else if (m.getReturnType() != List.class) { 116 | System.out.println("Unable to register tab completer " + m.getName() + ". Unexpected return type"); 117 | } else { 118 | for (String alias : comp.name()) { 119 | registerCompleter(alias, m, obj); 120 | } 121 | } 122 | } 123 | } 124 | } 125 | 126 | public void registerCommand(Command command, String label, Method m, Object obj) { 127 | 128 | commandMap.put(label.toLowerCase(), new AbstractMap.SimpleEntry(m, obj)); 129 | commandMap.put(this.plugin.getName() + ':' + label.toLowerCase(), new AbstractMap.SimpleEntry(m, obj)); 130 | String cmdLabel = label.split("\\.")[0].toLowerCase(); 131 | 132 | if (map.getCommand(cmdLabel) == null) { 133 | org.bukkit.command.Command cmd = new BukkitCommand(cmdLabel, this, plugin); 134 | map.register(plugin.getName(), cmd); 135 | } 136 | } 137 | 138 | public void registerCompleter(String label, Method m, Object obj) { 139 | String cmdLabel = label.split("\\.")[0].toLowerCase(); 140 | if (map.getCommand(cmdLabel) == null) { 141 | org.bukkit.command.Command command = new BukkitCommand(cmdLabel, this, plugin); 142 | map.register(plugin.getName(), command); 143 | } 144 | 145 | if (map.getCommand(cmdLabel) instanceof BukkitCommand) { 146 | BukkitCommand command = (BukkitCommand) map.getCommand(cmdLabel); 147 | if (command.completer == null) { 148 | command.completer = new BukkitCompleter(); 149 | } 150 | command.completer.addCompleter(label, m, obj); 151 | } else if (map.getCommand(cmdLabel) instanceof PluginCommand) { 152 | try { 153 | Object command = map.getCommand(cmdLabel); 154 | Field field = command.getClass().getDeclaredField("completer"); 155 | field.setAccessible(true); 156 | if (field.get(command) == null) { 157 | BukkitCompleter completer = new BukkitCompleter(); 158 | completer.addCompleter(label, m, obj); 159 | field.set(command, completer); 160 | } else if (field.get(command) instanceof BukkitCompleter) { 161 | BukkitCompleter completer = (BukkitCompleter) field.get(command); 162 | completer.addCompleter(label, m, obj); 163 | } else { 164 | System.out.println("Unable to register tab completer " + m.getName() 165 | + ". A tab completer is already registered for that command!"); 166 | } 167 | } catch (Exception ex) { 168 | ex.printStackTrace(); 169 | } 170 | } 171 | } 172 | private void defaultCommand(CommandArgs args) { 173 | args.getSender().sendMessage(args.getLabel() + " is not handled! Oh noes!"); 174 | } 175 | } 176 | --------------------------------------------------------------------------------