Opposite day

Discussion in 'Plugin Development' started by MajorSkillage, Sep 25, 2014.

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

    MajorSkillage

    Hey! I want it so that when a user is holding a diamond sword it allows them to pvp and gives them diamond armour otherwise if they're not holding it they can't PvP and if their previous item was a diamond sword on switch event that it will remove their armour and say that pvp is disabled.

    The error:
    Currently when i go to the diamond sword in my hotbar it wont work, then i switch to something that !Diamond sword and it says pvp enabled gives me my diamond armour and yeah... D:

    P.S are there any ways that if someone interacts with the armour equipped that it will cancel that event? thanks

    Code:
    package me.rockinroll99.dspvp;
     
     
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.player.PlayerItemHeldEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin implements Listener{
        Player p;
        boolean isHolding;
        public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
          }
        @EventHandler
        public void slotchange(PlayerItemHeldEvent e){
            ItemStack dsword = new ItemStack(Material.DIAMOND_SWORD);
            p = e.getPlayer();
                int itemid = e.getNewSlot();
                ItemStack item = e.getPlayer().getInventory().getItem(itemid);
                  if(item == dsword){
                    ItemStack dhelmet = new ItemStack(Material.DIAMOND_HELMET);
                    ItemStack dchest = new ItemStack(Material.DIAMOND_CHESTPLATE);
                    ItemStack dlegs = new ItemStack(Material.DIAMOND_LEGGINGS);
                    ItemStack dboots = new ItemStack(Material.DIAMOND_BOOTS);
                    p.setGameMode(GameMode.SURVIVAL);
                    p.getInventory().setHelmet(dhelmet);
                    p.getInventory().setChestplate(dchest);
                    p.getInventory().setLeggings(dlegs);
                    p.getInventory().setBoots(dboots);
                    p.sendMessage(ChatColor.GOLD + "[" + ChatColor.DARK_GREEN + "Biome" + ChatColor.RED + "Craft" + ChatColor.GOLD + "]" + ChatColor.GREEN + " PvP enabled.");
                    isHolding = true;
                } else if(item != dsword){
                    ItemStack air = new ItemStack(Material.AIR);
                    p.getInventory().setHelmet(air);
                    p.getInventory().setChestplate(air);
                    p.getInventory().setLeggings(air);
                    p.getInventory().setBoots(air);
                    isHolding = false;
                }
        }
        @EventHandler
        public void stopPvP(EntityDamageEvent e){
            Player player = (Player)e.getEntity();
            while(isHolding = false){
            if(e.getEntity() instanceof Player){
                player.sendMessage(ChatColor.GOLD + "[" + ChatColor.DARK_GREEN + "Biome" + ChatColor.RED + "Craft" + ChatColor.GOLD + "]" + " " + ChatColor.DARK_RED + "You must be holding your diamond sword in order to pvp!");
                e.setCancelled(true);
                }
            }
        }
    }
     
  2. Offline

    Unica

    • Cancel the damage on a EntityDamageByEntityEvent, rather than a EntityDamageEvent
    • Interaction with a player can be listened with PlayerInteractEntityEvent
     
  3. Offline

    MajorSkillage

    il cancel pvp with EntityDamageByEntityEvent but i still want people to be able to interact with each other for certain other plugins
    But what can i do about the opposite thing happening with what i want when i am holding the dsword?
     
Thread Status:
Not open for further replies.

Share This Page