Broadcast Help

Discussion in 'Plugin Development' started by jolbol1, Jul 23, 2013.

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

    jolbol1

    Hi, i need to know the best way to broadcast a server wide message from your plugin
    At the moment i use this
    Code:
    String message = prefix + this.getConfig().getString("KillMessage").replaceAll("&", "\u00A7").replaceAll("%killed", killed.getDisplayName()).replaceAll("%killer", killer.getDisplayName());
            Bukkit.broadcastMessage(message);
    However that does not seem to run. Heres my full code
    Code:
    package me.jolbol1.BetterPVP1;
     
    import java.util.ArrayList;
     
    import java.util.Random;
     
     
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.World;
     
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.entity.Player;
    import org.bukkit.event.entity.PlayerDeathEvent;
     
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.SkullMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class Death extends JavaPlugin implements Listener {
     
        public MyPlugin plugin;
     
        public Death(MyPlugin instance){
            plugin = instance;
        }
     
        @EventHandler
        public void onKill(PlayerDeathEvent e) {
            String prefix = ChatColor.GOLD + "[BetterPVP]" + ChatColor.RESET;
            Player killer = e.getEntity().getKiller();
            Player killed = e.getEntity();
           
           
     
            if(killer.hasPermission("Betterpvp.xp") && (e.getEntity().getKiller() instanceof Player) && killer != killed) {
                if(plugin.getConfig().getBoolean("Give-Xp")) {
                    int lvl = killed.getLevel();
                    killer.giveExpLevels(lvl);
                    killer.sendMessage(prefix + ChatColor.GREEN + "You Have Recieved " + killed.getDisplayName() + ChatColor.GREEN + "'s Xp Levels Of " + lvl);
                }
            }
     
            if(plugin.getConfig().getBoolean("Give-Money")) {
                if(killer.hasPermission("Betterpvp.money") && (e.getEntity().getKiller() instanceof Player) && killer !=killed) {
                    int low = plugin.getConfig().getInt("LowestPayout");
                    int high = plugin.getConfig().getInt("HighestPayout");
                    Random random = new Random();
                    int Money = random.nextInt((high - low) +1) + low;
                    MyPlugin.econ.depositPlayer(killer.getName(), Money);
                    killer.sendMessage(prefix + ChatColor.GREEN + "You Have Recieved " + Money + " For Winning A Battle");
                }
            }
     
            if(plugin.getConfig().getBoolean("PlayerHeadDrop")) {
                if(killer.hasPermission("Betterpvp.Trophy") && (e.getEntity().getKiller() instanceof Player) && killer !=killed) {
                    ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
                    SkullMeta meta = (SkullMeta) skull.getItemMeta();
                    meta.setOwner(killed.getName());
                    skull.setItemMeta(meta);
                   
                        killer.getWorld().dropItem(killer.getLocation(), skull);
                       
                   
                    }
                }
            if(plugin.getConfig().getBoolean("KillMessageOn"));
            String message = prefix + this.getConfig().getString("KillMessage").replaceAll("&", "\u00A7").replaceAll("%killed", killed.getDisplayName()).replaceAll("%killer", killer.getDisplayName());
            Bukkit.broadcastMessage(message);
           
            }
       
    }
    
    All other features work
     
  2. Offline

    psanker

    Don't use String.replaceAll(). That's intended for regex. String.replace() should work just fine.
     
  3. Offline

    MistPhizzle

    I'm not sure if this is the issue you're having, but remove this from this.getConfig(). If it's in your main class, which it looks like it is, remove this. It should just getConfig().getString("Path.To.String").

    Also, try using getServer().broadcastMessage("message).
     
  4. Offline

    jolbol1

    i changed that but when i kill a player, it still doesnt broadcast ?

    Tried These Both, Still wont Run. Heres that bit with the changes
    Code:
     if(plugin.getConfig().getBoolean("KillMessageOn"));
            String message = prefix + getConfig().getString("KillMessage").replace("&", "\u00A7").replace("%killed", killed.getDisplayName()).replace("%killer", killer.getDisplayName());
            getServer().broadcastMessage(message);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  5. Offline

    1Rogue

    What's the value of plugin.getConfig().getBoolean("KillMessageOn"); ? Print it out in your code.
     
  6. Offline

    jolbol1

    Well Heres My config.yml
    Code:
    # BetterPVP1 Config File
    Give-Xp: true
    Give-Money: true
    HighestPayout: 250
    LowestPayout: 0
    KillMessageOn: true
    KillMessage: %killer Killed %killed In A Brutal PVP fight
    PlayerHeadDrop: true
    Not to sure how to print out. :/
     
  7. Offline

    1Rogue

    System.out.println(plugin.getConfig().getBoolean("KillMessageOn"); ...
     
  8. Offline

    Samthelord1

    OnEnable and onDisable?
     
  9. Offline

    Adriano3ds

    You use plugin manager to "call" the event in the main class? Can you show your main class?
     
  10. Offline

    jolbol1

    Samthelord1
    I Have Another Main Class
    Adriano3ds
    Here :
    Code:
    package me.jolbol1.BetterPVP1;
     
    import java.io.File;
    import java.util.logging.Logger;
    import net.milkbowl.vault.chat.Chat;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    import net.milkbowl.vault.permission.Permission;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Server;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.FileConfigurationOptions;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.craftbukkit.libs.jline.internal.Log;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.*;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class MyPlugin extends JavaPlugin {
     
        FileConfiguration config;
        File cfile;
     
        public final Death pl = new Death(this);
        String prefix = ChatColor.GOLD + "[BetterPVP]" + ChatColor.RESET;
        private static final Logger log = Logger.getLogger("Minecraft");
        public static Economy econ = null;
     
        @Override
        public void onEnable() {
     
            File file = new File((new StringBuilder()).append(getDataFolder()).append(File.separator).append("config.yml").toString());
     
            if(!file.exists()) {
                getLogger().info("[BetterPVP] Generating config.yml...");
                getConfig().set("Give-Xp", Boolean.valueOf(true));
                getConfig().set("Give-Money", Boolean.valueOf(true));
                getConfig().set("LowestPayout", Integer.valueOf(0));
                getConfig().set("HighestPayout", Integer.valueOf(250));
                getConfig().set("KillMessageOn", Boolean.valueOf(true));
                getConfig().set("KillMessage", "'%killer Killed %killed In A Brutal PVP fight'");
                getConfig().set("PlayerHeadDrop", Boolean.valueOf(true));
                getConfig().options().copyDefaults(true);
                saveConfig();
            }
     
            if(!setupEconomy()) {
                log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", new Object[] {
                    getDescription().getName()
                }));
                getServer().getPluginManager().disablePlugin(this);
                return;
            } else {
                getServer().getPluginManager().registerEvents(pl, this);
                return;
            }
        }
     
        public boolean setupEconomy() {
            if(getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if(rsp == null) {
                return false;
            }
            econ = (Economy)rsp.getProvider();
            return econ != null;
        }
     
        @Override
        public void onDisable()
        {
        }
     
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("bpreload")) {
                reloadConfig();
                sender.sendMessage(ChatColor.GREEN + "Reloaded BetterPVP config!");
            } else if(cmd.getName().equalsIgnoreCase("betterpvp")) {
                    Player player = (Player)sender;
                    player.sendMessage(ChatColor.GOLD + "[BetterPVP]" + "Made By Jolbol1");
                    player.sendMessage(ChatColor.GOLD + "[BetterPVP]" + "Please Donate To Help Keep This Plugin Running");
            } else if(cmd.getName().equalsIgnoreCase("pvpmessage")) {
                    Player player = (Player)sender;
                    player.sendMessage(prefix + this.getConfig().getString("KillMessage").replaceAll("&", "\u00A7").replaceAll("%killed", player.getDisplayName()).replaceAll("%killer", player.getDisplayName()));
                    player.sendMessage(prefix + ChatColor.GREEN + "This is How The KillMessage Will Look, But With your Name As Both Variables ");
                }
           
            return true;
        }
    }
     
  11. Offline

    xTrollxDudex

    jolbol1
    You cant have 2 main classes
     
  12. Offline

    jolbol1

    xTrollxDudex
    I didnt mean it like that :) I meant i have another class, the Main class woops.
    Sorry for the confusion.
     
  13. Offline

    xTrollxDudex

    jolbol1
    You still have 2 classes that extend JavaPlugin
     
  14. Offline

    jolbol1

    Ah ok I have removed this now. So only my main class is extending Java
     
Thread Status:
Not open for further replies.

Share This Page