From 5bb489e8ba5da07fbd7967cc4fdc03bd87fe679a Mon Sep 17 00:00:00 2001 From: Super_ Date: Wed, 8 Jan 2020 18:00:15 -0500 Subject: [PATCH] new item --- .../totalfreedommod/fun/ItemFun.java | 221 ++++++++++++------ .../totalfreedommod/shop/ShopData.java | 85 +++++++ .../totalfreedommod/shop/ShopItem.java | 3 +- .../totalfreedommod/util/FUtil.java | 34 +++ 4 files changed, 266 insertions(+), 77 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/fun/ItemFun.java b/src/main/java/me/totalfreedom/totalfreedommod/fun/ItemFun.java index ebe1cbff..f6619ced 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/fun/ItemFun.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/fun/ItemFun.java @@ -1,15 +1,23 @@ package me.totalfreedom.totalfreedommod.fun; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Random; +import java.util.Timer; +import java.util.TimerTask; +import javax.security.auth.login.FailedLoginException; import me.totalfreedom.totalfreedommod.FreedomService; import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.player.FPlayer; import me.totalfreedom.totalfreedommod.shop.ShopData; +import me.totalfreedom.totalfreedommod.shop.ShopItem; import me.totalfreedom.totalfreedommod.util.DepreciationAggregator; +import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FUtil; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.entity.Arrow; import org.bukkit.entity.Entity; @@ -27,7 +35,6 @@ import org.bukkit.event.player.PlayerFishEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; -import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.util.Vector; @@ -38,6 +45,31 @@ public class ItemFun extends FreedomService private final Random random = new Random(); + private static final String COOLDOWN_MESSAGE = ChatColor.RED + "You're on cooldown for this item."; + + private final Timer timer = new Timer(); + private final Map cooldownTracker = new HashMap<>(); + + private final Map orientationTracker = new HashMap<>(); + + private void cooldown(Player player, Material material, int seconds) + { + cooldownTracker.put(player, material); + timer.schedule(new TimerTask() + { + @Override + public void run() + { + cooldownTracker.remove(player); + } + }, seconds * 1000); + } + + public boolean onCooldown(Player player, Material material) + { + return cooldownTracker.containsKey(player) && cooldownTracker.containsValue(material); + } + public ItemFun(TotalFreedomMod plugin) { super(plugin); @@ -284,6 +316,51 @@ public class ItemFun extends FreedomService break; } + + case NETHER_STAR: + { + if (onCooldown(player, Material.NETHER_STAR)) + { + FUtil.playerMsg(player, COOLDOWN_MESSAGE); + break; + } + + ShopData sd = plugin.sh.getData(player); + ItemStack stack = player.getInventory().getItemInMainHand(); + + if (!sd.validate(stack, ShopItem.THOR_STAR)) + { + break; + } + + event.setCancelled(true); + Block targetBlock = player.getTargetBlock(null, 20); + + for (int i = 0; i < 5; i++) + { + player.getWorld().strikeLightning(targetBlock.getLocation()); + } + Player rplayer = FUtil.getRandomPlayer(); + FUtil.bcastMsg("Thor's Star has granted " + rplayer.getName() + " a " + ChatColor.YELLOW + "Electrical Diamond Sword" + ChatColor.RED + "!", ChatColor.RED); + ShopData psd = plugin.sh.getData(rplayer); + String key = FUtil.generateKey(8); + psd.giveRawItem(key); + plugin.sh.save(psd); + FUtil.give(rplayer, Material.DIAMOND_SWORD, "&eElectrical Diamond Sword", 1, "&7RMB - Strike lightning", ChatColor.DARK_GRAY + key); + cooldown(player, Material.NETHER_STAR, 600); + break; + } + + case DIAMOND_SWORD: + { + ShopData sd = plugin.sh.getData(player); + ItemStack stack = player.getInventory().getItemInMainHand(); + if (sd.validate(stack, "Electrical Diamond Sword")) + { + player.getWorld().strikeLightning(player.getTargetBlock(null, 20).getLocation()); + } + break; + } } } @@ -323,85 +400,77 @@ public class ItemFun extends FreedomService ShopData sd = plugin.sh.getData(player); PlayerInventory inv = event.getPlayer().getInventory(); ItemStack rod = inv.getItemInMainHand(); - if (rod.getType() == Material.FISHING_ROD) // just to make sure it is this hand that has the rod + if (sd.validate(rod, ShopItem.GRAPPLING_HOOK)) { - if (rod.hasItemMeta()) + if (event.getState() == PlayerFishEvent.State.REEL_IN || event.getState() == PlayerFishEvent.State.IN_GROUND) { - ItemMeta meta = rod.getItemMeta(); - if (meta.hasDisplayName()) + double orientation = player.getLocation().getYaw(); + if (orientationTracker.containsKey(player)) { - if (meta.getDisplayName().contains("Grappling Hook")) - { - if (!meta.hasLore()) - { - return; - } - for (String item : sd.getItems()) - { - if (!meta.getLore().contains(ChatColor.DARK_GRAY + item)) - { - return; - } - } - if (event.getState() == PlayerFishEvent.State.REEL_IN || event.getState() == PlayerFishEvent.State.IN_GROUND) - { - double orientation = player.getLocation().getYaw(); - if (orientation < 0.0) - { - orientation += 360; - } - int speed = 5; - if (player.getLocation().subtract(0, 1, 0).getBlock().getType() == Material.AIR) - { - speed = 15; - } - double xVel = 0; - double yVel = 1; - double zVel = 0; - if (orientation >= 0.0 && orientation < 22.5) - { - zVel = speed; - } - else if (orientation >= 22.5 && orientation < 67.5) - { - xVel = -(speed / 2.0); - zVel = speed / 2.0; - } - else if (orientation >= 67.5 && orientation < 112.5) - { - xVel = -speed; - } - else if (orientation >= 112.5 && orientation < 157.5) - { - xVel = -(speed / 2.0); - zVel = -(speed / 2.0); - } - else if (orientation >= 157.5 && orientation < 202.5) - { - zVel = -speed; - } - else if (orientation >= 202.5 && orientation < 247.5) - { - xVel = speed / 2.0; - zVel = -(speed / 2.0); - } - else if (orientation >= 247.5 && orientation < 292.5) - { - xVel = speed; - } - else if (orientation >= 292.5 && orientation < 337.5) - { - xVel = speed / 2.0; - zVel = speed / 2.0; - } - else if (orientation >= 337.5 && orientation < 360.0) - { - zVel = speed; - } - player.setVelocity(new Vector(xVel, yVel, zVel)); - } - } + orientation = orientationTracker.get(player); } + if (orientation < 0.0) + { + orientation += 360; + } + int speed = 5; + if (player.getLocation().subtract(0, 1, 0).getBlock().getType() == Material.AIR) + { + speed = 15; + } + double xVel = 0; + double yVel = 1; + double zVel = 0; + if (orientation >= 0.0 && orientation < 22.5) + { + zVel = speed; + } + else if (orientation >= 22.5 && orientation < 67.5) + { + xVel = -(speed / 2.0); + zVel = speed / 2.0; + } + else if (orientation >= 67.5 && orientation < 112.5) + { + xVel = -speed; + } + else if (orientation >= 112.5 && orientation < 157.5) + { + xVel = -(speed / 2.0); + zVel = -(speed / 2.0); + } + else if (orientation >= 157.5 && orientation < 202.5) + { + zVel = -speed; + } + else if (orientation >= 202.5 && orientation < 247.5) + { + xVel = speed / 2.0; + zVel = -(speed / 2.0); + } + else if (orientation >= 247.5 && orientation < 292.5) + { + xVel = speed; + } + else if (orientation >= 292.5 && orientation < 337.5) + { + xVel = speed / 2.0; + zVel = speed / 2.0; + } + else if (orientation >= 337.5 && orientation < 360.0) + { + zVel = speed; + } + player.setVelocity(new Vector(xVel, yVel, zVel)); + } + + if (event.getState() == PlayerFishEvent.State.FISHING) + { + orientationTracker.put(player, player.getLocation().getYaw()); + } + else + { + orientationTracker.remove(player); } } } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/shop/ShopData.java b/src/main/java/me/totalfreedom/totalfreedommod/shop/ShopData.java index 11ee3dbe..0a0e5368 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/shop/ShopData.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/shop/ShopData.java @@ -91,6 +91,11 @@ public class ShopData implements ConfigLoadable, ConfigSavable, Validatable return signature; } + public void giveRawItem(String signature) + { + items.add(signature); + } + public boolean hasItem(ShopItem item) { for (String i : items) @@ -125,6 +130,86 @@ public class ShopData implements ConfigLoadable, ConfigSavable, Validatable return stack; } + public boolean validate(ItemStack stack, String nameSegment) + { + if (!stack.hasItemMeta()) + { + return false; + } + + if (!stack.getItemMeta().hasDisplayName()) + { + return false; + } + + if (!stack.getItemMeta().getDisplayName().contains(nameSegment)) + { + return false; + } + + if (!stack.getItemMeta().hasLore()) + { + return false; + } + + boolean loreValid = false; + + for (String i : items) + { + if (stack.getItemMeta().getLore().contains(ChatColor.DARK_GRAY + i)) + { + loreValid = true; + } + } + + if (!loreValid) + { + return false; + } + + return true; + } + + public boolean validate(ItemStack stack, ShopItem item) + { + if (!stack.hasItemMeta()) + { + return false; + } + + if (!stack.getItemMeta().hasDisplayName()) + { + return false; + } + + if (!stack.getItemMeta().getDisplayName().contains(item.getName())) + { + return false; + } + + if (!stack.getItemMeta().hasLore()) + { + return false; + } + + boolean loreValid = false; + + for (String i : items) + { + if (stack.getItemMeta().getLore().contains(ChatColor.DARK_GRAY + i)) + { + loreValid = true; + } + } + + if (!loreValid) + { + return false; + } + + return true; + } + @Override public boolean isValid() { diff --git a/src/main/java/me/totalfreedom/totalfreedommod/shop/ShopItem.java b/src/main/java/me/totalfreedom/totalfreedommod/shop/ShopItem.java index c1062e34..1a0314db 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/shop/ShopItem.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/shop/ShopItem.java @@ -6,7 +6,8 @@ import org.bukkit.Material; public enum ShopItem { - GRAPPLING_HOOK("Grappling Hook", Material.FISHING_ROD, 100, ChatColor.GREEN); + GRAPPLING_HOOK("Grappling Hook", Material.FISHING_ROD, 100, ChatColor.GREEN), + THOR_STAR("Thor's Star", Material.NETHER_STAR, 10000, ChatColor.LIGHT_PURPLE); @Getter private final String name; diff --git a/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java b/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java index 092881cf..efe296ac 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/util/FUtil.java @@ -27,9 +27,12 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.OfflinePlayer; +import org.bukkit.block.Block; import org.bukkit.command.CommandSender; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.scheduler.BukkitTask; import org.bukkit.Material; @@ -495,4 +498,35 @@ public class FUtil { return CHARACTER_STRING.charAt(new Random().nextInt(CHARACTER_STRING.length())); } + + public static void give(Player player, Material material, String coloredName, int amount, String... lore) + { + ItemStack stack = new ItemStack(material, amount); + ItemMeta meta = stack.getItemMeta(); + meta.setDisplayName(FUtil.colorize(coloredName)); + List loreList = new ArrayList<>(); + for (String entry : lore) + { + loreList.add(FUtil.colorize(entry)); + } + meta.setLore(loreList); + stack.setItemMeta(meta); + player.getInventory().setItem(player.getInventory().firstEmpty(), stack); + } + + public static String generateKey(int length) + { + StringBuilder key = new StringBuilder(); + for (int i = 0; i < length; i++) + { + key.append(getRandomCharacter()); + } + return key.toString(); + } + + public static Player getRandomPlayer() + { + List players = new ArrayList<>(Bukkit.getOnlinePlayers()); + return players.get(random(0, players.size() - 1)); + } }