Help with a very basic plugin

Discussion in 'Spigot Help' started by MCU, Dec 4, 2019.

  1. MCU

    MCU

    Hi, i am very new to coding in JAVA. Any tips are welcome! Here is my problem:
    I am recreating a game called Hide or hunt. Basic functionality of this plugin is: On beacon placed down - save the coords and name of placer. on beacon break shout out who broke the beacon, and whoose beacon it was. The part that doesent work is the thing where its supposed to shout out whoose the beacon was. It doesent post anything. This is my code:

    Main.java
    Code (Java):

    package org.mcuniversitydev.openhoh;

    import org.bukkit.plugin.java.JavaPlugin;
    import org.mcuniversitydev.openhoh.listeners.beaconlistener.BeaconListener;

    public class Main extends JavaPlugin {
       @Override
       public void onEnable() {
           saveDefaultConfig();
           
           new BeaconListener(this);
       }
    }

     
    BeaconListener.java
    Code (Java):

    package org.mcuniversitydev.openhoh.listeners.beaconlistener;


    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.mcuniversitydev.openhoh.Main;


    @SuppressWarnings("unused")
    public class BeaconListener implements Listener {
       
       String[][][] beacons = {};
       
       
       
       private Main plugin;
       
       public BeaconListener(Main plugin) {
           this.plugin = plugin;
           
           Bukkit.getPluginManager().registerEvents(this, plugin);
       }
       @EventHandler
       public void onBlockPlace(BlockPlaceEvent e) {
           Player p = e.getPlayer();
           int bcc[] = {e.getBlock().getX(), e.getBlock().getY(), e.getBlock().getZ()};
           int x = bcc[0];
           int y = bcc[1];
           int z = bcc[2];
           
           if (e.getBlock().getType() == Material.BEACON) {
           
           beacons[x][y][z]=e.getPlayer().getDisplayName();
       } else if (e.getBlock().getType() == Material.CRAFTING_TABLE || e.getBlock().getType() == Material.BLAST_FURNACE || e.getBlock().getType() == Material.FURNACE || e.getBlock().getType() == Material.ENCHANTING_TABLE || e.getBlock().getType() == Material.ANVIL || e.getBlock().getType() == Material.DAMAGED_ANVIL || e.getBlock().getType() == Material.CHIPPED_ANVIL) {
                      if(p.getLocation().getWorld().getBlockAt(x-1,y,z).getType() == Material.BEACON){
               } else if(p.getLocation().getWorld().getBlockAt(x+1,y,z).getType() == Material.BEACON){
               } else if(p.getLocation().getWorld().getBlockAt(x,y-1,z).getType() == Material.BEACON){      
               } else if(p.getLocation().getWorld().getBlockAt(x,y+1,z).getType() == Material.BEACON){      
               } else if(p.getLocation().getWorld().getBlockAt(x,y,z-1).getType() == Material.BEACON){      
               } else if(p.getLocation().getWorld().getBlockAt(x,y,z+1).getType() == Material.BEACON){      
               } else {
                   e.getBlock().breakNaturally();
                   p.sendMessage("You can only place this at your beacon!");
               }
         }
           }
       @EventHandler
       //@SuppressWarnings("unused")
       public void onBlockBreak(BlockBreakEvent e) {
           Player p = e.getPlayer();
           int bcc[] = {e.getBlock().getX(), e.getBlock().getY(), e.getBlock().getZ()};
           int x = bcc[0];
           int y = bcc[1];
           int z = bcc[2];
           
           if (e.getBlock().getType() == Material.BEACON) {
           if (e.isCancelled()) {
                 for (int i = 0;i<beacons.length;i++) {
                       for (int ii = 0;ii<beacons[i].length;ii++) {
                           for (int iii = 0;iii<beacons[i][i].length;iii++) {
                               if (i == x) {
                                   if (ii == y) {
                                       if (iii == z) {
                                           final String bname=beacons[i][ii][iii];
                                           //Bukkit.getOnlinePlayers().forEach(all -> {
                                           //   all.sendMessage(p.getDisplayName() + " has broken " + bname + "\'s beacon!");
                                           //  
                                           //});
                                           p.sendMessage("Common! You can do it! You allmost destroyed a beacon! It belongs to " + bname + "!");
                                           
                                           
                                           
                                           
                                           
                                           
                                       }
                                           
                                   }
                               }
                           }
                       }
                   }
               
               
           } else {
               //String bname = "";
               
                 for (int i = 0;i<beacons.length;i++) {
                   for (int ii = 0;ii<beacons[i].length;ii++) {
                       for (int iii = 0;iii<beacons[i][i].length;iii++) {
                           if (i == x) {
                               if (ii == y) {
                                   if (iii == z) {
                                       final String bname=beacons[i][ii][iii];
                                       Bukkit.getOnlinePlayers().forEach(all -> {
                                           all.sendMessage(p.getDisplayName() + " has broken " + bname + "\'s beacon!");
                                           
                                       });
                                       
                                       
                                       
                                       
                                       
                                       
                                   }
                                       
                               }
                           }
                       }
                   }
               }
             
               
               
               
               
               p.sendMessage("YES! You broke a beacon! Good job!");
               
           }
           }
       }
       
    }


     

    Tips and helps are appriciated, thnx. :D
     
  2. Strahan

    Benefactor

    OK. There's a lot to unpack here. So the idea is when the materials in that long ass if statement are placed, they need to be right next to a beacon, correct? If so, a MUCH easier way to accomplish that is to get a relative block. Also the tridimensional String is a very awkward way to log the beacons. You're much better off using a Map. If there are other things to log relative to the beacon I'd use a data class, but if it's just a matter of beacon location and name, a Map would suffice. I also do not see any logic to determine ownership. You are, in block break, checking if the event is cancelled. But you aren't cancelling it anywhere, so unless another plugin is doing it I don't think that'll ever be true. I could be wrong, but I don't think an event being cancelled is something that happens naturally. What I meant by ownership was that if, lets say that cancelled true code is fired for some reason. Even if the beacon is my own beacon, it will send me the "you're almost there" message because it simply is checking if the beacon location exists in the tridimensional String. You should tie the beacon to its owner to avoid that and you'll likely need it for scoring as well if you intend to score.

    Lastly, don't suppress things, fix what the compiler is bitching about.

    For example, if I were to do it, I'd make a Map<Location, UUID> for the beacons. When a beacon is placed, I'd enter it into the map by BlockPlaceEvent#getBlock().getLocation(), BlockPlaceEvent#.getPlayer().getUniqueId(). Then to check whose beacon is being attacked, you'd get the value from the map using the block location which gives you a player UUID from which you can get their information. If name is literally all you need to worry about, then the map could be Location, String and just store the PlayerObject#getName(). Just don't get in the habit of it, because if you intend to log data somewhere you want to log by UUID not name.
     
    • Funny Funny x 1
  3. You could also use a WeakHashMap and store the Player object itself. WeakHashMap is the same thing as HashMap but it destroys its keys when they’re no longer in use by the JVM. It uses “weak” references.
     
    • Informative Informative x 1