How to hook into multiple worlds?

Discussion in 'Plugin Development' started by Smex, Oct 8, 2011.

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

    Smex

    I've got a little bit of code:

    Code:
    public void onEnable() {
    
            //Configuration
            config = getConfiguration();
            config.setHeader("#If AEPonStart is false, the plugin has to be actiavted first!\n#If List is false, every block is not pickable!");
            aepacti = config.getBoolean("AEPonStart", false);
            worlds = config.getString("Act-on-Worlds", worlds);
            Listoption = config.getBoolean("List", false);
            intList= config.getIntList("NotPickable", null);
            notPick = new HashSet<Integer>(intList);
            config.save();
    
            System.out.println("Anti-EnderPick v1.2 is enabled!");
            if(Listoption==true){
            System.out.println("NotPickable" + intList);
            }
    
            System.out.println(worlds);
            //Ender Block
            List<World> worver = this.getServer().getWorlds();
            System.out.println(worver);
            pm = this.getServer().getPluginManager();
    
                    Listener enderbock = new enderbock(this);
                    pm.registerEvent(Event.Type.ENDERMAN_PICKUP, enderbock, Event.Priority.Normal, this);
        }
    You see that I get the worlds, where the listener/event should work, from the config.yml("worlds"),
    that works perfectly but how would I handle it so that the event get's only registred on the worlds
    im getting from the string "worlds"?

    EDIT:
    I think I figured it out, with
    Code:
    pm = ((Server) this.getServer().getWorld(worlds)).getPluginManager();[/S][/S]
    
    [S][S]                Listener enderbock = new enderbock(this);[/S][/S]
    [S][S]                pm.registerEvent(Event.Type.ENDERMAN_PICKUP, enderbock, Event.Priority.Normal, this);
    Ok this code makes an nullpointerexeption even if in worlds is something.

    //bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  2. Offline

    vaiquero

    Post the whole class and the stack trace from the exception please. (Just incase.)
     
    Smex likes this.
  3. Offline

    Smex

    The whole source code:
    PHP:
    package xems.antienderpick;
     
    import java.util.HashSet;
    import java.util.List;

    import org.bukkit.Server;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Event;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.config.Configuration;
    import xems.antienderpick.listeners.enderbock;

    public class 
    antienderpick extends JavaPlugin{
        public 
    Configuration config;
        public static 
    boolean aepacti;  // I changed this *** Note
        
    public static String worlds;
        
    PluginManager pm;
        public 
    String i;
        public static List<
    IntegerintList;
        public static 
    HashSet<IntegernotPick;
        public static 
    boolean Listoption;

        
    //Command
        
    public boolean onCommand(CommandSender senderCommand cmdString commandLabelString[] args) {

            if(
    cmd.getName().equalsIgnoreCase("antienderpick") || cmd.getName().equalsIgnoreCase("aep")) {

                if(
    args.length>0){

                if(
    args[0].equals("acti") || args[0].equals("on")) {
                    if (
    sender.hasPermission("antienderpick.acti")){
                    
    sender.sendMessage("Anti-EnderPick activated!");
                    
    aepacti true;
                    }else{
                            
    sender.sendMessage("Wannabe admin?");

                    }
            }else if(
    args[0].equals("deacti") || args[0].equals("off")) {
                if (
    sender.hasPermission("antienderpick.acti")){
                
    sender.sendMessage("Anti-EnderPick deactivated!");
                
    aepacti false;
                return 
    true;
                }else{
                            
    sender.sendMessage("Wannabe admin?");
                }
            }else if(
    args[0].equals("lon")){
                if (
    sender.hasPermission("antienderpick.acti")){
                    
    sender.sendMessage("Anti-EnderPick List activated!");
                    
    Listoption true;
                    return 
    true;
                    }else{
                                
    sender.sendMessage("Wannabe admin?");
                    }
            }else if(
    args[0].equals("loff")){
                if (
    sender.hasPermission("antienderpick.acti")){
                    
    sender.sendMessage("Anti-EnderPick List deactivated!");
                    
    Listoption false;
                    return 
    true;
                    }else{
                                
    sender.sendMessage("Wannabe admin?");
                    }
            }
            }else{
                
    sender.sendMessage("Use: /aep on, /aep off, /aep lon, /aep loff");
            }
            }
    //End of Command
            
    return false;
        }

        
    //Enable-Disable stuff
        
    @Override
        
    public void onDisable() {
            
    System.out.println("Anti-EnderPick v1.2 is disabled!");

        }
        @
    Override
        
    public void onEnable() {

            
    //Configuration
            
    config getConfiguration();
            
    config.setHeader("#If AEPonStart is false, the plugin has to be actiavted first!\n#If List is false, every block is not pickable!");
            
    aepacti config.getBoolean("AEPonStart"true);
            
    Listoption config.getBoolean("List"false);
            
    intList config.getIntList("Notpickable"intList);
            
    worlds config.getString("Act-on-Worlds"worlds);
            
    notPick = new HashSet<Integer>(intList);
            
    config.save();

            
    System.out.println("Anti-EnderPick v1.2 is enabled!");
            if(
    Listoption==true){
            
    System.out.println("Not-Pickable-Blocks" intList);
            }

            
    System.out.println(worlds);
            
    //Ender Block
            
    List<Worldworver this.getServer().getWorlds();
            
    System.out.println(worver);
            
    pm = ((Serverthis.getServer().getWorld(worlds)).getPluginManager();

                    
    Listener enderbock = new enderbock(this);
                    
    pm.registerEvent(Event.Type.ENDERMAN_PICKUPenderbockEvent.Priority.Normalthis);

            return;
        }

    }
     
  4. Offline

    vaiquero

    Please also provide the configuration and "error log".
     
    Smex likes this.
  5. Offline

    Smex

    console
    Code:
    ▓▓▓▓▓▓▓▓▓▓▓START SERVER!▓▓▓▓▓▓▓▓▓▓▓
    
    161 recipes
    17 achievements
    18:06:48 [INFO] Starting minecraft server version Beta 1.8.1
    18:06:48 [INFO] Loading properties
    18:06:48 [INFO] Starting Minecraft server on *:25565
    18:06:48 [INFO] This server is running Craftbukkit version git-Bukkit-0.0.0-1112
    -g54a13b2-b1240jnks (MC: 1.8.1)
    18:06:48 [INFO] Preparing level "world"
    18:06:48 [INFO] Default game type: 0
    18:06:48 [INFO] Preparing start region for level 0 (Seed: -5348891288454661354)
    18:06:49 [INFO] Preparing start region for level 1 (Seed: -5348891288454661354)
    18:06:49 [INFO] Preparing spawn area: 12%
    18:06:50 [INFO] Anti-EnderPick v1.2 is enabled!
    18:06:50 [INFO] [world, world_nether]
    18:06:50 [INFO] [CraftWorld{name=world}, CraftWorld{name=world_nether}]
    18:06:50 [SCHWERWIEGEND] Error occurred while enabling antienderpick v1.2 (Is it
     up to date?): null
    java.lang.NullPointerException
            at xems.antienderpick.antienderpick.onEnable(antienderpick.java:103)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:126)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:941)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:280)
            at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:174)
            at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:157
    )
            at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:297)
            at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:284)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:152)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:348)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)
    18:06:50 [INFO] CommandBook 1.5.2-customized enabled.
    18:06:50 [INFO] CommandBook: Maximum wrapper compatibility is enabled. Some feat
    ures have been disabled to be compatible with poorly written server wrappers.
    18:06:50 [INFO] CommandBook: 0 banned name(s) loaded.
    18:06:50 [INFO] CommandBook: 1 kit(s) loaded.
    18:06:50 [INFO] CommandBook: No known permissions plugin detected. Using configu
    ration file for permissions.
    18:06:50 [INFO] WorldEdit 4.6 enabled.
    18:06:50 [INFO] WorldEdit: No known permissions plugin detected. Using configura
    tion file for permissions.
    18:06:50 [INFO] Server permissions file permissions.yml is empty, ignoring it
    18:06:50 [INFO] Done (0.182s)! For help, type "help" or "?"
    >
    
    config.yml
    Code:
    #If AEPonStart is false, the plugin has to be actiavted first!
    #If List is false, every block is not pickable!
    AEPonStart: true
    Act-on-Worlds:
    - world
    - world_nether
    List: false
    
    plugin.yml
    Code:
    name: antienderpick
    author: XemsDoom
    main: xems.antienderpick.antienderpick
    version: 1.2
    permissions:
        antienderpick.acti:
            description: Activates the Block
            default: OP
    commands:
        antienderpick:
            usage:
        aep:
            usage:


    The line 103 in the source code is that one:
    pm = ((Server) this.getServer().getWorld(worlds)).getPluginManager();
     
  6. Offline

    vaiquero

    Code:
    package xems.antienderpick;
    
    import java.util.HashSet;
    
    import java.util.List;
    
    import org.bukkit.Server;
    
    import org.bukkit.World;
    
    import org.bukkit.command.Command;
    
    import org.bukkit.command.CommandSender;
    
    import org.bukkit.event.Event;
    
    import org.bukkit.event.Listener;
    
    import org.bukkit.plugin.PluginManager;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    import org.bukkit.util.config.Configuration;
    
    import xems.antienderpick.listeners.enderbock;
    
    public class antienderpick extends JavaPlugin {
    
    public Configuration config;
    
    public static boolean aepacti; // I changed this *** Note
    
    public static String worlds;
    
    PluginManager pm;
    
    public String i;
    
    public static List<Integer> intList;
    
    public static HashSet<Integer> notPick;
    
    public static boolean Listoption;
    
    //Command
    
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    
    if (cmd.getName().equalsIgnoreCase("antienderpick") || cmd.getName().equalsIgnoreCase("aep")) {
    
    if (args.length > 0) {
    
    if (args[0].equals("acti") || args[0].equals("on")) {
    
    if (sender.hasPermission("antienderpick.acti")) {
    
    sender.sendMessage("Anti-EnderPick activated!");
    
    aepacti = true;
    
    } else {
    
    sender.sendMessage("Wannabe admin?");
    
    }
    
    } else if (args[0].equals("deacti") || args[0].equals("off")) {
    
    if (sender.hasPermission("antienderpick.acti")) {
    
    sender.sendMessage("Anti-EnderPick deactivated!");
    
    aepacti = false;
    
    return true;
    
    } else {
    
    sender.sendMessage("Wannabe admin?");
    
    }
    
    } else if (args[0].equals("lon")) {
    
    if (sender.hasPermission("antienderpick.acti")) {
    
    sender.sendMessage("Anti-EnderPick List activated!");
    
    Listoption = true;
    
    return true;
    
    } else {
    
    sender.sendMessage("Wannabe admin?");
    
    }
    
    } else if (args[0].equals("loff")) {
    
    if (sender.hasPermission("antienderpick.acti")) {
    
    sender.sendMessage("Anti-EnderPick List deactivated!");
    
    Listoption = false;
    
    return true;
    
    } else {
    
    sender.sendMessage("Wannabe admin?");
    
    }
    
    }
    
    } else {
    
    sender.sendMessage("Use: /aep on, /aep off, /aep lon, /aep loff");
    
    }
    
    }//End of Command
    
    return false;
    
    }
    
    //Enable-Disable stuff
    
    @Override
    
    public void onDisable() {
    
    System.out.println("Anti-EnderPick v1.2 is disabled!");
    
    }
    
    @Override
    
    public void onEnable() {
    
    //Configuration
    
    config = getConfiguration();
    
    config.setHeader("#If AEPonStart is false, the plugin has to be activated first!\n#If List is false, every block is not pickable!");
    
    aepacti = config.getBoolean("AEPonStart", true);
    
    Listoption = config.getBoolean("List", false);
    
    intList = config.getIntList("Notpickable", intList);
    
    worlds = config.getString("Act-on-Worlds", worlds);
    
    notPick = new HashSet<Integer>(intList);
    
    config.save();
    
    System.out.println("Anti-EnderPick v1.2 is enabled!");
    
    if (Listoption == true) {
    
    System.out.println("Not-Pickable-Blocks" + intList);
    
    }
    
    for (String worldNameThing : worlds.split(", ")) {
    
    if (this.getServer().getWorld(worldNameThing) == null) {
    
    System.out.println("Invalid world specified in configuration: " + worldNameThing);
    
    }
    
    }
    
    Listener enderbock = new enderbock(this);
    
    pm.registerEvent(Event.Type.ENDERMAN_PICKUP, enderbock, Event.Priority.Normal, this);
    
    }
    
    }
    
    
    Alright then, I figured it out.

    New config:

    Code:
    AEPonStart: true
    Act-on-Worlds: world, world_nether
    List: false
    To do the rest of the multiworld functional code, give me your enderbock class.
     
    Smex likes this.
  7. Offline

    Smex

    Code:
    package xems.antienderpick.listeners;
    
    import org.bukkit.event.entity.EndermanPickupEvent;
    import org.bukkit.event.entity.EntityListener;
    
    import xems.antienderpick.antienderpick;
    
    public class enderbock extends EntityListener {
    
        antienderpick plugin;
    
        public enderbock(antienderpick plugin){
            this.plugin = plugin;
        }
       
        public void onEndermanPickup(EndermanPickupEvent u){
            int block = u.getBlock().getTypeId();
    
            //if aepacti is false the Plugin is "deactivated"
    
        if(antienderpick.aepacti==true){
            if(antienderpick.Listoption==false){
                u.setCancelled(true);
            }else{
            if(antienderpick.notPick.contains(block)){
                u.setCancelled(true);
                }
            }
        }
        }
    
    }
     
  8. Offline

    vaiquero

    Code:
    package xems.antienderpick.listeners;  import org.bukkit.event.entity.EndermanPickupEvent; import org.bukkit.event.entity.EntityListener;  import xems.antienderpick.antienderpick;  public class enderbock extends EntityListener {
    
            antienderpick plugin;
    
            public enderbock(antienderpick plugin) {
                this.plugin = plugin;
            }
    
            public void onEndermanPickup(EndermanPickupEvent u) {
                boolean doAction = false;
                for (String worldName : plugin.worlds.split(", ")) {
                    if (u.getBlock().getWorld().getName().equalsIgnoreCase(worldName)) {
                        doAction = true;
                    }
                }
                if (doAction) {
                    int block = u.getBlock().getTypeId();
    
                    //if aepacti is false the Plugin is "deactivated"
    
                    if (antienderpick.aepacti == true) {
                        if (antienderpick.Listoption == false) {
                            u.setCancelled(true);
                        } else {
                            if (antienderpick.notPick.contains(block)) {
                                u.setCancelled(true);
                            }
                        }
                    }
                } else {
                    u.setCancelled(true);
                }
            }
        }
    There you go..
     
    Smex likes this.
  9. Offline

    Smex

    Thank you very much!
    You just teached me how it works, really many thanks!
    nice persone. :)

    Ah, do you know why it doenst create the "Nopickable" thing
    into the config.yml?
     
  10. Offline

    vaiquero

    getIntList was not made to actually create itself if its not there.
    Use the setProperty method in getConfiguration in order to make one if its not there.
     
  11. Offline

    Smex

    oh ok thanks.
     
Thread Status:
Not open for further replies.

Share This Page