keep a special item on respawn!

Discussion in 'Plugin Development' started by yeongpin, Feb 15, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    yeongpin

    how to keep a special lore on respawn

    Code:
    package binditem;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.logging.Logger;
    import net.milkbowl.vault.chat.Chat;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Server;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.ServicesManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitScheduler;
    
    public class main extends JavaPlugin
      implements Listener
    {
      public static Economy econ = null;
      public static Chat chat = null;
    
      public void onEnable()
      {
        getServer().getPluginManager().registerEvents(this, this);
        Bukkit.getConsoleSender().sendMessage(getConfig().getString("binditems-load-head").replaceAll("&", "§"));
        Bukkit.getConsoleSender().sendMessage(getConfig().getString("binditems-load-prefix").replaceAll("&", "§"));
        Bukkit.getConsoleSender().sendMessage(getConfig().getString("binditems-load-body1").replaceAll("&", "§"));
        Bukkit.getConsoleSender().sendMessage(getConfig().getString("binditems-load-body2").replaceAll("&", "§"));
        Bukkit.getConsoleSender().sendMessage(getConfig().getString("binditems-load-body3").replaceAll("&", "§") + getDescription().getVersion() + ")");
        Bukkit.getConsoleSender().sendMessage(getConfig().getString("binditems-load-body4").replaceAll("&", "§") + ChatColor.RED + "(" + ChatColor.RED + getServer().getVersion() + ")");
        Bukkit.getConsoleSender().sendMessage(getConfig().getString("binditems-load-body5").replaceAll("&", "§"));
        Bukkit.getConsoleSender().sendMessage(getConfig().getString("binditems-load-body6").replaceAll("&", "§"));
        Bukkit.getConsoleSender().sendMessage(getConfig().getString("binditems-load-body7").replaceAll("&", "§"));
        Bukkit.getConsoleSender().sendMessage(getConfig().getString("binditems-load-body8").replaceAll("&", "§"));
        Bukkit.getConsoleSender().sendMessage(getConfig().getString("binditems-load-below").replaceAll("&", "§"));
        if (!setupEconomy()) {
          getLogger().severe(String.format("[%s] - Cant Found Vault , Plugin Disable", new Object[] { getDescription().getName() }));
          getServer().getPluginManager().disablePlugin(this);
          return;
        }
      }
    
      private boolean setupEconomy() {
        if (getServer().getPluginManager().getPlugin("Vault") == null) {
          return false;
        }
        RegisteredServiceProvider rsp = getServer().getServicesManager().getRegistration(Economy.class);
        if (rsp == null) {
          return false;
        }
        econ = (Economy)rsp.getProvider();
        return econ != null;
      }
    
      public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
      {
        if ((cmd.getName().equalsIgnoreCase("bounditem")) && ((sender instanceof Player)))
        {
          Player player = (Player)sender;
          ItemStack hand = player.getItemInHand();
          ItemMeta meta = hand.getItemMeta();
    
          EconomyResponse r = econ.withdrawPlayer(player.getName(), getConfig().getInt("money"));
          if (r.balance <= 0.0D) {
            player.sendMessage(getConfig().getString("nomoney").replaceAll("&", "§"));
            return true;
          }
    
          if (r.balance > 0.0D) {
            List lore = new ArrayList();
            lore.add(getConfig().getString("itemlore-main").replaceAll("&", "§"));
            lore.add(getConfig().getString("player-messager").replaceAll("&", "§") + player.getDisplayName());
            meta.setLore(lore);
            hand.setItemMeta(meta);
            sender.sendMessage(getConfig().getString("alreadybound").replaceAll("&", "§"));
            sender.sendMessage(getConfig().getString("successmoney").replaceAll("&", "§") + ChatColor.AQUA + ChatColor.BOLD + getConfig().getInt("money"));
            return true;
          }
    
        }
    
        return false;
      }
    
      @EventHandler
      public void onPlayerDeath(PlayerDeathEvent event)
      {
        List removed = new ArrayList();
    
        for (ItemStack item : event.getDrops())
        {
          ItemMeta meta = item.getItemMeta();
          List lore = meta.getLore();
    
          if ((lore != null) &&
            (lore.contains(getConfig().getString("itemlore-main").replaceAll("&", "§")))) {
            removed.add(item);
          }
        }
        event.getDrops().removeAll(removed);
      }
    
      public void onDisable() {
        Object localObject = Bukkit.getConsoleSender();
        ((ConsoleCommandSender)localObject).sendMessage(getConfig().getString("binditems-unload-head").replaceAll("&", "§"));
        Bukkit.getConsoleSender().sendMessage(getConfig().getString("binditems-unload-prefix").replaceAll("&", "§"));
        Bukkit.getConsoleSender().sendMessage(getConfig().getString("binditems-unload-body1").replaceAll("&", "§"));
        Bukkit.getConsoleSender().sendMessage(getConfig().getString("binditems-unload-body2").replaceAll("&", "§"));
        ((ConsoleCommandSender)localObject).sendMessage(getConfig().getString("binditems-unload-body3").replaceAll("&", "§") + getDescription().getVersion() + ")");
        Bukkit.getConsoleSender().sendMessage(getConfig().getString("binditems-unload-body4").replaceAll("&", "§") + ChatColor.RED + "(" + ChatColor.RED + getServer().getVersion() + ")");
        Bukkit.getConsoleSender().sendMessage(getConfig().getString("binditems-unload-body5").replaceAll("&", "§"));
        ((ConsoleCommandSender)localObject).sendMessage(getConfig().getString("binditems-unload-below").replaceAll("&", "§"));
      }
    
      @EventHandler
      public void onPlayerDropItem(PlayerDropItemEvent event) {
        final Player p = event.getPlayer();
        ItemStack item = event.getItemDrop().getItemStack();
    
        List lore = item.getItemMeta().getLore();
    
        if (lore.contains(getConfig().getString("itemlore-main").replaceAll("&", "§")))
        {
          p.sendMessage(getConfig().getString("cantdrop").replaceAll("&", "§"));
          event.setCancelled(true);
    
          getServer().getScheduler().runTask(this, new Runnable()
          {
            public void run()
            {
              p.updateInventory();
            }
          });
        }
      }
    }

    i have use this but cant

    Code:
    private static final Map<UUID, ArrayList<ItemStack>> itemsMap = new HashMap();
    
    
    Code:
    @EventHandler
      public void onPlayerDeath(PlayerDeathEvent event)
      {
        List removed = new ArrayList();
    
        for (ItemStack item : event.getDrops())
        {
          ItemMeta meta = item.getItemMeta();
          List lore = meta.getLore();
    
          if ((lore != null) &&
            (lore.contains(getConfig().getString("itemlore-main").replaceAll("&", "§")))) {
            itemsMap.put(event.getEntity().getUniqueId(), itemsList);
          }
        }
        event.getDrops().removeAll(removed);
      }

    Code:
    @EventHandler
      public void onPlayerRespawn(PlayerRespawnEvent event) {
        List itemsList = (List)itemsMap.get(event.getPlayer().getUniqueId());
        if (itemsList != null) {
            event.getPlayer().getInventory().addItem(new ItemStack[] { item });
          }
        }
      }
    }
     
  2. Offline

    MightyOne

    Well I see a lot of parts that aren't working like this. Why don't you tell what exactly you don't manage to do?
     
  3. Offline

    Tabuu_

    I've done something similar by setting the keepInventory to true on the PlayerDeathEvent and then dropping all items that do not meet the requirements of the item that should be kept.
     
  4. Offline

    MrDaniel

    @Tabuu_, I would actually recommend the exact opposite. On the playerdeathevent loop over the player's inventory and find the item with the lore. Once you found the item you would store it in a variable and then delete the item from the player's inventory. Then, when they respawned give them the item in that variable.
     
  5. Offline

    Tabuu_

    The reason I did not wanted to do that is because there are several reason a player could disconnect before re-spawning and receiving their items, for example the player could logout before re-spawning. If the server would reload between the death and the re-log/re-spawn this player would lose those items.

    Of course you could have the data to a config, but I found my method the most efficient since everything happens at the exact moment it should (on the players death). So no chance of weird behaviour, dupes, or other shenanigans.

    But both methods are of course valid, and the one that fits the situation best should be used.
     
Thread Status:
Not open for further replies.

Share This Page