Hello, I am working on a god command for fun and when I debug, it doesn't even send the beginning message. I have no clue what I am doing wrong and there are no console errors regarding this command. I have registered the event, the command, and put it in the plugin.yml. Part of the /god code: Code: package me.mehboss.rockclasses; 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.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.plugin.java.JavaPlugin; public class God extends JavaPlugin implements Listener, CommandExecutor { public static ArrayList<String> inGod = new ArrayList<String>(); public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { Player s = (Player) sender; if (commandLabel.equalsIgnoreCase("god")) { if (args.length == 0) { if (!inGod.contains(s.getName())) { inGod.add(s.getName()); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6God mode &cenabled&6!")); The event in the /god part: Code: @EventHandler public void onDamage(EntityDamageByEntityEvent e) { if (inGod.contains(e.getEntity().getName())) { e.setCancelled(true); } } } Main Class Code: Code: package me.mehboss.rockclasses; import org.bukkit.Bukkit; import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; public class RockEnable extends JavaPlugin implements Listener { public void onEnable() { getCommand("storm").setExecutor(new Weather()); getCommand("sun").setExecutor(new Weather()); getCommand("day").setExecutor(new Time()); getCommand("night").setExecutor(new Time()); getCommand("fly").setExecutor(new Fly()); getCommand("creative").setExecutor(new Gamemode()); getCommand("survival").setExecutor(new Gamemode()); getCommand("adventure").setExecutor(new Gamemode()); getCommand("spectator").setExecutor(new Gamemode()); getCommand("god").setExecutor(new God()); getCommand("me").setExecutor(new MeCommand()); getCommand("heal").setExecutor(new Heal()); registerConfig(); Bukkit.getPluginManager().registerEvents(this, new God()); } private void registerConfig() { getConfig().options().copyDefaults(true); saveConfig(); getLogger(); } }
Only create one instance of GameMode, and register all commands towarsds that one instance. Better yet, Use aliases instead. Same goes for these two. Don't blindly cast sender to a player. Add an instanceof check first. Use command.getName() instead of commandlabel.
Like this? Code: public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { Player s = (Player) sender; if (cmd.getName().equalsIgnoreCase("god")) { if (s instanceof Player) { if (args.length == 0) { if (!inGod.contains(s.getName())) { inGod.add(s.getName()); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6God mode &cenabled&6!")); } else if (inGod.contains(s.getName())) { inGod.remove(s.getName()); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6God mode &cdisabled&6!")); } I don't see how this would fix it as only my gamemode commands and /fly work.. the /god command doesn't, it shows no errors in console and does nothing in game. UPDATE: Here is the whole thing, if it helps. Code: package me.mehboss.rockclasses; 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.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.plugin.java.JavaPlugin; public class God extends JavaPlugin implements Listener, CommandExecutor { public ArrayList<String> inGod = new ArrayList<String>(); public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { Player s = (Player) sender; if (cmd.getName().equalsIgnoreCase("god")) { if (s instanceof Player) { if (args.length == 0) { if (!inGod.contains(s.getName())) { inGod.add(s.getName()); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6God mode &cenabled&6!")); } else if (inGod.contains(s.getName())) { inGod.remove(s.getName()); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6God mode &cdisabled&6!")); } } else { switch (args.length) { case 1: Player p = Bukkit.getPlayer(args[0]); if (!inGod.contains(p.getName())) { inGod.add(p.getName()); p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Your god mode has been &aenabled&6 by!&6 " + s)); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6You &aenabled " + p + "&6's god mode!")); } else if (inGod.contains(p.getName())) { inGod.remove(p.getName()); p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Your god mode has been &cdisabled &6by!&6 " + s)); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6You &cdisabled " + p + "&6's god mode!")); } } } } } return false; } @EventHandler public void onDamage(EntityDamageByEntityEvent e) { if (inGod.contains(e.getEntity().getName())) { e.setCancelled(true); } } }
Unless "God" is the main class(Which is isn't, since RockEnabled seems to be the one with the onEnable), you need to remove this bit. There can be only one class that extends Java Plugin.
@mehboss Instead of doing this complicated thing with a List, why not just use Bukkit's methods? Code:java player.setInvulnerable(true);
tried this just now: Code: s.setInvulnerable(true); Doesn't work it underlines it with the following error: "The method setInvulnerable(boolean) is undefined for the type Player" kk Fixed.. problem still not fixed. The command /god, still does not work. Still does nothing. @bwfcwalshy @AlvinB @Zombie_Striker thank you guys
There reason you are getting this error message is because you switched the objects. The first value is the listener, the class that has the events. The second value is the main class, which would be the on with the onEnable. To fix this, swap those two objects, remove the "extends JavaPlugin" from the GodCommand class, and also remove the "implements listener" from the Rock Enabled class (remove the listener because that class does not have any events in it).
@Zombie_Striker Yup, was just in a rush Hello @Zombie_Striker, you are probably familiar with this god command I am working on lol... the event is not working when I am in god mode, zombies and players can still damage me.. This one doesn't say anything at all. MAIN CODE: Code: package me.mehboss.rockclasses; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; public class RockEnable extends JavaPlugin { public void onEnable() { getCommand("storm").setExecutor(new Weather()); getCommand("sun").setExecutor(new Weather()); getCommand("day").setExecutor(new Time()); getCommand("night").setExecutor(new Time()); getCommand("fly").setExecutor(new Fly()); getCommand("creative").setExecutor(new Gamemode()); getCommand("survival").setExecutor(new Gamemode()); getCommand("adventure").setExecutor(new Gamemode()); getCommand("spectator").setExecutor(new Gamemode()); getCommand("god").setExecutor(new God()); getCommand("me").setExecutor(new MeCommand()); getCommand("heal").setExecutor(new Heal()); registerConfig(); Bukkit.getPluginManager().registerEvents(new God(), this); } private void registerConfig() { getConfig().options().copyDefaults(true); saveConfig(); getLogger(); } } GOD CODE: Code: package me.mehboss.rockclasses; 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.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent.DamageCause; public class God implements CommandExecutor, Listener { public ArrayList<String> inGod = new ArrayList<String>(); public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { if (cmd.getName().equalsIgnoreCase("god")) { if (sender instanceof Player) { Player s = (Player) sender; if (args.length == 0) { if (!inGod.contains(s.getName())) { inGod.add(s.getName()); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6God mode &cenabled&6!")); } else if (inGod.contains(s.getName())) { inGod.remove(s.getName()); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6God mode &cdisabled&6!")); } } else { switch (args.length) { case 1: Player p = Bukkit.getPlayer(args[0]); if (!inGod.contains(p.getName())) { inGod.add(p.getName()); p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Your god mode has been &aenabled&6 by!&6 " + s)); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6You &aenabled " + p + "&6's god mode!")); } else if (inGod.contains(p.getName())) { inGod.remove(p.getName()); p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Your god mode has been &cdisabled &6by!&6 " + s)); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6You &cdisabled " + p + "&6's god mode!")); } } } } } return false; } @EventHandler public void onDamage(EntityDamageByEntityEvent e) { if (e.getEntity() instanceof Player) { if (e.getCause() == DamageCause.ENTITY_ATTACK) { if (inGod.contains(e.getEntity().getName())) { e.setCancelled(true); } } } } } UPDATE: Also unrelated to this, my heal code doesn't work either. I have learned from my other extends listener mistakes and I have no clue what I did wrong here.. If you could not spoon feed me, but lead me in the right direction, much appreciated. I do want to figure this out with the least amount of help. ~ thanks. Heal Code: Code: package me.mehboss.rockclasses; 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; public class Heal implements CommandExecutor { public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { if (cmd.getName().equalsIgnoreCase("heal")) { if (sender instanceof Player) { Player s = (Player) sender; if (args.length == 0) { s.setHealth(20.0); s.setFireTicks(0); s.setFoodLevel(20); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6You have been healed!")); } else { switch (args.length) { case 1: Player p = Bukkit.getPlayer(args[0]); if (p == null) { sender.sendMessage("Player not found!"); return false; } if (p != null) { s.setHealth(20.0); s.setFireTicks(0); s.setFoodLevel(20); p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6You have been healed by&6 " + sender)); sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6You have healed&6" + p)); } } } } } return false; } }
These two lines are the problem. See how you are creating two instances of "God"? By creating two instance for the same class, you now have two objects that manage events and commands, but neither object interacts with the other. This means the commands have nothing to do with the events, which is a problem. Let me explain why this is the case. When you create two instances of god, you also create two instances of all the fields (Meaning the "inGod" List). That means there are two separate lists that do not interact with eachother. When the commands put a name into the list, the other one that interacts with the event is empty. When the event triggers, it will see its list is empty. The command's list may have the player's name in it, but that is not the list that will be checked. How to fix this: Make one instance of god first, and register that one instance for both. Here's an example: Code: God god = new God(); ...registerEvents(god, this) ... ...setExecutor(god)
@mehboss Just a question: Why are you using switch statements when an "else" would work... You're setting the health, fireTicks, etc of the sender instead of the targeted player. Also, make sure to return true;
@mehboss If args could go up to, like, 3-4, then a switch would be useful. But for only checking one, you're doing it complex for no reason ;p
What about now? Code: package me.mehboss.rockclasses; 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; public class Heal implements CommandExecutor { public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { if (cmd.getName().equalsIgnoreCase("heal")) { if (sender instanceof Player) { Player s = (Player) sender; if (args.length == 0) { s.setHealth(20.0); s.setFireTicks(0); s.setFoodLevel(20); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6You have been healed!")); } else if (args.length == 1) { Player p = Bukkit.getPlayer(args[0]); if (p == null) { sender.sendMessage("Player not found!"); return false; } else { p.setHealth(20.0); p.setFireTicks(0); p.setFoodLevel(20); p.sendMessage( ChatColor.translateAlternateColorCodes('&', "&6You have been healed by&6 " + sender)); sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6You have healed&6" + p)); return true; } } } } return false; } }
@mehboss Looks fine to me, just make sure you return true when the sender heals themselves. Maybe check for permissions too, that may happen in plugin.yml and I don't know about it. ;p
@Zombie_Striker @bwfcwalshy God command is not doing anything again. :L I did what you said Zomb MAIN CODE: PHP: package me.mehboss.rockclasses;import org.bukkit.Bukkit;import org.bukkit.plugin.java.JavaPlugin;public class RockEnable extends JavaPlugin { public void onEnable() { getCommand("storm").setExecutor(new Weather()); getCommand("sun").setExecutor(new Weather()); getCommand("day").setExecutor(new Time()); getCommand("night").setExecutor(new Time()); getCommand("fly").setExecutor(new Fly()); getCommand("creative").setExecutor(new Gamemode()); getCommand("survival").setExecutor(new Gamemode()); getCommand("adventure").setExecutor(new Gamemode()); getCommand("spectator").setExecutor(new Gamemode()); getCommand("me").setExecutor(new MeCommand()); getCommand("heal").setExecutor(new Heal()); registerConfig(); God god = new God(); getCommand("god").setExecutor(god); Bukkit.getPluginManager().registerEvents(god, this); } private void registerConfig() { getConfig().options().copyDefaults(true); saveConfig(); getLogger(); }} GOD CODE: PHP: package me.mehboss.rockclasses;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.Listener;import org.bukkit.event.entity.EntityDamageByEntityEvent;import org.bukkit.event.entity.EntityDamageEvent.DamageCause;public class God implements CommandExecutor, Listener { public ArrayList<String> inGod = new ArrayList<String>(); public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { if (cmd.getName().equalsIgnoreCase("god")) { if (sender instanceof Player) { Player s = (Player) sender; if (args.length == 0) { if (!inGod.contains(s.getName())) { inGod.add(s.getName()); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6God mode &cenabled&6!")); } else if (inGod.contains(s.getName())) { inGod.remove(s.getName()); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6God mode &cdisabled&6!")); } } else { switch (args.length) { case 1: Player p = Bukkit.getPlayer(args[0]); if (!inGod.contains(p.getName())) { inGod.add(p.getName()); p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Your god mode has been &aenabled&6 by!&6 " + s)); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6You &aenabled " + p + "&6's god mode!")); } else if (inGod.contains(p.getName())) { inGod.remove(p.getName()); p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Your god mode has been &cdisabled &6by!&6 " + s)); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6You &cdisabled " + p + "&6's god mode!")); } } } } } return false; } @EventHandler public void onDamage(EntityDamageByEntityEvent e) { if (e.getEntity() instanceof Player) { if (e.getCause() == DamageCause.ENTITY_ATTACK) { if (inGod.contains(e.getEntity().getName())) { e.setCancelled(true); } } } }} Also my heal command is working yet. CODE: PHP: package me.mehboss.rockclasses;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;public class Heal implements CommandExecutor { public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { if (cmd.getName().equalsIgnoreCase("heal")) { if (sender instanceof Player) { Player s = (Player) sender; if (args.length == 0) { s.setHealth(20.0); s.setFireTicks(0); s.setFoodLevel(20); s.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6You have been healed!")); return true; } else if (args.length == 1) { Player p = Bukkit.getPlayer(args[0]); if (p == null) { sender.sendMessage("Player not found!"); return false; } else { p.setHealth(20.0); p.setFireTicks(0); p.setFoodLevel(20); p.sendMessage( ChatColor.translateAlternateColorCodes('&', "&6You have been healed by&6 " + sender)); sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6You have healed&6" + p)); return true; } } } } return false; }} If this helps here is my plugin.yml Code: name: RockCore version: 1.0 main: me.mehboss.rockclasses.RockEnable author: mehboss commands: storm: description: Toggle thundering and rain! sun: description: Clear any rain. day: description: Change the time to day. night: description: Change the time to night. fly: description: Toggle flight mode! heal: description: Heal yourself or another player! god: description: Toggle god mode. me: description: Role play command! creative: description: Change gamemode to creative! spectator: description: Change gamemode to spectator! adventure: description: Change gamemode to adventure! survival: description: Change gamemode to survival!
@mehboss The error is being caused by the Me command. This is saying the problem is that there is a second class that extends Java Plugin. Mots likely, you let the class extend JavaPlugin. Remove that bit to fix this problem. [edit] If it does not extend JavaPlugin, post the "meCommand"class.
**fixed! Anything on the /god or /heal? UPDATE: Oh wow, that fixed the /heal and /god command! I have another question, probably something I missed. Only the damage by entity is working and not the fall or the other ones, meaning I can't get attacked but can still take fall damage and lava damage, ect. EVENT: PHP: @EventHandler public void onDamage(EntityDamageByEntityEvent e) { if (e.getEntity() instanceof Player) { if (inGod.contains(e.getEntity().getName())) { if (e.getCause() == DamageCause.ENTITY_ATTACK) { e.setCancelled(true); } if (e.getCause() == DamageCause.FALL) { e.setCancelled(true); } if (e.getCause() == DamageCause.FIRE) { e.setCancelled(true); } if (e.getCause() == DamageCause.FIRE_TICK) { e.setCancelled(true); } if (e.getCause() == DamageCause.POISON) { e.setCancelled(true); } if (e.getCause() == DamageCause.PROJECTILE) { e.setCancelled(true); } if (e.getCause() == DamageCause.FALLING_BLOCK) { e.setCancelled(true); } if (e.getCause() == DamageCause.ENTITY_EXPLOSION) { e.setCancelled(true); } if (e.getCause() == DamageCause.BLOCK_EXPLOSION) { e.setCancelled(true); } if (e.getCause() == DamageCause.CONTACT) { e.setCancelled(true); } if (e.getCause() == DamageCause.DROWNING) { e.setCancelled(true); } if (e.getCause() == DamageCause.LAVA) { e.setCancelled(true); } if (e.getCause() == DamageCause.LIGHTNING) { e.setCancelled(true); } if (e.getCause() == DamageCause.MAGIC) { e.setCancelled(true); } if (e.getCause() == DamageCause.MELTING) { e.setCancelled(true); } if (e.getCause() == DamageCause.STARVATION) { e.setCancelled(true); } if (e.getCause() == DamageCause.SUFFOCATION) { e.setCancelled(true); } if (e.getCause() == DamageCause.THORNS) { e.setCancelled(true); } if (e.getCause() == DamageCause.WITHER) { e.setCancelled(true); } } } }} lol is it EntityDamageEvent ? welp, yup it is..
@mehboss Instead of creating a new instance of Gamemode for each class, create 1 instance of it and call that for each command like you did with your god command