Solved Hashmap as global variable (static?)

Discussion in 'Spigot Plugin Development' started by ORENYT, May 31, 2020.

  1. mean like this
    Code (Java):
      public class Handler(Map<Player, Player> map, Map<Player, ItemStack[]> inventory) implements Listener {
    i
     
  2. Uhh that looks so ugly. In all honesty, that is completely wrong. I agree, with that current knowledge you will have a very hard time.
     
    • Agree Agree x 1
  3. so it is impossible :D, okay, nevermind, i will use your construction, thank you, commended.
     
  4. i will test your concstruction in game now and mark as soluted if everything working right
     
  5. lol, i just realized that i used same construction all the way upload_2020-6-4_0-59-15.png
     
  6. Plugin is not working
     
  7. Code (Java):
    package deathgame;

    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Random;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.GameMode;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.craftbukkit.libs.jline.internal.Log;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;

    import deathgame.Main;

    public class DGvizov implements CommandExecutor
    {
         Main pl;
         private final Map<Player, Inventory> InventoryMap;
         private final Map<Player, Player> FightersMap;
         public DGvizov(final Main pl, Map<Player, Player> map, Map<Player, Inventory> inventory) {
              this.FightersMap = map;
              this.InventoryMap = inventory;
         }
           public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
             
                if (!(sender instanceof Player)) {
                    sender.sendMessage("§8[§c§lNE§f§lKO§8]§fКоманду: §c/dgvizov §fможет §Aиспользовать§F только игрок");
                    return true;
                }
                pl.LoadConfig();
                FileConfiguration config = pl.getConfig();
                String str = sender.getName();
                if (config.getString(str+".abyl")==null) {
                    sender.sendMessage("§8[§c§lNE§f§lKO§8] §fВы не в игре! Откуда вы про неё узнали?:0");
                    return true;
                }
               
                if (args.length != 1) {
                    sender.sendMessage("§8[§c§lNE§f§lKO§8] §fИспользование: §c/dgstart <nickname>");
                    return true;
                }
               
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < args.length; i++){
                sb.append(args[i]).append(" ");
                }
             
                String name = sb.toString().trim();
               
                Player pl2 = Bukkit.getPlayer(name);
                Player pl1 = (Player) sender;
               
                if (Math.abs(pl1.getLocation().getBlockX()-pl2.getLocation().getBlockX())>10) {
                    sender.sendMessage("§8[§c§lNE§f§lKO§8] §fПодойдите вплотную к вашему противнику");
                    return true;
                }
               
                if (Math.abs(pl1.getLocation().getBlockY()-pl2.getLocation().getBlockY())>10) {
                    sender.sendMessage("§8[§c§lNE§f§lKO§8] §fПодойдите вплотную к вашему противнику");
                    return true;
                }
               
                if (Math.abs(pl1.getLocation().getBlockZ()-pl2.getLocation().getBlockZ())>10) {
                    sender.sendMessage("§8[§c§lNE§f§lKO§8] §fПодойдите вплотную к вашему противнику");
                    return true;
                }

                pl1.setFlying(false);
                pl1.setInvulnerable(false);
                pl1.setGameMode(GameMode.ADVENTURE);
                InventoryMap.put(pl1, pl1.getInventory());
               
                pl2.setFlying(false);
                pl2.setInvulnerable(false);
                pl2.setGameMode(GameMode.ADVENTURE);
                InventoryMap.put(pl2, pl2.getInventory());
                       
                FightersMap.put(pl1,pl2);
                FightersMap.put(pl2,pl1);
               
                pl1.getInventory().clear();
                pl2.getInventory().clear();
                return true;
           }
    }
     
  8. Code (Java):
    package deathgame;

    import org.bukkit.plugin.java.*;


    import org.bukkit.plugin.*;

    import java.io.File;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.Bukkit;
    import org.bukkit.command.*;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;

    public class Main extends JavaPlugin
    {
        private final Map<Player, Player> Fighters = new HashMap<>();
       
        private final Map<Player, Inventory> Inventory = new HashMap<>();
       
        public void onEnable() {
           
            new Handler(Fighters,Inventory);
           
            CommandRegister.reg((Plugin)this, (CommandExecutor)new DGstart(this), new String[]{"test"}, "ci", "/ci");
           
            CommandRegister.reg((Plugin)this, (CommandExecutor)new DGstart(this), new String[]{"dgstart"}, "ci", "/ci");
           
            CommandRegister.reg((Plugin)this, (CommandExecutor)new DGvizov(this, Fighters, Inventory), new String[]{"dgvizov"}, "ci", "/ci");
           
            CommandRegister.reg((Plugin)this, (CommandExecutor)new DGinfo(this), new String[]{"dginfo"}, "ci", "/ci");

            LoadConfig();
            }
        public void LoadConfig() {
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
    }


     
     
  9. unfortunally, i have an eternal error without error code
     
  10. Stop bumping this thread, it's has too many messages anyway.
    You could easily solve the problem by learning Java.
     
    • Agree Agree x 1
  11. i want to solve it for future users, when all will work right i will stop
     
  12. And it is. So SOLUTION is here
     
  13. and fixed my plugin this.pl = pl; forgot, now everything is working right
     
  14. Strahan

    Benefactor

    Holy Christ, seven posts in a row? I wonder what the Spigot record is for continually posting in a chain heh.

    You do realize there is an EDIT option, right? You can just edit the last post and add to it. MUCH MUCH better as it doesn't pepper other posters with notifications. Also, if you are going to post and say "plugin isn't working" and post code, call me crazy here, but maybe consider posting the error's stack trace? While with a lot of newb code it is easy to spot where the plugin will shit itself, posting the stack trace would save a little time.

    Glad to hear you got it working though :)
     
    • Funny Funny x 1
  15. This wouldn't reduce the amount of code you write, and it's broken as it doesn't implement hashCode and equals.
    That doesn't matter. The only thing that matters is that it implements a stable hashCode and equals method (which it does). They rely on the entity id and UUID, which will never change as long as the player is online.
    Multiple layers don't really matter. If you're using a singleton because you don't like the constructor chains, either consider an IoC container framework or rethink your dependency structure :)
    you're not storing it, Bukkit is. All you're storing is a reference to the player (which is likely around 8 bytes), and is the same thing you store when using UUIDs
     
    • Like Like x 1