Solved Freeze Plugin / Announcer Plugin!

Discussion in 'Plugin Development' started by Astrophylite, Aug 29, 2015.

Thread Status:
Not open for further replies.
  1. Hey all,

    I am working on a plugin and right now I am working on the Freeze part of it, however, I am stuck on getting it to actually freeze me... Bare in mind these are in different classes and they have been registered appropiately... I have already watched laods of videos and searched Google but none of the solutions work. Also, Eclipse Mars is not throwing any errors

    Main Class:
    Code:
    package me.zircon.zirconessentials;
    
    import org.bukkit.Bukkit;
    import org.bukkit.event.HandlerList;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.zircon.zirconessentials.commands.general.MOTDCommands;
    import me.zircon.zirconessentials.commands.general.Test;
    import me.zircon.zirconessentials.commands.inventory.ClearInventory;
    import me.zircon.zirconessentials.commands.inventory.Kits;
    import me.zircon.zirconessentials.commands.medic.Feed;
    import me.zircon.zirconessentials.commands.medic.Heal;
    import me.zircon.zirconessentials.commands.prank.FakeDeOP;
    import me.zircon.zirconessentials.commands.prank.FakeJoin;
    import me.zircon.zirconessentials.commands.prank.FakeOP;
    import me.zircon.zirconessentials.commands.prank.FakeQuit;
    import me.zircon.zirconessentials.commands.teleport.SetSpawn;
    import me.zircon.zirconessentials.commands.teleport.Spawn;
    import me.zircon.zirconessentials.commands.teleport.Teleport;
    import me.zircon.zirconessentials.events.general.MOTDEvent;
    import me.zircon.zirconessentials.other.Freeze;
    
    public class ZirconEssentials extends JavaPlugin {
      
        public static final HandlerList handlers = new HandlerList();
        PluginDescriptionFile pdfFile = this.getDescription();
        public void onEnable() {
            getConfig().options().copyDefaults(true);
            saveConfig();
            getLogger().info(pdfFile.getName() + " version " + pdfFile.getVersion() + " has been enabled!");
            getCommand("test").setExecutor(new Test());
            getCommand("heal").setExecutor(new Heal());
            getCommand("feed").setExecutor(new Feed());
            getCommand("fakeop").setExecutor(new FakeOP());
            getCommand("fakejoin").setExecutor(new FakeJoin());
            getCommand("fakequit").setExecutor(new FakeQuit());
            getCommand("fakedeop").setExecutor(new FakeDeOP());
            getCommand("motd").setExecutor(new MOTDCommands());
            getCommand("teleport").setExecutor(new Teleport());
            getCommand("spawn").setExecutor(new Spawn());
            getCommand("setspawn").setExecutor(new SetSpawn());
            getCommand("kit").setExecutor(new Kits());
            getCommand("clearinventory").setExecutor(new ClearInventory());
            getCommand("freeze").setExecutor(new Freeze());
            Bukkit.getServer().getPluginManager().registerEvents(new MOTDEvent(), this);
            Bukkit.getServer().getPluginManager().registerEvents(new Freeze(), this);
        }
        public void onDisable() {
            getLogger().info(pdfFile.getName() + " version " + pdfFile.getVersion() + " has been disabled!");
            HandlerList.unregisterAll();
        }
    }
    Freeze Class:
    Code:
    package me.zircon.zirconessentials.other;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    
    import me.zircon.zirconessentials.miscellaneous.Utils;
    
    public class Freeze implements CommandExecutor, Listener {
      
        ArrayList<String> frozen = new ArrayList<String>();
        public void onEnable() {
            frozen.clear();
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(label.equalsIgnoreCase("freeze")) {
                if(!sender.hasPermission("zirconessentials.freeze")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to freeze players!");
                    return false;
                } else {
                    if(args.length == 0) {
                        sender.sendMessage(ChatColor.RED + "Usage: /freeze <player>");
                        return false;
                    } else {
                        Player target = Bukkit.getPlayer(args[0]);
                        if(frozen.contains(target.getName())) {
                            sender.sendMessage(ChatColor.GREEN + "You froze " + target.getName() + "!");
                            target.sendMessage(ChatColor.GREEN + "You were frozen by " + target.getName() + "!");
                            frozen.add(target.getName());
                            return true;
                        } else {
                            sender.sendMessage(ChatColor.GREEN + "You unfroze " + target.getName() + "!");
                            target.sendMessage(ChatColor.GREEN + "You were unfrozen by " + target.getName() + "!");
                            frozen.add(target.getName());
                            return true;
                        }
                    }
                }
            }
            return false;
        }
      
        @EventHandler (priority = EventPriority.LOWEST)
        public void onPlayerMove(PlayerMoveEvent e) {
            Utils.instance().getLogger().info("Event handled!");
            Player p = e.getPlayer();
            if(frozen.contains(p.getName())) {
                e.setTo(e.getFrom());
                Utils.instance().getLogger().info("Player frozen successfully!");
            }
            Utils.instance().getLogger().info("Player freed successfully!");
        }
    }
     
  2. Offline

    meguy26

    @_zircon_
    Well umm, I know why.

    Code:
    getCommand("freeze").setExecutor(new Freeze());
    Bukkit.getServer().getPluginManager().registerEvents(new MOTDEvent(), this);
    Bukkit.getServer().getPluginManager().registerEvents(new Freeze(), this);
    Here you create a new Freeze for both the command and the listener. Each of these is an object instance (if you don't know what that is, go learn java). The ArrayList variable in the class freeze is created for each object instance, they do not share the same list, therefore, players put in the list in the first Freeze instance will not carry over to the other instance.

    A simple fix would be to use the same instance for both things:
    Code:
    Freeze freeze = new Freeze();
    getCommand("freeze").setExecutor(freeze);
    Bukkit.getServer().getPluginManager().registerEvents(new MOTDEvent(), this);
    Bukkit.getServer().getPluginManager().registerEvents(freez, this);
    EDIT:
    Also, in the PlayerMoveEvent, instead of setting the to to the from, just cancel the event:
    Code:
    event.setCancelled(true);
     
  3. I have put the new code in the plugin, just testing it now. However, I am now working on a announcer side to it. Stopping the announcer works perfectly but when I want to start it back up, it errors with:

    The Error Log. (open)
    Code:
    [12:41:53 WARN]: Unexpected exception while parsing console command "announcer start"
    org.bukkit.command.CommandException: Unhandled exception executing command 'announcer' in plugin ZirconEssentials v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) [spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_60]
    Caused by: java.lang.IllegalArgumentException: Plugin already initialized!
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:122) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at me.zircon.zirconessentials.ZirconEssentials.<init>(ZirconEssentials.java:28) ~[?:?]
            at me.zircon.zirconessentials.commands.general.AnnouncerCommands.onCommand(AnnouncerCommands.java:48) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            ... 8 more
    Caused by: java.lang.IllegalStateException: Initial initialization
            at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:125) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:66) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at me.zircon.zirconessentials.ZirconEssentials.<init>(ZirconEssentials.java:28) ~[?:?]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_60]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_60]
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_60]
            at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_60]
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_60]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:76) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:292) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:739) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.Bukkit.reload(Bukkit.java:535) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) ~[spigot_server.jar:git-Spigot-fdc1440-53fac9f]
            ... 8 more


    EDIT: Nope. Not working. I have used
    Code:
    e.setCancelled(true);
    but it isn't cancelling the event, and I have also used
    Code:
    e.getPlayer().teleport(e.getFrom());
    but it is still not working...
     
  4. Online

    timtower Administrator Administrator Moderator

  5. I have posted the full code. The code is in different classes.

    EDIT: Sorry, didn't realise my other post had been approved already :p. Here:
    Announcer code (open)

    Code:
    package me.zircon.zirconessentials.commands.general;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    import me.zircon.zirconessentials.ZirconEssentials;
    import me.zircon.zirconessentials.miscellaneous.Utils;
    import net.md_5.bungee.api.ChatColor;
    
    public class AnnouncerCommands implements CommandExecutor {
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("announcer")) {
                if(!sender.hasPermission("zirconessentials.announcer")) {
                    sender.sendMessage(ChatColor.RED + "You do not have permission to use the announcer command!");
                    Utils.instance().getLogger().info(sender.getName() + " attempted to run the announcer command but didn't have enough permissions!");
                    return false;
                } else {
                    if(args.length == 0) {
                        sender.sendMessage(ChatColor.RED + "Usages:");
                        sender.sendMessage(ChatColor.RED + "/announcer stop");
                        return false;
                    } else if(args.length > 0) {
                        if(args[0].equalsIgnoreCase("stop")) {
                            if(!sender.hasPermission("zirconessentials.announcer.stop")) {
                                sender.sendMessage(ChatColor.RED + "You do not have permission to stop the announcer!");
                                Utils.instance().getLogger().info(sender.getName() + " attempted to run the command /announcer stop but didn't have enough permissions!");
                                return false;
                            } else {
                                Bukkit.getServer().getScheduler().cancelAllTasks();
                                sender.sendMessage(ChatColor.RED + "The announcer has been successfully stopped!");
                                Utils.instance().getLogger().info(sender.getName() + " successfully stopped the announcer!");
                                return true;
                            }
                        }
                        if(args[0].equalsIgnoreCase("start")) {
                            if(!sender.hasPermission("zirconessentials.announcer.start")) {
                                sender.sendMessage(ChatColor.RED + "You do not have permission to start the announcer!");
                                Utils.instance().getLogger().info(sender.getName() + " attempted to run the command /announcer start but didn't have enough permissions!");
                                return false;
                            } else {
                                if(Utils.instance().getServer().getScheduler().isCurrentlyRunning(0)) {
                                    sender.sendMessage(ChatColor.RED + "The announcer task is already running.");
                                    return false;
                                } else {
                                    Utils.instance().getServer().getScheduler().scheduleSyncRepeatingTask(new ZirconEssentials(), new Runnable() {
                                        public void run() {
                                            Utils.instance().getServer().broadcastMessage(ChatColor.GREEN + "This is an announcement!");
                                        }
                                    }, 20, 40);
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }
    }
    


    This code is in a separate class, and as I said before, the stop is working perfectly but the start is erroring with the error listed above.
     
  6. Online

    timtower Administrator Administrator Moderator

    @_zircon_ scheduleSyncRepeatingTask(new ZirconEssentials(), new Runnable() {
    You can't create new instances of a class that is extending JavaPlugin, pass the original instance there instead
     
  7. If you look before it it says
    Code:
    Utils.instance().
    Utils is the class that allows me to use JavaPlugin stuff inside other classes and inside Utils is the instance method, which returns
    Code:
    Bukkit.getServer().getPluginManager().getPlugin("ZirconEssentials");
    EDIT: If in the case that is the problem though, can you give me some code to replace this with. Also, as you can see in the code, I cancel the task in the stop, doesn't that mean it makes it non-existent ?
     
  8. Online

    timtower Administrator Administrator Moderator

  9. Done. Can you give me the code to be able to run the task that was already created?
     
  10. Online

    timtower Administrator Administrator Moderator

    @_zircon_ I don't have that code, I usually just toggle a boolean so it will stop running. And I store the created instance if I need it later on.
     
  11. Mmk. I have just run into another problem. How do I reload the configuration file on command? Right now, I have a command called "zereload" and it is running
    Code:
    Utils.instance().reloadConfig();
    and it is not throwing any errors in Eclipse or the Console... The only error is that it doesn't reload the config :p

    EDIT: Code for the way you do it ?
     
    Last edited: Aug 30, 2015
  12. Online

    timtower Administrator Administrator Moderator

    @_zircon_ I generally don't deal with config reloads, you only need to set the config one time, then you save, start the server and don't look at it for months, never found that worth it to make a reload command.
     
  13. It is only useful if you are constantly changing stuff (in my case, the message that the announcer uses)...
    And, I don't want to make them use /reload because Cauldron servers don't allow the use of the command and it lags the server depending on how many plugins you have so yah.. :p
     
  14. Online

    timtower Administrator Administrator Moderator

    @_zircon_ My plugins usually overwrite the config when you stop the server so /reload wouldn't work anyways with my plugins.
    Not to mention the memory leaks.
     
  15. Yeah..
    Code:
    settings.setup(this);
    That is the code the grabs and saves the config file... However, that is in the "onEnable()" method, so it only happens when either /reload is executed or when the server starts up.

    So yah, how do I do config reloads... I already have this code:
    Code:
    public void reloadConfig() {
      Utils.instance().reloadConfig();
    }
    
    That is in my SettingsManager class.

    Code:
    SettingsManager settings = SettingsManager.getInstance();
    settings.reloadConfig();
    
    That is in my ReloadConfig class where the reload command is.

    Also, before I started with the announcer, meguy26 gave me a solution to the freeze thing, but that didn't work. Any ideas ?

    EDIT by Timtower: merged posts
     
  16. Online

    timtower Administrator Administrator Moderator

    @_zircon_ I don't know about the config reloads, like said: I don't use them.
    And define "That didn't work"
     
  17. Mmk. And it just doesn't freeze me even though it says that I have been frozen/unfrozen...
     
  18. Online

    timtower Administrator Administrator Moderator

    @_zircon_ Add a debug message to see if the event is being fired.
     
  19. Don't worry. It's fixed but it is now saying I have been unfrozen when I have been frozen, this happened with my AFK so I know what is wrong. :p

    EDIT: This thread can be locked/closed now.
    EDIT by Timtower: we don't lock threads when solved.
    EDIT: Okay Timtower :p I feel scared :p
     
    Last edited: Aug 31, 2015
Thread Status:
Not open for further replies.

Share This Page