Util *BROKEN* *PLEASE READ* Create a Minigame!

Discussion in 'Resources' started by JPG2000, Nov 11, 2013.

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

    StefanXX

    Seems to be fantastic, I'm gonna try to make my 1v1 plugin work better with this tutorial.
     
  2. Offline

    BeatCycle

    @JPG2000

    Hello, I have a problem when adding events to my plugin;

    is not a problem, it's actually something I'm doing wrong
    can you help me, my event is this:

    Code:java
    1. @EventHandler
    2. public void deathevent(PlayerDeathEvent event){
    3.  
    4. Player p = event.getEntity().getPlayer();
    5.  
    6. String arenaName = null;//how I define this? please<----------
    7.  
    8. Arena arena = ArenaManager.getManager().getArena(arenaName);
    9.  
    10. for (String s: arena.getPlayers()) {
    11. Player u = Bukkit.getPlayer(s);
    12. u.sendMessage(u + " died in the " + arenaName);
    13. }
    14.  
    15. }


    obviously is for nullpointer,
    but as defined in the arena that player?

    please help me


    PD: i don't speak english, but I can translate your reply
     
  3. Offline

    bluegru

    you could try

    Code:java
    1. for(Arena arena: arenaObjects){
    2.  
    3. if(arena.getPlayers().contains(p.getName){
    4.  
    5. arenaName = arena.getName();
    6.  
    7. } else {
    8.  
    9. arenaName = null;
    10.  
    11. }
    12.  
    13. }
     
  4. Offline

    AdamQpzm

    bluegru Sans the else statement - there's no reason to set it to null if it isn't the right arena, and doing so will cause problems if the player isn't in the last arena checked.
     
  5. Offline

    bluegru

    yeah, you're right

    Code:java
    1. String arenaName;
    2.  
    3. for(Arena arena: arenaObjects){
    4.  
    5. if(arena.getPlayers().contains(p.getName){
    6.  
    7. arenaName = arena.getName();
    8.  
    9. }
    10.  
    11. }
    12.  
    13. if(arenaName == null){
    14.  
    15. //Do something like send the player an errormessage
    16. return;
    17.  
    18. }


    and btw

    Code:java
    1.  
    2.  
    3. u.sendMessage(u + " died in the " + arenaName);
    4.  
    5.  


    is not what I think you want,
    better would be

    Code:java
    1.  
    2.  
    3. u.sendMessage(p.getName() + " died in the " + arenaName);
    4.  
    5.  


    !(Nothing tested)!
     
  6. Offline

    BeatCycle


    thank you very much for your help really [cake]

    I manipulate a bit that method and got what I needed, was this:

    Code:java
    1. for(Arena arena: Arena.arenaObjects){//all arenas
    2.  
    3.  
    4. arenaName = arena.getName();//arena names
    5.  
    6. Arena arenas = ArenaManager.getManager().getArena(arenaName);//get arena
    7.  
    8. if(arenas.getPlayers().contains(p.getName())){//player in arena
    9.  
    10. ArenaManager.getManager().removePlayer(p, arenaName);
    11. p.sendMessage( p.getName() + " died in " + arenaName);
    12.  
    13. }
    14.  
    15. if(arenaName == null){//arena don't exist
    16.  
    17. p.sendMessage("incorrect arena");
    18. return;
    19.  
    20. }
    21.  
    22.  
    23.  
    24. }

    I do not speak English, sorry for my spelling :(
     
  7. Offline

    bluegru

    you don't need to get the arena if you loop through every arena.

    so just
    Code:java
    1. for(Arena arena: Arena.arenaObjects){ //all arenas
    2. arenaName = arena.getName(); //get arenaname
    3.  
    4. if(arena.getPlayers().contains(p.getName())){ //if the player is in the arena
    5. ArenaManager.getManager().removePlayer(p, arenaName); //remove the player from the arena
    6. p.sendMessage( p.getName() + " died in " + arenaName); //that would just tell the player that he died
    7. }
    8. if(arenaName == null){ //if arena don't exist
    9. p.sendMessage("incorrect arena");
    10. return;
    11. }
    12. }
     
  8. Offline

    xTrollxDudex

    O(n) replacement for O(1)? I don't think so.
     
  9. Offline

    bluegru

    what do you maen?
     
  10. Offline

    bars96

    How to add players and start arena from another plugin?
     
  11. Offline

    Gamesareme

    @xTrollxDudex I saw this tutorial, and thought that it would be good to try and get it to work, so I could learn somthing from it. I am having a problem though. (Like every one else haha)
    I am getting the following error, every time I try and call on the "ArenaManager". So this is my code.

    Code:
    if(commandLabel.equalsIgnoreCase("test")){
                Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Working...");
                //Location loc = new Location(Bukkit.getWorld("world"), 0, 0, 0);
                ArenaManager.getManager().getArena(args[0]);
                Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Finished!");
            }
    And I get the following error.
    Code:
    [18:30:18 WARN]: Unexpected exception while parsing console command "test"
    org.bukkit.command.CommandException: Unhandled exception executing command 'test' in plugin PXSuperPB v0.0.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:703) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchServerCommand(CraftServer.java:690) [minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.aB(DedicatedServer.java:296) [minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:261) [minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    Caused by: java.lang.ExceptionInInitializerError
        at me.joxboyz.PXSuperPB.Commands.Commands.onCommand(Commands.java:25) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        ... 8 more
    Caused by: java.lang.IllegalArgumentException: Plugin already initialized!
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:98) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:59) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at me.joxboyz.PXSuperPB.Main.Main.<init>(Main.java:13) ~[?:?]
        at me.joxboyz.PXSuperPB.ArenaManager.ArenaManager.<clinit>(ArenaManager.java:20) ~[?:?]
        at me.joxboyz.PXSuperPB.Commands.Commands.onCommand(Commands.java:25) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        ... 8 more
    Caused by: java.lang.IllegalStateException: Initial initialization
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:101) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:59) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at me.joxboyz.PXSuperPB.Main.Main.<init>(Main.java:13) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_05]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_05]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_05]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:408) ~[?:1.8.0_05]
        at java.lang.Class.newInstance(Class.java:433) ~[?:1.8.0_05]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:52) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:127) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:328) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugins(CraftServer.java:357) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.<init>(CraftServer.java:319) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.PlayerList.<init>(PlayerList.java:68) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.DedicatedPlayerList.<init>(SourceFile:14) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.init(DedicatedServer.java:126) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:436) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        ... 1 more
    I have tried to read this my self, and I believe that it may be saying that I am not Instantiating the code.
    But then a little further down the error, it says that the plugin is already initialized. Can you see something in the stack trace that I have not seen?
     
    ChipDev likes this.
  12. Offline

    mythbusterma

    @Gamesareme

    ....you're creating a new instance of your Main class each time you call ArenaManager, don't do that.
     
  13. Offline

    Gamesareme

    @mythbusterma When am I doing that?
    Hang on, I think I found something. I am going to look into it.
     
    Last edited: Dec 15, 2014
  14. Offline

    nverdier

    @Gamesareme

    Instead of doing
    Code:
    Main main = new Main();
    do
    Code:
    Main main;
    
    public <classname>(Main main){
    this.main = main;
    }
     
    Last edited by a moderator: Dec 15, 2014
  15. Offline

    Gamesareme

    @nverdier How would I then call that? That code conflicts with other things in the ArenaManager.
    For example there is this code in it.
    Code:
    public class ArenaManager {
    
        private static ArenaManager am = new ArenaManager();
    I believe that you would then change that to.
    Code:
    public class ArenaManager {
    
        private static ArenaManager am = new ArenaManager(null);
    Then in my main class I believe you have this.
    Code:
    public final ArenaManager am = new ArenaManager(this);
    I tried this, but it throws a null pointer error.

    @mythbusterma I am really upset with my self, that I did not pick that up. In a moment when I was not thinking, I had made another Main class object.

    @nverdier I have found this code, but is it the best way to go about this?
    Code:
        private static Main main = (Main)Bukkit.getPluginManager().getPlugin("PXSuperPB");
    
     
    Last edited by a moderator: May 3, 2015
  16. Offline

    mythbusterma

    @Gamesareme

    I would seriously recommend you learn Java. It will help you a lot more than asking a question each time a line doesn't work here.

    The Java™ Tutorials
     
  17. Offline

    Gamesareme

    @mythbusterma Come on, there are so many threads that I see you tell people to go learn Java. I am defiantly not a perfect coder, but I have a really strong grasp on coding. If you new how many problems that I have worked through my self, you would know that I do not ask for help on every line that I get stuck on. The problem that I was asking about above was because of my experimenting and learning, and then forgetting to clean up after finishing.

    Also is it wrong getting the opinion of other coders on what you have made?
     
    ChipDev likes this.
  18. Offline

    mythbusterma

    @Gamesareme

    There is a difference between experimentation and recklessness, and what you have shown is blind disregard to that ideal. Yes, there are a lot of threads on this forum telling people to go learn Java, do you know why that is? It is a prerequisite to being able to write plugins, and not knowing when to and when not use the static keyword is a prime example of why this needs to be done. Statically invoking a constructor of a class at its initialization is not how you build a singleton, and there is no reason to do so, other than sloppy code. The idea is you dynamically (as opposed to statically) assign a static reference to the only instance of the class in a constructor, or at the first call to getInstance(), depending on the method used.

    As for the code snippet you posted:
    Code:
      private static Main main = (Main)Bukkit.getPluginManager().getPlugin("PXSuperPB");
    That is just garbage, do you even understand what you're doing there? In what context would that make sense? I would even be willing to bet it wouldn't work at all, due to the fact that your plugin isn't loaded at clinit time of your Main class.

    There is nothing inherently wrong with experimenting, however there are serious issues with blind recklessness with regards to your code. Being able to understand what you write is not only imperative, it surely preempts any attempts at experimentation.
     
  19. Offline

    nverdier

    Well I really don't want to spoonfeed you so I'm just going to link you.

    Then you can just use the instance like
    Code:
     main.getPluginManager() blah blah blah
     
  20. Offline

    Gamesareme

    @mythbusterma From what you said, that code snippet I posted should not make sense. Well, I am not sure if I should, but it makes sense to me, AND it also works in the code. However, I am going to rewrite it so that I us @nverdier idea. Just to let you know, I have already thought about that, which was why I was asking about that code snippet I had above. Damn, I have been coding in servile languages for over 2 years. The mane code I use however is not Java, which is why every now and then I need a little help. The fist post I did on this page, I had my main class been instantiated twice, that was just a bad mistake. I now under stand why it did not work, and also how to prevent it in the future. So I am learning Java, at a rate lot faster than most.

    Finally, I want to thank you both for your help, and I know that I will not be repeating this mistake again.
     
  21. Offline

    mythbusterma

    @Gamesareme

    If that is indeed the case, then you should seriously consider reading the entire Java tutorail. Java is much more in depth and complicated than PHP or JS and you can't just assume you'll get the hang of it, as has been shown already. This is how you create a Singleton:

    Code:
    public class Main {
        private static Main instance;
      
        public Main () {
            instance = this;
        }
        public static Main getInstance() {
             return instance;
        }
    }
       
    Is this really so difficult as to need a rewrite?

    Also, using singletons in plugins generally isn't the way you want to go about things, as it kind of violates the principles of OOP.
     
  22. Offline

    ProStriker123

    @JPG2000, dude i tryed to use your arena manager and from my opinion its wonderfull but if could you help me its not working for me? after reload its says "could not find arena you need for" its dosent takes it from the configuration.
     
  23. Offline

    Xtrololologamer

    How can i do to detect the player arena??
     
  24. Offline

    ChipDev

    I really think that is a boring way to learn it,
    link someone to a tutorial. Its hard.
     
  25. Offline

    mythbusterma

    @ChipDev

    This isn't about having fun. It's about writing code. Get over the boringness if you want to program.
     
  26. Offline

    ChipDev

    Programming is fun.
    If you can do it correctly; If you link someone to a java tutorials, that is the most boring way to do it. You can give him some code, Teach him how it works, not be lazy and link him to some random tutorial. I mean; Like, that didn't help me at all when you did it to me about 5 months ago. I went and learned by myself not with the java website, Maybe, You can be a little happier when saying that and not be 'go do this and that and learn and take..'..
     
  27. Offline

    mythbusterma

    @ChipDev

    Programming is fun if you find it fun, I suppose. Yes, Java tutorials are boring, and if you look at the code above, he needs a lot more help than we can give him here.

    Perhaps it didn't help because you didn't try.
     
  28. Offline

    ChipDev

    Didn't try.
    #slakers
     
  29. Offline

    lightlord323

    I've tried this, however, the loadArenas() method doesnt seem to work, it doesnt grab the arenas from the config for some reason...
     
  30. Offline

    DogeDebugger

    I don't see how loadArenas() does anything?
     
Thread Status:
Not open for further replies.

Share This Page