Skip to content

Commit 65d4d2b

Browse files
committed
Chimaera wing code is a little bit less disgusting now Fixes #5049
1 parent 61c5ded commit 65d4d2b

File tree

2 files changed

+67
-57
lines changed

2 files changed

+67
-57
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
package com.gmail.nossr50.runnables.items;
22

3+
import com.gmail.nossr50.datatypes.interactions.NotificationType;
34
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
45
import com.gmail.nossr50.locale.LocaleLoader;
56
import com.gmail.nossr50.mcMMO;
67
import com.gmail.nossr50.util.CancellableRunnable;
78
import com.gmail.nossr50.util.ChimaeraWing;
89
import com.gmail.nossr50.util.ItemUtils;
910
import com.gmail.nossr50.util.Misc;
11+
import com.gmail.nossr50.util.player.NotificationManager;
1012
import com.gmail.nossr50.util.skills.SkillUtils;
13+
import com.gmail.nossr50.util.sounds.SoundManager;
14+
import com.gmail.nossr50.util.sounds.SoundType;
1115
import org.bukkit.Location;
16+
import org.bukkit.Material;
1217
import org.bukkit.entity.Player;
1318
import org.bukkit.inventory.ItemStack;
1419

20+
import static com.gmail.nossr50.util.ChimaeraWing.expendChimaeraWing;
21+
1522
public class ChimaeraWingWarmup extends CancellableRunnable {
16-
private final McMMOPlayer mcMMOPlayer;
23+
private final McMMOPlayer mmoPlayer;
24+
private final Location location;
1725

18-
public ChimaeraWingWarmup(McMMOPlayer mcMMOPlayer) {
19-
this.mcMMOPlayer = mcMMOPlayer;
26+
public ChimaeraWingWarmup(McMMOPlayer mmoPlayer, Location location) {
27+
this.mmoPlayer = mmoPlayer;
28+
this.location = location;
2029
}
2130

2231
@Override
@@ -25,23 +34,24 @@ public void run() {
2534
}
2635

2736
private void checkChimaeraWingTeleport() {
28-
Player player = mcMMOPlayer.getPlayer();
29-
Location previousLocation = mcMMOPlayer.getTeleportCommenceLocation();
37+
final Player player = mmoPlayer.getPlayer();
38+
final Location previousLocation = mmoPlayer.getTeleportCommenceLocation();
3039

31-
if (player.getLocation().distanceSquared(previousLocation) > 1.0 || !player.getInventory().containsAtLeast(ChimaeraWing.getChimaeraWing(0), 1)) {
40+
if (player.getLocation().distanceSquared(previousLocation) > 1.0
41+
|| !player.getInventory().containsAtLeast(ChimaeraWing.getChimaeraWing(1), 1)) {
3242
player.sendMessage(LocaleLoader.getString("Teleport.Cancelled"));
33-
mcMMOPlayer.setTeleportCommenceLocation(null);
43+
mmoPlayer.setTeleportCommenceLocation(null);
3444
return;
3545
}
3646

37-
ItemStack inHand = player.getInventory().getItemInMainHand();
47+
final ItemStack inHand = player.getInventory().getItemInMainHand();
3848

3949
if (!ItemUtils.isChimaeraWing(inHand) || inHand.getAmount() < mcMMO.p.getGeneralConfig().getChimaeraUseCost()) {
4050
player.sendMessage(LocaleLoader.getString("Skills.NeedMore", LocaleLoader.getString("Item.ChimaeraWing.Name")));
4151
return;
4252
}
4353

44-
long recentlyHurt = mcMMOPlayer.getRecentlyHurt();
54+
long recentlyHurt = mmoPlayer.getRecentlyHurt();
4555
int hurtCooldown = mcMMO.p.getGeneralConfig().getChimaeraRecentlyHurtCooldown();
4656

4757
if (hurtCooldown > 0) {
@@ -53,6 +63,32 @@ private void checkChimaeraWingTeleport() {
5363
}
5464
}
5565

56-
ChimaeraWing.chimaeraExecuteTeleport();
66+
chimaeraExecuteTeleport();
67+
}
68+
69+
private void chimaeraExecuteTeleport() {
70+
final Player player = mmoPlayer.getPlayer();
71+
72+
if (mcMMO.p.getGeneralConfig().getChimaeraUseBedSpawn() && player.getBedSpawnLocation() != null) {
73+
mcMMO.p.getFoliaLib().getScheduler().teleportAsync(player, player.getBedSpawnLocation());
74+
} else {
75+
final Location spawnLocation = player.getWorld().getSpawnLocation();
76+
if (spawnLocation.getBlock().getType() == Material.AIR) {
77+
mcMMO.p.getFoliaLib().getScheduler().teleportAsync(player, spawnLocation);
78+
} else {
79+
mcMMO.p.getFoliaLib().getScheduler().teleportAsync(
80+
player, player.getWorld().getHighestBlockAt(spawnLocation).getLocation());
81+
}
82+
}
83+
84+
expendChimaeraWing(player, mcMMO.p.getGeneralConfig().getChimaeraUseCost(), player.getInventory().getItemInMainHand());
85+
mmoPlayer.actualizeChimeraWingLastUse();
86+
mmoPlayer.setTeleportCommenceLocation(null);
87+
88+
if (mcMMO.p.getGeneralConfig().getChimaeraSoundEnabled()) {
89+
SoundManager.sendSound(player, location, SoundType.CHIMAERA_WING);
90+
}
91+
92+
NotificationManager.sendPlayerInformation(player, NotificationType.ITEM_MESSAGE, "Item.ChimaeraWing.Pass");
5793
}
5894
}

src/main/java/com/gmail/nossr50/util/ChimaeraWing.java

+21-47
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import com.gmail.nossr50.util.player.UserManager;
1010
import com.gmail.nossr50.util.skills.CombatUtils;
1111
import com.gmail.nossr50.util.skills.SkillUtils;
12-
import com.gmail.nossr50.util.sounds.SoundManager;
13-
import com.gmail.nossr50.util.sounds.SoundType;
1412
import org.bukkit.ChatColor;
1513
import org.bukkit.Location;
1614
import org.bukkit.Material;
@@ -25,9 +23,6 @@
2523
import java.util.List;
2624

2725
public final class ChimaeraWing {
28-
private static McMMOPlayer mcMMOPlayer;
29-
private static Location location;
30-
3126
private ChimaeraWing() {}
3227

3328
/**
@@ -40,7 +35,7 @@ public static void activationCheck(Player player) {
4035
return;
4136
}
4237

43-
ItemStack inHand = player.getInventory().getItemInMainHand();
38+
final ItemStack inHand = player.getInventory().getItemInMainHand();
4439

4540
if (!ItemUtils.isChimaeraWing(inHand)) {
4641
return;
@@ -51,7 +46,7 @@ public static void activationCheck(Player player) {
5146
return;
5247
}
5348

54-
mcMMOPlayer = UserManager.getPlayer(player);
49+
final McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
5550

5651
//Not loaded
5752
if (mcMMOPlayer == null)
@@ -61,10 +56,10 @@ public static void activationCheck(Player player) {
6156
return;
6257
}
6358

64-
int amount = inHand.getAmount();
59+
int amountInHand = inHand.getAmount();
6560

66-
if (amount < mcMMO.p.getGeneralConfig().getChimaeraUseCost()) {
67-
NotificationManager.sendPlayerInformation(player, NotificationType.REQUIREMENTS_NOT_MET, "Item.ChimaeraWing.NotEnough",String.valueOf(mcMMO.p.getGeneralConfig().getChimaeraUseCost() - amount), "Item.ChimaeraWing.Name");
61+
if (amountInHand < mcMMO.p.getGeneralConfig().getChimaeraUseCost()) {
62+
NotificationManager.sendPlayerInformation(player, NotificationType.REQUIREMENTS_NOT_MET, "Item.ChimaeraWing.NotEnough",String.valueOf(mcMMO.p.getGeneralConfig().getChimaeraUseCost() - amountInHand), "Item.ChimaeraWing.Name");
6863
return;
6964
}
7065

@@ -92,13 +87,13 @@ public static void activationCheck(Player player) {
9287
}
9388
}
9489

95-
location = player.getLocation();
90+
final Location playerLocation = player.getLocation();
9691

9792
if (mcMMO.p.getGeneralConfig().getChimaeraPreventUseUnderground()) {
98-
if (location.getY() < player.getWorld().getHighestBlockYAt(location)) {
99-
player.getInventory().setItemInMainHand(new ItemStack(getChimaeraWing(amount - mcMMO.p.getGeneralConfig().getChimaeraUseCost())));
100-
NotificationManager.sendPlayerInformation(player, NotificationType.REQUIREMENTS_NOT_MET, "Item.ChimaeraWing.Fail");
101-
player.updateInventory();
93+
if (playerLocation.getY() < player.getWorld().getHighestBlockYAt(playerLocation)) {
94+
expendChimaeraWing(player, amountInHand, inHand);
95+
NotificationManager.sendPlayerInformation(player,
96+
NotificationType.REQUIREMENTS_NOT_MET, "Item.ChimaeraWing.Fail");
10297
player.setVelocity(new Vector(0, 0.5D, 0));
10398
CombatUtils.dealDamage(player, Misc.getRandom().nextInt((int) (player.getHealth() - 10)));
10499
mcMMOPlayer.actualizeChimeraWingLastUse();
@@ -107,44 +102,23 @@ public static void activationCheck(Player player) {
107102
}
108103

109104
mcMMOPlayer.actualizeTeleportCommenceLocation(player);
110-
111-
long warmup = mcMMO.p.getGeneralConfig().getChimaeraWarmup();
112-
113-
if (warmup > 0) {
114-
NotificationManager.sendPlayerInformation(player, NotificationType.ITEM_MESSAGE, "Teleport.Commencing", String.valueOf(warmup));
115-
mcMMO.p.getFoliaLib().getScheduler().runAtEntityLater(player, new ChimaeraWingWarmup(mcMMOPlayer), 20 * warmup);
105+
long teleportDelay = mcMMO.p.getGeneralConfig().getChimaeraWarmup();
106+
if (teleportDelay > 0) {
107+
NotificationManager.sendPlayerInformation(player, NotificationType.ITEM_MESSAGE, "Teleport.Commencing", String.valueOf(teleportDelay));
108+
mcMMO.p.getFoliaLib().getScheduler().runAtEntityLater(player, new ChimaeraWingWarmup(mcMMOPlayer, playerLocation), 20 * teleportDelay);
116109
} else {
117-
chimaeraExecuteTeleport();
110+
mcMMO.p.getFoliaLib().getScheduler().runAtEntityLater(player, new ChimaeraWingWarmup(mcMMOPlayer, playerLocation), 0);
118111
}
119112
}
120113

121-
public static void chimaeraExecuteTeleport() {
122-
Player player = mcMMOPlayer.getPlayer();
123-
124-
if (mcMMO.p.getGeneralConfig().getChimaeraUseBedSpawn() && player.getBedSpawnLocation() != null) {
125-
// player.teleport(player.getBedSpawnLocation());
126-
mcMMO.p.getFoliaLib().getScheduler().teleportAsync(player, player.getBedSpawnLocation());
114+
public static void expendChimaeraWing(Player player, int amountInHand, ItemStack inHand) {
115+
int amountAfterUse = amountInHand - mcMMO.p.getGeneralConfig().getChimaeraUseCost();
116+
if (amountAfterUse >= 1) {
117+
inHand.setAmount(amountAfterUse);
118+
player.getInventory().setItemInMainHand(inHand);
127119
} else {
128-
Location spawnLocation = player.getWorld().getSpawnLocation();
129-
if (spawnLocation.getBlock().getType() == Material.AIR) {
130-
// player.teleport(spawnLocation);
131-
mcMMO.p.getFoliaLib().getScheduler().teleportAsync(player, spawnLocation);
132-
} else {
133-
// player.teleport(player.getWorld().getHighestBlockAt(spawnLocation).getLocation());
134-
mcMMO.p.getFoliaLib().getScheduler().teleportAsync(player, player.getWorld().getHighestBlockAt(spawnLocation).getLocation());
135-
}
120+
player.getInventory().removeItem(inHand);
136121
}
137-
138-
player.getInventory().setItemInMainHand(new ItemStack(getChimaeraWing(player.getInventory().getItemInMainHand().getAmount() - mcMMO.p.getGeneralConfig().getChimaeraUseCost())));
139-
player.updateInventory();
140-
mcMMOPlayer.actualizeChimeraWingLastUse();
141-
mcMMOPlayer.setTeleportCommenceLocation(null);
142-
143-
if (mcMMO.p.getGeneralConfig().getChimaeraSoundEnabled()) {
144-
SoundManager.sendSound(player, location, SoundType.CHIMAERA_WING);
145-
}
146-
147-
NotificationManager.sendPlayerInformation(player, NotificationType.ITEM_MESSAGE, "Item.ChimaeraWing.Pass");
148122
}
149123

150124
public static ItemStack getChimaeraWing(int amount) {

0 commit comments

Comments
 (0)