Invited

Discussion in 'Plugin Development' started by NoSpanMan, Apr 4, 2015.

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

    NoSpanMan

    I found an good code for invation accepts:
    Code:
    [LIST=1]
    [*]public static HashMap<String, Long> invitedPlayers = new HashMap<String, Long>();
    [*]
    
    [*]public void invitePlayer(Player player, int seconds) { //Invite the playe for a specified time in seconds
    [*]if (seconds > 0) {
    [*]invitedPlayers.put(player.getName(), ((seconds * 1000) + System.currentTimeMillis()));
    [*]player.sendMessage(ChatColor.GOLD + "You have been invited to a game!"); //You can change this as need be
    [*]player.sendMessage(ChatColor.GRAY + seconds + " seconds left to type /accept and join the game."); //Same with this message
    [*]}
    [*]}
    [*]
    
    [*]public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    [*]if (sender instanceof Player) {
    [*]Player player = (Player) sender;
    [*]if (command.getName().equalsIgnoreCase("accept")) {
    [*]if (invitedPlayers.containsKey(player.getName())) { //If the player was invited at some point, check if the invitation has expired
    [*]long inviteEnds = invitedPlayers.get(player.getName());
    [*]if (inviteEnds >= System.currentTimeMillis()) { //If the invitation is still valid, let him join the game
    [/LIST]
    
    
    [LIST=1]
    [*]//can i do here my stuff when he types /accept ?
    [/LIST]
    
    
    [LIST=1]
    [*]} else { //If the invitation has expired, tell the player and remove him from the invitation list
    [*]invitedPlayers.remove(player.getName());
    [*]player.sendMessage(ChatColor.RED + "You're invitation has expired!");
    [*]player.sendMessage(ChatColor.GRAY + "You'll need to get invited again to join the game.");
    [*]}
    [*]} else { //If the player hasn't ever received an invite or the last one expired and was removed, tell him
    [*]player.sendMessage(ChatColor.RED + "You need to receive an invitation before you can join a game!");
    [*]}
    [*]}
    [*]}
    [*]}
    [/LIST]
    
    Where can i put the code what happens when he type /accept and how can i invite someone?
     
  2. Offline

    1Rogue

    I'm wondering how you've determined how this is "good" code.

    Especially since any code you copy/paste blindly into projects is basically automatically terrible. You should be writing these things yourself, this is plugin development, not plugin plagiarism.
     
    mine-care and CodePlaysMinecraft like this.
  3. Offline

    NoSpanMan

    @1Rogue my plan is to gonna edit this i don't can't find tutorials for this..............
     
  4. @NoSpanMan Doing an invite and accept command is fairly simple. I highly suggest you learn Java before you try and learn Bukkit. Also, please try and speak English (even if it's not your first language, you can still use a translator), it's very hard to understand what you're trying to say (I'm not making fun of you, I just want to help you).
     
  5. Offline

    nverdier

    @NoSpanMan If you don't know where to learn Java, read this.
     
    CodePlaysMinecraft likes this.
  6. Offline

    NoSpanMan

    I know al the things about java what i need for bukkit
     
  7. Offline

    nverdier

    @NoSpanMan Well you copy and pasted your code...
     
  8. Offline

    NoSpanMan

    I dont know how i can make a invite system but i know java
     
  9. Offline

    1Rogue

    Okay, if that's the case, you should have no problem writing this code yourself.

    I'd say have a manager class which holds Request objects, and use a hash table (BiHashTable, even) to see requests between two people
     
    dlange and nverdier like this.
  10. Offline

    NoSpanMan

    I have this:
    Code:
    getConfig().set(p.getName() + ".world", p.getLocation().getWorld().getName());
           getConfig().set(p.getName() + ".x", p.getLocation().getX());
           getConfig().set(p.getName() + ".y", p.getLocation().getY());
           getConfig().set(p.getName() + ".z", p.getLocation().getZ());
           saveConfig();
    
    And how can i add now that the maker of the home types /add <playername> and that player can type /home <anotherplayername>
     
  11. Offline

    mine-care

    @NoSpanMan @1Rogue is pretty pretty right, prefer to ask for help in your own code, this way answers can turn your attention to pieces of your own code that you have logically processed and therefore you understand.
    Also i dont think a lot of people would be able to read the code just juding by the looks of it with tags like [*] all over the place.

    Uppon what was said by @1Rogue you may want to have a timeout system by storing the system miliseconds + some seconds at the time the request was thrown and uppon accepting, check if it has timed out. if so dont proceed any further.

    Put it in between the brackets of your class.

    -----------------------

    Oh so you are making a /sethome like plugin? i thought you where making tp plugin :3
    I sugest you avoid constant config saving & loading, it is extremely inefficient Also dont use playernames because of 1.8 switch to UUID's, if a player changes his name, then automatically he lost his house...

    How i think of the plugin:
    (leaving data management up to you to handle)
    1. when someone creates a home, create an object that holds the location(s) for this specific player
    2. in this object also store other players that are allowed in to the house(s) of the player of reference
    3. when requested simply add them to the list of players allowed.
     
  12. Offline

    NoSpanMan

    I have created a code:
    Code:
    if (cmd.getName().equalsIgnoreCase("add")) {
           if (args.length == 0) {
             p.sendMessage(ChatColor.RED + "Please do /add <playername>");
          return true;
           }
           Player target = Bukkit.getServer().getPlayer(args[0]);
           if (target == null) {
           p.sendMessage(ChatColor.RED + "Can't find the player!");
             return true;
           }
           p.sendMessage("fdsf");
           getConfig().set(p.getName() + ".other" + target.getName() + ".world", getConfig().getString(p.getName() + ".world"));
           getConfig().set(p.getName() + ".other" + target.getName() + ".x", getConfig().getInt(p.getName() + ".x"));
           getConfig().set(p.getName() + ".other" + target.getName() + ".x", getConfig().getInt(p.getName() + ".y"));
           getConfig().set(p.getName() + ".other" + target.getName() + ".x", getConfig().getInt(p.getName() + ".z"));
           saveConfig();
           }
         }
         return false;
       }
    }
    
    But when i makes (you cant see that code) a home and then /add the /add command doesn't work good. He don't says can't find player and please enter he says nothing and he don't change the config.

    This is the config.yml when i typed /sethome:
    Code:
    <sendersname>:
      world: world
      x: -53.416247469667695
      y: 68.0
      z: 236.09139734870857
    
    @nverdier Are you dutch ?

    <Edit by mrCookieSlime: Merged posts. Please don't triple post. There is an Edit Button right next to the Date.>
     
    Last edited by a moderator: Apr 4, 2015
  13. Offline

    nverdier

  14. Offline

    NoSpanMan

    @nverdier Im asking because you are the same times online as me :). And do you now whats wrong with my last comment the code?
     
    nverdier likes this.
  15. Offline

    mine-care

    @NoSpanMan have you registered the command in your Plugin.yml ?
     
  16. Offline

    NoSpanMan

  17. @NoSpanMan Please post your onEnable and plugin.yml
     
  18. Offline

    NoSpanMan

    @bwfcwalshy
    Code:
    public void onEnable() {
         getConfig().options().copyDefaults(true);
         saveConfig();
       }
    
    /home and /sethome does works
     
  19. @NoSpanMan You haven't registered it.

    #getCommand(String).setExecutor(CommandExecutor);
     
  20. Offline

    NoSpanMan

    But i dont have 2 classes?
     
  21. @NoSpanMan Please post your whole main class.
     
  22. Offline

    NoSpanMan

    Code:
    package me.coding.home4you;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    
       public void onEnable() {
         getConfig().options().copyDefaults(true);
         saveConfig();
       }
       
       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         Player p = (Player) sender;
         if(cmd.getName().equalsIgnoreCase("sethome")) {
           getConfig().set(p.getName() + ".world", p.getLocation().getWorld().getName());
           getConfig().set(p.getName() + ".x", p.getLocation().getX());
           getConfig().set(p.getName() + ".y", p.getLocation().getY());
           getConfig().set(p.getName() + ".z", p.getLocation().getZ());
           saveConfig();
           return true;
         }
         
         if(cmd.getName().equalsIgnoreCase("home")) {
           if (getConfig().contains(p.getName() + ".world")) {
           World w = Bukkit.getServer().getWorld(getConfig().getString(p.getName() + ".world"));
           int x = getConfig().getInt(p.getName() + ".x");
           int y = getConfig().getInt(p.getName() + ".y");
           int z = getConfig().getInt(p.getName() + ".z");
           p.teleport(new Location(w, x, y, z));
           } else {
             p.sendMessage(ChatColor.RED + "You don't have an home");
             return true;
           }
           
         if (cmd.getName().equalsIgnoreCase("add")) {
           if (args.length == 0) {
             p.sendMessage(ChatColor.RED + "Please do /add <playername>");
          return true;
           }
           Player target = Bukkit.getServer().getPlayer(args[0]);
           if (target == null) {
           p.sendMessage(ChatColor.RED + "Can't find the player!");
             return true;
           }
           getConfig().set(p.getName() + ".other" + target.getName() + ".world", getConfig().getString(p.getName() + ".world"));
           getConfig().set(p.getName() + ".other" + target.getName() + ".x", getConfig().getInt(p.getName() + ".x"));
           getConfig().set(p.getName() + ".other" + target.getName() + ".x", getConfig().getInt(p.getName() + ".y"));
           getConfig().set(p.getName() + ".other" + target.getName() + ".x", getConfig().getInt(p.getName() + ".z"));
           saveConfig();
           }
         }
         return false;
       }
    }
    
     
  23. @NoSpanMan Check before casting!
    Code:
    if (sender instanceof Player) {
    Player player = (Player) sender;
    }
    (Read @mine-care signature!)
    You're also casting the sender to a player before the command is even DONE. Please, try to think logically. You're not creating the config onEnable, you're just copying defaults and saving it (correct me if I'm wrong, never dealt with Bukkits way of making configs).
     
    Last edited: Apr 5, 2015
    mine-care likes this.
  24. Offline

    dlange

     
    mine-care likes this.
  25. Offline

    mine-care

    @NoSpanMan uppon what @CodePlaysMinecraft said, efficiency wise, use nested if instead of a simple if statement,
    because onCommand fires each time a command is executed, the executed command cannot be both "home" and "add"
    you might want to use
    if(homecomamnd){

    } else if (addcommand){

    }
     
    dlange and CodePlaysMinecraft like this.
Thread Status:
Not open for further replies.

Share This Page