Not working correctly

Discussion in 'Plugin Development' started by MajorSkillage, Oct 3, 2014.

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

    MajorSkillage

    Pretty much ive been working on a plugin so while the player is holding the id the user set in the config they can pvp and there are options to enable/disable joining with that id and such but when i try to interact with the item it wont do anything here is the code

    Code:
    package me.rockinroll99.itempvp;
     
    import java.util.Arrays;
    import java.util.List;
     
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main
    extends JavaPlugin
    implements Listener
    {
        int id = getConfig().getInt("id");
      Player p;
     
      public void loadConfiguration(){
          this.saveDefaultConfig();
          this.saveConfig();
      }
      public void onEnable()
      {
          loadConfiguration();
        getServer().getPluginManager().registerEvents(this, this);
      }
      @EventHandler
      public void onDrop(PlayerDropItemEvent e) { Player p = e.getPlayer();
        if ((!p.hasPermission("pvp.override")) || (!p.isOp()) || getConfig().getString("stop entity drops") == "true");
        {
          e.getPlayer().sendMessage(ChatColor.RED + "You can't drop stuff in the hub! :O");
          e.setCancelled(true);
        } }
     
      @EventHandler
      public void join(PlayerJoinEvent e) {
        if (!e.getPlayer().getInventory().contains(id) && getConfig().getString("id on join") == "true");
        {
          ItemStack dsword = new ItemStack(id);
          e.getPlayer().getInventory().addItem(dsword);
        }
      }
      @EventHandler
      public void onInvclick(InventoryClickEvent e) {
        Player p = ((OfflinePlayer)e).getPlayer();
        if ((!p.isOp()) || (!p.hasPermission("pvp.override")))
        {
            ItemStack dsword = new ItemStack(id);
            if(p.getItemInHand().equals(dsword))
          e.setCancelled(true);
        }
      }
      @EventHandler
      public void onSwordInteract(PlayerInteractEvent e) {
        p = e.getPlayer();
        int item = this.p.getItemInHand().getTypeId();
        if (item == id) {
          ItemStack dhelmet = new ItemStack(getConfig().getInt("helmet on hold"));
          ItemStack dchest = new ItemStack(getConfig().getInt("chestplate on hold"));
          ItemStack dlegs = new ItemStack(getConfig().getInt("leggings on hold"));
          ItemStack dboots = new ItemStack(getConfig().getInt("boots on hold"));
          this.p.getInventory().setHelmet(dhelmet);
          this.p.getInventory().setChestplate(dchest);
          this.p.getInventory().setLeggings(dlegs);
          this.p.getInventory().setBoots(dboots);
        } else if (item != id && getConfig().getString("remove armour after pvp") == "true") {
          ItemStack air = new ItemStack(Material.AIR);
          this.p.getInventory().setHelmet(air);
          this.p.getInventory().setChestplate(air);
          this.p.getInventory().setLeggings(air);
          this.p.getInventory().setBoots(air);
        }
      }
      @EventHandler
      public void stopPvP(EntityDamageByEntityEvent e) {
        Player dmger = (Player)e.getDamager();
        Player player = (Player)e.getEntity();
        if (dmger.getItemInHand().getTypeId() != id || player.getItemInHand().getTypeId() != id) {
            if(dmger.getGameMode().equals(GameMode.CREATIVE)){
                dmger.sendMessage(ChatColor.GOLD + "[" + ChatColor.RED + "PVP" + ChatColor.GOLD + "]" + ChatColor.RED + " " + "Why would you pvp in creative you nub!");
            }
            dmger.sendMessage(ChatColor.GOLD + "[" + ChatColor.RED + "PVP" + ChatColor.GOLD + "]" + ChatColor.RED + " " + player.getName() + " does not have the pvp item in their hand!");
            e.setCancelled(true);
          }
          dmger.sendMessage(ChatColor.GOLD + "[" + ChatColor.RED + "PVP" + ChatColor.GOLD + "]" + ChatColor.RED + " You must be holding your diamond sword to pvp!");
          e.setCancelled(true);
        }
      public boolean onCommand(CommandSender Sender, Command cmd, String pcom, String[] args){
          Player p = (Player)Sender;
          if(pcom.equalsIgnoreCase("pvp reload") && p.hasPermission("pvp.reload")){
              this.reloadConfig();
          }
        return false;
      }
      }
    and here is the config
    id on join: 'false'
    remove armour after pvp: 'true'
    helmet on hold: '310'
    chestplate on hold: '311'
    leggings on hold: '312'
    boots on hold: '313'
    stop entity drops: 'true'
    id: '276'
    p.s Does anyone have an idea to disable this for certain worlds?
     
  2. Offline

    Gamesareme

    MajorSkillage Have you tried debugging by adding messages to see where the problem is?
     
  3. Offline

    MajorSkillage

    console just spams heaps of stuff when i have a diamond sword in my hand even though i havn't interacted with it
     
  4. Offline

    Totom3

    MajorSkillage
    1. Do not compare Strings with ==, use .equals() or .equalsIgnoreCase() instead.
    2. Command label will never be 'pvp reload', and anyways don't compare with the label, use cmd.getName()
    3. You'll get a nice NPE if a player hits someone with empty hands
    4. If a value in the config can be either true or false, get it as a boolean instead of a String
    5. I personally wouldn't say "Why would you pvp in creative you nub!".... but anyways... after all you decide...
     
  5. Offline

    MajorSkillage

    kk, thanks i am rather new to config files xI

    also how can i stop people clicking their armour whilst holding the id to stop them from duping their armour? :I I want it to stop them dropping armour only

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  6. Offline

    Totom3

  7. Offline

    MajorSkillage

    i already have but i don't want people having heaps of diamond armour in their inventory...

    when I try using .equals() it comes up with an error even though they're both integers in eclipse it says "cannot invoke equals(int) on the primitive type int"
    Also doesn''t == return true or false?

    nvm ._. just realised it was an integer i have a major headache atm D:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
Thread Status:
Not open for further replies.

Share This Page