Need Help figuring out how to accomplish plugin goal

Discussion in 'Plugin Development' started by coderboy14, May 25, 2017.

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

    coderboy14

    Hi. I was asked by a friend to develop a plugin for them, and it initially seemed like a feasible task. Now, I'm unsure of how to proceed. This is the general idea of the plugin: Players can increase the spawning rate of mob spawners, by clicking on it, and clicking a confirmation text. This will cost xp (experience that's built into vanilla Minecraft.

    I've tried Googleing, but to no avail. Can somebody give me some steps in the right direction. I've tried researching on modifying metadata and NBT tags, but from the tiny bit I could understand, it didn't help. If you could, can you provide me with basic example code. Sometimes trying to understand the documentation and some forums is almost impossible for me, so if you would't mind, that would be amazing. Thanks!
     
    Last edited: May 25, 2017
  2. Offline

    Zombie_Striker

  3. Offline

    coderboy14

    Thank you. I just can't figure out how to get it to work. This is what I've done
    Code:
    if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    
                event.getClickedBlock().setDelay(0);
    
            }
    
    Thanks in advance.
     
  4. Offline

    Zombie_Striker

    @coderboy14
    You have to cast the block's state to a CreatureSpawner first before you can use that method. So use something like:
    Code:
                ((CreatureSpawner)event.getClickedBlock().getState()).setDelay(0);
     
  5. Offline

    coderboy14

    Thank you. Thank fixed it, and now it's working. :)

    I though it was working; I put a bit of text to notify me when it's loaded, but it's not recognizing my clicks. Can you tell me what I did wrong?
    Code:
    import java.io.File;
    
    import java.util.HashMap;
    
    
    
    import org.bukkit.Bukkit;
    
    import org.bukkit.Material;
    
    import org.bukkit.block.CreatureSpawner;
    
    import org.bukkit.command.Command;
    
    import org.bukkit.command.CommandSender;
    
    import org.bukkit.entity.Player;
    
    import org.bukkit.event.block.Action;
    
    import org.bukkit.event.player.PlayerInteractEvent;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    
    import net.minecraft.server.v1_11_R1.NBTTagCompound;
    
    
    
    public class Main extends JavaPlugin {
    
       
    
        intspeedChange = 10;
    
        intcostperLevel = 10;
    
       
    
        HashMap<String, CreatureSpawner> playerData = new HashMap<String, CreatureSpawner>();
    
       
    
        public void onEnable() {
    
            System.out.println("==============================================================PLUGIN RAN=============================");
    
            Bukkit.broadcastMessage("This plugin is protected under the following agreements, which you can find at this link: ");
    
            Bukkit.broadcastMessage("https://www.gnu.org/licenses/gpl-3.0.en.html");
    
            Bukkit.broadcastMessage("Programmed by Ethan Manzi");
    
        }
    
        public void onPlayerInteract(PlayerInteractEvent event) {
    
             
    
            /*if (event.getAction.equals(Action.RIGHT_CLICK_BLOCK) {
    
             
    
            //Do Stuff
    
             
    
            }*/
    
           
    
            if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    
                System.out.println("YOLOLOLOLOLOLOLOLOLOL");
    
                if (event.getClickedBlock().getType() == Material.MOB_SPAWNER) {
    
                CreatureSpawner n = ((CreatureSpawner)event.getClickedBlock().getState());
    
                Player p = event.getPlayer();
    
                //double xp = p.getExp();
    
                playerData.put(p.getName(), n);
    
                p.sendMessage("To upgrade this spawners speed from " + n.getDelay() * 4 + "/s to " + (n.getDelay()+speedChange) + "/s, with a cost"
    
                        + "of " + (((n.getDelay()*4)/speedChange)*costperLevel) + " xp, type /acceptUpgrade");
    
            }
    
            }
    
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if (cmd.getName().equalsIgnoreCase("/acceptupgrade")) {
    
                CreatureSpawner c = playerData.get(sender.getName());
    
                Player p = Bukkit.getServer().getPlayer(sender.getName());
    
                double xp = p.getExp();
    
                if (xp>costperLevel) {
    
                    xp = xp - costperLevel;
    
                    c.setDelay(c.getDelay() - speedChange);
    
                    p.setExp((float) xp);
    
                    p.sendMessage("You have sucessfully upgraded the spawner!");
    
                } else {
    
                    p.sendMessage("Insufficent XP");
    
                }
    
            }
    
           
    
            returntrue;
    
        }
    
    }
    
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2017
  6. Offline

    timtower Administrator Administrator Moderator

  7. Offline

    coderboy14

    Okay, I did that. Still, neither the message my player should get, nor the terminal message, are coming up.
    Code:
    import java.io.File;
    
    import java.util.HashMap;
    
    
    
    import org.bukkit.Bukkit;
    
    import org.bukkit.Material;
    
    import org.bukkit.block.CreatureSpawner;
    
    import org.bukkit.command.Command;
    
    import org.bukkit.command.CommandSender;
    
    import org.bukkit.entity.Player;
    
    import org.bukkit.event.EventHandler;
    
    import org.bukkit.event.block.Action;
    
    import org.bukkit.event.player.PlayerInteractEvent;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    
    import net.minecraft.server.v1_11_R1.NBTTagCompound;
    
    
    
    public class Main extends JavaPlugin {
    
       
    
        intspeedChange = 10;
    
        intcostperLevel = 10;
    
       
    
        HashMap<String, CreatureSpawner> playerData = new HashMap<String, CreatureSpawner>();
    
       
    
        public void onEnable() {
    
            System.out.println("==============================================================PLUGIN RAN=============================");
    
            Bukkit.broadcastMessage("This plugin is protected under the following agreements, which you can find at this link: ");
    
            Bukkit.broadcastMessage("https://www.gnu.org/licenses/gpl-3.0.en.html");
    
            Bukkit.broadcastMessage("Programmed by Ethan Manzi");
    
        }
    
        @EventHandler
    
        public void onPlayerInteract(PlayerInteractEvent event) {
    
             
    
            /*if (event.getAction.equals(Action.RIGHT_CLICK_BLOCK) {
    
             
    
            //Do Stuff
    
             
    
            }*/
    
           
    
            if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    
                System.out.println("YOLOLOLOLOLOLOLOLOLOL");
    
                if (event.getClickedBlock().getType() == Material.MOB_SPAWNER) {
    
                CreatureSpawner n = ((CreatureSpawner)event.getClickedBlock().getState());
    
                Player p = event.getPlayer();
    
                //double xp = p.getExp();
    
                playerData.put(p.getName(), n);
    
                p.sendMessage("To upgrade this spawners speed from " + n.getDelay() * 4 + "/s to " + (n.getDelay()+speedChange) + "/s, with a cost"
    
                        + "of " + (((n.getDelay()*4)/speedChange)*costperLevel) + " xp, type /acceptUpgrade");
    
            }
    
            }
    
        }
    
        @EventHandler
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if (cmd.getName().equalsIgnoreCase("/acceptupgrade")) {
    
                CreatureSpawner c = playerData.get(sender.getName());
    
                Player p = Bukkit.getServer().getPlayer(sender.getName());
    
                double xp = p.getExp();
    
                if (xp>costperLevel) {
    
                    xp = xp - costperLevel;
    
                    c.setDelay(c.getDelay() - speedChange);
    
                    p.setExp((float) xp);
    
                    p.sendMessage("You have sucessfully upgraded the spawner!");
    
                } else {
    
                    p.sendMessage("Insufficent XP");
    
                }
    
            }
    
           
    
            returntrue;
    
        }
    
    }
    
    
    
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    coderboy14

    I forgot; how do I do that. It's been a while since I've done anything Bukkit related.
     
  10. Offline

    Zombie_Striker

    @coderboy14
    getServer().getPluginManager().registerEvents(LISTENER, MAINCLASS);
     
Thread Status:
Not open for further replies.

Share This Page