Only one toggle enabled

Discussion in 'Plugin Development' started by XxZHALO13Xx, Dec 18, 2014.

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

    XxZHALO13Xx

    When i do a command i have a gui open.. when they click on a item it toggles it. how do i make it so they can only have one toggled. if they try the other itll cancel it
     
  2. Offline

    AdamQpzm

  3. Offline

    Dragonphase

    Store a reference to the toggled item?

    Code:
    private ItemStack selectedItem;
    
    // When you click an item to toggle it:
    selectedItem = selectedItem == null || selectedItem != someItemStack ? someItemStack : null;
    
    // Or, you could store the index of the item, using -1 to indicate a non-toggled state:
    
    private int selectedIndex = -1;
    
    // When you click an item to toggle it:
    selectedIndex = selectedIndex == -1 || selectedIndex != someItemIndex ? someItemIndex : -1;
    
    // Use this index to indicate the index of the item in the inventory.
     
  4. Offline

    XxZHALO13Xx

    @AdamQpzm heres a class.. i tried an if statement

    Code:
    @SuppressWarnings("static-access")
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            if (!e.getInventory().getName().equalsIgnoreCase(spellTraitClass.spellTraitGUI.getName())) return;
            if (e.getCurrentItem().getItemMeta() == null) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Strong Trait")){
                e.setCancelled(true);
                if(toggledStrong.contains(p.getName())){
                    toggledStrong.remove(p.getName());
                    p.sendMessage(Core.MAIN + strongDS);
                    p.removePotionEffect(PotionEffectType.INCREASE_DAMAGE);
                    p.removePotionEffect(PotionEffectType.SLOW);
                    p.setWalkSpeed(0.2F);
                }
                else{
                    toggledStrong.add(p.getName());
                    p.sendMessage(Core.MAIN + strongEN);
                    p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 160000, 1));
                    p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 160000, 1));
                    if(traitAccel.toggledAccelerate.contains(p.getName())){
                        traitAccel.toggledAccelerate.remove(p.getName());
                        traitStrong.toggledStrong.remove(p.getName());
                        p.sendMessage(Core.MAIN + ChatColor.RED + "You may not have two spells on at once! Removing current spells...");
                        p.getActivePotionEffects().clear();
                        p.setWalkSpeed(0.2F);
               
            }
                   
                }
           
            }
                p.closeInventory();
            }
    }
    
    
    
    @AdamQpzm i get
    Code:
    [10:02:56 INFO]: XxZHALO13Xx issued server command: /spelltrait
    [10:02:58 ERROR]: Could not pass event InventoryClickEvent to MagicFight v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:294) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :1361) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInWindowClick.a(SourceFile:32)
    [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInWindowClick.handle(SourceFil
    e:10) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
    Caused by: java.lang.NullPointerException
            at me.XxZHALO13Xx.MagicFight.Spells.Player.TraitAccelerate.onInventoryCl
    ick(TraitAccelerate.java:64) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-35-gd6ac518-b3061jnks]
            ... 13 more
    >
    64 is if(traitStrongClass.toggledStrong.contains(p.getName())){

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  5. Offline

    AdamQpzm

     
  6. Offline

    XxZHALO13Xx

    @AdamQpzm i added if(traitAccel.toggledAccelerate != null){ and still get the error on 64 which is the if statement i just did
     
  7. Offline

    AdamQpzm

  8. Offline

    XxZHALO13Xx

    @AdamQpzm ok theres no error now but i dont get a message or get potion effects removed

    @AdamQpzm
    Code:
    @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            if (!e.getInventory().getName().equalsIgnoreCase(spellTraitClass.spellTraitGUI.getName())) return;
            if (e.getCurrentItem().getItemMeta() == null) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Accelerate Trait")){
                e.setCancelled(true);
                if(toggledAccelerate.contains(p.getName())){
                    toggledAccelerate.remove(p.getName());
                    p.sendMessage(Core.MAIN + accDS);
                    p.removePotionEffect(PotionEffectType.SPEED);
                    p.setWalkSpeed(0.2F);
                }
                else{
                    toggledAccelerate.add(p.getName());
                    p.sendMessage(Core.MAIN + accEN);
                    p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 160000, 1));
                    if(traitStrongClass != null){
                    if(traitStrongClass.toggledStrong != null){
                    if(traitStrongClass.toggledStrong.contains(p.getName())){
                            traitStrongClass.toggledStrong.remove(p.getName());
                            traitAccel.toggledAccelerate.remove(p.getName());
                            p.sendMessage(Core.MAIN + ChatColor.RED + "You may not have two spells on at once! Removing current spells...");
                            p.getActivePotionEffects().clear();
                            p.setWalkSpeed(0.2F);
        }
                   
                    }
                    else{
                        return;
                    }
            }
                    else{
                        return;
                    }
                   
                }
           
            }
                p.closeInventory();
            }
    }
    @Dragonphase /\

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  9. Offline

    Burnett

    @XxZHALO13Xx Reformat your code and ensure you include reference to the lists then post it here again. [syntax:java]test[/syntax] looks better. Replace ':' with '='.
     
    Last edited: Dec 18, 2014
  10. Offline

    XxZHALO13Xx

    @Burnett

    Code:java
    1.  
    2. package me.XxZHALO13Xx.MagicFight.Spells.Player;
    3.  
    4. import java.util.ArrayList;
    5. import java.util.Arrays;
    6. import java.util.List;
    7.  
    8. import me.XxZHALO13Xx.MagicFight.Core;
    9. import me.XxZHALO13Xx.MagicFight.Spells.SpellTraits;
    10.  
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.Location;
    13. import org.bukkit.Material;
    14. import org.bukkit.entity.Player;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.inventory.InventoryClickEvent;
    18. import org.bukkit.event.player.PlayerMoveEvent;
    19. import org.bukkit.inventory.ItemStack;
    20. import org.bukkit.inventory.meta.ItemMeta;
    21. import org.bukkit.potion.PotionEffect;
    22. import org.bukkit.potion.PotionEffectType;
    23.  
    24. public class TraitAccelerate implements Listener{
    25.  
    26. public static ItemStack traitAccelerate;
    27.  
    28. private String accEN = ChatColor.RED + "" + ChatColor.BOLD + "Accelerate Trait ENABLED";
    29. private String accDS = ChatColor.RED + "" + ChatColor.BOLD + "Accelerate Trait DISABLED";
    30.  
    31. private static SpellTraits spellTraitClass;
    32.  
    33. public List<String> toggledAccelerate = new ArrayList<String>();
    34.  
    35. public static TraitAccelerate traitAccel;
    36. private static TraitStrong traitStrongClass;
    37.  
    38. public static ItemStack item2(String name){
    39. ItemStack i = new ItemStack(new ItemStack(Material.FEATHER, 1));
    40. ItemMeta im = i.getItemMeta();
    41. im.setDisplayName(name);
    42. im.setLore(Arrays.asList("┬žaMakes you rapid!"));
    43. i.setItemMeta(im);
    44. return i;
    45. }
    46.  
    47. @SuppressWarnings("static-access")
    48. @EventHandler
    49. public void onInventoryClick(InventoryClickEvent e) {
    50. Player p = (Player) e.getWhoClicked();
    51. if (!e.getInventory().getName().equalsIgnoreCase(spellTraitClass.spellTraitGUI.getName())) return;
    52. if (e.getCurrentItem().getItemMeta() == null) return;
    53. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Accelerate Trait")){
    54. e.setCancelled(true);
    55. if(toggledAccelerate.contains(p.getName())){
    56. toggledAccelerate.remove(p.getName());
    57. p.sendMessage(Core.MAIN + accDS);
    58. p.removePotionEffect(PotionEffectType.SPEED);
    59. p.setWalkSpeed(0.2F);
    60. }
    61. else{
    62. toggledAccelerate.add(p.getName());
    63. p.sendMessage(Core.MAIN + accEN);
    64. p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 160000, 1));
    65. if(traitStrongClass != null){
    66. if(traitStrongClass.toggledStrong != null){
    67. if(traitStrongClass.toggledStrong.contains(p.getName())){
    68. traitStrongClass.toggledStrong.remove(p.getName());
    69. traitAccel.toggledAccelerate.remove(p.getName());
    70. p.sendMessage(Core.MAIN + ChatColor.RED + "You may not have two spells on at once! Removing current spells...");
    71. p.getActivePotionEffects().clear();
    72. p.setWalkSpeed(0.2F);
    73. }
    74.  
    75. }
    76. else{
    77. return;
    78. }
    79. }
    80. else{
    81. return;
    82. }
    83.  
    84. }
    85.  
    86. }
    87. p.closeInventory();
    88. }
    89. }
    90.  
    91.  
    92.  
    93. package me.XxZHALO13Xx.MagicFight.Spells.Player;
    94.  
    95. import java.util.ArrayList;
    96. import java.util.Arrays;
    97. import java.util.List;
    98.  
    99. import me.XxZHALO13Xx.MagicFight.Core;
    100. import me.XxZHALO13Xx.MagicFight.Spells.SpellTraits;
    101.  
    102. import org.bukkit.ChatColor;
    103. import org.bukkit.Material;
    104. import org.bukkit.entity.Player;
    105. import org.bukkit.event.EventHandler;
    106. import org.bukkit.event.Listener;
    107. import org.bukkit.event.inventory.InventoryClickEvent;
    108. import org.bukkit.inventory.ItemStack;
    109. import org.bukkit.inventory.meta.ItemMeta;
    110. import org.bukkit.potion.PotionEffect;
    111. import org.bukkit.potion.PotionEffectType;
    112.  
    113. public class TraitStrong implements Listener{
    114.  
    115. public static ItemStack traitStrongItem;
    116.  
    117. private String strongEN = ChatColor.RED + "" + ChatColor.BOLD + "Strong Trait ENABLED";
    118. private String strongDS = ChatColor.RED + "" + ChatColor.BOLD + "Strong Trait DISABLED";
    119.  
    120. private static SpellTraits spellTraitClass;
    121.  
    122. public List<String> toggledStrong = new ArrayList<String>();
    123.  
    124. public static TraitStrong traitStrong;
    125. private static TraitAccelerate traitAccel;
    126.  
    127. public static ItemStack item2(String name){
    128. ItemStack i = new ItemStack(new ItemStack(Material.BRICK, 1));
    129. ItemMeta im = i.getItemMeta();
    130. im.setDisplayName(name);
    131. im.setLore(Arrays.asList("┬žaMakes you strong! (Has side effects!)"));
    132. i.setItemMeta(im);
    133. return i;
    134. }
    135.  
    136. @SuppressWarnings("static-access")
    137. @EventHandler
    138. public void onInventoryClick(InventoryClickEvent e) {
    139. Player p = (Player) e.getWhoClicked();
    140. if (!e.getInventory().getName().equalsIgnoreCase(spellTraitClass.spellTraitGUI.getName())) return;
    141. if (e.getCurrentItem().getItemMeta() == null) return;
    142. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Strong Trait")){
    143. e.setCancelled(true);
    144. if(toggledStrong.contains(p.getName())){
    145. toggledStrong.remove(p.getName());
    146. p.sendMessage(Core.MAIN + strongDS);
    147. p.removePotionEffect(PotionEffectType.INCREASE_DAMAGE);
    148. p.removePotionEffect(PotionEffectType.SLOW);
    149. p.setWalkSpeed(0.2F);
    150. }
    151. else{
    152. toggledStrong.add(p.getName());
    153. p.sendMessage(Core.MAIN + strongEN);
    154. p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 160000, 1));
    155. p.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 160000, 1));
    156. if(traitAccel != null){
    157. if(traitAccel.toggledAccelerate != null){
    158.  
    159. if(traitAccel.toggledAccelerate.contains(p.getName())){
    160. traitAccel.toggledAccelerate.remove(p.getName());
    161. traitStrong.toggledStrong.remove(p.getName());
    162. p.sendMessage(Core.MAIN + ChatColor.RED + "You may not have two spells on at once! Removing current spells...");
    163. p.getActivePotionEffects().clear();
    164. p.setWalkSpeed(0.2F);
    165. }
    166.  
    167. }
    168. else{
    169. return;
    170. }
    171. }
    172. else{
    173. return;
    174. }
    175.  
    176. }
    177.  
    178. }
    179. p.closeInventory();
    180. }
    181. }
    182.  
    183.  
    184.  
    185. package me.XxZHALO13Xx.MagicFight.Spells;
    186.  
    187. import me.XxZHALO13Xx.MagicFight.Core;
    188. import me.XxZHALO13Xx.MagicFight.Spells.Player.FireSpell;
    189. import me.XxZHALO13Xx.MagicFight.Spells.Player.TraitAccelerate;
    190. import me.XxZHALO13Xx.MagicFight.Spells.Player.TraitHider;
    191. import me.XxZHALO13Xx.MagicFight.Spells.Player.TraitStrong;
    192.  
    193. import org.bukkit.Bukkit;
    194. import org.bukkit.ChatColor;
    195. import org.bukkit.command.Command;
    196. import org.bukkit.command.CommandExecutor;
    197. import org.bukkit.command.CommandSender;
    198. import org.bukkit.entity.Player;
    199. import org.bukkit.event.Listener;
    200. import org.bukkit.inventory.Inventory;
    201.  
    202. @SuppressWarnings("static-access")
    203. public class SpellTraits implements Listener, CommandExecutor{
    204.  
    205. public static TraitAccelerate traitAccel;
    206. public static TraitStrong traitStrong;
    207. public static TraitHider traitHider;
    208. public static Inventory spellTraitGUI = Bukkit.createInventory(null, 18, ChatColor.LIGHT_PURPLE + "Spell Traits");
    209.  
    210.  
    211. static
    212. {
    213.  
    214. traitAccel.traitAccelerate = traitAccel.item2(ChatColor.RED + "Accelerate Trait");
    215. traitStrong.traitStrongItem = traitStrong.item2(ChatColor.RED + "Strong Trait");
    216. traitHider.traitHiderItem = traitHider.item3(ChatColor.RED + "Hider Trait");
    217.  
    218. spellTraitGUI.setItem(0, traitAccel.traitAccelerate);
    219. spellTraitGUI.setItem(1, traitStrong.traitStrongItem);
    220. spellTraitGUI.setItem(2, traitHider.traitHiderItem);
    221. }
    222.  
    223.  
    224. public boolean onCommand(CommandSender sender, Command cmd, String arg2, String[] arg3) {
    225.  
    226. if(cmd.getName().equalsIgnoreCase("spelltrait")){
    227. if(sender instanceof Player){
    228. Player p = (Player) sender;
    229. p.sendMessage(Core.MAIN + ChatColor.GOLD + "Opening Spell-Trait...");
    230. p.openInventory(spellTraitGUI);
    231. }
    232. else{
    233. sender.sendMessage("Error");
    234. }
    235. }
    236. return false;
    237. }
    238.  
    239.  
    240.  
    241. }
    242.  
    243.  
    244.  


    @Burnett could i make another class to where if one is toggled the other gets untoggled? i think it would be easier so i can add other traits and things and just see if they are in two then remove them

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  11. Offline

    Dragonphase

    @XxZHALO13Xx

    Please read up on Java and OOP. Your use of statics is wrong and I don't have the energy to explain why in terms simpler than "You shouldn't use static to retrieve mutable data from other classes."

    Your IDE is even warning you about static access; don't ignore them.

    For more information on Java and OOP, see my thread here.
     
    Burnett and teej107 like this.
Thread Status:
Not open for further replies.

Share This Page