From 616c6ea5179f3c0f63dc8e612345e0d542d9231d Mon Sep 17 00:00:00 2001 From: SticksDev Date: Mon, 5 Jun 2023 19:52:03 -0400 Subject: [PATCH] Add javadocs, add "requiresNearbyEntity" flag to ItemBasedSpell, respect dmg and range values, use .getTargetEntity instead of the randomness before. Signed-off-by: SticksDev --- .../sticksdev/runicspells/Runic_spells.java | 10 ++ .../runicspells/handlers/CooldownHandler.java | 25 +++- .../runicspells/handlers/ManaHandler.java | 59 ++++++++- .../runicspells/handlers/SpellHandler.java | 42 ++++++- .../runicspells/spells/EarthSpell.java | 11 +- .../runicspells/spells/FireSpell.java | 3 +- .../runicspells/spells/LightningSpell.java | 31 ++--- .../runicspells/spells/WaterSpell.java | 13 +- .../runicspells/structures/BaseSpell.java | 27 ++++- .../structures/ItemBasedSpell.java | 112 +++++++++++++++++- .../runicspells/structures/SpellOverride.java | 35 ++++-- .../sticksdev/runicspells/utils/Logger.java | 20 ++++ .../me/sticksdev/runicspells/utils/Redis.java | 21 +++- .../me/sticksdev/runicspells/utils/Utils.java | 11 ++ .../me/sticksdev/runicspells/utils/Yaml.java | 33 +++++- src/main/resources/plugin.yml | 7 ++ src/main/resources/spells.yml | 4 +- 17 files changed, 403 insertions(+), 61 deletions(-) diff --git a/src/main/java/me/sticksdev/runicspells/Runic_spells.java b/src/main/java/me/sticksdev/runicspells/Runic_spells.java index 17e5dd9..90457ca 100644 --- a/src/main/java/me/sticksdev/runicspells/Runic_spells.java +++ b/src/main/java/me/sticksdev/runicspells/Runic_spells.java @@ -1,5 +1,6 @@ package me.sticksdev.runicspells; +import me.sticksdev.runicspells.commands.ReloadCmd; import me.sticksdev.runicspells.handlers.CooldownHandler; import me.sticksdev.runicspells.handlers.ManaHandler; import me.sticksdev.runicspells.handlers.SpellHandler; @@ -13,6 +14,7 @@ public final class Runic_spells extends JavaPlugin { private static Yaml config; private static CooldownHandler cooldownHandler; private static ManaHandler manaHandler; + private static SpellHandler spellHandler; @Override public void onEnable() { @@ -38,7 +40,11 @@ public final class Runic_spells extends JavaPlugin { getServer().getPluginManager().registerEvents(manaHandler, this); // Register spells + spellHandler = new SpellHandler(); SpellHandler.registerSpells(); + + // Register commands + getCommand("reloadrs").setExecutor(new ReloadCmd()); } @Override @@ -67,4 +73,8 @@ public final class Runic_spells extends JavaPlugin { public ManaHandler getManaHandler() { return manaHandler; } + + public SpellHandler getSpellHandler() { + return spellHandler; + } } diff --git a/src/main/java/me/sticksdev/runicspells/handlers/CooldownHandler.java b/src/main/java/me/sticksdev/runicspells/handlers/CooldownHandler.java index cdc93d5..bc07baa 100644 --- a/src/main/java/me/sticksdev/runicspells/handlers/CooldownHandler.java +++ b/src/main/java/me/sticksdev/runicspells/handlers/CooldownHandler.java @@ -7,20 +7,37 @@ import org.bukkit.entity.Player; import redis.clients.jedis.JedisPooled; import redis.clients.jedis.params.SetParams; -import java.util.HashMap; - +/** + * Handles cooldowns + */ public class CooldownHandler { private final Redis redis = Runic_spells.getInstance().getRedisHandler(); private final JedisPooled pool = redis.getPool(); // Note: Java doesn't support union types for parameters, so we explicitly define the type of the spell // Even though it *technically* could be a BaseSpell, it's not worth the hassle of casting it + + /** + * Sets the cooldown for a player + * + * @param player The player to set the cooldown for + * @param spell The spell to set the cooldown for (type ItemBasedSpell) + * @param cooldown The cooldown to set (in seconds) + */ public void setCooldown(Player player, ItemBasedSpell spell, int cooldown) { - pool.set(player.getUniqueId().toString() + ":" + spell.getSpellID() + ":cooldown", String.valueOf(cooldown), new SetParams().ex(cooldown)); + pool.set(player.getUniqueId() + ":" + spell.getSpellID() + ":cooldown", String.valueOf(cooldown), new SetParams().ex(cooldown)); } + + /** + * Gets the cooldown for a player + * + * @param player The player to get the cooldown for + * @param spell The spell to get the cooldown for (type ItemBasedSpell) + * @return The cooldown for the player (in seconds) + */ public double getCooldown(Player player, ItemBasedSpell spell) { - String key = player.getUniqueId().toString() + ":" + spell.getSpellID() + ":cooldown"; + String key = player.getUniqueId() + ":" + spell.getSpellID() + ":cooldown"; String cooldown = pool.get(key); // Check if the key exists in Redis diff --git a/src/main/java/me/sticksdev/runicspells/handlers/ManaHandler.java b/src/main/java/me/sticksdev/runicspells/handlers/ManaHandler.java index 9d480ff..21de4db 100644 --- a/src/main/java/me/sticksdev/runicspells/handlers/ManaHandler.java +++ b/src/main/java/me/sticksdev/runicspells/handlers/ManaHandler.java @@ -1,7 +1,6 @@ package me.sticksdev.runicspells.handlers; import me.sticksdev.runicspells.Runic_spells; -import me.sticksdev.runicspells.structures.ItemBasedSpell; import me.sticksdev.runicspells.utils.Redis; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -13,15 +12,28 @@ import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.scheduler.BukkitRunnable; import redis.clients.jedis.JedisPooled; +/** + * Handles mana + */ public class ManaHandler implements Listener { private final Redis redis = Runic_spells.getInstance().getRedisHandler(); private final JedisPooled pool = redis.getPool(); private BukkitRunnable manaTimer; + /** + * Sets the mana of a player + * + * @param uuid The UUID of the player + * @param mana The amount of mana to set + */ public void setMana(String uuid, int mana) { pool.set(uuid + ":mana", String.valueOf(mana)); } + /** + * Creates the main BukkitRunnable for the mana timer to regenerate mana + * This is called in the onEnable() method of the main class + */ public void startTimers() { manaTimer = new BukkitRunnable() { @Override @@ -56,10 +68,20 @@ public class ManaHandler implements Listener { manaTimer.runTaskTimer(Runic_spells.getInstance(), 0, 20); } + /** + * Cancels the mana timer + * This is called in the onDisable() method of the main class + */ public void destroyTimers() { manaTimer.cancel(); } + /** + * Gets the mana of a player + * + * @param uuid The UUID of the player + * @return The amount of mana the player has + */ public int getMana(String uuid) { String mana = pool.get(uuid + ":mana"); @@ -70,6 +92,12 @@ public class ManaHandler implements Listener { return Integer.parseInt(mana); } + /** + * Adds mana to a player and updates their EXP bar + * + * @param player The player to add mana to + * @param mana The amount of mana to add + */ public void addMana(Player player, int mana) { String uuid = player.getUniqueId().toString(); int currentMana = getMana(uuid); @@ -77,6 +105,12 @@ public class ManaHandler implements Listener { setEXPBar(player); } + /** + * Removes mana from a player and updates their EXP bar + * + * @param player The player to remove mana from + * @param mana The amount of mana to remove + */ public void removeMana(Player player, int mana) { String uuid = player.getUniqueId().toString(); int currentMana = getMana(uuid); @@ -85,21 +119,34 @@ public class ManaHandler implements Listener { } + /** + * Sets the EXP bar of a player to their current mana + * + * @param player The player to set the EXP bar of + */ public void setEXPBar(Player player) { int mana = getMana(player.getUniqueId().toString()); player.setExp((float) mana / 100); } - public void refundSpell(Player player, ItemBasedSpell spell) { - addMana(player, spell.getManaCost()); - setEXPBar(player); - } + /** + * Checks if a player can cast a spell + * + * @param player The player to check + * @param manaCost The mana cost of the spell + * @return Whether the player can cast the spell + */ public boolean canCast(Player player, int manaCost) { return getMana(player.getUniqueId().toString()) >= manaCost; } - // On player join + /** + * On player join, check if they have mana, if not, set it to 100 + * If they do, set their EXP bar to their current mana + * + * @param event The PlayerJoinEvent + */ @EventHandler public void onPlayerJoin(PlayerJoinEvent event) { boolean exists = pool.exists(event.getPlayer().getUniqueId() + ":mana"); diff --git a/src/main/java/me/sticksdev/runicspells/handlers/SpellHandler.java b/src/main/java/me/sticksdev/runicspells/handlers/SpellHandler.java index 5f01eb9..7c81669 100644 --- a/src/main/java/me/sticksdev/runicspells/handlers/SpellHandler.java +++ b/src/main/java/me/sticksdev/runicspells/handlers/SpellHandler.java @@ -11,19 +11,28 @@ import me.sticksdev.runicspells.utils.Yaml; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.Material; +import org.bukkit.event.player.PlayerInteractEvent; import java.util.HashMap; +/** + * Handles all spells + */ public class SpellHandler { public static HashMap itemBasedSpells = new HashMap<>(); private static final Yaml config = Runic_spells.getInstance().getConfigHandler(); private static final Runic_spells plugin = Runic_spells.getInstance(); + /** + * Registers an item-based spell + * + * @param spell The spell to register + */ public static void registerItemSpell(ItemBasedSpell spell) { // Check if the item provided is a valid material try { - Material itemMaterial = Material.valueOf(spell.getItem()); + Material.valueOf(spell.getItem()); } catch (IllegalArgumentException e) { Logger.warning("Spell " + spell.getName() + " has an invalid item material!"); Logger.warning("Provided material: " + spell.getItem()); @@ -61,10 +70,23 @@ public class SpellHandler { Logger.info("Registered spell " + spell.getName() + "!"); } + /** + * Gets an item-based spell by name + * + * @param name The name of the spell + * @return The spell + */ public static ItemBasedSpell getItemSpell(String name) { return itemBasedSpells.get(name); } + /** + * Executes an item-based spell + * + * @param name The name of the spell + * @param player The player who cast the spell + * @param nearestEntity The nearest entity to the player + */ public void executeSpell(String name, Player player, Entity nearestEntity) { ItemBasedSpell spell = getItemSpell(name); if (spell != null) { @@ -72,6 +94,9 @@ public class SpellHandler { } } + /** + * Registers all spells (called on plugin startup) + */ public static void registerSpells() { // Register spells Logger.info("Registering spells..."); @@ -81,4 +106,19 @@ public class SpellHandler { registerItemSpell(new LightningSpell()); Logger.info("Registered " + itemBasedSpells.size() + " spells!"); } + + /** + * Reloads all spells (called on plugin reload) + */ + public void reload() { + // Clear all listeners for spells + Logger.info("Clearing all spell listeners..."); + + // Remove PlayerInteractEvent listeners + PlayerInteractEvent.getHandlerList().unregister(plugin); + + // Clear registered spells and re-initialize + itemBasedSpells.clear(); + registerSpells(); + } } diff --git a/src/main/java/me/sticksdev/runicspells/spells/EarthSpell.java b/src/main/java/me/sticksdev/runicspells/spells/EarthSpell.java index 81cab54..046a3d0 100644 --- a/src/main/java/me/sticksdev/runicspells/spells/EarthSpell.java +++ b/src/main/java/me/sticksdev/runicspells/spells/EarthSpell.java @@ -1,5 +1,4 @@ package me.sticksdev.runicspells.spells; - import me.sticksdev.runicspells.Runic_spells; import me.sticksdev.runicspells.structures.ItemBasedSpell; import me.sticksdev.runicspells.utils.Utils; @@ -11,10 +10,12 @@ import org.bukkit.World; public class EarthSpell extends ItemBasedSpell { public EarthSpell() { - super("Earth", "Launches an earth projectile", 2, "DIRT", 25, 40, 15, EarthSpell::castEarth); + super("Earth", "Launches an earth projectile", 2, 10, "DIRT", 25, 40, 15, true, EarthSpell::castEarth); } private static void castEarth(Player player, Entity nearestEntity) { + EarthSpell earthSpell = new EarthSpell(); + // Launches an earth projectile Projectile earth = player.launchProjectile(Snowball.class, Utils.getProjectileVelocity(player, nearestEntity)); @@ -41,12 +42,18 @@ public class EarthSpell extends ItemBasedSpell { // Set the fuse ticks to 40 (2 seconds) tnt.setFuseTicks(40); + tnt.setIsIncendiary(false); } } } } } + if (nearestEntity instanceof LivingEntity) { + // Damage the nearest entity + ((LivingEntity) nearestEntity).damage(earthSpell.getDamage()); + } + // Cancel the task cancel(); } diff --git a/src/main/java/me/sticksdev/runicspells/spells/FireSpell.java b/src/main/java/me/sticksdev/runicspells/spells/FireSpell.java index f18b48e..05aedb0 100644 --- a/src/main/java/me/sticksdev/runicspells/spells/FireSpell.java +++ b/src/main/java/me/sticksdev/runicspells/spells/FireSpell.java @@ -11,11 +11,10 @@ import org.bukkit.entity.Player; import org.bukkit.entity.Projectile; import org.bukkit.entity.Snowball; import org.bukkit.scheduler.BukkitRunnable; -import org.bukkit.util.Vector; public class FireSpell extends ItemBasedSpell { public FireSpell() { - super("Fireball", "Launches a fireball projectile", 1, "FIRE_CHARGE", 6, 25, 15, FireSpell::castFireball); + super("Fireball", "Launches a fireball projectile", 1, 15, "FIRE_CHARGE", 6, 25, 15, true, FireSpell::castFireball); } private static void castFireball(Player player, Entity nearestEntity) { diff --git a/src/main/java/me/sticksdev/runicspells/spells/LightningSpell.java b/src/main/java/me/sticksdev/runicspells/spells/LightningSpell.java index d5b79b3..760a55f 100644 --- a/src/main/java/me/sticksdev/runicspells/spells/LightningSpell.java +++ b/src/main/java/me/sticksdev/runicspells/spells/LightningSpell.java @@ -1,33 +1,28 @@ package me.sticksdev.runicspells.spells; -import me.sticksdev.runicspells.Runic_spells; + import me.sticksdev.runicspells.structures.ItemBasedSpell; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.TextComponent; -import net.kyori.adventure.text.format.NamedTextColor; -import net.kyori.adventure.text.format.TextDecoration; +import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.entity.Entity; +import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; public class LightningSpell extends ItemBasedSpell { public LightningSpell() { - super("Lightning", "Strikes lightning at the target", 4, "BLAZE_ROD", 3, 10, 0, LightningSpell::castLightning); + super("Lightning", "Strikes lightning at the target", 4, 25, "BLAZE_ROD", 3, 10, 0, true, LightningSpell::castLightning); } private static void castLightning(Player player, Entity nearestEntity) { - if (player.getTargetBlock(null, 100).getType().isAir()) { - // Strike lightning at the player - player.getWorld().strikeLightning(player.getTargetBlock(null, 100).getLocation()); - } else if(nearestEntity != null) { - // Strike lightning at the nearest entity - player.getWorld().strikeLightning(nearestEntity.getLocation()); - } else { - final TextComponent errorMessage = Component.text() - .append(Component.text("[!]", NamedTextColor.RED, TextDecoration.BOLD)) - .append(Component.text(" You must be near an entity/mob or player to cast this spell!", NamedTextColor.RED)) - .build(); + LightningSpell lightningSpell = new LightningSpell(); - player.sendMessage(errorMessage); + World world = nearestEntity.getLocation().getWorld(); + Location location = nearestEntity.getLocation(); + + world.strikeLightningEffect(location); + + if (nearestEntity instanceof LivingEntity) { + ((LivingEntity) nearestEntity).damage(lightningSpell.getDamage()); } } } diff --git a/src/main/java/me/sticksdev/runicspells/spells/WaterSpell.java b/src/main/java/me/sticksdev/runicspells/spells/WaterSpell.java index d6d76a6..e3f1db4 100644 --- a/src/main/java/me/sticksdev/runicspells/spells/WaterSpell.java +++ b/src/main/java/me/sticksdev/runicspells/spells/WaterSpell.java @@ -3,10 +3,7 @@ package me.sticksdev.runicspells.spells; import me.sticksdev.runicspells.Runic_spells; import me.sticksdev.runicspells.structures.ItemBasedSpell; import me.sticksdev.runicspells.utils.Utils; -import org.bukkit.entity.Entity; -import org.bukkit.entity.Player; -import org.bukkit.entity.Projectile; -import org.bukkit.entity.Snowball; +import org.bukkit.entity.*; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.Location; import org.bukkit.Material; @@ -14,12 +11,13 @@ import org.bukkit.World; public class WaterSpell extends ItemBasedSpell { public WaterSpell() { - super("Water", "Launches a water projectile", 3, "WATER_BUCKET", 3, 10, 0, WaterSpell::castWater); + super("Water", "Launches a water projectile", 3, 15, "WATER_BUCKET", 3, 10, 0, true, WaterSpell::castWater); } private static void castWater(Player player, Entity nearestEntity) { // Launches a water projectile Projectile water = player.launchProjectile(Snowball.class, Utils.getProjectileVelocity(player, nearestEntity)); + WaterSpell waterSpell = new WaterSpell(); // Wait for it to hit something new BukkitRunnable() { @@ -47,6 +45,11 @@ public class WaterSpell extends ItemBasedSpell { } } + if (nearestEntity instanceof LivingEntity) { + // Damage the nearest entity + ((LivingEntity) nearestEntity).damage(waterSpell.getDamage()); + } + // Cancel the task cancel(); } diff --git a/src/main/java/me/sticksdev/runicspells/structures/BaseSpell.java b/src/main/java/me/sticksdev/runicspells/structures/BaseSpell.java index 35bbd63..ddf40e6 100644 --- a/src/main/java/me/sticksdev/runicspells/structures/BaseSpell.java +++ b/src/main/java/me/sticksdev/runicspells/structures/BaseSpell.java @@ -7,21 +7,46 @@ public class BaseSpell { public String name; public String description; public int spellID; + public int range; - public BaseSpell(String name, String description, int spellID) { + /** + * Creates a new BaseSpell object. All parameters are required. + * + * @param name The name of the spell. + * @param description The description of the spell. + * @param range The range of the spell. + * @param spellID The ID of the spell, hardcoded in the spell's class and not editable. + */ + public BaseSpell(String name, String description, int range, int spellID) { this.name = name; this.description = description; this.spellID = spellID; + this.range = range; } + /** + * Gets the name of the spell. + * + * @return The name of the spell. + */ public String getName() { return name; } + /** + * Gets the description of the spell. + * + * @return The description of the spell. + */ public String getDescription() { return description; } + /** + * Gets the range of the spell. + * + * @return The range of the spell. + */ public int getSpellID() { return spellID; } diff --git a/src/main/java/me/sticksdev/runicspells/structures/ItemBasedSpell.java b/src/main/java/me/sticksdev/runicspells/structures/ItemBasedSpell.java index d2802aa..0d57e47 100644 --- a/src/main/java/me/sticksdev/runicspells/structures/ItemBasedSpell.java +++ b/src/main/java/me/sticksdev/runicspells/structures/ItemBasedSpell.java @@ -19,18 +19,37 @@ import org.jetbrains.annotations.Nullable; import java.util.function.BiConsumer; +/** + * The main spells class that most spells extend from + * This class is used for spells that use an item to cast (e.g. a stick) + */ public class ItemBasedSpell extends BaseSpell implements Listener { // Spell that uses Minecraft in-game item to use/cast private String item; private int cooldown; private int manaCost; private int damage; - private final BiConsumer castHandler; + private boolean requiresNearbyEntity; + private BiConsumer castHandler; private final CooldownHandler cooldownHandler = Runic_spells.getInstance().getCooldownHandler(); private final ManaHandler manaHandler = Runic_spells.getInstance().getManaHandler(); - public ItemBasedSpell(String name, String description, int spellId, String item, int cooldown, int manaCost, int damage, BiConsumer castHandler) { - super(name, description, spellId); + /** + * Creates a new ItemBasedSpell object. All parameters are required (some are pulled from BaseSpell). + * + * @param name The name of the spell + * @param description The description of the spell + * @param spellId The ID of the spell + * @param range The range of the spell (how far away it can be cast, in blocks) + * @param item The item to cast the spell with (e.g. "stick") + * @param cooldown The cooldown of the spell (in seconds) + * @param manaCost The mana cost of the spell + * @param damage The damage to the spell (NOTE: some spells may not use this due to API limitations) + * @param requiresNearbyEntity Whether the spell requires a nearby entity to cast + * @param castHandler The handler to run when the spell is cast + */ + public ItemBasedSpell(String name, String description, int spellId, int range, String item, int cooldown, int manaCost, int damage, boolean requiresNearbyEntity, BiConsumer castHandler) { + super(name, description, range, spellId); this.name = name; this.description = description; this.item = item; @@ -38,47 +57,105 @@ public class ItemBasedSpell extends BaseSpell implements Listener { this.manaCost = manaCost; this.castHandler = castHandler; this.damage = damage; + this.requiresNearbyEntity = requiresNearbyEntity; + this.range = range; } + /** + * Gets the item to cast the spell with + * + * @return The item to cast the spell with + */ public String getItem() { return item; } + /** + * Sets the item to cast the spell with + * + * @param item The item to cast the spell with + */ public void setItem(String item) { this.item = item; } + /** + * Gets the cooldown of the spell + * + * @return The cooldown of the spell + */ public int getCooldown() { return cooldown; } + /** + * Sets the cooldown of the spell + * + * @param cooldown The cooldown of the spell + */ public void setCooldown(int cooldown) { this.cooldown = cooldown; } + /** + * Gets the mana cost of the spell + * + * @return The mana cost of the spell + */ public int getManaCost() { return manaCost; } + /** + * Sets the mana cost of the spell + * + * @param manaCost The mana cost of the spell + */ public void setManaCost(int manaCost) { this.manaCost = manaCost; } + /** + * Calls the BiConsumer cast handler of the spell and then casts the spell + */ public void cast(Player player, Entity nearestEntity) { if (castHandler != null) { castHandler.accept(player, nearestEntity); } } + /** + * Gets the damage of the spell + * + * @return The damage of the spell + */ public int getDamage() { return damage; } + /** + * Sets the damage of the spell + * + * @param damage The damage of the spell + */ public void setDamage(int damage) { this.damage = damage; } - // Listen to the interact event for only the item specified + /** + * Sets the spell's range (how far away it can be cast, in blocks) + * + * @param range The spell's range (how far away it can be cast, in blocks) + */ + public void setRange(int range) { + this.range = range; + } + + /** + * The main handler for spells that use an item to cast + * + * @param event The PlayerInteractEvent + */ @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { // TODO: Little messy, could clean this up later @@ -94,8 +171,22 @@ public class ItemBasedSpell extends BaseSpell implements Listener { // No cooldown check if they have enough mana boolean canCast = manaHandler.canCast(player, this.manaCost); if (canCast) { - // They have enough mana, so cast the spell - cast(player, event.getPlayer().getNearbyEntities(10, 10, 10).stream().findFirst().orElse(null)); + // Get nearest entity (in player's line of sight) + Entity nearestEntity = player.getTargetEntity(this.range); + if (nearestEntity == null && requiresNearbyEntity) { + // No entity found, so tell them + final TextComponent manaMessage = Component.text() + .append(Component.text("[!]", NamedTextColor.RED, TextDecoration.BOLD)) + .append(Component.text(" Could not find a nearby entity or player to cast ")) + .append(Component.text(this.name, NamedTextColor.BLUE, TextDecoration.BOLD)) + .append(Component.text("!")) + .build(); + + player.sendMessage(manaMessage); + return; + } + + cast(player, nearestEntity); cooldownHandler.setCooldown(player, this, this.cooldown); manaHandler.removeMana(player, this.manaCost); @@ -138,6 +229,11 @@ public class ItemBasedSpell extends BaseSpell implements Listener { } } + /** + * Set's the spell's overrides + * + * @param overrides The spell's overrides (nullable) + */ public void setOverrides(@Nullable SpellOverride overrides) { if (overrides != null) { if (overrides.OverrideTool != null) { @@ -164,6 +260,10 @@ public class ItemBasedSpell extends BaseSpell implements Listener { this.damage = overrides.OverrideDamage; Logger.info("Overriding damage for spell " + this.name + " to " + overrides.OverrideDamage); } + if (overrides.OverrideRange != null && overrides.OverrideRange != 0) { + this.range = overrides.OverrideRange; + Logger.info("Overriding range for spell " + this.name + " to " + overrides.OverrideRange); + } } else { Logger.warning("setOverrides() was called for spell " + this.name + " but no overrides were provided - falling back to defaults."); } diff --git a/src/main/java/me/sticksdev/runicspells/structures/SpellOverride.java b/src/main/java/me/sticksdev/runicspells/structures/SpellOverride.java index fd091b3..8b70be5 100644 --- a/src/main/java/me/sticksdev/runicspells/structures/SpellOverride.java +++ b/src/main/java/me/sticksdev/runicspells/structures/SpellOverride.java @@ -1,6 +1,11 @@ package me.sticksdev.runicspells.structures; + import org.jetbrains.annotations.Nullable; + +/** + * Class to handle overriding a spell's values. (e.g. overriding a spell's tool, mana cost, cooldown, damage, or range) + */ public class SpellOverride { @Nullable String OverrideTool; @@ -14,21 +19,37 @@ public class SpellOverride { @Nullable Integer OverrideDamage; - public SpellOverride(@Nullable String overrideSpellTool, @Nullable String overrideManaCost, @Nullable String overrideCooldown, @Nullable String overrideDamage) { + @Nullable + Integer OverrideRange; + + /** + * Creates a new SpellOverride object. If any of the parameters are null, they will not be overridden and default to the spell's values. + * + * @param overrideSpellTool The tool to override the spell's tool with. + * @param overrideManaCost The mana cost to override the spell's mana cost with. + * @param overrideCooldown The cooldown to override the spell's cooldown with. + * @param overrideDamage The damage to override the spell's damage with. + * @param overrideRange The range to override the spell's range with. + */ + public SpellOverride(@Nullable String overrideSpellTool, int overrideManaCost, int overrideCooldown, int overrideDamage, int overrideRange) { if (overrideSpellTool != null) { this.OverrideTool = overrideSpellTool; } - if (overrideManaCost != null) { - this.OverrideManaCost = Integer.parseInt(overrideManaCost); + if (overrideManaCost != 0) { + this.OverrideManaCost = overrideManaCost; } - if (overrideCooldown != null) { - this.OverrideCooldown = Integer.parseInt(overrideCooldown); + if (overrideCooldown != 0) { + this.OverrideCooldown = overrideCooldown; } - if (overrideDamage != null) { - this.OverrideDamage = Integer.parseInt(overrideDamage); + if (overrideDamage != 0) { + this.OverrideDamage = overrideDamage; + } + + if (overrideRange != 0) { + this.OverrideRange = overrideRange; } } } diff --git a/src/main/java/me/sticksdev/runicspells/utils/Logger.java b/src/main/java/me/sticksdev/runicspells/utils/Logger.java index f116a78..27d6299 100644 --- a/src/main/java/me/sticksdev/runicspells/utils/Logger.java +++ b/src/main/java/me/sticksdev/runicspells/utils/Logger.java @@ -3,18 +3,38 @@ package me.sticksdev.runicspells.utils; import me.sticksdev.runicspells.Runic_spells; import org.jetbrains.annotations.NotNull; +/** + * A simple logger class for the plugin. + * Uses the plugin's logger to log messages. + * See {@link java.util.logging.Logger} for more information. + */ public class Logger { private static final Runic_spells plugin = Runic_spells.getInstance(); private static final java.util.logging.@NotNull Logger logger = plugin.getLogger(); + /** + * Logs a message to the console. + * + * @param message The message to log. + */ public static void info(String message) { logger.info(message); } + /** + * Logs a warning to the console. + * + * @param message The message to log. + */ public static void warning(String message) { logger.warning(message); } + /** + * Logs a severe error to the console. + * + * @param message The message to log. + */ public static void severe(String message) { logger.severe(message); } diff --git a/src/main/java/me/sticksdev/runicspells/utils/Redis.java b/src/main/java/me/sticksdev/runicspells/utils/Redis.java index a38696b..c0baf19 100644 --- a/src/main/java/me/sticksdev/runicspells/utils/Redis.java +++ b/src/main/java/me/sticksdev/runicspells/utils/Redis.java @@ -1,12 +1,13 @@ package me.sticksdev.runicspells.utils; - import me.sticksdev.runicspells.Runic_spells; -import redis.clients.jedis.CommandObject; import redis.clients.jedis.JedisPooled; -import redis.clients.jedis.params.SetParams; -import java.util.HashMap; +/** + * Redis connection handler + * This class is used to handle the Redis connections and + * operations for the plugin + */ public class Redis { private final Runic_spells plugin = Runic_spells.getInstance(); private final String host = plugin.getConfigHandler().getConfig().getString("redis.host"); @@ -15,6 +16,9 @@ public class Redis { // Create a new JedisPooled instance private static JedisPooled pool; + /** + * Initialize the Redis connection for the current instance + */ public void init() { try { Logger.info("Connecting to Redis..."); @@ -31,10 +35,19 @@ public class Redis { } } + /** + * Gets the current pool handle of the instance + * + * @return JedisPooled instance + */ public JedisPooled getPool() { return pool; } + /** + * Shuts down the current Redis connection + * This is only called on plugin disable + */ public void close() { pool.close(); } diff --git a/src/main/java/me/sticksdev/runicspells/utils/Utils.java b/src/main/java/me/sticksdev/runicspells/utils/Utils.java index 5e609db..65f1b95 100644 --- a/src/main/java/me/sticksdev/runicspells/utils/Utils.java +++ b/src/main/java/me/sticksdev/runicspells/utils/Utils.java @@ -4,7 +4,18 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.util.Vector; +/** + * Simple Utils class for repeated code or tasks. + */ public class Utils { + /** + * Gets the velocity of a projectile based on the player's location and the target's location. + * + * @param player The player to get the velocity from. + * @param target The target to get the velocity to. + * @return The velocity of the projectile. + * @see org.bukkit.util.Vector Vector + */ public static Vector getProjectileVelocity(Player player, Entity target) { Vector direction; if (target != null) { diff --git a/src/main/java/me/sticksdev/runicspells/utils/Yaml.java b/src/main/java/me/sticksdev/runicspells/utils/Yaml.java index 998da99..cb61c72 100644 --- a/src/main/java/me/sticksdev/runicspells/utils/Yaml.java +++ b/src/main/java/me/sticksdev/runicspells/utils/Yaml.java @@ -11,6 +11,10 @@ import java.io.File; import java.io.IOException; import java.util.List; +/** + * YAML file handler + * This class is used to handle the YAML files for the plugin (spells and config) + */ public class Yaml { // Basic YAML Loader and data holder for minecraft plugins private final Runic_spells plugin = Runic_spells.getInstance(); @@ -96,6 +100,12 @@ public class Yaml { return configConfig; } + /** + * Returns the spell overrides for a given spell + * + * @param spellName Name of the spell to get overrides for + * @return SpellOverride object or null if no overrides are found + */ @Nullable public SpellOverride getSpellOverrides(String spellName) { ConfigurationSection spellOverridesBlock = getSpellsConfig().getConfigurationSection("overrideSpells"); @@ -113,14 +123,21 @@ public class Yaml { } String overrideSpellTool = spellOverrides.getString("OverrideTool"); - String overrideManaCost = spellOverrides.getString("OverrideManaCost"); - String overrideCooldown = spellOverrides.getString("OverrideCooldown"); - String overrideDamage = spellOverrides.getString("OverrideDamage"); + int overrideManaCost = spellOverrides.getInt("OverrideManaCost", 0); + int overrideCooldown = spellOverrides.getInt("OverrideCooldown", 0); + int overrideDamage = spellOverrides.getInt("OverrideDamage", 0); + int overrideRange = spellOverrides.getInt("OverrideRange", 0); - return new SpellOverride(overrideSpellTool, overrideManaCost, overrideCooldown, overrideDamage); + return new SpellOverride(overrideSpellTool, overrideManaCost, overrideCooldown, overrideDamage, overrideRange); } + /** + * Returns whether a spell is enabled in the config + * + * @param spellName Name of the spell to check + * @return boolean + */ public boolean getIsSpellEnabled(String spellName) { // Check if it's in the enabledSpells list List enabledSpells = getSpellsConfig().getList("enabledSpells"); @@ -132,4 +149,12 @@ public class Yaml { return enabledSpells.contains(spellName); } + + /** + * Reloads the config file(s) + */ + public void reload() { + destroy(); + init(); + } } \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 7130cb7..8e8e81f 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -2,3 +2,10 @@ name: runic-spells version: '${project.version}' main: me.sticksdev.runicspells.Runic_spells api-version: 1.19 + +commands: + reloadrs: + description: Reloads the plugin and configuration files. + usage: / + permission: runicspells.reload + aliases: [ rl ] \ No newline at end of file diff --git a/src/main/resources/spells.yml b/src/main/resources/spells.yml index 640e9ea..64a7de1 100644 --- a/src/main/resources/spells.yml +++ b/src/main/resources/spells.yml @@ -18,6 +18,7 @@ enabledSpells: # OverrideManaCost: 10 # OverrideCooldown: 10 # OverrideDamage: 10 +# OverrideRange: 10 # By default, this is commented out # overrideSpells: @@ -25,4 +26,5 @@ enabledSpells: # OverrideTool: "DIAMOND_HOE" # OverrideManaCost: 10 # OverrideCooldown: 10 -# OverrideDamage: 10 \ No newline at end of file +# OverrideDamage: 10 +# OverrideRange: 10 \ No newline at end of file