[Urgent] Chest refill not working

Discussion in 'Plugin Development' started by 2016mfransen, Jun 24, 2014.

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

    2016mfransen

    I'm working on a chest refill plugin that generates chest then stores their location in a map but when I try to refill them It doesn't work, when I check the type (block.getType()==Material.chest) it returns true but when I check if the block.getState() instanceof Chest it returns false. Any help?
    Here is my task:
    Code:java
    1. package net.xstonegames.chestpopulator;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5. import java.util.Random;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.Material;
    10. import org.bukkit.World;
    11. import org.bukkit.block.Block;
    12. import org.bukkit.block.Chest;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. import org.bukkit.scheduler.BukkitRunnable;
    15. import org.bukkit.util.Vector;
    16.  
    17. public class ChestPopulatorTask extends BukkitRunnable {
    18. Random rand;
    19. World world;
    20. public ChestPopulatorTask(World world, Random rand)
    21. {
    22. this.rand = rand;
    23. this.world = world;
    24. }
    25. @Override
    26. public void run() {
    27. if(JavaPlugin.getPlugin(ChestPopulatorPlugin.class).chestLocations.get(world.getName())==null)
    28. {
    29. Bukkit.getScheduler().cancelTask(this.getTaskId());
    30. return;
    31. }else
    32. {
    33. List<Vector> vectors = new ArrayList<Vector>(JavaPlugin.getPlugin(ChestPopulatorPlugin.class).chestLocations.get(world.getName()));
    34. for(Vector v : vectors)
    35. {
    36. Vector vector = v.clone();
    37. Block b = world.getBlockAt(vector.getBlockX(),vector.getBlockY(),vector.getBlockZ());
    38. if(b.getType() == Material.CHEST)
    39. {
    40. JavaPlugin.getPlugin(ChestPopulatorPlugin.class).populateChest((Chest) b.getState(), rand);
    41. Bukkit.broadcastMessage(ChatColor.GREEN+"[chest refilled at "+v.getBlockX()+","+v.getBlockY()+","+v.getBlockZ());
    42. }else
    43. {
    44. Bukkit.broadcastMessage(ChatColor.RED+b.getState().getClass().getSimpleName());
    45. }
    46. }
    47. }
    48.  
    49. }
    50.  
    51. }
    52.  
     
  2. Offline

    AlexHH251997

    Hey,

    Looking over you code I found a much easier way to do this, admitting with my way you have to have all chunks preloaded by it still works:

    Code:java
    1. public void fillAllChests(String s){
    2. for(Chunk chunk : plugin.getServer().getWorld(s).getLoadedChunks()){
    3. for(BlockState entities : chunk.getTileEntities()){
    4. if(entities instanceof Chest){
    5. Inventory inv = ((Chest) entities).getInventory();
    6. fillChests(inv);
    7. }
    8. }
    9. }
    10. }

    Code:java
    1. public void fillChests(Inventory inv){
    2. inv.clear();
    3. Random itemnum = new Random();
    4. int items = 3+itemnum.nextInt(getItems(inv));
    5. for(int i = 1; i < items+1; i++){
    6. Random slotnum = new Random();
    7. Random itemrand = new Random();
    8. int item = 1+itemrand.nextInt(55);
    9. int slot = slotnum.nextInt(inv.getSize());
    10. if(item == 1 || item == 19 || item == 52){
    11. inv.setItem(slot, new ItemStack(Material.ENDER_PEARL, 1));
    12. }else if(item == 2 || item == 19){
    13. inv.setItem(slot, new ItemStack(Material.FEATHER, 2));
    14. }
    15. }
    16. }

    Example Usage:
    Code:java
    1. @Override
    2. public void run() {
    3. String worldName = ...;
    4. World world = ...;
    5. getLogger().info("FILLING CHESTS ON: " + worldName);
    6. fillAllChests(worldName);
    7. getLogger().info("FILLED CHESTS ON: " + worldName);
    8. }


    Hope this helps, maybe you can find a way to merge our code or possibly just move to mine.

    Regards,
    Alex Harris
     
Thread Status:
Not open for further replies.

Share This Page