HOW to make a plugin PLACE/GENERATE a block?

Discussion in 'Plugin Development' started by insanj, May 14, 2011.

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

    insanj

    This problem has been resolved. Any comments about it will still be responded to by me below.

    Okay, so I'm trying to make a crazy plugin.

    The general idea is this: When a player places a special block (a Portal, in this case), my plugin will place wood (or any other material) blocks in a specific pattern, in order to suffocate and kill the player.

    Sounds pretty strange. But, if I can get it to work, it will provide some serious background for Bukkit plugin development, because it requires a lot of calls to Bukkit/Player methods.

    My only issue: How do I make the plugin place a block? When the plugin detects the portal is placed, it needs to place blocks in a to-be-determined pattern until it kills the player... But, I can't even get started if I can't place any blocks!

    Any luck?
    If you can help, I'll give you all future versions, and access to my server, even though I doubt any of you want it. :p

    TL;DR- I'm making a crazy plugin that, when activated, places blocks until it kills the player, but I can't figure out how to make plugins place blocks in any way.
     
  2. Look at the source code of my zombies plugin in my huge plugin tutorial, the link is in my signature.
     
  3. Offline

    insanj

    Fantastic. I think that was exactly what I needed.
    However, after a couple hours of more coding, wheneven I try to run my CraftBukkit server, the console spits out this error:

    Code:
    14:25:50 [SEVERE] Could not load 'plugins/replicator.jar' in folder 'plugins':
    java.lang.NoClassDefFoundError: JavaPlugin
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:36)
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:24)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:247)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:160)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:191)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:115)
        at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:100)
        at org.bukkit.craftbukkit.CraftServer.reload(CraftServer.java:336)
        at org.bukkit.command.SimpleCommandMap$ReloadCommand.execute(SimpleCommandMap.java:201)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:85)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:270)
        at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:396)
        at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:381)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:287)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:394)
    Caused by: java.lang.ClassNotFoundException: JavaPlugin
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:36)
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:24)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
        ... 27 more
    
    Can you help me at all with this?
    Is it the code, the compression/jar?
    Thanks!

    (Here is the current jar. Decompile for source.)
     
  4. Hard to tell from that alone but is your main class extends JavaPlugin?
     
  5. Offline

    insanj

    Yes, it does.
    Most of the code that isn't Replicator-specific (as in Portal placement, etc.) I've taken from sample plugins, and other resources like that; I doubt many things are wrong with the main class, only maybe the BlockListener class.

    For your convenience, here's the entire replicator.java file (even though it might take up a bit too much space). If there are any issues you spot, at all, please let me know, it'd be nice to cook up a great, clean plugin out of all this.

    Code:
    package me.insanj.replicator;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.logging.Logger;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class replicator extends JavaPlugin
    {
        private static final Logger log = Logger.getLogger("Minecraft");
        private final replicatorBlockListener blockListener = new replicatorBlockListener(this);
        public final HashMap<Player, ArrayList<Block>> replicatorUsers = new HashMap<Player, ArrayList<Block>>();
    
        @Override
        public void onEnable()
        {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvent(Event.Type.BLOCK_PLACE, blockListener, Event.Priority.Normal, this);
            log.info("REPLICATOR STARTED");
    
        }//end method onEnable()
    
        @Override
        public void onDisable()
        {
            log.info("REPLICATOR DISABLED");
    
        }//end method onDisable()
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
    
            if( commandLabel.equalsIgnoreCase("replicator") || commandLabel.equalsIgnoreCase("r") )
            {
                toggleReplicator((Player) sender);
                return true;
            }//end if
    
            else
                return false;
    
        }//end method onCommand()
    
        private void toggleReplicator(Player player)
        {
    
            if(enabled(player))
            {
                this.replicatorUsers.remove(player);
                player.sendMessage("Replicator disabled.");
            }//end if
    
            else
            {
                this.replicatorUsers.put(player, null);
                player.sendMessage("Replicator enabled.");
            }//end else
    
        }//end method toggleReplicator()
    
        public boolean enabled(Player player)
        {
            return this.replicatorUsers.containsKey(player);
    
        }//end method enabled()
    
    }//end class replicator
    
    Also, I should note that I am pretty competent at Java, and understand almost everything from Methods to ArrayLists, minus the HashMaps (which I've only had a bit of work with).

    Any luck? :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 16, 2016
  6. Offline

    yottabyte

    Looking at the compiled files it doesn't seem like you've added Bukkit to the build path.

    Edit: And also, why use a HashMap just to store null, wouldn't an ArrayList be better in that case?
     
  7. Offline

    insanj

    Alright, alright, AWESOME.
    The plugin is up and running... minus the running.
    The server is able to handle the plugin running perfectly, and when I enable the plugin and place a block, everything is perfect. However, after it says "YOU STARTED THE REPLICATOR," nothing happens, and no matter what I do, there is no more response from the running Replicator.

    Any ideas?


    Edit: Oh, you mean link the API to the program in Eclipse? Yes, I did that, in order to properly code it as well. Any other things you notice? :/
    Bukkit to the build path?
    As in the plugin.yml, or something else?

    Edit: Hmm. Sounds like a fantastic idea, but, first I just want to get the plugin working! Screw it, I changed the HashMap to an ArrayList, added a couple annotations, cleaned up the code, and bundled it up nicely again. Same link as before, but here it is again.
     
  8. Offline

    yottabyte

    Did you put @Override above onEnable()?
     
  9. Offline

    insanj

    Okay, long story short, I got it to work.
    Most of the problem was with the calls to Bukkit methods and whatnot.
    I'll submit future versions to the official list, but for now, I'll be quietly slaving over this evolving plugin- which won't be about killing yourself anymore. :)
     
Thread Status:
Not open for further replies.

Share This Page