Two Problems

Discussion in 'Plugin Development' started by pigeontroll, Feb 19, 2014.

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

    pigeontroll

    setBanned() Has been fixed! Thanks! Problem 1 out of 2

    Both problems have been fixed! Thank you all!

    So I have encountered two problems in my coding. My first one is a Gamemode Inventory, where when you click a compass, it will open up an inventory that, when clicked, will change your gamemode. Now, the code for that is perfectly fine, but there is an error whenever a player interacts with anything in their hand with something else. Let me just first show.
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. if (e.getPlayer().getItemInHand().getItemMeta() == null) return;
    4. if (e.getPlayer().getItemInHand().getType() == Material.COMPASS) {
    5. if (e.getAction() == Action.RIGHT_CLICK_AIR
    6. || e.getAction() == Action.RIGHT_CLICK_BLOCK
    7. || e.getAction() == Action.LEFT_CLICK_AIR
    8. || e.getAction() == Action.LEFT_CLICK_BLOCK) {
    9. e.setCancelled(true);
    10. gminv.show(e.getPlayer());
    11. }
    12. }
    13. else return;
    14. }
    15.  
    16. }

    This works fine, but whenever I step on a pressure plate with something in my hand, I will just get a bunch of errors in the log. Does anyone know the fix?

    Second problem:
    setBanned() is Deprecated. Does anyone know the solution?
    Code:java
    1. final Player target = Bukkit.getServer().getPlayer(args[0]);
    2. if (l == 2) {
    3. target.kickPlayer(ChatColor.GOLD+"Warning> "+ChatColor.GRAY+"You have been warned for "+ChatColor.RED+msg+ChatColor.GRAY+"3/3 times. You have been temporarily banned for "+ChatColor.RED+"10 seconds"+ChatColor.GRAY+".");
    4. target.setBanned(true);
    5.  
    6. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    7. public void run() {
    8. target.setBanned(false);
    9. }
    10. }, 10 * 20);
    11. return true;
    12. }
    13.  

    This is just part of the code, but the setBanned will be crossed out.
     
  2. Offline

    MrAwellstein

    setBanned();

    Shouldnt be deprecated
    Also you should ban them before you kick them. I think it matters
     
  3. Offline

    NinjaWAffles

    @pigeontroll
    @MrAwellstein

    Incorrect, good sir. It should be deprecated. Any CB version after DEV #3007, it'll be deprecated because of the ban API that mbaxter implemented. If you would like to see the commit, click here.

    Anyway, OP, because of the new ban API, here is how you'd accomplish the ban:

    To ban a player:
    Show Spoiler
    1. Get the new BanList class from the static Bukkit class or the JavaPlugin's 'getServer()' method:
    Code:Java
    1. BanList bukkitBanList = Bukkit.getBanList(BanList.Type.NAME);
    2.  
    3. BanList serverBanList = getServer().getBanList(BanList.Type.NAME);

    Note: The parameters are either 'BanList.Type.NAME' or 'BanList.Type.IP.' If you want to ban an IP, do the IP type; if you want to ban a player by name, do the name type. Should be pretty self-explanatory.

    2. To add a person to the ban list, you call the addBan method.
    Code:Java
    1. serverBanList.addBan(player.getName(), null, null, null);

    Note: The first parameter must be filled in. It cannot be null. If you're banning a player by name, you'll put in the player's name. The second parameter is the ban reason. This parameter can be left null or, if you want a custom reason for banning, you'll put that in there. If it is left null, it'll default to "Banned by an operator." The third parameter is a Date object setting when the ban should expire. This too can be left null. If it is left null, it'll be interpreted as "forever." The last (fourth) object is the source of the ban. If it is left null, it'll default to "(Unknown)."

    3. To release a person of their ban, you'd call the pardon function.
    Code:Java
    1. serverBanList.pardon(player.getName());

    Note: It takes one parameter -- the person's name.

    End Note: If you're wanting to ban someone's IP instead of their name, just replace the player's name in 'addBan' with the player's IP, and replace the player's name in 'pardon' with the player's IP again.

    That should at least help you with the ban issue. If you want to check out more functions, click here to view the JavaDoc page.
     
  4. Offline

    Alshain01

    Code:java
    1.  
    2. ItemStack hand = e.getPlayer.getItemInHand();
    3. if (hand == null || !hand.hasItemMeta()) return;
    4.  


    I'm not certain but I think getItemInHand can be null.
     
  5. Offline

    Buizelfan2

    @pigeontroll I made a plugin (didn't upload it) that was a gm inventory but i used a command but i just made it on interaction with a compass and it works if you want to see code i could show you.
     
  6. Offline

    xTrollxDudex

    pigeontroll
    You use hasItemMeta by the way, and you should check if the item in hand is null before checking item meta. I don't see the point of checking item meta though, just remove it.
     
  7. Offline

    pigeontroll

    Thanks for all the feedback! The setBanned() now works, but I still can't squash the other bug. It says that the error (on the log) is an "EventException". I've tried changing the code to this. If you know a solution, please help!
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. ItemStack hand = e.getPlayer().getItemInHand();
    4. if (hand == null || !hand.hasItemMeta()) return;
    5. if (!(hand.getType() == Material.COMPASS)) return;
    6. if (!(e.getAction() == Action.RIGHT_CLICK_AIR)
    7. || !(e.getAction() == Action.RIGHT_CLICK_BLOCK)
    8. || !(e.getAction() == Action.LEFT_CLICK_AIR)
    9. || !(e.getAction() == Action.LEFT_CLICK_BLOCK)) return;
    10. e.setCancelled(true);
    11. gminv.show(e.getPlayer());
    12. }
     
  8. Offline

    NinjaWAffles

    Just for future reference, it's nice if you can attach the exact stack-trace from your logs. Also, if you could that here, it'd help.
     
  9. Offline

    pigeontroll

    Alright, I have the stack-trace.
    [20:54:46 ERROR]: Could not pass event PlayerInteractEvent to MenuInv v1.0.0.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:320) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:471) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at org.bukkit.craftbukkit.v1_7_R1.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:195) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g
    85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.BlockPressurePlateBinary.e(BlockPressure
    PlateBinary.java:53) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks
    ]
    at net.minecraft.server.v1_7_R1.BlockPressurePlateAbstract.a(BlockPressu
    rePlateAbstract.java:92) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022
    jnks]
    at net.minecraft.server.v1_7_R1.BlockPressurePlateAbstract.a(BlockPressu
    rePlateAbstract.java:86) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022
    jnks]
    at net.minecraft.server.v1_7_R1.Entity.I(Entity.java:756) [craftbukkit.j
    ar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.Entity.move(Entity.java:414) [craftbukki
    t.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.EntityLiving.e(EntityLiving.java:1210) [
    craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.EntityHuman.e(EntityHuman.java:1259) [cr
    aftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.EntityLiving.e(EntityLiving.java:1466) [
    craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.EntityHuman.e(EntityHuman.java:390) [cra
    ftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.EntityLiving.h(EntityLiving.java:1299) [
    craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.EntityHuman.h(EntityHuman.java:162) [cra
    ftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.EntityPlayer.i(EntityPlayer.java:240) [c
    raftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :342) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInFlying.a(SourceFile:137) [cr
    aftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInPosition.handle(SourceFile:6
    3) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    Caused by: java.lang.NullPointerException
    at me.pigeontroll.menuinv.MenuInv.onPlayerInteract(MenuInv.java:29) ~[?:
    ?]
    at sun.reflect.GeneratedMethodAccessor18.invoke(Unknown Source) ~[?:?]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_45]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_45]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:318) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-2-g85f5776-b3022jnks]
    ... 26 more
    >

    I was stepping on a pressure plate (that does nothing)
     
  10. Offline

    Buizelfan2

    pigeontroll
    For the one i have i have 2 seperate classes

    Class One (The one creating the inventory) :
    Code:java
    1. public class Menu implements Listener {
    2.  
    3. private Inventory inv;
    4. private ItemStack c, s, a;
    5.  
    6. public Menu(Plugin p) {
    7. inv = Bukkit.getServer().createInventory(null, 9, "Choose Gamemode");
    8.  
    9. c = createItem(DyeColor.GREEN, ChatColor.GREEN + "Creative Mode");
    10. s = createItem(DyeColor.YELLOW, ChatColor.YELLOW + "Survival Mode");
    11. a = createItem(DyeColor.RED, ChatColor.RED + "Adventure Mode");
    12.  
    13. inv.setItem(2, c);
    14. inv.setItem(4, s);
    15. inv.setItem(6, a);
    16.  
    17. Bukkit.getServer().getPluginManager().registerEvents(this, p);
    18. }
    19.  
    20. private ItemStack createItem(DyeColor dc, String name) {
    21. ItemStack i = new Wool(dc).toItemStack();
    22. ItemMeta im = i.getItemMeta();
    23. im.setDisplayName(name);
    24. im.setLore(Arrays.asList("Set To: " + name));
    25. i.setItemMeta(im);
    26. return i;
    27. }
    28.  
    29. public void show(Player p) {
    30. p.openInventory(inv);
    31. }
    32.  
    33. @EventHandler
    34. public void onInventoryClick(InventoryClickEvent e) {
    35. if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
    36. if (e.getCurrentItem() == null) return;
    37. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Creative Mode")) {
    38. e.setCancelled(true);
    39. e.getWhoClicked().setGameMode(GameMode.CREATIVE);
    40. e.getWhoClicked().closeInventory();
    41. }
    42. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Survival Mode")) {
    43. e.setCancelled(true);
    44. e.getWhoClicked().setGameMode(GameMode.SURVIVAL);
    45. e.getWhoClicked().closeInventory();
    46. }
    47. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Adventure Mode")) {
    48. e.setCancelled(true);
    49. e.getWhoClicked().setGameMode(GameMode.ADVENTURE);
    50. e.getWhoClicked().closeInventory();
    51. }
    52. }
    53. }


    Class Two(The one opening the inventory):
    Code:java
    1. public class MenuInv extends JavaPlugin implements Listener {
    2.  
    3. private Menu menu;
    4.  
    5. public void onEnable() {
    6. menu = new Menu(this);
    7. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    8. }
    9.  
    10. @EventHandler
    11. public void onPlayerInteract(PlayerInteractEvent event) {
    12. Player p = event.getPlayer();
    13. if (((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK) || (event.getAction() == Action.LEFT_CLICK_AIR) || (event.getAction() == Action.LEFT_CLICK_BLOCK)) && (p.getItemInHand().getType() == Material.COMPASS)) {
    14. event.setCancelled(true);
    15. menu.show(p);
    16. }
    17. }
    18. }
    19.  
     
  11. Offline

    pigeontroll

    Do you get the error when you step on something that's not that?
     
Thread Status:
Not open for further replies.

Share This Page