Enchantment table?

Discussion in 'Plugin Development' started by BeastCraft3, Nov 24, 2014.

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

    BeastCraft3

    Hello, I'm thinking to make a plugin which include /enchant which will open up an visual enchantment table with the levels: 5, 15 and 30.
    I honestly don't know how to do any of that that stuff ;P all I know is that it has something to do with gui, at least thats what am told.
    anyone know how to do this please comment ;=)
     
  2. Offline

    mrCookieSlime

  3. Offline

    BeastCraft3

    mrCookieSlime
    I didn't understand all that completly right, but did you mean something like this?
    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. if(!(sender instanceof Player)) {
    4. sender.sendMessage("Only players can use this command");
    5. return true;
    6. }
    7. if(cmd.getName().equalsIgnoreCase("enchant")) {
    8. Player p = (Player) sender;
    9. if(p.hasPermission("KeptenCustom.Enchant")) {
    10. onEnchant(null);
    11. }
    12. }
    13. return false;
    14. }
    15. @EventHandler
    16. public void onEnchant(PrepareItemEnchantEvent e) {
    17. e.getExpLevelCostsOffered()[0/1/2] = 5/15/30;
    18. }
     
  4. Offline

    mrCookieSlime

    BeastCraft3
    Nope, that is not how Bukkits Event System works.
    Just do p.openEnchanting(); and add the players uuid to an arraylist and in the PrepareItemenchantevent event check whether the list contains the uuid and if so, set the levels.
    Also the slashes are not tob be used in the actual code, it was meant for demonstrating that you need to do this for each of the values.
     
  5. Offline

    BeastCraft3

    mrCookieSlime
    What about now?
    btw, I have better experince with hashsets instead of lists so I used that instead :p
    Code:java
    1. package com.Beast.KeptenCustom;
    2.  
    3. import java.util.HashSet;
    4. import java.util.UUID;
    5.  
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.enchantment.PrepareItemEnchantEvent;
    13.  
    14. public class ItemEnchantEvent implements CommandExecutor, Listener{
    15.  
    16. HashSet<UUID> list = new HashSet<UUID>();
    17.  
    18. @Override
    19. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    20. if(!(sender instanceof Player)) {
    21. sender.sendMessage("Only players can use this command");
    22. return true;
    23. }
    24. if(cmd.getName().equalsIgnoreCase("enchant")) {
    25. Player p = (Player) sender;
    26. if(p.hasPermission("KeptenCustom.Enchant")) {
    27. p.openEnchanting(p.getLocation(), true);
    28. list.add(p.getUniqueId());
    29. } else {
    30. p.sendMessage("&cYou don't got permission to use this command!");
    31. }
    32. }
    33. return false;
    34. }
    35. @EventHandler
    36. public void onEnchant(PrepareItemEnchantEvent e) {
    37. Player p = e.getEnchanter();
    38. if(list.contains(p)) {
    39. p.sendMessage("ยง6Opening enchantment table...");
    40. e.getExpLevelCostsOffered()[0] = 5;
    41. e.getExpLevelCostsOffered()[1] = 15;
    42. e.getExpLevelCostsOffered()[2] = 30;
    43. } else {
    44. e.setCancelled(true);
    45. }
    46. }
    47.  
    48. }
    49.  
     
  6. BeastCraft3 Just saying, you have grammar issues on line 30, Should be you don't have permission not you don't got xD just saying :p
     
  7. Offline

    BeastCraft3

    TheGamesHawk2001
    shut up xD
    I'm norwegian, grammer have never really been my strong side to say it easy :p
     
  8. Offline

    mrCookieSlime

    BeastCraft3
    Should be working. However you probably also want to remove the Players UUID from the HashSet when he closes the GUI.
    Use the InventoryCloseEvent for this.
    Also, cancelling the Event is not needed.
     
  9. Offline

    BeastCraft3

    mrCookieSlime
    I'm not sure if I needed to check if the player was still in the list, so I just added it for safty :p
    Code:java
    1. @EventHandler
    2. public void onCancell(InventoryCloseEvent e) {
    3. HumanEntity p = e.getPlayer();
    4. if(list.contains(p)) {
    5. list.remove(p);
    6. }
    7. }
     
Thread Status:
Not open for further replies.

Share This Page