diff --git a/build.gradle.kts b/build.gradle.kts index 9dd2ffd..ec9d8ac 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,8 +1,9 @@ plugins { + id("java") // Tell gradle this is a java project. + id("java-library") // Import helper for source-based libraries. + id("com.diffplug.spotless") version "7.0.4" // Import auto-formatter. id("com.gradleup.shadow") version "8.3.6" // Import shadow API. - java // Tell gradle this is a java project. eclipse // Import eclipse plugin for IDE integration. - kotlin("jvm") version "2.1.21" // Import kotlin jvm plugin for kotlin/java integration. } java { @@ -11,20 +12,17 @@ java { } group = "net.trueog.flyingbowog" // Declare bundle identifier. + version = "1.1" // Declare plugin version (will be in .jar). + val apiVersion = "1.19" // Declare minecraft server target version. tasks.named("processResources") { - val props = mapOf( - "version" to version, - "apiVersion" to apiVersion - ) + val props = mapOf("version" to version, "apiVersion" to apiVersion) inputs.properties(props) // Indicates to rerun if version changes. - filesMatching("plugin.yml") { - expand(props) - } + filesMatching("plugin.yml") { expand(props) } from("LICENSE") { // Bundle license into .jars. into("/") } @@ -33,19 +31,15 @@ tasks.named("processResources") { repositories { mavenCentral() gradlePluginPortal() - maven { - url = uri("https://repo.purpurmc.org/snapshots") - } - maven { - url = uri("https://repo.codemc.io/repository/maven-public") - } + maven { url = uri("https://repo.purpurmc.org/snapshots") } + maven { url = uri("https://repo.codemc.io/repository/maven-public") } } dependencies { compileOnly("org.purpurmc.purpur:purpur-api:1.19.4-R0.1-SNAPSHOT") // Declare purpur API version to be packaged. compileOnly("io.github.miniplaceholders:miniplaceholders-api:2.2.3") // Import MiniPlaceholders API. compileOnly("de.tr7zw:item-nbt-api-plugin:2.11.2") - compileOnly(project(":libs:Utilities-OG")) + compileOnlyApi(project(":libs:Utilities-OG")) } tasks.withType().configureEach { // Ensure reproducible .jars @@ -54,18 +48,17 @@ tasks.withType().configureEach { // Ensure reproducible .ja } tasks.shadowJar { - archiveClassifier.set("") // Use empty string instead of null. exclude("io.github.miniplaceholders.*") // Exclude the MiniPlaceholders package from being shadowed. + archiveClassifier.set("") // Use empty string instead of null. minimize() } tasks.build { + dependsOn(tasks.spotlessApply) dependsOn(tasks.shadowJar) } -tasks.jar { - archiveClassifier.set("part") -} +tasks.jar { archiveClassifier.set("part") } tasks.withType().configureEach { options.compilerArgs.add("-parameters") @@ -74,13 +67,20 @@ tasks.withType().configureEach { options.isFork = true } -kotlin { - jvmToolchain(17) -} - java { toolchain { languageVersion = JavaLanguageVersion.of(17) vendor = JvmVendorSpec.GRAAL_VM } } + +spotless { + java { + removeUnusedImports() + palantirJavaFormat() + } + kotlinGradle { + ktfmt().kotlinlangStyle().configure { it.setMaxWidth(120) } + target("build.gradle.kts", "settings.gradle.kts") + } +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index a4b76b9..1b33c55 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 002b867..ff23a68 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.2-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew b/gradlew index f5feea6..23d15a9 100755 --- a/gradlew +++ b/gradlew @@ -86,8 +86,7 @@ done # shellcheck disable=SC2034 APP_BASE_NAME=${0##*/} # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) -APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s -' "$PWD" ) || exit +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD=maximum @@ -115,7 +114,7 @@ case "$( uname )" in #( NONSTOP* ) nonstop=true ;; esac -CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar +CLASSPATH="\\\"\\\"" # Determine the Java command to use to start the JVM. @@ -206,7 +205,7 @@ fi DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Collect all arguments for the java command: -# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, # and any embedded shellness will be escaped. # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be # treated as '${Hostname}' itself on the command line. @@ -214,7 +213,7 @@ DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' set -- \ "-Dorg.gradle.appname=$APP_BASE_NAME" \ -classpath "$CLASSPATH" \ - org.gradle.wrapper.GradleWrapperMain \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ "$@" # Stop when "xargs" is not available. diff --git a/gradlew.bat b/gradlew.bat index 9b42019..5eed7ee 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -70,11 +70,11 @@ goto fail :execute @rem Setup the command line -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar +set CLASSPATH= @rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* :end @rem End local scope for the variables with windows NT shell diff --git a/libs/Utilities-OG b/libs/Utilities-OG index 6151b2d..e68d3fe 160000 --- a/libs/Utilities-OG +++ b/libs/Utilities-OG @@ -1 +1 @@ -Subproject commit 6151b2de7fbf1ac4fa68fa7aeec7bcdedeff5049 +Subproject commit e68d3fec7a08ffb7f5850b06df963b34802b3dbf diff --git a/settings.gradle.kts b/settings.gradle.kts index cf50be4..a9555b7 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,11 +1,10 @@ rootProject.name = "FlyingBow-OG" // Run the bootstrap at configuration time. -val process = ProcessBuilder("sh", "bootstrap.sh") - .directory(rootDir) - .start() +val process = ProcessBuilder("sh", "bootstrap.sh").directory(rootDir).start() val exitValue = process.waitFor() + if (exitValue != 0) { throw GradleException("bootstrap.sh failed with exit code $exitValue") } diff --git a/src/main/java/net/trueog/flyingbowog/Commands/CommandsTabCompleter.java b/src/main/java/net/trueog/flyingbowog/Commands/CommandsTabCompleter.java index 2ab6f61..6a2a0a8 100644 --- a/src/main/java/net/trueog/flyingbowog/Commands/CommandsTabCompleter.java +++ b/src/main/java/net/trueog/flyingbowog/Commands/CommandsTabCompleter.java @@ -2,7 +2,6 @@ package net.trueog.flyingbowog.Commands; import java.util.ArrayList; import java.util.List; - import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; @@ -11,19 +10,18 @@ import org.jetbrains.annotations.Nullable; public class CommandsTabCompleter implements TabCompleter { - @Override - public @Nullable List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + @Override + public @Nullable List onTabComplete( + @NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - if(args.length == 1){ + if (args.length == 1) { - ArrayList result = new ArrayList<>(); - result.add("give"); + ArrayList result = new ArrayList<>(); + result.add("give"); - return result; + return result; + } - } - - return null; - } - -} \ No newline at end of file + return null; + } +} diff --git a/src/main/java/net/trueog/flyingbowog/Commands/GiveCommand.java b/src/main/java/net/trueog/flyingbowog/Commands/GiveCommand.java index 000586b..058e23a 100644 --- a/src/main/java/net/trueog/flyingbowog/Commands/GiveCommand.java +++ b/src/main/java/net/trueog/flyingbowog/Commands/GiveCommand.java @@ -1,7 +1,9 @@ package net.trueog.flyingbowog.Commands; import java.util.Collections; - +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.TextColor; +import net.kyori.adventure.text.format.TextDecoration; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -11,49 +13,43 @@ import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.TextColor; -import net.kyori.adventure.text.format.TextDecoration; - public class GiveCommand implements CommandExecutor { - @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args){ + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - // if the command is /flyingbow - if(command.getName().equalsIgnoreCase("flyingbow")){ - // if the command is just /flyingbow give - if(args.length == 1) { - if (args[0].equalsIgnoreCase("give")) { - Player player = (Player) sender; - // if the player is in creative mode, allow them to - // give bows - if (player.hasPermission("flyingbow.cangive")) { - // create the flying bow item - ItemStack flyingBowItem = new ItemStack(Material.BOW); - ItemMeta bowMeta = flyingBowItem.getItemMeta(); - bowMeta.displayName(Component.text("Flying Bow") - .color(TextColor.color(0, 255, 255)) - .decorate(TextDecoration.ITALIC)); - bowMeta.lore(Collections.singletonList(Component.text("Shoot this bow to FLY"))); - flyingBowItem.setItemMeta(bowMeta); - flyingBowItem.addItemFlags(ItemFlag.HIDE_DYE); + // if the command is /flyingbow + if (command.getName().equalsIgnoreCase("flyingbow")) { + // if the command is just /flyingbow give + if (args.length == 1) { + if (args[0].equalsIgnoreCase("give")) { + Player player = (Player) sender; + // if the player is in creative mode, allow them to + // give bows + if (player.hasPermission("flyingbow.cangive")) { + // create the flying bow item + ItemStack flyingBowItem = new ItemStack(Material.BOW); + ItemMeta bowMeta = flyingBowItem.getItemMeta(); + bowMeta.displayName(Component.text("Flying Bow") + .color(TextColor.color(0, 255, 255)) + .decorate(TextDecoration.ITALIC)); + bowMeta.lore(Collections.singletonList(Component.text("Shoot this bow to FLY"))); + flyingBowItem.setItemMeta(bowMeta); + flyingBowItem.addItemFlags(ItemFlag.HIDE_DYE); - player.getInventory().addItem(flyingBowItem); - } - // tell the player that they don't have permission to use this command - else{ - sender.sendMessage(Component.text("[FlyingBow]") - .color(TextColor.color(0, 255, 0)) - .append(Component.text(" You Don't Have Permission to do That!") - .color(TextColor.color(255, 0, 0)))); - } - } - } - } + player.getInventory().addItem(flyingBowItem); + } + // tell the player that they don't have permission to use this command + else { + sender.sendMessage(Component.text("[FlyingBow]") + .color(TextColor.color(0, 255, 0)) + .append(Component.text(" You Don't Have Permission to do That!") + .color(TextColor.color(255, 0, 0)))); + } + } + } + } - return true; - - } - -} \ No newline at end of file + return true; + } +} diff --git a/src/main/java/net/trueog/flyingbowog/FlyingBowOG.java b/src/main/java/net/trueog/flyingbowog/FlyingBowOG.java index b07427b..e1eefeb 100644 --- a/src/main/java/net/trueog/flyingbowog/FlyingBowOG.java +++ b/src/main/java/net/trueog/flyingbowog/FlyingBowOG.java @@ -1,17 +1,6 @@ package net.trueog.flyingbowog; import java.util.Collections; - -import org.bukkit.Bukkit; -import org.bukkit.Material; -import org.bukkit.NamespacedKey; -import org.bukkit.event.Listener; -import org.bukkit.inventory.ItemFlag; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.ShapedRecipe; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.plugin.java.JavaPlugin; - import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextDecoration; @@ -22,56 +11,58 @@ import net.trueog.flyingbowog.Listeners.OnEntityDamageEvent; import net.trueog.flyingbowog.Listeners.OnEntityShootBowEvent; import net.trueog.flyingbowog.Listeners.OnGenericGameEvent; import net.trueog.flyingbowog.Listeners.OnPrepareAnvilEvent; - +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.event.Listener; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.ShapedRecipe; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.plugin.java.JavaPlugin; public final class FlyingBowOG extends JavaPlugin implements Listener { - private static FlyingBowOG plugin; + private static FlyingBowOG plugin; - @Override - public void onEnable() { - // Plugin startup logic - plugin = this; + @Override + public void onEnable() { + // Plugin startup logic + plugin = this; - // create the flying bow item - ItemStack flyingBowItem = new ItemStack(Material.BOW); - ItemMeta bowMeta = flyingBowItem.getItemMeta(); - bowMeta.displayName(Component.text("Flying Bow") - .color(TextColor.color(0, 255, 255)) - .decorate(TextDecoration.ITALIC)); - bowMeta.lore(Collections.singletonList(Component.text("Shoot this bow to FLY"))); - flyingBowItem.setItemMeta(bowMeta); + // create the flying bow item + ItemStack flyingBowItem = new ItemStack(Material.BOW); + ItemMeta bowMeta = flyingBowItem.getItemMeta(); + bowMeta.displayName( + Component.text("Flying Bow").color(TextColor.color(0, 255, 255)).decorate(TextDecoration.ITALIC)); + bowMeta.lore(Collections.singletonList(Component.text("Shoot this bow to FLY"))); + flyingBowItem.setItemMeta(bowMeta); - // adding a useless itemFlag to identify this as a flying bow - // this was easier for me than adding an attribute, and no other bow should have - // this flag - flyingBowItem.addItemFlags(ItemFlag.HIDE_DYE); + // adding a useless itemFlag to identify this as a flying bow + // this was easier for me than adding an attribute, and no other bow should have + // this flag + flyingBowItem.addItemFlags(ItemFlag.HIDE_DYE); + // add the recipe into the game + ShapedRecipe flyingBowRecipe = new ShapedRecipe(new NamespacedKey(this, "flying_bow"), flyingBowItem); + flyingBowRecipe.shape("sd ", "sed", "sd "); + flyingBowRecipe.setIngredient('s', Material.STICK); + flyingBowRecipe.setIngredient('e', Material.ELYTRA); + flyingBowRecipe.setIngredient('d', Material.DIAMOND); - // add the recipe into the game - ShapedRecipe flyingBowRecipe = new ShapedRecipe(new NamespacedKey(this, "flying_bow"), - flyingBowItem); - flyingBowRecipe.shape("sd ", "sed", "sd "); - flyingBowRecipe.setIngredient('s', Material.STICK); - flyingBowRecipe.setIngredient('e', Material.ELYTRA); - flyingBowRecipe.setIngredient('d', Material.DIAMOND); + Bukkit.addRecipe(flyingBowRecipe); + getServer().getPluginManager().registerEvents(new OnEntityShootBowEvent(), this); + getServer().getPluginManager().registerEvents(new OnGenericGameEvent(), this); + getServer().getPluginManager().registerEvents(new OnEntityDamageEvent(), this); + getServer().getPluginManager().registerEvents(new OnPrepareAnvilEvent(), this); + getServer().getPluginManager().registerEvents(new OnAnvilUpdateResultEvent(), this); - Bukkit.addRecipe(flyingBowRecipe); + getCommand("flyingbow").setExecutor(new GiveCommand()); + getCommand("flyingbow").setTabCompleter(new CommandsTabCompleter()); + } - getServer().getPluginManager().registerEvents(new OnEntityShootBowEvent(), this); - getServer().getPluginManager().registerEvents(new OnGenericGameEvent(), this); - getServer().getPluginManager().registerEvents(new OnEntityDamageEvent(), this); - getServer().getPluginManager().registerEvents(new OnPrepareAnvilEvent(), this); - getServer().getPluginManager().registerEvents(new OnAnvilUpdateResultEvent(), this); - - getCommand("flyingbow").setExecutor(new GiveCommand()); - getCommand("flyingbow").setTabCompleter(new CommandsTabCompleter()); - - } - - public static FlyingBowOG getPlugin(){ - return plugin; - } - -} \ No newline at end of file + public static FlyingBowOG getPlugin() { + return plugin; + } +} diff --git a/src/main/java/net/trueog/flyingbowog/Listeners/OnAnvilUpdateResultEvent.java b/src/main/java/net/trueog/flyingbowog/Listeners/OnAnvilUpdateResultEvent.java index e6aedca..f3f2d5f 100644 --- a/src/main/java/net/trueog/flyingbowog/Listeners/OnAnvilUpdateResultEvent.java +++ b/src/main/java/net/trueog/flyingbowog/Listeners/OnAnvilUpdateResultEvent.java @@ -1,7 +1,6 @@ package net.trueog.flyingbowog.Listeners; import java.util.ArrayList; - import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -10,22 +9,20 @@ import org.purpurmc.purpur.event.inventory.AnvilUpdateResultEvent; public class OnAnvilUpdateResultEvent implements Listener { - @EventHandler - public void onAnvilUpdateResultEvent(AnvilUpdateResultEvent event){ + @EventHandler + public void onAnvilUpdateResultEvent(AnvilUpdateResultEvent event) { - // cast each HumanEntity to a player so that - // we can update their inventories - ArrayList players = new ArrayList<>(); - for (HumanEntity human : event.getViewers()){ - if(human instanceof Player) { - players.add((Player) human); - } - } + // cast each HumanEntity to a player so that + // we can update their inventories + ArrayList players = new ArrayList<>(); + for (HumanEntity human : event.getViewers()) { + if (human instanceof Player) { + players.add((Player) human); + } + } - for(Player player : players){ - player.updateInventory(); - } - - } - -} \ No newline at end of file + for (Player player : players) { + player.updateInventory(); + } + } +} diff --git a/src/main/java/net/trueog/flyingbowog/Listeners/OnEntityDamageEvent.java b/src/main/java/net/trueog/flyingbowog/Listeners/OnEntityDamageEvent.java index c0a1a60..a26a654 100644 --- a/src/main/java/net/trueog/flyingbowog/Listeners/OnEntityDamageEvent.java +++ b/src/main/java/net/trueog/flyingbowog/Listeners/OnEntityDamageEvent.java @@ -10,48 +10,45 @@ import org.bukkit.potion.PotionType; public class OnEntityDamageEvent implements Listener { - @EventHandler - public void onEntityDamage(EntityDamageByEntityEvent event){ + @EventHandler + public void onEntityDamage(EntityDamageByEntityEvent event) { - if (event.getDamager() instanceof Arrow){ - Arrow arrow = (Arrow) event.getDamager(); - Color arrowColor = arrow.getColor(); - Player player = null; - Player shooter = null; + if (event.getDamager() instanceof Arrow) { + Arrow arrow = (Arrow) event.getDamager(); + Color arrowColor = arrow.getColor(); + Player player = null; + Player shooter = null; - if(event.getEntity() instanceof Player){ - player = (Player) event.getEntity(); - } - else{ - return; - } - if(arrow.getShooter() instanceof Player){ - shooter = (Player) arrow.getShooter(); - } - else{ - return; - } + if (event.getEntity() instanceof Player) { + player = (Player) event.getEntity(); + } else { + return; + } + if (arrow.getShooter() instanceof Player) { + shooter = (Player) arrow.getShooter(); + } else { + return; + } - // only disable damage if you are the one that shot the arrow - if(shooter.getName().equals(player.getName())) { + // only disable damage if you are the one that shot the arrow + if (shooter.getName().equals(player.getName())) { - if (arrowColor != null) { - // if the arrow has a certain color - if (arrowColor.equals(Color.fromRGB(0, 255, 255))) { - // if the arrow has the UNCRAFTABLE potion on it - PotionType arrowPotion = arrow.getBasePotionData().getType(); + if (arrowColor != null) { + // if the arrow has a certain color + if (arrowColor.equals(Color.fromRGB(0, 255, 255))) { + // if the arrow has the UNCRAFTABLE potion on it + PotionType arrowPotion = arrow.getBasePotionData().getType(); - if (arrowPotion.equals(PotionType.UNCRAFTABLE)) { + if (arrowPotion.equals(PotionType.UNCRAFTABLE)) { - // delete the arrow - arrow.remove(); - // make the arrow do no damage - event.setCancelled(true); - } - } - } - } - } - } - -} \ No newline at end of file + // delete the arrow + arrow.remove(); + // make the arrow do no damage + event.setCancelled(true); + } + } + } + } + } + } +} diff --git a/src/main/java/net/trueog/flyingbowog/Listeners/OnEntityShootBowEvent.java b/src/main/java/net/trueog/flyingbowog/Listeners/OnEntityShootBowEvent.java index c6120d1..d037062 100644 --- a/src/main/java/net/trueog/flyingbowog/Listeners/OnEntityShootBowEvent.java +++ b/src/main/java/net/trueog/flyingbowog/Listeners/OnEntityShootBowEvent.java @@ -1,7 +1,7 @@ package net.trueog.flyingbowog.Listeners; import java.util.Map; - +import net.trueog.flyingbowog.FlyingBowOG; import org.bukkit.Bukkit; import org.bukkit.Color; import org.bukkit.Location; @@ -17,76 +17,79 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionData; import org.bukkit.potion.PotionType; -import net.trueog.flyingbowog.FlyingBowOG; - public class OnEntityShootBowEvent implements Listener { - @EventHandler - public void onEntityShootBow(EntityShootBowEvent event) { + @EventHandler + public void onEntityShootBow(EntityShootBowEvent event) { - // if the entity was a player - if (event.getEntity() instanceof Player) { - if (event.getProjectile() instanceof Arrow) { - Player eventEntity = (Player) event.getEntity(); - String eventWorld = eventEntity.getWorld().getKey().toString(); - ItemStack eventBow = ((Player) eventEntity) - .getInventory() - .getItemInMainHand(); - Map bowEnchantments = eventBow.getEnchantments(); - Location eventEntityLocation = eventEntity.getLocation(); - Arrow eventArrow = (Arrow) event.getProjectile(); - Entity previousVehicle = null; - if (eventEntity.getVehicle() != null) { - previousVehicle = eventEntity.getVehicle(); - } + // if the entity was a player + if (event.getEntity() instanceof Player) { + if (event.getProjectile() instanceof Arrow) { + Player eventEntity = (Player) event.getEntity(); + String eventWorld = eventEntity.getWorld().getKey().toString(); + ItemStack eventBow = ((Player) eventEntity).getInventory().getItemInMainHand(); + Map bowEnchantments = eventBow.getEnchantments(); + Location eventEntityLocation = eventEntity.getLocation(); + Arrow eventArrow = (Arrow) event.getProjectile(); + Entity previousVehicle = null; + if (eventEntity.getVehicle() != null) { + previousVehicle = eventEntity.getVehicle(); + } - // if the player is holding a flying bow - if (eventBow.getItemFlags().contains(ItemFlag.HIDE_DYE)) { + // if the player is holding a flying bow + if (eventBow.getItemFlags().contains(ItemFlag.HIDE_DYE)) { - // eject the player from the previous arrow, if they were riding - // on one before - if (previousVehicle != null) { - previousVehicle.removePassenger(eventEntity); - // only delete the entity if it is an arrow - if(previousVehicle instanceof Arrow) { - previousVehicle.remove(); - } - } + // eject the player from the previous arrow, if they were riding + // on one before + if (previousVehicle != null) { + previousVehicle.removePassenger(eventEntity); + // only delete the entity if it is an arrow + if (previousVehicle instanceof Arrow) { + previousVehicle.remove(); + } + } - // add a color to the arrow but not an effect - // so that it can be identified later - eventArrow.setBasePotionData(new PotionData(PotionType.UNCRAFTABLE)); - eventArrow.setColor(Color.fromRGB(0, 255, 255)); + // add a color to the arrow but not an effect + // so that it can be identified later + eventArrow.setBasePotionData(new PotionData(PotionType.UNCRAFTABLE)); + eventArrow.setColor(Color.fromRGB(0, 255, 255)); - // tp the player to the location they were at when the arrow was shot 2 ticks after it - // was shot to force the server to track the player's position - // then make the player ride the arrow - Bukkit.getScheduler().runTaskLater(FlyingBowOG.getPlugin(), () -> { - // check if the player and arrow are still in the same world - // if not, don't try to make the player ride the arrow - if (eventEntity.getWorld().getKey().toString().equals(eventWorld)) { - eventEntity.teleport(eventEntityLocation); - eventArrow.addPassenger(eventEntity); - } - }, 1L); + // tp the player to the location they were at when the arrow was shot 2 ticks after it + // was shot to force the server to track the player's position + // then make the player ride the arrow + Bukkit.getScheduler() + .runTaskLater( + FlyingBowOG.getPlugin(), + () -> { + // check if the player and arrow are still in the same world + // if not, don't try to make the player ride the arrow + if (eventEntity + .getWorld() + .getKey() + .toString() + .equals(eventWorld)) { + eventEntity.teleport(eventEntityLocation); + eventArrow.addPassenger(eventEntity); + } + }, + 1L); - // if the bow has infinity on it, remove infinity + // if the bow has infinity on it, remove infinity - // infinity is just way too powerful on an item like this + // infinity is just way too powerful on an item like this - // I'm leaving the item enchantable since we still want mending and unbreaking - // to work for the bow, just like for an elytra + // I'm leaving the item enchantable since we still want mending and unbreaking + // to work for the bow, just like for an elytra - // even though mending can't be put on a bow with infinity, grindstones exist - // and an infinity bow can be made to last forever, it just takes some time - // at an exp farm to repair it - if (bowEnchantments.containsKey(Enchantment.ARROW_INFINITE)) { - // remove infinity from the bow - eventBow.removeEnchantment(Enchantment.ARROW_INFINITE); - } - } - } - } - } - -} \ No newline at end of file + // even though mending can't be put on a bow with infinity, grindstones exist + // and an infinity bow can be made to last forever, it just takes some time + // at an exp farm to repair it + if (bowEnchantments.containsKey(Enchantment.ARROW_INFINITE)) { + // remove infinity from the bow + eventBow.removeEnchantment(Enchantment.ARROW_INFINITE); + } + } + } + } + } +} diff --git a/src/main/java/net/trueog/flyingbowog/Listeners/OnGenericGameEvent.java b/src/main/java/net/trueog/flyingbowog/Listeners/OnGenericGameEvent.java index 3fcb4f0..932cdf3 100644 --- a/src/main/java/net/trueog/flyingbowog/Listeners/OnGenericGameEvent.java +++ b/src/main/java/net/trueog/flyingbowog/Listeners/OnGenericGameEvent.java @@ -1,6 +1,5 @@ package net.trueog.flyingbowog.Listeners; - import org.bukkit.GameEvent; import org.bukkit.Location; import org.bukkit.entity.Arrow; @@ -11,43 +10,37 @@ import org.bukkit.event.world.GenericGameEvent; public class OnGenericGameEvent implements Listener { - @EventHandler - public void onGenericGameEvent(GenericGameEvent event){ + @EventHandler + public void onGenericGameEvent(GenericGameEvent event) { + // if the event was a projectile landing + if (event.getEvent().equals(GameEvent.PROJECTILE_LAND)) { - // if the event was a projectile landing - if(event.getEvent().equals(GameEvent.PROJECTILE_LAND)){ + Entity entity = null; - Entity entity = null; + if (event.getEntity() != null) { + entity = event.getEntity(); + } else { + return; + } - if(event.getEntity() != null) { - entity = event.getEntity(); - } - else{ - return; - } + if (entity.getVehicle() != null) { + if (entity.getVehicle() instanceof Arrow) { + Arrow arrow = (Arrow) entity.getVehicle(); - if(entity.getVehicle() != null) { - if (entity.getVehicle() instanceof Arrow) { - Arrow arrow = (Arrow) entity.getVehicle(); + // tp the entity to the center of the block they + // landed on + Location newLocation = entity.getLocation(); + newLocation.setX(newLocation.getBlockX() + 0.5); + newLocation.setY(newLocation.getBlockY()); + newLocation.setZ(newLocation.getBlockZ() + 0.5); - // tp the entity to the center of the block they - // landed on - Location newLocation = entity.getLocation(); - newLocation.setX(newLocation.getBlockX() + 0.5); - newLocation.setY(newLocation.getBlockY()); - newLocation.setZ(newLocation.getBlockZ() + 0.5); + entity.teleport(newLocation); - entity.teleport(newLocation); - - // delete the arrow - arrow.remove(); - - } - } - - } - - } - -} \ No newline at end of file + // delete the arrow + arrow.remove(); + } + } + } + } +} diff --git a/src/main/java/net/trueog/flyingbowog/Listeners/OnPrepareAnvilEvent.java b/src/main/java/net/trueog/flyingbowog/Listeners/OnPrepareAnvilEvent.java index 799b635..34f5f11 100644 --- a/src/main/java/net/trueog/flyingbowog/Listeners/OnPrepareAnvilEvent.java +++ b/src/main/java/net/trueog/flyingbowog/Listeners/OnPrepareAnvilEvent.java @@ -1,5 +1,8 @@ package net.trueog.flyingbowog.Listeners; +import de.tr7zw.nbtapi.NBT; +import net.kyori.adventure.text.Component; +import net.trueog.flyingbowog.FlyingBowOG; import org.bukkit.Material; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -9,79 +12,81 @@ import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Damageable; -import de.tr7zw.nbtapi.NBT; -import net.kyori.adventure.text.Component; -import net.trueog.flyingbowog.FlyingBowOG; - public class OnPrepareAnvilEvent implements Listener { - @EventHandler - public void onPrepareAnvilEvent(PrepareAnvilEvent event){ - AnvilInventory anvil = event.getInventory(); + @EventHandler + public void onPrepareAnvilEvent(PrepareAnvilEvent event) { + AnvilInventory anvil = event.getInventory(); - ItemStack firstItem = anvil.getFirstItem(); - ItemStack secondItem = anvil.getSecondItem(); + ItemStack firstItem = anvil.getFirstItem(); + ItemStack secondItem = anvil.getSecondItem(); - if(firstItem != null){ - if(secondItem != null){ - if(firstItem.getItemFlags().contains(ItemFlag.HIDE_DYE) && firstItem.getType().equals(Material.BOW)){ - if(secondItem.getType().equals(Material.PHANTOM_MEMBRANE)){ - // create the flying bow item - ItemStack flyingBowItem = anvil.getFirstItem().clone(); + if (firstItem != null) { + if (secondItem != null) { + if (firstItem.getItemFlags().contains(ItemFlag.HIDE_DYE) + && firstItem.getType().equals(Material.BOW)) { + if (secondItem.getType().equals(Material.PHANTOM_MEMBRANE)) { + // create the flying bow item + ItemStack flyingBowItem = anvil.getFirstItem().clone(); - // set its durability to 25% more than the other one - // make sure the cap the durability to full using Math.max() - Damageable flyingBowItemMeta = (Damageable) flyingBowItem.getItemMeta(); - // the amount that the bow gets healed by. 96 is 1/4 the max durability of a bow, which is 384 - final int bowHeal = 96; - flyingBowItemMeta.setDamage(Math.max(0, flyingBowItemMeta.getDamage() - (bowHeal * anvil.getSecondItem().getAmount()))); + // set its durability to 25% more than the other one + // make sure the cap the durability to full using Math.max() + Damageable flyingBowItemMeta = (Damageable) flyingBowItem.getItemMeta(); + // the amount that the bow gets healed by. 96 is 1/4 the max durability of a bow, which is 384 + final int bowHeal = 96; + flyingBowItemMeta.setDamage(Math.max( + 0, + flyingBowItemMeta.getDamage() + - (bowHeal * anvil.getSecondItem().getAmount()))); - // set the bow name IF the player is trying to rename it - if(!anvil.getRenameText().equals("Flying Bow")){ - flyingBowItemMeta.displayName(Component.text(anvil.getRenameText())); - } + // set the bow name IF the player is trying to rename it + if (!anvil.getRenameText().equals("Flying Bow")) { + flyingBowItemMeta.displayName(Component.text(anvil.getRenameText())); + } - flyingBowItem.setItemMeta(flyingBowItemMeta); + flyingBowItem.setItemMeta(flyingBowItemMeta); - int previousRepairCost = NBT.get(anvil.getFirstItem(), nbt -> nbt.getInteger("RepairCost")); - int newRepairCost = previousRepairCost + 1; + int previousRepairCost = NBT.get(anvil.getFirstItem(), nbt -> nbt.getInteger("RepairCost")); + int newRepairCost = previousRepairCost + 1; - NBT.modify(flyingBowItem, nbt -> { - nbt.setInteger("RepairCost", newRepairCost); - }); + NBT.modify(flyingBowItem, nbt -> { + nbt.setInteger("RepairCost", newRepairCost); + }); - // set the anvil result to this new flying bow - event.setResult(flyingBowItem); + // set the anvil result to this new flying bow + event.setResult(flyingBowItem); - // set the exp cost for the repair - // it's very long, and ugly, but it works - // it runs a syncrounous task that sets the anvil cost - // before the client is told about the change - // so that the client can register what the exp amount is - // (I did not figure this out, I just feel I should say that) - FlyingBowOG.getPlugin().getServer().getScheduler().runTask( - FlyingBowOG.getPlugin(), - () -> anvil.setRepairCost(Math.min(4, anvil.getSecondItem().getAmount()) + newRepairCost) - ); - } - } + // set the exp cost for the repair + // it's very long, and ugly, but it works + // it runs a syncrounous task that sets the anvil cost + // before the client is told about the change + // so that the client can register what the exp amount is + // (I did not figure this out, I just feel I should say that) + FlyingBowOG.getPlugin() + .getServer() + .getScheduler() + .runTask( + FlyingBowOG.getPlugin(), + () -> anvil.setRepairCost(Math.min( + 4, anvil.getSecondItem().getAmount()) + + newRepairCost)); + } + } - - // check that a normal bow is not being used to repair the flying bow - // if the first item is a bow - if(firstItem.getType().equals(Material.BOW)){ - // if the second item is a bow - if(secondItem.getType().equals(Material.BOW)){ - // if only one bow has the HIDE_DYE flag - if(firstItem.getItemFlags().contains(ItemFlag.HIDE_DYE) ^ secondItem.getItemFlags().contains(ItemFlag.HIDE_DYE)){ - // set the result to null - event.setResult(null); - } - } - } - } - } - - } - -} \ No newline at end of file + // check that a normal bow is not being used to repair the flying bow + // if the first item is a bow + if (firstItem.getType().equals(Material.BOW)) { + // if the second item is a bow + if (secondItem.getType().equals(Material.BOW)) { + // if only one bow has the HIDE_DYE flag + if (firstItem.getItemFlags().contains(ItemFlag.HIDE_DYE) + ^ secondItem.getItemFlags().contains(ItemFlag.HIDE_DYE)) { + // set the result to null + event.setResult(null); + } + } + } + } + } + } +}