Need help with my code

Discussion in 'Plugin Development' started by ArmandPeanuts, Apr 13, 2014.

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

    ArmandPeanuts

    package ArmandPeanuts.TestPlugin;

    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;

    public class Commands implements Listener {

    private static final ItemStack Null = null;
    Plugin plugin ;
    public Commands(TestPlugin testPlugin) {
    this.plugin = testPlugin;
    }

    @SuppressWarnings("null")
    @EventHandler (priority = EventPriority.LOWEST)
    public void onPlayerInteract(PlayerInteractEvent e) {

    Action eAction = e.getAction();

    if (eAction == Action.RIGHT_CLICK_AIR || eAction == Action.RIGHT_CLICK_BLOCK ) {

    PlayerInteractEvent event = null;
    if ((event.getItem() != Null) && (event.getItem().getType() == Material.GOLDEN_CARROT)){
    Player player = null;
    player.performCommand("/bending choose fire");
    //do something

    }
    }
    }}



    In fact I wanted to create a plugin what will link a command to a specific object.

    I think the problem is with Material.GOLDEN_CARROT. Cuz its not in the bukkit reference and i dont find it. But there is another problem somemwhere but i'm not able to find it. And sorry for the spaces they are not there.

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

    Bionicrm

    Tip: Use the code styling provided in the boxes like so:
    Code:java
    1. if (me.isTired()) {
    2. System.out.println("Good night");
    3. } else {
    4. me.jog();
    5. }

    Also, what's the question/problem?
     
  3. Offline

    TigerHix

    Code:java
    1. @EventHandler (priority = EventPriority.LOWEST)
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3.  
    4. Action eAction = e.getAction();
    5.  
    6. if (eAction == Action.RIGHT_CLICK_AIR || eAction == Action.RIGHT_CLICK_BLOCK ) {
    7.  
    8. PlayerInteractEvent event = null; // <-- WHAT!?
    9. if ((event.getItem() != Null) && (event.getItem().getType() == Material.GOLDEN_CARROT)){ // Why it's Null not null!?
    10. Player player = null; // <-- WHATTTT!?
    11. player.performCommand("/bending choose fire"); // <-- How can you performCommand on null!??
    12. //do something <-- Have you pasted Wiki's example here..
    13.  
    14. }
    15. }


    To fix:

    Code:java
    1. @EventHandler (priority = EventPriority.LOWEST)
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3.  
    4. Action eAction = e.getAction();
    5.  
    6. if (eAction == Action.RIGHT_CLICK_AIR || eAction == Action.RIGHT_CLICK_BLOCK ) {
    7.  
    8. // PlayerInteractEvent event = null; Remove this line, this is redundant. Just use the perimeter 'e' in your method (onPlayerInteract(PlayerInteractEvent e))
    9. if ((e.getItem() != null) && (e.getItem().getType() == Material.GOLDEN_CARROT)){ // So we directly use the 'e' variable, of course change Null to null
    10. Player player = e.getPlayer(); // We get the player from a function of the PlayerInteractEvent called getPlayer(), obviously, it gets the player who interacts for you.
    11. player.performCommand("/bending choose fire"); // And then the player could perform some command. Great!
    12.  
    13. }
    14. }
     
  4. Offline

    penguin_cjw

    for some reason 'i have inefficient privileges to post here' when making a topic so can i just ask for some help with my code here? seeing both me and ArmandPeanuts are both in the same situation? My code is about setting up an arena. I'm trying to figure out how to delete a arena from the config and be able to load all the arenas from the config every time the server restarts or reloads. here it is:
    Code:java
    1. import org.bukkit.Bukkit;
    2. import org.bukkit.Location;
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.inventory.ItemStack;
    5. import java.util.ArrayList;
    6. import java.util.HashMap;
    7. import java.util.List;
    8. import java.util.Map;
    9.  
    10. public class ArenaManager {
    11.  
    12. //save where the player was at
    13. public Map<String, Location> locs = new HashMap<String, Location>();
    14. //the class instance
    15. private static ArenaManager am;
    16. //a few other fields
    17. Map<String, ItemStack[]> inv = new HashMap<String, ItemStack[]>();
    18. Map<String, ItemStack[]> armor = new HashMap<String, ItemStack[]>();
    19. //list of arenas
    20. List<Arena> arenas = new ArrayList<Arena>();
    21. int arenaSize = 0;
    22.  
    23. public ArenaManager() {
    24. }
    25.  
    26. //we want to get an instance of the manager to work with it statically
    27. public static ArenaManager getManager() {
    28. if(am == null)
    29. am = new ArenaManager();
    30.  
    31. return am; //NOT THREAD SAFE!
    32. }
    33.  
    34. //get an Arena object from the list
    35. public Arena getArena(int i) {//finding arena
    36. for(Arena a : arenas) {
    37. if(a.getId() == i) {//arena found
    38. return a;//puts it into arenas ^^ the list of arenas
    39. }
    40. }
    41. return null;
    42. }
    43.  
    44. //add players to the arena, save their inventory
    45. public void addPlayer(Player p, int i) {
    46. Arena a = getArena(i);//get the arena you want to join
    47. if(a == null) {//null as in there are no arenas on the list
    48. p.sendMessage("Arena not found!");//sends this msg to player
    49. return;
    50. }
    51.  
    52. a.getPlayers().add(p.getName());//add them to the arena list of players
    53. inv.put(p.getName(), p.getInventory().getContents());//save inventory to hashmap
    54. armor.put(p.getName(), p.getInventory().getArmorContents());//save armor to hashmap
    55.  
    56. p.getInventory().setArmorContents(null);//clearing armor
    57. p.getInventory().clear();//clearing inventory
    58.  
    59. locs.put(p.getName(), p.getLocation());//locs is the save of his'/her's location where the player was before teleported
    60. p.teleport(a.spawn);//teleport to the arena spawn
    61. }
    62.  
    63. //remove players
    64. public void removePlayer(Player p) {
    65. Arena a = null;//make an arena
    66. for(Arena arena : arenas) {//looks in arena list
    67. if(arena.getPlayers().contains(p.getName())) {//searches if the attempt of player removal is in the arena in the first place
    68. a = arena;//if the arena has the player, the arena field would be the arena containing the player
    69. }//arena is now called 'a'
    70. //if none is found, the arena will be null
    71. }
    72. if(a == null || !a.getPlayers().contains(p.getName())) {//'||' means 'or' and '!' before a object, in this case, 'a.getPlayers().contains(p.getName())' means it is NOT what you are asking. Like saying you want a bird and you DONT want a bird the ! is the NOT or DONT
    73. p.sendMessage("Player is not in the specified arena!");//sends a reply to why the command the player sent didnt work (sends msg to player)
    74. return;
    75. }
    76.  
    77. a.getPlayers().remove(p.getName());//remove from arena
    78.  
    79. p.getInventory().clear();//clears the inventory you had in the arena
    80. p.getInventory().setArmorContents(null);//clears armor you had in the arena
    81.  
    82. p.getInventory().setContents(inv.get(p.getName()));//looks for inv in the hashmap which belongs to specified player and sets it back as their inventory
    83. p.getInventory().setArmorContents(armor.get(p.getName()));//looks for armor in the hashmap which belongs to specified player and sets it back as their armor contents
    84.  
    85. inv.remove(p.getName());//removes inv entry from hashmap
    86. armor.remove(p.getName());//removes armor entry from hashmap
    87. p.teleport(locs.get(p.getName()));//teleports player back to where they were before joining the arena which was saved as locs in the hashmap
    88. locs.remove(p.getName());//removes locs entry from hashmap
    89.  
    90. p.setFireTicks(0);//if the player was on fire they would have the fire taken away
    91. }
    92.  
    93. //create arena
    94. public Arena createArena(Location l) {//creates a arena at 'Location l'
    95. int num = arenaSize + 1;//the size is saved as 'arenaSize' with 1 added on
    96. arenaSize++;//this is arenaSize +1
    97.  
    98. Arena a = new Arena(l, num);//creates a new arena
    99. arenas.add(a);//adds the new arena 'a' to the arena list 'arenas'
    100.  
    101. plugin.getConfig().set("Arenas." + num, serializeLoc(l));//sets what arena is made, the number it is and what it's location is
    102. List<Integer> list = plugin.getConfig().getIntegerList("Arenas.Arenas");//adds 'Arenas.Arenas' to arenas list
    103. list.add(num);//adds the list to a number that is then able to be added to a config file
    104. plugin.getConfig().set("Arenas.Arenas", list);//adds the list to the config file
    105. plugin.saveConfig();//saves the config fil.e this config will hold record of the arena just made
    106.  
    107. return a;// to say that a arena has been added
    108. }
    109.  
    110. public boolean isInGame(Player p) {//checks if player is in the arena
    111. for(Arena a :arenas) {//looks in the arenas list
    112. if(a.getPlayers().contains(p.getName()))//tests if the player is contains in any of the arena, if is then it returns true
    113. return true;
    114. }
    115. return false;//if not then it returns false
    116. }
    117.  
    118. public String serializeLoc(Location l) {//'Location 1' is identified below v
    119. return l.getWorld().getName()+","+l.getBlockX()+","+l.getBlockY()+","+l.getBlockZ();//this defines the location including what world and X, Y and Z coords
    120. }
    121. public Location deserializeLoc(String s) {//adds properties together
    122. String[] st = s.split(",");//resplits defined by ','
    123. return new Location(Bukkit.getWorld(st[0]), Integer.parseInt(st[1]), Integer.parseInt(st[2]), Integer.parseInt(st[3]));//resplits into a in-game teleportable location
    124. }
    125.  
    126. //remove arena
    127. public Arena deleteArena(Location l) {//deletes the arena at 'Location l'
    128. int num = arenaSize + 1;//the size is saved as 'arenaSize' with 1 added on
    129. arenaSize++;//this is arenaSize +1
    130.  
    131. arenas.remove(a);//remove the arena in the arena list 'arenas'
    132.  
    133. plugin.getConfig().clear("Arenas." + num, serializeLoc(l));//sets what arena is made, the number it is and what it's location is
    134. List<Integer> list = plugin.getConfig().getIntegerList("Arenas.Arenas");//adds 'Arenas.Arenas' to arenas list
    135. list.remove(num);//adds the list to a number that is then able to be added to a config file
    136. plugin.getConfig().set("Arenas.Arenas", list);//adds the list to the config file
    137. plugin.saveConfig();//saves the config fil.e this config will hold record of the arena just made
    138.  
    139. return a;// to say that a arena has been added
    140. }
    141.  
    142. }


    I added alot of notes to myself as i went along so that i knew what i was doing. you can ignore them or look at them if they help. just please try to tell me what i could do to delete an arena (its the last section at the bottom) and the adding of arena is a few sections up. also just say how the loading could work and ill be as happy as can be :D also could you say how i would write the commands for loading and deleting arenas this is what i have so far:
    Code:java
    1. import org.bukkit.Material;
    2. import org.bukkit.command.Command;
    3. import org.bukkit.command.CommandSender;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.inventory.ItemStack;
    6. import org.bukkit.plugin.PluginManager;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class ArenaCommands extends JavaPlugin {
    10.  
    11. @Override
    12. public void onEnable() {
    13. getLogger().info("KingsForces has been enabled");
    14. }
    15.  
    16. @Override
    17. public void onDisable() {
    18.  
    19. }
    20.  
    21. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    22.  
    23. if (cmd.getName().equalsIgnoreCase("kf arenacreate") && sender instanceof Player) {
    24.  
    25. Player p = (Player) sender;
    26.  
    27. if(p.hasPermission("kf.arenacreate")) {
    28. ArenaManager.getManager().createArena(p.getLocation());//sets the arena spawn to the current player location
    29.  
    30. return true;
    31.  
    32. }
    33.  
    34. return false;
    35.  
    36. if (cmd.getName().equalsIgnoreCase("kf arenajoin 1") && sender instanceof Player) {
    37.  
    38. Player p = (Player) sender;
    39.  
    40. if(p.hasPermission("kf.arenajoin.1")) {
    41. int num = 0;
    42. try{
    43. num = Integer.parseInt(args[0]);
    44. p.sendMessage("Invalid arena ID");
    45. }
    46. ArenaManager.getManager().addPlayer(p, num);
    47.  
    48. return true;
    49.  
    50. }
    51.  
    52. return false;
    53.  
    54. if (cmd.getName().equalsIgnoreCase("kf arenajoin 2") && sender instanceof Player) {
    55.  
    56. Player p = (Player) sender;
    57.  
    58. if(p.hasPermission("kf.arenajoin.2")) {
    59. int num = 1;
    60. try{
    61. num = Integer.parseInt(args[0]);
    62. p.sendMessage("Invalid arena ID");
    63. }
    64. ArenaManager.getManager().addPlayer(p, num);
    65.  
    66. return true;
    67.  
    68. }
    69.  
    70. return false;
    71.  
    72. if (cmd.getName().equalsIgnoreCase("kf arenajoin 3") && sender instanceof Player) {
    73.  
    74. Player p = (Player) sender;
    75.  
    76. if(p.hasPermission("kf.arenajoin.3")) {
    77. int num = 2;
    78. try{
    79. num = Integer.parseInt(args[0]);
    80. p.sendMessage("Invalid arena ID");
    81. }
    82. ArenaManager.getManager().addPlayer(p, num);
    83.  
    84. return true;
    85.  
    86. }
    87.  
    88. return false;
    89.  
    90. if (cmd.getName().equalsIgnoreCase("kf arenaleave 0") && sender instanceof Player) {
    91.  
    92. Player p = (Player) sender;
    93.  
    94. if(p.hasPermission("kf.arenaleave")) {
    95. ArenaManager.getManager().removePlayer(p);
    96.  
    97. return true;
    98.  
    99. }
    100.  
    101. return false;
    102. }
    103.  
    104. }


    P.s. there are 3 arenas i am making so that is why there is 3 join arena commands (for each one) hopefully i did those right too :D :S
     
  5. Offline

    ArmandPeanuts

    Thanks alot, it's working ! But how can I make that the golden carrot will disappear from the player's enventory ?
     
  6. Offline

    penguin_cjw

    can any one help with my code then?
     
  7. Offline

    penguin_cjw

    bump

    haHA! I have perms to post a thread ill post my query there

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page