Solved Vanish plugin Problem Second Account

Discussion in 'Spigot Plugin Development' started by miguelrebolo, Jun 13, 2021.

  1. Hello i created one plugin based on a vanish system.I tried lot of things but i can not make work the plugin with second account.
    Here is the code, on console i do not have any error:
    Main.class
    Code (Text):
    package me.miguel.visibility;

    import org.bukkit.Bukkit;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    import me.miguel.listeners.PlayerListeners;

    public class Main extends JavaPlugin{

    public void onEnable() {
    PluginManager pm = Bukkit.getServer().getPluginManager();
    pm.registerEvents(new PlayerListeners(this), this);
    }
    }
     
    Listener
    Code (Text):
    package me.miguel.listeners;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import me.miguel.visibility.Main;


    public class PlayerListeners implements Listener{
    Main plugin;
    public PlayerListeners(Main plugin) {
    this.plugin = plugin;  
    }
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
    Player p = e.getPlayer();

    ItemStack VisStick = new ItemStack(Material.STICK);
    ItemMeta vs = VisStick.getItemMeta();
    vs.setDisplayName(ChatColor.GOLD+"Toggle Players on");
    VisStick.setItemMeta(vs);
    p.getInventory().setItem(5, VisStick);
    }

    @EventHandler
    public void onReJoin(PlayerRespawnEvent e) {
    Player p = e.getPlayer();

    ItemStack VisStick = new ItemStack(Material.STICK);
    ItemMeta vs = VisStick.getItemMeta();
    vs.setDisplayName(ChatColor.GOLD+"Toggle Players on");
    VisStick.setItemMeta(vs);
    p.getInventory().setItem(5, VisStick);
    }

    @SuppressWarnings("deprecation")
    @EventHandler
    public void onInteract(PlayerInteractEvent e) {
    Player p = e.getPlayer();  
    if(e.getAction() == Action.RIGHT_CLICK_AIR) {
    if(p.getItemInHand().getType() == Material.BLAZE_ROD) {
    for(Player hplayer : Bukkit.getOnlinePlayers()){
    p.hidePlayer(hplayer);
    p.sendMessage(ChatColor.GOLD+"You can no longer see other players");
    ItemStack VisOff = new ItemStack(Material.STICK);
    ItemMeta voff = VisOff.getItemMeta();
    voff.setDisplayName(ChatColor.GOLD+"Toggle Players on");
    VisOff.setItemMeta(voff);
    p.getInventory().getItemInHand();
    p.getInventory().setItemInHand(VisOff);
    p.getInventory().setItem(5, VisOff);
    return;
    }
    }
    if(e.getAction() == Action.RIGHT_CLICK_AIR) {
    if(p.getItemInHand().getType() == Material.STICK) {
    for(Player hplayer : Bukkit.getOnlinePlayers()){
    p.showPlayer(hplayer);
    p.sendMessage(ChatColor.GOLD+"You can now see other players");
    ItemStack VisOn = new ItemStack(Material.BLAZE_ROD);
    ItemMeta von = VisOn.getItemMeta();
    von.setDisplayName(ChatColor.GOLD+"Toggle Players off");
    VisOn.setItemMeta(von);
    p.getInventory().getItemInHand();
    p.getInventory().setItemInHand(VisOn);
    p.getInventory().setItem(5, VisOn);
    }
    }
    }
    }
    }
    }
     
    I got this message duplicated
    PHOTO:
    [​IMG]
     
  2. In some cases, PlayerInteractEvent can fire twice. This is why the message may sometimes duplicate.
     
  3. PlayerInteractEvent works for both hands, check which hand has clicked.
     
  4. Ok i changed this lines
    Code (Text):
    p.getItemInHand().getType() == Material.BLAZE_ROD
    to
    Code (Text):
    p.getInventory().getItemInMainHand().getType() == Material.BLAZE_ROD
    Code (Text):
    p.getItemInHand().getType() == Material.STICK
    to
    Code (Text):
    p.getInventory().getItemInMainHand().getType() == Material.STICK
    Only works when the last account join the server.But not with the first.Example Account A its online but Account B enter.When the account B use right click on the item the player A dissapear.But when the player A try to make the same, he cant.
     
  5. The code is still going to be ran for both hands, because it fires the event for both hands. I personally meant something like this:
    Code (Java):
    if (event.getHand() != EquipmentSlot.HAND) {
      return;
    }
    This piece of code will check whether the hand used is the main hand, or something else. It will return when it's not the main hand.
     
  6. Thanks i redo all the plugin.Also you need check if the item has item meta if item has displayname,Check if(e.getitem.gettype == Material.GLASS) for example.I create a arraylist for the plugin.