Help with my first plugin

Discussion in 'Plugin Development' started by Crosant, May 15, 2011.

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

    Crosant

    I have a problem and that is that i want to send an messange to the whole server.

    My Warning.java
    Code:
    package me.Crosant.Warning;
    
    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 Warning extends JavaPlugin {
        private static final Logger log = Logger.getLogger("Minecraft");
        private final WarningBlockListener blockListener = new WarningBlockListener(this);
        public final HashMap<Player, ArrayList<Block>> tdUsers = new HashMap<Player, ArrayList<Block>>();
        public void onEnable() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener,
                    Event.Priority.Normal, this);
            log.info("Warnings STARTED");
        }
    
        public void onDisable() {
            log.info("Warnings DISABLED");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("Warn")){
                toggleTD((Player) sender);
                return true;
            }
            return false;
        }
        public void toggleTD(Player player){
            if(enabled(player)){
                this.tdUsers.remove(player);
                player.sendMessage("Warnings Disabled");
            }else{
                this.tdUsers.put(player, null);
                player.sendMessage("Warnings Enabled");
            }
        }
        public boolean enabled(Player player){
            return this.tdUsers.containsKey(player);
        }
    }
    

    My WarningBlockListener.java
    Code:
    package me.Crosant.Warning;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockListener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.plugin.Plugin;
    
    public class WarningBlockListener extends BlockListener {
        public static Warning plugin;
    
        public WarningBlockListener(Warning instance) {
            plugin = instance;
        }
        public void onBlockCreate (BlockPlaceEvent event){
            Block block = event.getBlock();
            Player player = event.getPlayer();
            if(block.getType() == Material.TNT && plugin.enabled(player)){
                server.sendMessage(ChatColor.RED + (player + "place TNT.")); // i want to say PlayerXXX has placed TNT to the server
            }
        }
        public void onBlockBreak(BlockBreakEvent event){
            Block block = event.getBlock();
            Player player = event.getPlayer();
            if(block.getType() == Material.TNT && plugin.enabled(player)){
                server.sendMessage(ChatColor.RED + (player + "broke a tnt.")); // i want to say PlayerXXX has destroy TNT to the server
            }
        }
    }
     
  2. Offline

    kman2010

    lol look at my TD plugin it does exactlt that
     
  3. Offline

    Crosant

    yes i have made it after seeing the td tutorial but I think it just say to the player "You have brocken torch"
    and not to all players with the name of the player who broken it.


    kan someone see this

    Sorry 4 my bad english
     

    Attached Files:

  4. Offline

    Crosant

    No idears?
     
  5. Offline

    kman2010

    i can show you how to give a warning to console but idk how to broadcast it
     
  6. You need to get the server instance and then use it to broadcast a message.

    So in the class MyWarning declare a server variable
    PHP:
    public static Server server;
    Then inside of on enabled register that server.
    PHP:
    server this.getServer();
    Then inside the class you want to broadcast a message, player listener I guess
    PHP:
    MyWarning.server.broadcastMessage("message here");
     
  7. Offline

    Crosant

    Thanks i will Test that
     
  8. Let me know if it works out for you ;)
     
  9. Offline

    Crosant

    no it doesent work
    I bekomm an failure when I start the server
     

    Attached Files:

  10. Offline

    Crosant

    Help I can start the server but ingame nothing is going

    Warning.java

    Code:
    package me.Crosant.Warning;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.logging.Logger;
    
    import org.bukkit.Server;
    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 Warning extends JavaPlugin {
        private static final Logger log = Logger.getLogger("Minecraft");
        public static Server server;
        private final WarningBlockListener blockListener = new WarningBlockListener(this);
        public final HashMap<Player, ArrayList<Block>> tdUsers = new HashMap<Player, ArrayList<Block>>();
        public void onEnable() {
            server = this.getServer();
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener,
                    Event.Priority.Normal, this);
            log.info("Warnings STARTED");
        }
    
        public void onDisable() {
            log.info("Warnings DISABLED");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("Warning")){
                toggleWarn((Player) sender);
                return true;
            }
            return false;
        }
        public void toggleWarn(Player player){
            if(enabled(player)){
                this.tdUsers.remove(player);
                player.sendMessage("Warnings Disabled");
            }
            else{
                this.tdUsers.put(player, null);
                player.sendMessage("Warnings Enabled");
            }
        }
        public boolean enabled(Player player){
            return this.tdUsers.containsKey(player);
        }
    }

    WarningBlockListener.java

    Code:
    package me.Crosant.Warning;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockListener;
    import org.bukkit.event.block.BlockPlaceEvent;
    
    public class WarningBlockListener extends BlockListener {
        public static Warning plugin;
    
        public WarningBlockListener(Warning instance) {
            plugin = instance;
        }
        public void onBlockCreate (BlockPlaceEvent event){
            Block block = event.getBlock();
            Player player = event.getPlayer();
            if(block.getType() == Material.TNT && plugin.enabled(player)){
                Warning.server.broadcastMessage(ChatColor.RED + (player + "place TNT.")); // i want to say PlayerXXX has placed TNT to the server
            }
        }
        public void onBlockBreak(BlockBreakEvent event){
            Block block = event.getBlock();
            Player player = event.getPlayer();
            if(block.getType() == Material.TNT && plugin.enabled(player)){
                Warning.server.broadcastMessage(ChatColor.RED + (player + "broke a tnt.")); // i want to say PlayerXXX has destroy TNT to the server
            }
        }
    }
    


    Plugin.yml

    Code:
    name: Warning
    main: me.Crosant.Warning.Warning
    version: 1
    commands:
      Warning:
        description: Controls Warnings.
        usage: |
               /warn 
     
  11. Offline

    Crosant

    No idears ???
     
  12. Offline

    0ffn00b

    to broadcast to a server i use:
    plugin.getServer().broadcastMessage("Stuff you want to broadcast");
     
  13. Offline

    dreadiscool

    Well, here's one that works for me (I'm running the latest build of Bukkit btw, are you sure you are too)

    Keep in mind that Message is a String:
    String Message = "";

    Code:
    Bukkit.getServer().broadcastMessage(Message);
    tell me if this works out for you:D
     
  14. Offline

    0ffn00b

    yep same thing but you don't need to declare Message as string its part of the method.
     
  15. Offline

    Crosant

    how to send an message when a block is placed?
     
  16. Offline

    killerrj8

    u need to register am event... but i also have problems with that dont know why :/
     
  17. Offline

    Crosant

    Thanks all its going :)
     
  18. Replied to your PM.
     
Thread Status:
Not open for further replies.

Share This Page