From 7731d33d844ebaf29b6cd642e13362f4b0a9e14e Mon Sep 17 00:00:00 2001 From: RealizedMC Date: Mon, 6 Aug 2018 22:53:10 +0900 Subject: [PATCH] v3.1.2 - projectile hit message, 1.8.3 compat, api changes, and bug fixes --- build.gradle | 2 +- .../java/me/realized/duels/api/Duels.java | 33 +++++++---- .../duels/api/extension/DuelsExtension.java | 9 ++- .../java/me/realized/duels/DuelsPlugin.java | 55 +++++++++++++------ .../java/me/realized/duels/config/Config.java | 6 ++ .../java/me/realized/duels/data/ItemData.java | 4 +- .../duels/extension/ExtensionClassLoader.java | 2 +- .../duels/extra/ProjectileHitListener.java | 49 +++++++++++++++++ .../me/realized/duels/extra/Teleport.java | 2 +- .../gui/inventory/buttons/EffectsButton.java | 2 +- .../buttons/PotionCounterButton.java | 2 +- .../me/realized/duels/kit/KitManager.java | 2 - .../me/realized/duels/logging/LogManager.java | 14 ++++- .../duels/util/compat/CompatBase.java | 7 ++- .../duels/util/compat/CompatUtil.java | 9 ++- .../duels/util/compat/ReflectionUtil.java | 17 ++++-- duels-plugin/src/main/resources/config.yml | 9 ++- duels-plugin/src/main/resources/lang.yml | 13 +++-- 18 files changed, 180 insertions(+), 57 deletions(-) create mode 100644 duels-plugin/src/main/java/me/realized/duels/extra/ProjectileHitListener.java diff --git a/build.gradle b/build.gradle index 88985432..8d389b1d 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ buildscript { allprojects { group 'me.realized' - version '3.1.1' + version '3.1.2' } subprojects { diff --git a/duels-api/src/main/java/me/realized/duels/api/Duels.java b/duels-api/src/main/java/me/realized/duels/api/Duels.java index 60ebbc87..daf0a675 100644 --- a/duels-api/src/main/java/me/realized/duels/api/Duels.java +++ b/duels-api/src/main/java/me/realized/duels/api/Duels.java @@ -1,11 +1,11 @@ package me.realized.duels.api; import javax.annotation.Nonnull; -import lombok.NonNull; import me.realized.duels.api.arena.ArenaManager; import me.realized.duels.api.command.SubCommand; import me.realized.duels.api.kit.KitManager; import me.realized.duels.api.user.UserManager; +import org.bukkit.event.Listener; import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitTask; @@ -31,6 +31,13 @@ public interface Duels extends Plugin { boolean registerSubCommand(@Nonnull final String command, @Nonnull final SubCommand subCommand); + /** + * @param listener Listener to register + * @since 3.1.2 + */ + void registerListener(@Nonnull final Listener listener); + + /** * Reloads the plugin. * @@ -46,7 +53,7 @@ public interface Duels extends Plugin { * @return BukkitTask executed * @since 3.1.0 */ - BukkitTask doSync(@NonNull final Runnable task); + BukkitTask doSync(@Nonnull final Runnable task); /** @@ -57,7 +64,7 @@ public interface Duels extends Plugin { * @return BukkitTask executed * @since 3.1.0 */ - BukkitTask doSyncAfter(@NonNull final Runnable task, long delay); + BukkitTask doSyncAfter(@Nonnull final Runnable task, long delay); /** @@ -69,7 +76,7 @@ public interface Duels extends Plugin { * @return BukkitTask executed * @since 3.1.0 */ - BukkitTask doSyncRepeat(@NonNull final Runnable task, long delay, long interval); + BukkitTask doSyncRepeat(@Nonnull final Runnable task, long delay, long interval); /** @@ -79,7 +86,7 @@ public interface Duels extends Plugin { * @return BukkitTask executed * @since 3.1.0 */ - BukkitTask doAsync(@NonNull final Runnable task); + BukkitTask doAsync(@Nonnull final Runnable task); /** @@ -90,7 +97,7 @@ public interface Duels extends Plugin { * @return BukkitTask executed * @since 3.1.0 */ - BukkitTask doAsyncAfter(@NonNull final Runnable task, long delay); + BukkitTask doAsyncAfter(@Nonnull final Runnable task, long delay); /** @@ -102,31 +109,35 @@ public interface Duels extends Plugin { * @return BukkitTask executed * @since 3.1.0 */ - BukkitTask doAsyncRepeat(@NonNull final Runnable task, long delay, long interval); + BukkitTask doAsyncRepeat(@Nonnull final Runnable task, long delay, long interval); /** * @param message message to log + * @since 3.1.0 */ - void info(@NonNull final String message); + void info(@Nonnull final String message); /** * @param message message to log + * @since 3.1.0 */ - void warn(@NonNull final String message); + void warn(@Nonnull final String message); /** * @param message message to log + * @since 3.1.0 */ - void error(@NonNull final String message); + void error(@Nonnull final String message); /** * @param message message to log + * @since 3.1.0 */ - void error(@NonNull final String message, @NonNull Throwable thrown); + void error(@Nonnull final String message, @Nonnull Throwable thrown); /** diff --git a/duels-api/src/main/java/me/realized/duels/api/extension/DuelsExtension.java b/duels-api/src/main/java/me/realized/duels/api/extension/DuelsExtension.java index 5b6f391e..ac690017 100644 --- a/duels-api/src/main/java/me/realized/duels/api/extension/DuelsExtension.java +++ b/duels-api/src/main/java/me/realized/duels/api/extension/DuelsExtension.java @@ -8,10 +8,9 @@ import java.net.URL; import java.net.URLConnection; import java.util.Objects; -import java.util.logging.Level; +import javax.annotation.Nonnull; import javax.annotation.Nullable; import lombok.Getter; -import lombok.NonNull; import me.realized.duels.api.Duels; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; @@ -43,7 +42,7 @@ final void init(final Duels api, final String name, final File folder, final Fil this.configFile = new File(dataFolder, "config.yml"); } - @NonNull + @Nonnull public final String getName() { return name; } @@ -62,7 +61,7 @@ public final void setEnabled(final boolean enabled) { this.enabled = enabled; } - public void saveResource(@NonNull String resourcePath) { + public void saveResource(@Nonnull String resourcePath) { Objects.requireNonNull(resourcePath, "resourcePath"); resourcePath = resourcePath.replace('\\', '/'); @@ -97,7 +96,7 @@ public void saveResource(@NonNull String resourcePath) { } @Nullable - public InputStream getResource(@NonNull final String filename) { + public InputStream getResource(@Nonnull final String filename) { Objects.requireNonNull(filename, "filename"); try { diff --git a/duels-plugin/src/main/java/me/realized/duels/DuelsPlugin.java b/duels-plugin/src/main/java/me/realized/duels/DuelsPlugin.java index 00a5de45..88f4fec7 100644 --- a/duels-plugin/src/main/java/me/realized/duels/DuelsPlugin.java +++ b/duels-plugin/src/main/java/me/realized/duels/DuelsPlugin.java @@ -13,7 +13,6 @@ import java.util.stream.Collectors; import javax.annotation.Nonnull; import lombok.Getter; -import lombok.NonNull; import me.realized.duels.api.Duels; import me.realized.duels.api.command.SubCommand; import me.realized.duels.arena.ArenaManager; @@ -25,10 +24,12 @@ import me.realized.duels.config.Lang; import me.realized.duels.data.UserManager; import me.realized.duels.duel.DuelManager; +import me.realized.duels.extension.ExtensionClassLoader; import me.realized.duels.extension.ExtensionManager; import me.realized.duels.extra.DamageListener; import me.realized.duels.extra.KitItemListener; import me.realized.duels.extra.PotionListener; +import me.realized.duels.extra.ProjectileHitListener; import me.realized.duels.extra.SoupListener; import me.realized.duels.extra.Teleport; import me.realized.duels.extra.TeleportListener; @@ -50,6 +51,8 @@ import me.realized.duels.util.command.AbstractCommand; import me.realized.duels.util.gui.GuiListener; import org.bukkit.command.CommandSender; +import org.bukkit.event.HandlerList; +import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitTask; import org.inventivetalent.update.spiget.SpigetUpdate; @@ -106,6 +109,7 @@ public class DuelsPlugin extends JavaPlugin implements Duels, LogSource { @Getter private ExtensionManager extensionManager; private final Map> commands = new HashMap<>(); + private final List registeredListeners = new ArrayList<>(); @Getter private volatile boolean updateAvailable; @@ -147,12 +151,6 @@ public void onEnable() { loadables.add(teleport = new Teleport(this)); loadables.add(extensionManager = new ExtensionManager(this)); - registerCommands( - new DuelCommand(this), - new SpectateCommand(this), - new DuelsCommand(this) - ); - if (!load()) { getPluginLoader().disablePlugin(this); return; @@ -163,6 +161,7 @@ public void onEnable() { new PotionListener(this); new TeleportListener(this); new SoupListener(this); + new ProjectileHitListener(this); new Metrics(this); @@ -202,6 +201,12 @@ public void onDisable() { * @return true if load was successful, otherwise false */ private boolean load() { + registerCommands( + new DuelCommand(this), + new SpectateCommand(this), + new DuelsCommand(this) + ); + for (final Loadable loadable : loadables) { try { loadable.handleLoad(); @@ -220,6 +225,15 @@ private boolean load() { * @return true if unload was successful, otherwise false */ private boolean unload() { + registeredListeners.forEach(HandlerList::unregisterAll); + registeredListeners.clear(); + // Unregister all extension listeners that isn't using the method Duels#registerListener + HandlerList.getRegisteredListeners(this) + .stream() + .filter(listener -> listener.getListener().getClass().getClassLoader().getClass().isAssignableFrom(ExtensionClassLoader.class)) + .forEach(listener -> HandlerList.unregisterAll(listener.getListener())); + commands.clear(); + for (final Loadable loadable : Lists.reverse(loadables)) { try { if (loadables.indexOf(loadable) > lastLoad) { @@ -265,6 +279,13 @@ protected void execute(final CommandSender sender, final String label, final Str return true; } + @Override + public void registerListener(@Nonnull final Listener listener) { + Objects.requireNonNull(listener, "listener"); + registeredListeners.add(listener); + getServer().getPluginManager().registerEvents(listener, this); + } + @Override public boolean reload() { if (!(unload() && load())) { @@ -296,61 +317,61 @@ public boolean reload(final Loadable loadable) { } @Override - public BukkitTask doSync(@NonNull final Runnable task) { + public BukkitTask doSync(@Nonnull final Runnable task) { Objects.requireNonNull(task, "task"); return getServer().getScheduler().runTask(this, task); } @Override - public BukkitTask doSyncAfter(@NonNull final Runnable task, final long delay) { + public BukkitTask doSyncAfter(@Nonnull final Runnable task, final long delay) { Objects.requireNonNull(task, "task"); return getServer().getScheduler().runTaskLater(this, task, delay); } @Override - public BukkitTask doSyncRepeat(@NonNull final Runnable task, final long delay, final long period) { + public BukkitTask doSyncRepeat(@Nonnull final Runnable task, final long delay, final long period) { Objects.requireNonNull(task, "task"); return getServer().getScheduler().runTaskTimer(this, task, delay, period); } @Override - public BukkitTask doAsync(@NonNull final Runnable task) { + public BukkitTask doAsync(@Nonnull final Runnable task) { Objects.requireNonNull(task, "task"); return getServer().getScheduler().runTaskAsynchronously(this, task); } @Override - public BukkitTask doAsyncAfter(@NonNull final Runnable task, final long delay) { + public BukkitTask doAsyncAfter(@Nonnull final Runnable task, final long delay) { Objects.requireNonNull(task, "task"); return getServer().getScheduler().runTaskLaterAsynchronously(this, task, delay); } @Override - public BukkitTask doAsyncRepeat(@NonNull final Runnable task, final long delay, final long period) { + public BukkitTask doAsyncRepeat(@Nonnull final Runnable task, final long delay, final long period) { Objects.requireNonNull(task, "task"); return getServer().getScheduler().runTaskTimerAsynchronously(this, task, delay, period); } @Override - public void info(@NonNull final String message) { + public void info(@Nonnull final String message) { Objects.requireNonNull(message, "message"); Log.info(message); } @Override - public void warn(@NonNull final String message) { + public void warn(@Nonnull final String message) { Objects.requireNonNull(message, "message"); Log.warn(message); } @Override - public void error(@NonNull final String message) { + public void error(@Nonnull final String message) { Objects.requireNonNull(message, "message"); Log.error(message); } @Override - public void error(@NonNull final String message, @NonNull final Throwable thrown) { + public void error(@Nonnull final String message, @Nonnull final Throwable thrown) { Objects.requireNonNull(message, "message"); Objects.requireNonNull(thrown, "thrown"); Log.error(message, thrown); diff --git a/duels-plugin/src/main/java/me/realized/duels/config/Config.java b/duels-plugin/src/main/java/me/realized/duels/config/Config.java index 716d7ec5..3c754689 100644 --- a/duels-plugin/src/main/java/me/realized/duels/config/Config.java +++ b/duels-plugin/src/main/java/me/realized/duels/config/Config.java @@ -83,6 +83,10 @@ public class Config extends AbstractConfiguration { @Getter private List endCommands; @Getter + private boolean projectileHitMessageEnabled; + @Getter + private List projectileHitMessageTypes; + @Getter private boolean preventInventoryOpen; @Getter private boolean removeEmptyBottle; @@ -224,6 +228,8 @@ protected void loadValues(FileConfiguration configuration) throws Exception { startCommands = configuration.getStringList("duel.match.start-commands.commands"); endCommandsEnabled = configuration.getBoolean("duel.match.end-commands.enabled", false); endCommands = configuration.getStringList("duel.match.end-commands.commands"); + projectileHitMessageEnabled = configuration.getBoolean("duel.projectile-hit-message.enabled", true); + projectileHitMessageTypes = configuration.getStringList("duel.projectile-hit-message.types"); preventInventoryOpen = configuration.getBoolean("duel.prevent-inventory-open", true); removeEmptyBottle = configuration.getBoolean("duel.remove-empty-bottle", true); preventTpToMatchPlayers = configuration.getBoolean("duel.prevent-teleport-to-match-players", true); diff --git a/duels-plugin/src/main/java/me/realized/duels/data/ItemData.java b/duels-plugin/src/main/java/me/realized/duels/data/ItemData.java index 487a5a16..ebc653b5 100644 --- a/duels-plugin/src/main/java/me/realized/duels/data/ItemData.java +++ b/duels-plugin/src/main/java/me/realized/duels/data/ItemData.java @@ -112,7 +112,7 @@ public ItemData(final ItemStack item) { lore = StringUtil.reverseColor(meta.getLore()); } - if (!CompatUtil.isPre1_8() && !meta.getItemFlags().isEmpty()) { + if (CompatUtil.hasItemFlag() && !meta.getItemFlags().isEmpty()) { this.flags = new ArrayList<>(); for (final ItemFlag flag : meta.getItemFlags()) { @@ -216,7 +216,7 @@ public ItemStack toItemStack() { meta.setLore(StringUtil.color(lore)); } - if (!CompatUtil.isPre1_8() && flags != null && !flags.isEmpty()) { + if (CompatUtil.hasItemFlag() && flags != null && !flags.isEmpty()) { for (final String flag : flags) { meta.addItemFlags(ItemFlag.valueOf(flag)); } diff --git a/duels-plugin/src/main/java/me/realized/duels/extension/ExtensionClassLoader.java b/duels-plugin/src/main/java/me/realized/duels/extension/ExtensionClassLoader.java index 381a7371..be99401c 100644 --- a/duels-plugin/src/main/java/me/realized/duels/extension/ExtensionClassLoader.java +++ b/duels-plugin/src/main/java/me/realized/duels/extension/ExtensionClassLoader.java @@ -18,7 +18,7 @@ import me.realized.duels.api.extension.DuelsExtension; import sun.misc.CompoundEnumeration; -class ExtensionClassLoader extends URLClassLoader { +public class ExtensionClassLoader extends URLClassLoader { private final JarFile jar; private final Manifest manifest; diff --git a/duels-plugin/src/main/java/me/realized/duels/extra/ProjectileHitListener.java b/duels-plugin/src/main/java/me/realized/duels/extra/ProjectileHitListener.java new file mode 100644 index 00000000..33638133 --- /dev/null +++ b/duels-plugin/src/main/java/me/realized/duels/extra/ProjectileHitListener.java @@ -0,0 +1,49 @@ +package me.realized.duels.extra; + +import me.realized.duels.DuelsPlugin; +import me.realized.duels.arena.ArenaManager; +import me.realized.duels.config.Config; +import me.realized.duels.config.Lang; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.entity.Projectile; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageByEntityEvent; + +public class ProjectileHitListener implements Listener { + + private final Config config; + private final Lang lang; + private final ArenaManager arenaManager; + + public ProjectileHitListener(final DuelsPlugin plugin) { + this.config = plugin.getConfiguration(); + this.lang = plugin.getLang(); + this.arenaManager = plugin.getArenaManager(); + + if (plugin.getConfiguration().isProjectileHitMessageEnabled()) { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void on(final EntityDamageByEntityEvent event) { + if (!(event.getEntity() instanceof LivingEntity + && event.getDamager() instanceof Projectile && config.getProjectileHitMessageTypes().contains(event.getDamager().getType().name()) + && ((Projectile) event.getDamager()).getShooter() instanceof Player)) { + return; + } + + final Player player = (Player) ((Projectile) event.getDamager()).getShooter(); + + if (!arenaManager.isInMatch(player)) { + return; + } + + final LivingEntity entity = (LivingEntity) event.getEntity(); + final double health = Math.max(Math.ceil(entity.getHealth() - event.getFinalDamage()) * 0.5, 0); + lang.sendMessage(player, "DUEL.projectile-hit-message", "name", entity.getName(), "health", health); + } +} diff --git a/duels-plugin/src/main/java/me/realized/duels/extra/Teleport.java b/duels-plugin/src/main/java/me/realized/duels/extra/Teleport.java index 6798f1c3..f8e23e2e 100644 --- a/duels-plugin/src/main/java/me/realized/duels/extra/Teleport.java +++ b/duels-plugin/src/main/java/me/realized/duels/extra/Teleport.java @@ -22,7 +22,7 @@ */ public final class Teleport implements Loadable, Listener { - public static final String METADATA_KEY = Teleport.class.getSimpleName(); + static final String METADATA_KEY = Teleport.class.getSimpleName(); private final DuelsPlugin plugin; diff --git a/duels-plugin/src/main/java/me/realized/duels/gui/inventory/buttons/EffectsButton.java b/duels-plugin/src/main/java/me/realized/duels/gui/inventory/buttons/EffectsButton.java index b6d2d2a0..e8e81199 100644 --- a/duels-plugin/src/main/java/me/realized/duels/gui/inventory/buttons/EffectsButton.java +++ b/duels-plugin/src/main/java/me/realized/duels/gui/inventory/buttons/EffectsButton.java @@ -24,7 +24,7 @@ public EffectsButton(final DuelsPlugin plugin, final Player player) { "duration", (effect.getDuration() / 20))).collect(Collectors.toList())) .build()); editMeta(meta -> { - if (!CompatUtil.isPre1_8()) { + if (CompatUtil.hasItemFlag()) { meta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS); } }); diff --git a/duels-plugin/src/main/java/me/realized/duels/gui/inventory/buttons/PotionCounterButton.java b/duels-plugin/src/main/java/me/realized/duels/gui/inventory/buttons/PotionCounterButton.java index 6fcc0bca..441db644 100644 --- a/duels-plugin/src/main/java/me/realized/duels/gui/inventory/buttons/PotionCounterButton.java +++ b/duels-plugin/src/main/java/me/realized/duels/gui/inventory/buttons/PotionCounterButton.java @@ -16,7 +16,7 @@ public PotionCounterButton(final DuelsPlugin plugin, final int count) { .build() ); editMeta(meta -> { - if (!CompatUtil.isPre1_8()) { + if (CompatUtil.hasItemFlag()) { meta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS); } }); diff --git a/duels-plugin/src/main/java/me/realized/duels/kit/KitManager.java b/duels-plugin/src/main/java/me/realized/duels/kit/KitManager.java index 258d3c96..ee66cd3b 100644 --- a/duels-plugin/src/main/java/me/realized/duels/kit/KitManager.java +++ b/duels-plugin/src/main/java/me/realized/duels/kit/KitManager.java @@ -1,6 +1,5 @@ package me.realized.duels.kit; -import com.google.common.collect.Lists; import com.google.gson.reflect.TypeToken; import java.io.File; import java.io.FileInputStream; @@ -11,7 +10,6 @@ import java.io.Reader; import java.io.Writer; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; diff --git a/duels-plugin/src/main/java/me/realized/duels/logging/LogManager.java b/duels-plugin/src/main/java/me/realized/duels/logging/LogManager.java index 95af68ee..050a32bf 100644 --- a/duels-plugin/src/main/java/me/realized/duels/logging/LogManager.java +++ b/duels-plugin/src/main/java/me/realized/duels/logging/LogManager.java @@ -2,6 +2,8 @@ import java.io.File; import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; import java.util.Date; import java.util.logging.FileHandler; import java.util.logging.Formatter; @@ -52,7 +54,17 @@ public void handleLoad() throws IOException { handler.setFormatter(new Formatter() { @Override public String format(final LogRecord record) { - return "[" + DateUtil.formatDatetime(record.getMillis()) + "] [" + record.getLevel().getName() + "] " + record.getMessage() + '\n'; + String thrown = ""; + + if (record.getThrown() != null) { + final StringWriter stringWriter = new StringWriter(); + final PrintWriter printWriter = new PrintWriter(stringWriter); + record.getThrown().printStackTrace(printWriter); + printWriter.close(); + thrown = stringWriter.toString(); + } + + return "[" + DateUtil.formatDatetime(record.getMillis()) + "] [" + record.getLevel().getName() + "] " + record.getMessage() + '\n' + thrown; } }); logger.addHandler(handler); diff --git a/duels-plugin/src/main/java/me/realized/duels/util/compat/CompatBase.java b/duels-plugin/src/main/java/me/realized/duels/util/compat/CompatBase.java index 37d9feed..a92c7d68 100644 --- a/duels-plugin/src/main/java/me/realized/duels/util/compat/CompatBase.java +++ b/duels-plugin/src/main/java/me/realized/duels/util/compat/CompatBase.java @@ -35,8 +35,8 @@ class CompatBase { static final Method GET_ONLINE_PLAYERS; static final Class TITLE_PACKET; - static final Class TITLE_ACTIONS; static final Class CHAT_COMPONENT; + static Class TITLE_ACTIONS; static final Class CHAT_SERIALIZER; static { @@ -77,9 +77,12 @@ class CompatBase { boolean pre1_8 = CompatUtil.isPre1_8(); TITLE_PACKET = !pre1_8 ? ReflectionUtil.getNMSClass("PacketPlayOutTitle") : null; - TITLE_ACTIONS = !pre1_8 ? ReflectionUtil.getNMSClass("PacketPlayOutTitle$EnumTitleAction") : null; CHAT_COMPONENT = !pre1_8 ? ReflectionUtil.getNMSClass("IChatBaseComponent") : null; + TITLE_ACTIONS = !pre1_8 ? ReflectionUtil.getNMSClass("PacketPlayOutTitle$EnumTitleAction") : null; CHAT_SERIALIZER = !pre1_8 ? ReflectionUtil.getNMSClass("ChatComponentText") : null; + if (!pre1_8 && TITLE_ACTIONS == null) { + TITLE_ACTIONS = ReflectionUtil.getNMSClass("EnumTitleAction"); + } } } diff --git a/duels-plugin/src/main/java/me/realized/duels/util/compat/CompatUtil.java b/duels-plugin/src/main/java/me/realized/duels/util/compat/CompatUtil.java index 0d9401ca..96d7713e 100644 --- a/duels-plugin/src/main/java/me/realized/duels/util/compat/CompatUtil.java +++ b/duels-plugin/src/main/java/me/realized/duels/util/compat/CompatUtil.java @@ -6,14 +6,21 @@ public final class CompatUtil { private static final int SUB_VERSION; + private static final boolean ITEM_FLAGS; static { + ITEM_FLAGS = ReflectionUtil.getClassUnsafe("org.bukkit.inventory.ItemFlag") != null; final String packageName = Bukkit.getServer().getClass().getPackage().getName(); - SUB_VERSION = NumberUtil.parseInt(packageName.substring(packageName.lastIndexOf('.') + 1).split("_")[1]).orElse(0); + final String[] versionInfo = packageName.substring(packageName.lastIndexOf('.') + 1).split("_"); + SUB_VERSION = NumberUtil.parseInt(versionInfo[1]).orElse(0); } private CompatUtil() {} + public static boolean hasItemFlag() { + return ITEM_FLAGS; + } + public static boolean isPre1_13() { return SUB_VERSION < 13; } diff --git a/duels-plugin/src/main/java/me/realized/duels/util/compat/ReflectionUtil.java b/duels-plugin/src/main/java/me/realized/duels/util/compat/ReflectionUtil.java index a519ee69..2a30284d 100644 --- a/duels-plugin/src/main/java/me/realized/duels/util/compat/ReflectionUtil.java +++ b/duels-plugin/src/main/java/me/realized/duels/util/compat/ReflectionUtil.java @@ -2,6 +2,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; +import me.realized.duels.util.Log; import org.bukkit.Bukkit; public final class ReflectionUtil { @@ -14,11 +15,19 @@ public final class ReflectionUtil { private ReflectionUtil() {} + public static Class getClassUnsafe(final String name) { + try { + return Class.forName(name); + } catch (ClassNotFoundException ex) { + return null; + } + } + public static Class getNMSClass(final String name) { try { return Class.forName("net.minecraft.server." + VERSION + "." + name); } catch (ClassNotFoundException ex) { - ex.printStackTrace(); + Log.error(ex.getMessage(), ex); return null; } } @@ -27,7 +36,7 @@ public static Class getCBClass(final String path) { try { return Class.forName("org.bukkit.craftbukkit." + VERSION + "." + path); } catch (ClassNotFoundException ex) { - ex.printStackTrace(); + Log.error(ex.getMessage(), ex); return null; } } @@ -36,7 +45,7 @@ public static Method getMethod(final Class clazz, final String name, final Cl try { return clazz.getMethod(name, parameters); } catch (NoSuchMethodException ex) { - ex.printStackTrace(); + Log.error(ex.getMessage(), ex); return null; } } @@ -45,7 +54,7 @@ public static Field getField(final Class clazz, final String name) { try { return clazz.getField(name); } catch (NoSuchFieldException ex) { - ex.printStackTrace(); + Log.error(ex.getMessage(), ex); return null; } } diff --git a/duels-plugin/src/main/resources/config.yml b/duels-plugin/src/main/resources/config.yml index ec4d8425..8d9db87e 100644 --- a/duels-plugin/src/main/resources/config.yml +++ b/duels-plugin/src/main/resources/config.yml @@ -1,5 +1,5 @@ # DO NOT EDIT THIS VALUE! -config-version: 3 +config-version: 4 # If set to 'true', a notification and a download link will be printed on the console whenever an update is available. # default: true @@ -147,6 +147,13 @@ duel: - 'give %winner% diamond 1' - 'give %loser% dirt 1' + projectile-hit-message: + # If set to 'true', a message from lang.yml will be sent to the shooter of the projectile. + enabled: true + + # Projectile types to display the message. + types: [ARROW, TIPPED_ARROW, SPECTRAL_ARROW, TRIDENT] + # If set to 'true', players in duel will not be able to open any kind of inventories except their own. # default: true prevent-inventory-open: true diff --git a/duels-plugin/src/main/resources/lang.yml b/duels-plugin/src/main/resources/lang.yml index bece2e27..56e616a0 100644 --- a/duels-plugin/src/main/resources/lang.yml +++ b/duels-plugin/src/main/resources/lang.yml @@ -1,5 +1,5 @@ # DO NOT EDIT THIS VALUE! -config-version: 4 +config-version: 5 # Define placeholders usable in any message below. # Example: Adding 'PREFIX: "[Cool]"' below and then putting '{PREFIX}' in a message will display '[Cool]' when the message is sent in game. @@ -245,11 +245,6 @@ COMMAND: DUEL: - prevent: - item-drop: '&cYou cannot drop items while in a duel.' - command: '&cYou cannot use ''%command%'' while in a duel.' - teleportation: '&cYou cannot teleport while in a duel.' - inventory-open: '&cYou cannot open inventories while in a duel.' start-failure: no-kit-selected: '{FAIL_PREFIX} No kit was selected!' player-moved: '{FAIL_PREFIX} You or your opponent moved after sending/accepting the request!' @@ -258,6 +253,12 @@ DUEL: no-arena-available: '{FAIL_PREFIX} No arena is currently available. Please try again later.' arena-in-use: '{FAIL_PREFIX} That arena is already in use. Please select a different arena.' not-enough-money: '{FAIL_PREFIX} You or your opponent does not have $%bet_amount%.' + prevent: + item-drop: '&cYou cannot drop items while in a duel.' + command: '&cYou cannot use ''%command%'' while in a duel.' + teleportation: '&cYou cannot teleport while in a duel.' + inventory-open: '&cYou cannot open inventories while in a duel.' + projectile-hit-message: '{PREFIX} &f%name% &7is now at &d%health%♥&7!' on-end: tie: '{PREFIX} &7Tie Game!' plugin-disable: '{PREFIX} &cPlugin is disabling! Ending match automatically.'