Turning players into strings and converting back to Player

Discussion in 'Plugin Development' started by McCastleWars, Feb 3, 2013.

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

    McCastleWars

    Title prob makes no fucking sense but. What I did is in my economy plugin I made players strings. But if I want to give those people money in that economy system I have to have all the players in my Main plugin strings too and that way i'm converting them is.

    Code:
    public static void test(String player1){
    player = Bukkit.getPlayer(player1).toString();
     
    player.setCanPickUpItems(false);
    }
    And I get this: The method setCanPickupItems(boolean) is undefined for the type String

    This prob again could be cause I'm tired and completely dumb at this time. But I want to get progress at night since I did nothing in the morning
     
  2. Offline

    RainoBoy97

    Player -> String: player.getName()
    String -> Player: Bukkit.getPlayer(String);
     
  3. Offline

    McCastleWars

    K heres a quick piece of code I made up to give more detail. And test

    Code:
        public static void joining(String leaver){
           
            Bukkit.getPlayer(leaver).setGameMode(GameMode.ADVENTURE); //Line 16
            Bukkit.getPlayer(leaver).teleport(Spawn.teleportLobbySpawn());
            if(!CastleWars.playersInGame.contains(leaver)){
                CastleWars.spectate.add(leaver);
                Bukkit.getPlayer(leaver).setCanPickupItems(false);
                Bukkit.getPlayer(leaver).getInventory().setHelmet(new ItemStack(Material.WOOL, 1));
                Bukkit.broadcastMessage(CastleWars.name + ChatColor.GREEN + " " + leaver +" Has Joined Spectator");
            }else if(CastleWars.playersInGame.contains(leaver)){
                Bukkit.getPlayer(leaver).sendMessage(CastleWars.name + " You tried to join a team. But you are currently in one please do /team leave to rejoin");
            }
    And I get this
    Code:
    Caused by: java.lang.NullPointerException
        at me.borlea.castlewars.events.SpectateTeam.joining(SpectateTeam.java:16)
        at me.borlea.castlewars.listeners.SignListener.onPlayerInteractSign(SignListener.java:53)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 16 more
    SignListener line 53: SpectateTeam.joining(player);
    Full signListener class

    Code:
        @EventHandler
        public void onPlayerInteractSign(PlayerInteractEvent e){
            String player = e.getPlayer().toString();
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
                if(e.getClickedBlock().getState() instanceof Sign){
                    Sign s = (Sign) e.getClickedBlock().getState();
     
                    if(s.getLine(0).equalsIgnoreCase(ChatColor.GOLD + "[CastleWars]")){
                        if(s.getLine(1).equalsIgnoreCase("random")){
                            TeamJoining.joinRandom(player);
                            s.setLine(2, ChatColor.WHITE + "" + CastleWars.playersInGame.size() + "/48" /**+ CastleWars.plugin.getConfig().getInt("Players.MaxPlayers")*/);
                            if(CastleWars.gameOn){
                                s.setLine(3, ChatColor.GREEN + "In-Game");
                                s.update();
                            }else if(!CastleWars.gameOn){
                                s.setLine(3, ChatColor.RED + "Not-InGame");
                                s.update();
                            }else{
                                s.setLine(3, ChatColor.MAGIC + "ERROR");
                                s.update();
                            }
                        }else if(s.getLine(1).equalsIgnoreCase("spectate")){
                            SpectateTeam.joining(player);
                        }else if(s.getLine(1).equalsIgnoreCase(ChatColor.RED + "leave")){
                            if(CastleWars.blueTeam.contains(player)){
                                BlueTeam.leaving(player);
                            }else if(CastleWars.redTeam.contains(player)){
                                RedTeam.leaving(player);
                            }else if(CastleWars.spectate.contains(player)){
                                SpectateTeam.leaving(player);
                            }else{
                                e.getPlayer().kickPlayer("HEY! how did you get to that castlewars leave sign. CHEATER");
                            }
                        }
                    }
                }
            }
        }
    Don't talk shit about my bad code :p its called Pre-Alpha
     
  4. Offline

    RainoBoy97

    You can do
    Player player = Bukkit.getPlayer(leaver);
    and then use player.setGamemode to make the code more clean ;)
     
  5. Offline

    McCastleWars

    Great done. Thing is it wasn't like that cause I was trying to fix the error.. Since its still here
     
  6. Offline

    raGan.

    If player is not online, Bukkit.getPlayer() will return null. You need to get offline player or check if player is null before you do something with it. Also use Bukkit.getPlayer() as infrequently as possible (i.e. not in huge loops or very fast repeating tasks).
     
  7. Offline

    McCastleWars

    2nd part i'll remember but the first part still wont fix my error..
     
  8. Offline

    raGan.

    Why are you even passing playerName to those methods ? Just pass Player object and use it as necessary.
     
  9. Offline

    McCastleWars

    Because I'm broadcasting who joined the team and who left.. I'm also adding them to an arraylist

    Its ok I figured it out. Thanks guys
     
  10. Offline

    raGan.

    I mean that you can do CastleWars.spectate.add(leaver.getName()); if you are passing player object. It's much faster and easier thing to do.
     
Thread Status:
Not open for further replies.

Share This Page