OH MY GOD MASSIVE ERROR! (NEW PROBLEM!)

Discussion in 'Plugin Development' started by Jnorr44, Jun 22, 2012.

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

    Firefly

    event.getToBlock().getLocation() ?
     
  2. Offline

    Jnorr44

    I used event.getToBlock().getWorld()

    I don't get any errors anymore, but instead the event doesnt work.
    Code:
    package com.github.JamesNorris.Flow;
     
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.HandlerList;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockFromToEvent;
     
    public class StreamPreventer implements Listener {
        private static final HandlerList handlers = new HandlerList();
     
        private Flow plugin;
        public StreamPreventer(Flow instance) {
            plugin = instance;
            this.setPlugin(plugin);
        }
        public Flow getPlugin() {
            return plugin;
        }
        public void setPlugin(Flow plugin) {
            this.plugin = plugin;
        }
     
        @EventHandler(priority = EventPriority.HIGHEST)
        public void BFTE(BlockFromToEvent event){
            final World world = event.getToBlock().getWorld();
            for (int x = -1; x < 2; x++) {
                for (int y = -1; y < 2; y++) {
                    for (int z = -1; z < 2; z++) {
                        Block stream = world.getBlockAt(x,y,z);
                        if (stream.getTypeId() == 9 || stream.getTypeId() == 11) {
                            if(plugin.getConfig().getBoolean("enableWater")) {
                                if (stream.getTypeId() == 9) {
                                    stream.setType(Material.STATIONARY_WATER);
                                }
                            }
                            if(plugin.getConfig().getBoolean("enableLava")) {
                                if (stream.getTypeId() == 11) {
                                    stream.setType(Material.STATIONARY_LAVA);
                                }
                            }
                        }
                    }
                }
            }
        }
        public HandlerList getHandlers() {
            return handlers;
        }
     
        public static HandlerList getHandlerList() {
            return handlers;
        }
    }
     
  3. Offline

    Firefly

    Define "doesn't work." Is it not being called? Are you getting another Stack Trace? Try adding some traces throughout the code like "player.sendMessage("Trace 1"); to see where in your code it is exiting or going a different path then you expect.
     
  4. Offline

    Jnorr44

    Working on it, Ill tell you if I figure this out.

    Firefly
    It gets up to the third check. I found that I was getting the same blockID twice, and removed that. Before that it wouldn't go past 3. Now it goes past 3 and gets a stack trace (below.
    Code:
    package com.github.JamesNorris.Flow;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.HandlerList;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockFromToEvent;
     
    public class StreamPreventer implements Listener {
        private static Logger log = Logger.getLogger("Minecraft");
        private static final HandlerList handlers = new HandlerList();
     
        private Flow plugin;
        public StreamPreventer(Flow instance) {
            plugin = instance;
            this.setPlugin(plugin);
        }
        public Flow getPlugin() {
            return plugin;
        }
        public void setPlugin(Flow plugin) {
            this.plugin = plugin;
        }
     
        @EventHandler(priority = EventPriority.HIGHEST)
        public void BFTE(BlockFromToEvent event){
            log.warning("Flow check 1 - BFTE is working!");
            final World world = event.getToBlock().getWorld();
            log.warning("Flow check 2 - BFTE gets the world!");
            for (int x = -1; x < 2; x++) {
                for (int y = -1; y < 2; y++) {
                    for (int z = -1; z < 2; z++) {
                        Block stream = world.getBlockAt(x,y,z);
                        log.warning("Flow check 3 - BFTE gets the block at x,y,z!");
                            if(plugin.getConfig().getBoolean("enableWater")) {
                                log.warning("Flow check 4 - BFTE gets the flow config!");
                                if (stream.getTypeId() == 9) {
                                    log.warning("Flow check 5 - BFTE gets the separate IDs!");
                                    stream.setType(Material.STATIONARY_WATER);
                                    log.warning("Flow check 6 - BFTE set the block to the specified type!");
                                }
                            }
                            if(plugin.getConfig().getBoolean("enableLava")) {
                                if (stream.getTypeId() == 11) {
                                    stream.setType(Material.STATIONARY_LAVA);
                                }
                            }
                        }
                    }
                }
            }
        public HandlerList getHandlers() {
            return handlers;
        }
     
        public static HandlerList getHandlerList() {
            return handlers;
        }
    }
    Stacktrace:

    Code:
    2012-06-22 14:17:12 [WARNING] Flow check 1 - BFTE is working!
    2012-06-22 14:17:12 [WARNING] Flow check 2 - BFTE gets the world!
    2012-06-22 14:17:12 [WARNING] Flow check 3 - BFTE gets the block at x,y,z!
    2012-06-22 14:17:12 [SEVERE] Could not pass event BlockFromToEvent to Flow
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:304)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.TimedRegisteredListener.callEvent(TimedRegisteredListener.java:30)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:460)
        at net.minecraft.server.BlockFlowing.a(BlockFlowing.java:144)
        at net.minecraft.server.World.a(World.java:2407)
        at net.minecraft.server.World.doTick(World.java:1755)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:554)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
        at com.github.JamesNorris.Flow.StreamPreventer.BFTE(StreamPreventer.java:40)
        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:302)
        ... 9 more
    line 40 = if(plugin.getConfig().getBoolean("enableWater") == true) {

    so it won't get the config?

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

    Firefly

    Ok. Either you don't have the default config file generated, or the path "enableWater" isn't in the file, or the object located under the path "enableWater" isn't a boolean.
     
  6. Offline

    Jnorr44

    defaults generate in the onEnable:
    Code:
    //-----------------------------
    //FLOW MAIN CLASS
    //FOR: BUKKIT PLUGIN - "FLOW"
    //CREATOR: JAMESNORRIS
    //-----------------------------
     
    package com.github.JamesNorris.Flow;
     
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
     
    import java.io.File;
    import java.util.logging.Logger;
     
    public class Flow extends JavaPlugin implements CommandExecutor, Listener {
        private FixCommand flowFixExecutor;
        private FlowCommand flowExecutor;
        private ConfigCommands configExecutor;
        private File config;
        private static Logger log = Logger.getLogger("Minecraft");
        int configVersion = 0;
        int configVersionNumber = 3; //TODO add 1 every time there's an update to the config!
        String version = "v1.1.2"; //TODO set this every time there's an update! ex. vX.X.X
        @Override
        public void onEnable(){
            getServer().getPluginManager().registerEvents(new StreamPreventer(null), this);
            this.getConfig().set("configVersion", configVersionNumber);
            if(config == null || configVersion != configVersionNumber){
                config = new File(getDataFolder(), "config.yml");
                log.info("Loading config.yml...");
                this.getConfig().set("version", version);
                getConfig().options().copyDefaults(true);
                this.getConfig().addDefault("enabled", true);
                this.getConfig().addDefault("enableLava", true);
                this.getConfig().addDefault("enableWater", true);
                this.getConfig().addDefault("fixBelow", true);
            }   
            if(getConfig().getBoolean("enabled") == false){
                log.warning("Flow has been disabled in the config! Check the config.yml to change this!");
                this.setEnabled(false);
            }
            flowFixExecutor = new FixCommand(this);
            getCommand("flowfix").setExecutor(flowFixExecutor);
            flowExecutor = new FlowCommand(this);
            getCommand("flow").setExecutor(flowExecutor);
            configExecutor = new ConfigCommands(this);
            getCommand("flowset").setExecutor(configExecutor);
        }
        public void onDisable(){
            log.info("Flow has been disabled.");
            this.saveConfig();
        }
    }
    As you can see its also set to a boolean, and enableWater is in there.
     
  7. Offline

    Firefly

    Hmm. Based on the code I see, you probably don't have a config.yml in your Flow folder. You never save the config file, so it's never generated. I think the method is config.save(), which needs to be surrounded by a try/catch.
     
  8. Offline

    Jnorr44

    in the onDisable : this.saveConfig();
    and I have all of the booleans in the config file, and it gets saved
     
  9. Offline

    MucTweezer

    There might be an issue instantiating the StreamPreventer with "null" instead of "this".
     
  10. Offline

    Firefly

    :O Why would you put it there? First time users of your plugin will have to reload twice to use your plugin!

    Also, I read through some of the Javadocs and found saveDefaultConfig() try using that instead.
     
  11. Offline

    Jnorr44

    I ran log messages through each stage of BFTE, the only error is on line 39, which gets the config. Its the same line I use for everything else, and it works there.
     
  12. Offline

    Firefly

    Ahh, didn't notice that. Good catch.
     
  13. Offline

    Jnorr44

    I did what MucTweezer said, and I got to check 4. The same code as above is in StreamPreventer
     
  14. Offline

    Firefly

    So, the stream doesn't have a type id of 9 or 11.
     
  15. Offline

    Jnorr44

    The checks only run when I place a lava or water source, and the stream forms.
     
  16. Offline

    Firefly

    So...? You might want to consider listening to a BucketEmptyEvent if that's the case.
     
  17. Offline

    Jnorr44

    No, I want it so streams turn into sources when they are touching 2 other sources.
     
  18. Offline

    MucTweezer

    I'm thinking that the BlockFromToEvent is only thrown when the stream actually moves from one space to another. If the stream is already there, the event is not thrown. Therefore, this should function when you drop a bucket of water, as the BlockFromToEvent would be thrown as the new water flows.

    However, if you want it to fix nearby streams that already exist, you might consider writing a command that can be input by the player that will run the code fixing water within a certain radius.

    Edit: Wait. I don't think my response had anything to do with what you were saying.
     
  19. Offline

    Jnorr44

    This event will just fix the formation of streams, like bergerkillers' streamremover. It wont do anything to pre-existing streams, I already have commands for that.
     
  20. Offline

    MucTweezer

    The way the code seems to look right now, if it detects flowing water within a BlockFromToEvent, then it will convert that flowing water into stationary water. There doesn't seem to be any checks for whether that flowing water is touching two other sources of water.
     
  21. Offline

    Jnorr44

    It should still move on to getTypeId() though. I don't understand whats going on with it. As for the 2 source part, I think I have the solution
     
  22. Offline

    Firefly

    Well, I think it may be, since he's for-looping the blocks -1 in every direction. It's quite messy though.

    Try logging the typeId it does get at that part in your code.

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

    MucTweezer

    Ah, so the checks run but it doesn't actually function?

    I'm just shooting in the dark here (as I'm not infront of my setup and therefore can't test anything), but the Minecraft Wiki suggests to me that type ID 9 is stationary water (and 11 is stationary lava). So the code would attempt to change stationary water into stationary water; obviously not what you're looking for. Maybe type ID 8 would work?
     
    Firefly likes this.
  24. Offline

    Jnorr44

    I looked at that too, and tried that with the command that is also in my plugin. It uses the same ids 9 and 11, and it works. When I set it to 8 and 10 it no longer works. I think they just screwed that up on the wiki.

    Ok, I got the config, now I just need it to change the blocks, then stop the event... cause its running in a loop in the same spot forever ahaha.

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

    Firefly

    As far as I know, MucTweezer is correct: ID 9 is stationary water. Lemme test this quickly...
     
  26. Offline

    MucTweezer

    So, at this point, the code does not run if you, say, empty a bucket? But instead, it only runs if you place a source water block?

    Again, shooting in the dark, but that suggests to me that this is the wrong event to be doing this in. Is BlockSpreadEvent thrown when water flows?
     
  27. Offline

    Jnorr44

    OK, new code. For some reason it doesnt work, but the code gets completely run through to check 6

    Code:
    package com.github.JamesNorris.Flow;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.block.BlockFace;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.HandlerList;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockFromToEvent;
     
    public class StreamPreventer implements Listener {
        private static Logger log = Logger.getLogger("Minecraft");
        private static final HandlerList handlers = new HandlerList();
     
        private Flow plugin;
        public StreamPreventer(Flow instance) {
            plugin = instance;
            this.setPlugin(plugin);
        }
        public Flow getPlugin() {
            return plugin;
        }
        public void setPlugin(Flow plugin) {
            this.plugin = plugin;
        }
        @EventHandler(priority = EventPriority.HIGHEST)
        public void BFTE(BlockFromToEvent event){
            final World world = event.getToBlock().getWorld();
            for (int x = -1; x < 2; x++) {
                for (int y = -1; y < 2; y++) {
                    for (int z = -1; z < 2; z++) {
                        Block stream = world.getBlockAt(x,y,z);
                        for (BlockFace f:new BlockFace[]{BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST}) {
                            if(plugin.getConfig().getBoolean("enableWater") == true) {
                                if (event.getBlock().getRelative(f, 1).getTypeId() == 9) {
                                    stream.setType(Material.STATIONARY_WATER);
                                    log.warning("Flow check 6 - BFTE sets the block type!");
                                    break;
                                }
                            }
                            if(plugin.getConfig().getBoolean("enableLava") == true) {
                                if (event.getBlock().getRelative(f, 1).getTypeId() == 11) {
                                    stream.setType(Material.STATIONARY_WATER);
                                    log.warning("Flow check 6 - BFTE sets the block type!");
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        public HandlerList getHandlers() {
            return handlers;
        }
     
        public static HandlerList getHandlerList() {
            return handlers;
        }
    }
    Nope, only a BlockFromTo, it says it on the APIdocs
    Also, the correct stream and sources ARE mixed up, I ran the command with 8 and 10, and nothing happened.
    And the code runs if any stream is formed, either from breaking a block, or placing water using a bucket

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

    MucTweezer

    So the checks run but nothing happens?
     
  29. Offline

    Firefly

    I'd suggest changing 9 and 11 to 8 and 10. Also, you can remove the == true here and take advantage of your variable type in "enableWater"

    [EDIT] ignore by 8/10 switch :p
    if(plugin.getConfig().getBoolean("enableWater") == true)
     
  30. Offline

    MucTweezer

    Okay, so, the next thing that I'm looking at is what the stream block is that you're changing.

    Code:
    Block stream = world.getBlockAt(x,y,z);
    
    Where x, y, and z are a matrix between -1 and 1. If this code is being run, you're changing these blocks way down in the bottom corner of your map to stationary water blocks; obviously not what you're looking for. I think this stream block needs to be made relative to the block that you're interested in (if not the very block itself).
     
Thread Status:
Not open for further replies.

Share This Page