Solved WeatherChangeEvent Help

Discussion in 'Plugin Help/Development/Requests' started by iClaw, Apr 2, 2015.

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

    iClaw

    I'm trying to make it so when the weather changes into rain, granite blocks will be changed to diorite blocks. Can anyone help me with this?
    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        public void onWeather(WeatherChangeEvent e){
            World world = Bukkit.getWorld("thornmc");
            for(Chunk chunk : world.getLoadedChunks()) {
                 int aX = chunk.getX() << 4;
                 int aZ = chunk.getZ() << 4;
                 for(int x = aX; x < aX + 16; x++){
                     for(int z = aZ; z < aZ + 16; z++){
                         for(int y = 0; y < 128; y++){
                             Block b = world.getBlockAt(x, y, z);
                             if(b.getType() == Material.STONE){
                                 if(b.getData() == (byte)1){
                                     b.setType(Material.STONE);
                                     b.setData((byte)3);
                                 }
                             }
                         }
                     }
                 }
            }
        }
     
  2. I used this:
    Code:java
    1. for(Chunk c : world.getLoadedChunks()) {
    2. for(BlockState bs : c.getTileEntities()) {
    3. if(bs.getType().equals(Material.COMMAND)) {
    4.  
    5. }
    6. }
    7. }

    And I would suggest you checking if its actually raining because the event fires too if it switches from rain to sun
     
  3. Offline

    iClaw

    hmm. doesn't work for me :/

    @FisheyLP
    Okay i did this:
    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        public void onWeather(WeatherChangeEvent e){
            if(!e.isCancelled()){
                if(e.toWeatherState()){
                    World world = Bukkit.getWorld("thornmc");
                    for(Chunk chunk : world.getLoadedChunks()) {
                        Bukkit.getServer().broadcastMessage("1");
                        int aX = chunk.getX() << 4;
                        int aZ = chunk.getZ() << 4;
                        for(int x = aX; x < aX + 16; x++){
                            for(int z = aZ; z < aZ + 16; z++){
                                for(int y = 0; y < 128; y++){
                                    Block b = world.getBlockAt(x, y, z);
                                        if(b.getType() == Material.STONE){
                                            Bukkit.getServer().broadcastMessage("3");
                                        if(b.getData() == (byte)1){
                                            b.setType(Material.STONE);
                                            b.setData((byte)3);
                                            Bukkit.getServer().broadcastMessage("4. worked");
                                         }
                                     }
                                 }
                             }
                         }
                    }
                }
            }
    it doesn't broadcast the message '3' or '4. worked'.

    This doesn't work either:
    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        public void onWeather(WeatherChangeEvent e){
            if(!e.isCancelled()){
                if(e.toWeatherState()){
                    World world = Bukkit.getWorld("thornmc");
                    for(Chunk chunk : world.getLoadedChunks()) {
                        for(BlockState bs : chunk.getTileEntities()) {
                            Bukkit.broadcastMessage("1");
                            if(bs.getType().equals(Material.STONE)){
                                Bukkit.broadcastMessage("2");
                                if(bs.getData().equals(1)){
                                    Bukkit.broadcastMessage("3");
                                    bs.getBlock().setType(Material.STONE);
                                    bs.getBlock().setData((byte)3);
                                }
                            }
                        }
                    }
                }
            }
        }
    it stops at the message '1'
     
    Last edited by a moderator: Apr 3, 2015
  4. getData() returns the MaterialData. You need to use getData().getData()
     
  5. Offline

    iClaw

    @FisheyLP
    Still not working. Stops at '1'. And should ''for(BlockState bs : chunk.getTileEntities()) {'' be changed to ''for(BlockState bs : chunk.getBlock(int, int, int)) {''?
    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        public void onWeather(WeatherChangeEvent e){
            if(!e.isCancelled()){
                if(e.toWeatherState()){
                    World world = Bukkit.getWorld("thornmc");
                    for(Chunk chunk : world.getLoadedChunks()) {
                        Bukkit.broadcastMessage("1");
                        for(BlockState bs : chunk.getTileEntities()) {
                            Bukkit.broadcastMessage("2");
                            if(bs.getType().equals(Material.STONE)){
                                Bukkit.broadcastMessage("3");
                                if(bs.getData().getData() == (byte)1){
                                    Bukkit.broadcastMessage("4");
                                    bs.getBlock().setType(Material.STONE);
                                    bs.getBlock().setData((byte)3);
                                }
                            }
                        }
                    }
                }
            }
        }
     
  6. The code works for me. Maybe the world is wrong or there are no loaded chunks in the world?
     
  7. Offline

    Zombie_Striker

    Are you both using the same Java/Bukkit Version?
     
  8. Offline

    iClaw

    @Zombie_Striker
    I'm using craftbukkit 1.8

    @FisheyLP
    World is not wrong and it has loaded chunks

    EDIT by Timtower: merged posts
     
    Last edited: Apr 4, 2015
  9. Offline

    timtower Administrator Administrator Moderator

    Moved to Bukkit alternatives
     
  10. Offline

    iClaw

    bump

    ok i was messing around with that and i finally managed to make it work
    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        public void onWeather(WeatherChangeEvent e){
            if(!e.isCancelled()){
                if(e.toWeatherState()){
                    for(Player p : Bukkit.getOnlinePlayers()){
                        World world = p.getWorld();
                         for(Chunk chunk : world.getLoadedChunks()) {
                             int aX = chunk.getX() << 4;
                             int aZ = chunk.getZ() << 4;
                             for(int x = aX; x < aX + 16; x++){
                                 for(int z = aZ; z < aZ + 16; z++){
                                     for(int y = 0; y < 128; y++){
                                         if(chunk.getBlock(x, y, z).getType() == Material.STONE){
                                             if(chunk.getBlock(x, y, z).getData() == (byte)1){
                                                 world.getBlockAt(new Location(world, x, y, z)).setType(Material.STONE);
                                                 world.getBlockAt(new Location(world, x, y, z)).setData((byte) 3);
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                    }
                }
            }
        }
     
    Last edited by a moderator: Apr 5, 2015
Thread Status:
Not open for further replies.

Share This Page