Solved HashMap empty after putting

Discussion in 'Plugin Development' started by rozbrajaczpoziomow, Apr 21, 2020.

Thread Status:
Not open for further replies.
  1. So,
    I've been trying to create a quarry plugin.
    And I've ran into an issue.

    After I call QuarryBlockBreaks.put(somelocation)
    It reports (console) that the size is one
    And then after I wait for the loop, it always reports size 0.

    QuarryPlaceEvent (BlockPlaceEvent):
    Code:
    package Quarry;
    
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    
    public class QuarryPlaceEvent implements Listener {
    
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) {
            if(!e.getItemInHand().getItemMeta().getDisplayName().equals((new QuarryItem()).Get(1).getItemMeta().getDisplayName())) { e.getPlayer().sendMessage("no"); return; }
            QuarryBlockBreaks handler = new QuarryBlockBreaks();
            handler.put(e.getBlock().getLocation());
            e.getPlayer().sendMessage((new QuarryItem()).Get(1).getItemMeta().getDisplayName() + " placed");
        }
    }
    
    QuarryBlockBreaks (The actual breaking mechanism):
    Code:
    package Quarry;
    
    import org.bukkit.*;
    import org.bukkit.block.Block;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.inventory.ItemStack;
    
    import java.util.HashMap;
    
    public class QuarryBlockBreaks {
        public HashMap<Location, Object[]> Quarries = new HashMap<>();
        public ConsoleCommandSender s = (Main.getPlugin(Main.class)).getServer().getConsoleSender();
        public void onServerStart() {
            s.sendRawMessage("a");
            Bukkit.getScheduler().scheduleSyncRepeatingTask((Main.getPlugin(Main.class)), () -> {
                s.sendRawMessage("r " + Quarries.size());
                for(Object[] Quarriesv: Quarries.values()) {
                    s.sendRawMessage("inside of for");
                    Location qLocation = (Location) Quarriesv[0];
                    int x = (int) qLocation.getX();
                    int z = (int) qLocation.getZ();
                    World qWorld = qLocation.getWorld();
                    int CurrentY = (int) Quarriesv[1];
                    s.sendRawMessage("past setting values");
                    if(qWorld.getBlockAt(x, CurrentY-1, z).getType() != Material.BEDROCK) {
                        s.sendRawMessage("!bedrock");
                        CurrentY--;
                        Block CurrentBlock = qWorld.getBlockAt(x, CurrentY, z);
                        if(CurrentBlock.isLiquid() || CurrentBlock.isEmpty()) {
                            s.sendRawMessage("liquid / empty");
                            CurrentBlock.setType(Material.AIR);
                        } else {
                            s.sendRawMessage("!liquid / !empty");
                            qWorld.dropItemNaturally(new Location(qWorld, x, CurrentY, z), new ItemStack(qWorld.getBlockAt(x, CurrentY, z).getType(), 1));
                        }
                        s.sendRawMessage("about to replace " + Quarries.size());
                        Quarries.replace(qLocation, new Object[] {
                                qLocation,
                                CurrentY
                        });
                        s.sendRawMessage("after replacing" + Quarries.size());
                    } else {
                        qWorld.getBlockAt(qLocation).setType(Material.AIR);
                        qWorld.dropItemNaturally(qLocation, (new QuarryItem()).Get(1));
                        s.sendRawMessage("removing quarry @ " + qLocation.getX() + " " + qLocation.getY() + " " + qLocation.getZ());
                        Quarries.remove(qLocation);
                    }
                }
            }, 0L, 20L);
        }
        public void put(Location l) {
            Quarries.put(l, new Object[]{
                    l,
                    l.getY()
            });
            s.sendRawMessage("put");
            s.sendRawMessage(Integer.toString(Quarries.size()));
        }
    }
    
    And after I place the quarry, in console is says:
     
  2. Online

    timtower Administrator Administrator Moderator

    @rozbrajaczpoziomow That is because you are using different instances of QuarryBlockBreaks
     
  3. I'm sorry but I don't understand...
     
  4. Online

    timtower Administrator Administrator Moderator

    @rozbrajaczpoziomow You QuarryBlockBreaks where you start the timer is a different one than the one in the event:
    QuarryBlockBreaks handler = new QuarryBlockBreaks();
    Pass it along using a constructor instead.
     
  5. Sorry, but I'm a bit new to Java so idk how to do this, tried googling it, but it came up with a weird thing that I don't understand.
     
  6. Online

    timtower Administrator Administrator Moderator

    Then put the event in the QuarryBlockBreaks class.
     
  7. I put the onBlockPlace event in QuarryBlockBreaks, registered it, and tested it, and it seems like it's... putting but the loop always says it's 0

    Code now:
    Code:
    package Quarry;
    
    import org.bukkit.*;
    import org.bukkit.block.Block;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.inventory.ItemStack;
    
    import java.util.HashMap;
    
    public class QuarryBlockBreaks implements Listener {
        public HashMap<Location, Object[]> Quarries = new HashMap<>();
        public ConsoleCommandSender s = (Main.getPlugin(Main.class)).getServer().getConsoleSender();
        public void onServerStart() {
            s.sendRawMessage("a");
            Bukkit.getScheduler().scheduleSyncRepeatingTask((Main.getPlugin(Main.class)), () -> {
                s.sendRawMessage("r " + Quarries.size());
                for(Object[] Quarriesv: Quarries.values()) {
                    s.sendRawMessage("inside of for");
                    Location qLocation = (Location) Quarriesv[0];
                    int x = (int) qLocation.getX();
                    int z = (int) qLocation.getZ();
                    World qWorld = qLocation.getWorld();
                    int CurrentY = (int) Quarriesv[1];
                    s.sendRawMessage("past setting values");
                    if(qWorld.getBlockAt(x, CurrentY-1, z).getType() != Material.BEDROCK) {
                        s.sendRawMessage("!bedrock");
                        CurrentY--;
                        Block CurrentBlock = qWorld.getBlockAt(x, CurrentY, z);
                        if(CurrentBlock.isLiquid() || CurrentBlock.isEmpty()) {
                            s.sendRawMessage("liquid / empty");
                            CurrentBlock.setType(Material.AIR);
                        } else {
                            s.sendRawMessage("!liquid / !empty");
                            qWorld.dropItemNaturally(new Location(qWorld, x, CurrentY, z), new ItemStack(qWorld.getBlockAt(x, CurrentY, z).getType(), 1));
                        }
                        s.sendRawMessage("about to replace " + Quarries.size());
                        Quarries.replace(qLocation, new Object[] {
                                qLocation,
                                CurrentY
                        });
                        s.sendRawMessage("after replacing" + Quarries.size());
                    } else {
                        qWorld.getBlockAt(qLocation).setType(Material.AIR);
                        qWorld.dropItemNaturally(qLocation, (new QuarryItem()).Get(1));
                        s.sendRawMessage("removing quarry @ " + qLocation.getX() + " " + qLocation.getY() + " " + qLocation.getZ());
                        Quarries.remove(qLocation);
                    }
                }
            }, 0L, 20L);
        }
    
        @EventHandler
        public void onBlockPlace(BlockPlaceEvent e) {
            if(!e.getItemInHand().getItemMeta().getDisplayName().equals((new QuarryItem()).Get(1).getItemMeta().getDisplayName())) { e.getPlayer().sendMessage("no"); return; }
            s.sendRawMessage(Quarries.size() + "s");
            Quarries.put(e.getBlock().getLocation(), new Object[] {
                    e.getBlock().getLocation(),
                    e.getBlock().getLocation().getY()
            });
            s.sendRawMessage(Quarries.size() + "z");
            e.getPlayer().sendMessage((new QuarryItem()).Get(1).getItemMeta().getDisplayName() + " placed");
        }
    }
    
    Console output:
    Main:
    Code:
    package Quarry;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
        @Override
        public void onEnable() {
            getCommand("quarry").setExecutor(new QuarryCommand());
            getServer().getPluginManager().registerEvents(new QuarryBlockBreaks(), this);
            (new QuarryBlockBreaks()).onServerStart();
            s(true);
        }
    
        @Override
        public void onDisable() {
            s(false);
        }
    
        private void s(boolean S) { getServer().getConsoleSender().sendRawMessage(getName() + " has been " + (S? "enabled" : "disabled")); }
    }
    
     
    Last edited: Apr 22, 2020
  8. Online

    timtower Administrator Administrator Moderator

  9. I edited the previous message to include Main to avoid double-posting.
     
  10. Online

    timtower Administrator Administrator Moderator

    You could have put that in this message...
    You are making multiple instances.
    Make an instance of QuarryBlockBreaks
    Register the events for that.
    Call onServerStart on it.
     
  11. Ok, it works now. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page