瀏覽代碼

Chimaera wing code is a little bit less disgusting now Fixes #5049

nossr50 1 月之前
父節點
當前提交
65d4d2b059

+ 46 - 10
src/main/java/com/gmail/nossr50/runnables/items/ChimaeraWingWarmup.java

@@ -1,5 +1,6 @@
 package com.gmail.nossr50.runnables.items;
 
+import com.gmail.nossr50.datatypes.interactions.NotificationType;
 import com.gmail.nossr50.datatypes.player.McMMOPlayer;
 import com.gmail.nossr50.locale.LocaleLoader;
 import com.gmail.nossr50.mcMMO;
@@ -7,16 +8,24 @@ import com.gmail.nossr50.util.CancellableRunnable;
 import com.gmail.nossr50.util.ChimaeraWing;
 import com.gmail.nossr50.util.ItemUtils;
 import com.gmail.nossr50.util.Misc;
+import com.gmail.nossr50.util.player.NotificationManager;
 import com.gmail.nossr50.util.skills.SkillUtils;
+import com.gmail.nossr50.util.sounds.SoundManager;
+import com.gmail.nossr50.util.sounds.SoundType;
 import org.bukkit.Location;
+import org.bukkit.Material;
 import org.bukkit.entity.Player;
 import org.bukkit.inventory.ItemStack;
 
+import static com.gmail.nossr50.util.ChimaeraWing.expendChimaeraWing;
+
 public class ChimaeraWingWarmup extends CancellableRunnable {
-    private final McMMOPlayer mcMMOPlayer;
+    private final McMMOPlayer mmoPlayer;
+    private final Location location;
 
-    public ChimaeraWingWarmup(McMMOPlayer mcMMOPlayer) {
-        this.mcMMOPlayer = mcMMOPlayer;
+    public ChimaeraWingWarmup(McMMOPlayer mmoPlayer, Location location) {
+        this.mmoPlayer = mmoPlayer;
+        this.location = location;
     }
 
     @Override
@@ -25,23 +34,24 @@ public class ChimaeraWingWarmup extends CancellableRunnable {
     }
 
     private void checkChimaeraWingTeleport() {
-        Player player = mcMMOPlayer.getPlayer();
-        Location previousLocation = mcMMOPlayer.getTeleportCommenceLocation();
+        final Player player = mmoPlayer.getPlayer();
+        final Location previousLocation = mmoPlayer.getTeleportCommenceLocation();
 
-        if (player.getLocation().distanceSquared(previousLocation) > 1.0 || !player.getInventory().containsAtLeast(ChimaeraWing.getChimaeraWing(0), 1)) {
+        if (player.getLocation().distanceSquared(previousLocation) > 1.0
+                || !player.getInventory().containsAtLeast(ChimaeraWing.getChimaeraWing(1), 1)) {
             player.sendMessage(LocaleLoader.getString("Teleport.Cancelled"));
-            mcMMOPlayer.setTeleportCommenceLocation(null);
+            mmoPlayer.setTeleportCommenceLocation(null);
             return;
         }
 
-        ItemStack inHand = player.getInventory().getItemInMainHand();
+        final ItemStack inHand = player.getInventory().getItemInMainHand();
 
         if (!ItemUtils.isChimaeraWing(inHand) || inHand.getAmount() < mcMMO.p.getGeneralConfig().getChimaeraUseCost()) {
             player.sendMessage(LocaleLoader.getString("Skills.NeedMore", LocaleLoader.getString("Item.ChimaeraWing.Name")));
             return;
         }
 
-        long recentlyHurt = mcMMOPlayer.getRecentlyHurt();
+        long recentlyHurt = mmoPlayer.getRecentlyHurt();
         int hurtCooldown = mcMMO.p.getGeneralConfig().getChimaeraRecentlyHurtCooldown();
 
         if (hurtCooldown > 0) {
@@ -53,6 +63,32 @@ public class ChimaeraWingWarmup extends CancellableRunnable {
             }
         }
 
-        ChimaeraWing.chimaeraExecuteTeleport();
+        chimaeraExecuteTeleport();
+    }
+
+    private void chimaeraExecuteTeleport() {
+        final Player player = mmoPlayer.getPlayer();
+
+        if (mcMMO.p.getGeneralConfig().getChimaeraUseBedSpawn() && player.getBedSpawnLocation() != null) {
+            mcMMO.p.getFoliaLib().getScheduler().teleportAsync(player, player.getBedSpawnLocation());
+        } else {
+            final Location spawnLocation = player.getWorld().getSpawnLocation();
+            if (spawnLocation.getBlock().getType() == Material.AIR) {
+                mcMMO.p.getFoliaLib().getScheduler().teleportAsync(player, spawnLocation);
+            } else {
+                mcMMO.p.getFoliaLib().getScheduler().teleportAsync(
+                        player, player.getWorld().getHighestBlockAt(spawnLocation).getLocation());
+            }
+        }
+
+        expendChimaeraWing(player, mcMMO.p.getGeneralConfig().getChimaeraUseCost(), player.getInventory().getItemInMainHand());
+        mmoPlayer.actualizeChimeraWingLastUse();
+        mmoPlayer.setTeleportCommenceLocation(null);
+
+        if (mcMMO.p.getGeneralConfig().getChimaeraSoundEnabled()) {
+            SoundManager.sendSound(player, location, SoundType.CHIMAERA_WING);
+        }
+
+        NotificationManager.sendPlayerInformation(player, NotificationType.ITEM_MESSAGE, "Item.ChimaeraWing.Pass");
     }
 }

+ 21 - 47
src/main/java/com/gmail/nossr50/util/ChimaeraWing.java

@@ -9,8 +9,6 @@ import com.gmail.nossr50.util.player.NotificationManager;
 import com.gmail.nossr50.util.player.UserManager;
 import com.gmail.nossr50.util.skills.CombatUtils;
 import com.gmail.nossr50.util.skills.SkillUtils;
-import com.gmail.nossr50.util.sounds.SoundManager;
-import com.gmail.nossr50.util.sounds.SoundType;
 import org.bukkit.ChatColor;
 import org.bukkit.Location;
 import org.bukkit.Material;
@@ -25,9 +23,6 @@ import java.util.ArrayList;
 import java.util.List;
 
 public final class ChimaeraWing {
-    private static McMMOPlayer mcMMOPlayer;
-    private static Location location;
-
     private ChimaeraWing() {}
 
     /**
@@ -40,7 +35,7 @@ public final class ChimaeraWing {
             return;
         }
 
-        ItemStack inHand = player.getInventory().getItemInMainHand();
+        final ItemStack inHand = player.getInventory().getItemInMainHand();
 
         if (!ItemUtils.isChimaeraWing(inHand)) {
             return;
@@ -51,7 +46,7 @@ public final class ChimaeraWing {
             return;
         }
 
-        mcMMOPlayer = UserManager.getPlayer(player);
+        final McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
 
         //Not loaded
         if (mcMMOPlayer == null)
@@ -61,10 +56,10 @@ public final class ChimaeraWing {
             return;
         }
 
-        int amount = inHand.getAmount();
+        int amountInHand = inHand.getAmount();
 
-        if (amount < mcMMO.p.getGeneralConfig().getChimaeraUseCost()) {
-            NotificationManager.sendPlayerInformation(player, NotificationType.REQUIREMENTS_NOT_MET, "Item.ChimaeraWing.NotEnough",String.valueOf(mcMMO.p.getGeneralConfig().getChimaeraUseCost() - amount), "Item.ChimaeraWing.Name");
+        if (amountInHand < mcMMO.p.getGeneralConfig().getChimaeraUseCost()) {
+            NotificationManager.sendPlayerInformation(player, NotificationType.REQUIREMENTS_NOT_MET, "Item.ChimaeraWing.NotEnough",String.valueOf(mcMMO.p.getGeneralConfig().getChimaeraUseCost() - amountInHand), "Item.ChimaeraWing.Name");
             return;
         }
 
@@ -92,13 +87,13 @@ public final class ChimaeraWing {
             }
         }
 
-        location = player.getLocation();
+        final Location playerLocation = player.getLocation();
 
         if (mcMMO.p.getGeneralConfig().getChimaeraPreventUseUnderground()) {
-            if (location.getY() < player.getWorld().getHighestBlockYAt(location)) {
-                player.getInventory().setItemInMainHand(new ItemStack(getChimaeraWing(amount - mcMMO.p.getGeneralConfig().getChimaeraUseCost())));
-                NotificationManager.sendPlayerInformation(player, NotificationType.REQUIREMENTS_NOT_MET, "Item.ChimaeraWing.Fail");
-                player.updateInventory();
+            if (playerLocation.getY() < player.getWorld().getHighestBlockYAt(playerLocation)) {
+                expendChimaeraWing(player, amountInHand, inHand);
+                NotificationManager.sendPlayerInformation(player,
+                        NotificationType.REQUIREMENTS_NOT_MET, "Item.ChimaeraWing.Fail");
                 player.setVelocity(new Vector(0, 0.5D, 0));
                 CombatUtils.dealDamage(player, Misc.getRandom().nextInt((int) (player.getHealth() - 10)));
                 mcMMOPlayer.actualizeChimeraWingLastUse();
@@ -107,44 +102,23 @@ public final class ChimaeraWing {
         }
 
         mcMMOPlayer.actualizeTeleportCommenceLocation(player);
-
-        long warmup = mcMMO.p.getGeneralConfig().getChimaeraWarmup();
-
-        if (warmup > 0) {
-            NotificationManager.sendPlayerInformation(player, NotificationType.ITEM_MESSAGE, "Teleport.Commencing", String.valueOf(warmup));
-            mcMMO.p.getFoliaLib().getScheduler().runAtEntityLater(player, new ChimaeraWingWarmup(mcMMOPlayer), 20 * warmup);
+        long teleportDelay = mcMMO.p.getGeneralConfig().getChimaeraWarmup();
+        if (teleportDelay > 0) {
+            NotificationManager.sendPlayerInformation(player, NotificationType.ITEM_MESSAGE, "Teleport.Commencing", String.valueOf(teleportDelay));
+            mcMMO.p.getFoliaLib().getScheduler().runAtEntityLater(player, new ChimaeraWingWarmup(mcMMOPlayer, playerLocation), 20 * teleportDelay);
         } else {
-            chimaeraExecuteTeleport();
+            mcMMO.p.getFoliaLib().getScheduler().runAtEntityLater(player, new ChimaeraWingWarmup(mcMMOPlayer, playerLocation), 0);
         }
     }
 
-    public static void chimaeraExecuteTeleport() {
-        Player player = mcMMOPlayer.getPlayer();
-
-        if (mcMMO.p.getGeneralConfig().getChimaeraUseBedSpawn() && player.getBedSpawnLocation() != null) {
-//            player.teleport(player.getBedSpawnLocation());
-            mcMMO.p.getFoliaLib().getScheduler().teleportAsync(player, player.getBedSpawnLocation());
+    public static void expendChimaeraWing(Player player, int amountInHand, ItemStack inHand) {
+        int amountAfterUse = amountInHand - mcMMO.p.getGeneralConfig().getChimaeraUseCost();
+        if (amountAfterUse >= 1) {
+            inHand.setAmount(amountAfterUse);
+            player.getInventory().setItemInMainHand(inHand);
         } else {
-            Location spawnLocation = player.getWorld().getSpawnLocation();
-            if (spawnLocation.getBlock().getType() == Material.AIR) {
-//                player.teleport(spawnLocation);
-                mcMMO.p.getFoliaLib().getScheduler().teleportAsync(player, spawnLocation);
-            } else {
-//                player.teleport(player.getWorld().getHighestBlockAt(spawnLocation).getLocation());
-                mcMMO.p.getFoliaLib().getScheduler().teleportAsync(player, player.getWorld().getHighestBlockAt(spawnLocation).getLocation());
-            }
+            player.getInventory().removeItem(inHand);
         }
-
-        player.getInventory().setItemInMainHand(new ItemStack(getChimaeraWing(player.getInventory().getItemInMainHand().getAmount() - mcMMO.p.getGeneralConfig().getChimaeraUseCost())));
-        player.updateInventory();
-        mcMMOPlayer.actualizeChimeraWingLastUse();
-        mcMMOPlayer.setTeleportCommenceLocation(null);
-
-        if (mcMMO.p.getGeneralConfig().getChimaeraSoundEnabled()) {
-            SoundManager.sendSound(player, location, SoundType.CHIMAERA_WING);
-        }
-
-        NotificationManager.sendPlayerInformation(player, NotificationType.ITEM_MESSAGE, "Item.ChimaeraWing.Pass");
     }
 
     public static ItemStack getChimaeraWing(int amount) {