1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Fix storm staff usage bug

Also improve the way it is done, there was no way to go through all possible usages checking the lore on each one...
Downside: It will reset the usage on current items due to it being saved/loaded in a different way
This commit is contained in:
WalshyDev 2019-09-10 21:36:34 +01:00
parent 75a2a9611d
commit d3b13e62f5
2 changed files with 32 additions and 27 deletions

View File

@ -6,6 +6,7 @@ import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.items.StormStaff;
import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
@ -460,7 +461,9 @@ public final class SlimefunItems {
public static final ItemStack STAFF_WIND = new CustomItem(Material.STICK, "&6Elemental Staff &7- &b&oWind", "", "&7Element: &b&oWind", "", "&eRight Click&7 to launch yourself forward");
public static final ItemStack STAFF_FIRE = new CustomItem(Material.STICK, "&6Elemental Staff &7- &c&oFire", "", "&7Element: &c&oFire");
public static final ItemStack STAFF_WATER = new CustomItem(Material.STICK, "&6Elemental Staff &7- &1&oWater", "", "&7Element: &1&oWater", "", "&eRight Click&7 to extinguish yourself");
public static final ItemStack STAFF_STORM = new CustomItem(Material.STICK, "&6Elemental Staff &7- &8&oStorm", "", "&7Element: &8&oStorm", "", "&eRight Click&7 to summon a lightning", "&eX Uses &7left");
public static final ItemStack STAFF_STORM = new CustomItem(Material.STICK, "&6Elemental Staff &7- &8&oStorm", "",
"&7Element: &8&oStorm", "", "&eRight Click&7 to summon a lightning",
"&e" + StormStaff.MAX_USES + " Uses &7left");
static {
STAFF_WIND.addUnsafeEnchantment(Enchantment.LUCK, 1);

View File

@ -1,12 +1,14 @@
package me.mrCookieSlime.Slimefun.Objects.SlimefunItem.items;
import java.util.List;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Sound;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.inventory.ItemStack;
@ -21,11 +23,14 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SimpleSlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.handlers.ItemInteractionHandler;
import me.mrCookieSlime.Slimefun.Setup.Messages;
import me.mrCookieSlime.Slimefun.Setup.SlimefunManager;
import org.bukkit.persistence.PersistentDataType;
public class StormStaff extends SimpleSlimefunItem<ItemInteractionHandler> {
private static final int MAX_USES = 8;
public static final int MAX_USES = 8;
private static final NamespacedKey usageKey = new NamespacedKey(SlimefunPlugin.instance, "stormstaff_usage");
public StormStaff(Category category, ItemStack item, String id, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, id, recipeType, recipe, getCraftedOutput());
}
@ -56,13 +61,13 @@ public class StormStaff extends SimpleSlimefunItem<ItemInteractionHandler> {
ItemStack sfItem = getItem();
ItemMeta sfItemMeta = sfItem.getItemMeta();
List<String> sfItemLore = sfItemMeta.getLore();
// Index 1 and 3 in SlimefunItems.STAFF_STORM has lores with words and stuff so we check for them.
if (itemLore.size() < 6 && itemLore.get(1).equals(sfItemLore.get(1)) && itemLore.get(3).equals(sfItemLore.get(3))) {
if (p.getFoodLevel() >= 4 || p.getGameMode() == GameMode.CREATIVE) {
// Get a target block with max. 30 blocks of distance
Location loc = p.getTargetBlock(null, 30).getLocation();
if (loc.getWorld() != null && loc.getChunk().isLoaded()) {
if (loc.getWorld().getPVP() && SlimefunPlugin.getProtectionManager().hasPermission(p, loc, ProtectionModule.Action.PVP)) {
loc.getWorld().strikeLightning(loc);
@ -72,33 +77,30 @@ public class StormStaff extends SimpleSlimefunItem<ItemInteractionHandler> {
Bukkit.getPluginManager().callEvent(event);
p.setFoodLevel(event.getFoodLevel());
}
for (int i = MAX_USES; i > 0; i--) {
if (i == 1 && ChatColor.translateAlternateColorCodes('&', "&e1 Use &7left").equals(itemLore.get(4))) {
e.setCancelled(true);
p.playSound(p.getLocation(), Sound.ENTITY_ITEM_BREAK, 1, 1);
item.setAmount(0);
return true;
}
else if (ChatColor.translateAlternateColorCodes('&', "&e" + i + " Uses &7left").equals(itemLore.get(4))) {
itemLore.set(4, ChatColor.translateAlternateColorCodes('&', "&e" + (i - 1) + ' ' + (i > 2 ? "Uses": "Use") + " &7left"));
e.setCancelled(true);
// Saving the changes to lore and item.
itemMeta.setLore(itemLore);
item.setItemMeta(itemMeta);
return true;
}
int currentUses = itemMeta.getPersistentDataContainer()
.getOrDefault(usageKey, PersistentDataType.INTEGER, MAX_USES);
e.setCancelled(true);
if (currentUses == 1) {
p.playSound(p.getLocation(), Sound.ENTITY_ITEM_BREAK, 1, 1);
item.setAmount(0);
} else {
itemMeta.getPersistentDataContainer().set(
usageKey, PersistentDataType.INTEGER, --currentUses
);
itemLore.set(4, ChatColor.translateAlternateColorCodes('&',
"&e" + currentUses + ' ' + (currentUses == 1 ? "Uses": "Use") + " &7left"));
itemMeta.setLore(itemLore);
item.setItemMeta(itemMeta);
}
return false;
}
return true;
}
else {
Messages.local.sendTranslation(p, "messages.no-pvp", true);
}
}
}
}
else {
Messages.local.sendTranslation(p, "messages.hungry", true);
}