[Help]Setting player armour and moving variables between classes?

Discussion in 'Plugin Development' started by 6SidedPentagon, Aug 24, 2012.

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

    6SidedPentagon

    Hi, im having a problem with my code in these lines, it should set the players armour to gold when they respawn but it isnt working, should this be in an on respawn event with a global variable of the player obtained from the death event?

    My listener class

    Code:
    public class PlayerHitListener implements Listener
    {
        @EventHandler
        public void onDeath(PlayerDeathEvent event)
        {
            if (event.getEntityType() == EntityType.PLAYER)
            {
                Player PlayerKilled = event.getEntity();
                Bukkit.broadcastMessage("Your Killer Was: " + PlayerKilled.getKiller());
                Bukkit.broadcastMessage("you have joined the seekers, go kill all others!");
               
                ItemStack GoldBody = new ItemStack(Material.GOLD_CHESTPLATE);
                ItemStack GoldLegs = new ItemStack(Material.GOLD_LEGGINGS);
                PlayerKilled.getInventory().setChestplate(GoldBody);
                PlayerKilled.getInventory().setLeggings(GoldLegs);
               
                PlayerKilled.teleport(SeekerStartLocation);
            }
        }
    }
    My other problem is that the "SeekerStartLocation" is created in another class and i was wondering how i could get this variable (which is a location) into this class so i can set where the player will respawn

    My main class (where the "SeekerStartLocation" is created)

    Code:
    public class HideAndSeek extends JavaPlugin
    {
       
        Logger log = Logger.getLogger("minecraft");
        PluginManager PM;
        World world;
        String[] AllPlayers;
        String[] DeadPlayers;
        Player SetStartPlayer;
        Player PlayerKilled;
        Player currentPlayer;
        Location StartLocation;
        Location SeekerStartLocation;
        Random rand;
        Player FirstSeeker;
        int SeekerXLocation;
        int SeekerNumber;
        int Max;
        int Min;
        int XLocation;
        int YLocation;
        int ZLocation;
       
        @Override
        public void onDisable()
        {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.log.info(pdfFile.getName() + "version" + pdfFile.getVersion() + "is now disabled");
        }
       
        @Override
        public void onEnable()
        {
            PM = this.getServer().getPluginManager();
            world = Bukkit.getServer().getWorld("World");
            PluginDescriptionFile pdfFile = this.getDescription();
            this.log.info(pdfFile.getName() + "version" + pdfFile.getVersion() + "is now enabled");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            if(commandLabel.equalsIgnoreCase("setstartpoint"))
            {
                SetStartPlayer = (Player)sender;
                StartLocation = SetStartPlayer.getLocation();
                XLocation = (int) StartLocation.getX();
                YLocation = (int) StartLocation.getY();
                ZLocation = (int) StartLocation.getZ();
                SetStartPlayer.sendMessage(ChatColor.LIGHT_PURPLE + "Set reset spawn location to:" + ChatColor.GREEN + XLocation + "," + YLocation + "," + ZLocation);
            }
           
            if(commandLabel.equalsIgnoreCase("start"))
            {
               
                AllPlayers =  new String[getServer().getOnlinePlayers().length];
                for(int i=0;i<AllPlayers.length;i++)
                {
                    Player tempPlayer = Bukkit.getServer().getOnlinePlayers()[i];
                    AllPlayers[i] = tempPlayer.getName();
                    System.out.println(AllPlayers[i]);
                }
               
                DeadPlayers = new String[AllPlayers.length];
               
                /*Min = 1;
                Max = getServer().getOnlinePlayers().length;
                System.out.println(Max);
                //SeekerNumber = rand.nextInt(Max - Min + 1) + Min;
                System.out.println(Min);
                System.out.println(Max);
                //System.out.println(SeekerNumber);*/
               
               
                FirstSeeker = (Player)sender;
                FirstSeeker.sendMessage("You have been selected as the first seeker!");
                FirstSeeker.sendMessage("Wait to be released and catch the others!");
                SeekerXLocation = XLocation - 10;
               [COLOR=#000000] SeekerStartLocation[/COLOR] = createLocation(world,SeekerXLocation,YLocation,ZLocation);
                FirstSeeker.getInventory().clear();
                FirstSeeker.getInventory().setHelmet(null);
                FirstSeeker.getInventory().setChestplate(null);
                FirstSeeker.getInventory().setLeggings(null);
                FirstSeeker.getInventory().setBoots(null);
               
                ItemStack GoldBody = new ItemStack(Material.GOLD_CHESTPLATE);
                ItemStack GoldLegs = new ItemStack(Material.GOLD_LEGGINGS);
               
                FirstSeeker.getInventory().setChestplate(GoldBody);
                FirstSeeker.getInventory().setLeggings(GoldLegs);
               
                Block Block1;
                Block Block2;
                Block Block3;
                Block Block4;
                Block Block5;
                Block Block6;
                Block Block7;
                Block Block8;
                Block Block9;
                Block Block10;
               
               
                Block1 = world.getBlockAt(SeekerXLocation + 1, YLocation, ZLocation);
                Block2 = world.getBlockAt(SeekerXLocation - 1, YLocation, ZLocation);
                Block3 = world.getBlockAt(SeekerXLocation + 1, YLocation + 1, ZLocation);
                Block4 = world.getBlockAt(SeekerXLocation - 1, YLocation + 1, ZLocation);
                Block5 = world.getBlockAt(SeekerXLocation, YLocation, ZLocation + 1);
                Block6 = world.getBlockAt(SeekerXLocation, YLocation, ZLocation - 1);
                Block7 = world.getBlockAt(SeekerXLocation, YLocation + 1, ZLocation + 1);
                Block8 = world.getBlockAt(SeekerXLocation, YLocation + 1, ZLocation - 1);
                Block9 = world.getBlockAt(SeekerXLocation, YLocation - 1, ZLocation);
                Block10 = world.getBlockAt(SeekerXLocation, YLocation + 2, ZLocation);
               
                Block1.setTypeId(7);
                Block2.setTypeId(7);
                Block3.setTypeId(7);
                Block4.setTypeId(7);
                Block5.setTypeId(7);
                Block6.setTypeId(7);
                Block7.setTypeId(7);
                Block8.setTypeId(7);
                Block9.setTypeId(7);
                Block10.setTypeId(7);
               
                DeadPlayers[0] = FirstSeeker.getName();
               
                for(int i = 0; i < getServer().getOnlinePlayers().length; i++)
                {
                    currentPlayer = getServer().getOnlinePlayers()[i];
                    currentPlayer.getInventory().clear();
                    currentPlayer.getInventory().setHelmet(null);
                    currentPlayer.getInventory().setChestplate(null);
                    currentPlayer.getInventory().setLeggings(null);
                    currentPlayer.getInventory().setBoots(null);
                    currentPlayer.teleport(StartLocation);
                }
                FirstSeeker.teleport(SeekerStartLocation);
               
                PM.registerEvents(new PlayerHitListener(),this);
            }
       
            return false;
        }
    Any help appreciated thanks
     
  2. Offline

    Timr

    You can add a constructor to your listener class, then construct it in your onEnable()

    Code:
    public HideAndSeek plugin;
     
    public PlayerHitListener(HideAndSeek plugin) {
        this.plugin = plugin;
    }
    Main class:

    Code:
    public PlayerHitListener phl;
     
    public void onEnable() {
    //code
     
    phl = new PlayerHitListener(this);
    getServer().getPluginManager().registerEvents(phl, this);
     
    //code
    }
    Edit: In your main class, be sure to declare all variables you may want to access in the listener as Public (public String string; ). In your listener you can access the public variables in your main class by using: plugin.<variable name>
     
  3. Offline

    sternmin8or

    and the other part of what you needed is: yes, you need to add armor when they respawn, not when they die
     
  4. Offline

    6SidedPentagon

    thanks alot Timr :), that solution has worked perfectly
     
Thread Status:
Not open for further replies.

Share This Page