Importing Key files

Discussion in 'Plugin Development' started by Flyincreeper24_7, Jun 23, 2013.

Thread Status:
Not open for further replies.
  1. Hello, my plugin requires a client side mod so I got sput and added it to a build bath. The only problem is I don't really know how to implement it! Someone help
    Code:java
    1. package whoppercraft;
    2.  
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.EventPriority;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.entity.EntityDamageEvent;
    12. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.event.player.PlayerJoinEvent;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.PlayerInventory;
    17. import org.getspout.spoutapi.keyboard;
    18.  
    19. public class Hat implements Listener {
    20. private static Main pl;
    21. @SuppressWarnings("unused")
    22. private int keyCode;
    23. boolean spoutplugin = false;
    24.  
    25. public Hat(Main instance) {
    26. pl = instance;
    27. }
    28.  
    29. public Hat() {
    30. }
    31.  
    32. @EventHandler(priority = EventPriority.HIGH)
    33. public void onPlayerInteract(PlayerInteractEvent event) {
    34. Player player = event.getPlayer();
    35. Action action = event.getAction();
    36. this.keyCode = 65;
    37. if (action == Action.LEFT_CLICK_AIR) {
    38. setHat(player);
    39.  
    40. }
    41. }
    42.  
    43. public static void setHat(Player player) {
    44. ItemStack hand = player.getItemInHand().clone();
    45. PlayerInventory inv = player.getInventory();
    46. ItemStack head = inv.getHelmet();
    47. hand.setAmount(1);
    48. inv.remove(hand);
    49. inv.setHelmet(hand);
    50. inv.setItemInHand(head);
    51. player.sendMessage(ChatColor.AQUA + "Enjoy your hat!");
    52. }
    53.  
    54. public static void setHat(Player player, ItemStack item) {
    55. PlayerInventory inv = player.getInventory();
    56. inv.setHelmet(item);
    57. player.sendMessage(ChatColor.AQUA + "Enjoy your hat!");
    58. }
    59.  
    60. @EventHandler
    61. public void onPlayerJoin(PlayerJoinEvent event) {
    62. setHat(event.getPlayer(), new ItemStack(pl.getConfig().getInt("startID")));
    63. }
    64. @EventHandler
    65. public void getHelmet(EntityDamageEvent event){
    66. DamageCause drowning = DamageCause.DROWNING;
    67. DamageCause cause = event.getCause();
    68.  
    69. if(cause == drowning && event.getEntity() instanceof Player){
    70. Player player = (Player) event.getEntity();
    71. ItemStack helmet = player.getInventory().getHelmet();
    72. if(helmet.getType().equals(Material.GLASS));
    73. event.setCancelled(true);
    74. }
    75. }
    76. }

    Please help!
     
  2. Offline

    Nitnelave

    Well, we don't know what you want to implement, so we're going to have some trouble helping you with it....
     
  3. I want to implemet this package org.getspout.spoutapi.keyboard; but I don't know how and I tried importing it but of course that was a fail

    I'm trying to implement org.getspout.spoutapi.keyboard; I don't really know how to though. Please Help!

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

    Nitnelave

    I'm not sure you understand what you're doing... You don't implement a package. You implement an interface, you inherit a class, but you don't implement a package! Do you know how to code in java very well, or are you just learning (it's ok if you're learning, we're here to help)?
     
  5. Well, I know a lot for my age. I mean I'm only 13. So Yes, I am still learning

    And I appreciate you helping me! :)

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

    Nitnelave

    Ok, let's try to do this together: What are you trying to do? What is your plugin supposed to do once finished? Why do you need a client mod? Why do you need spout?

    Wouldn't it be better to start with a simpler plugin to learn and get some experience before trying to do something like that?
     
  7. Nitnelave
    I am trying to make it so you have to press F5 before left clicking and putting the block on your head. It's suppose to be able to make it so you can where blocks on your head. I need a client mod so I can use the keyboard package they have. I need spout because it is a client side mod.

    No, I don't need to start with an easier one. I learn as I go. :)
     
  8. Offline

    Nitnelave

    Are you sure you don't want to do that with a command? F5 is already used by minecraft (don't remember for what just now, but I'm pretty sure it is). Why not have a command like /sethat that would make the block you're holding your hat?
    If you insist on relying on spout, you will lose a big part of the community, because not everyone has spout. By using only Bukkit API, you're almost guaranteed to have support for clients running spout, forge, or any popular mod.
     
  9. No, first of all. I do use a command too. Secondly, I'm using the plugin spout. And if not is there any way we can create our own version with Bukkit?

    P.S F5 is what puts you in different views
     
  10. Offline

    Nitnelave

    I'm just saying that doing it only with a command is simpler, and there really is no need to be able to change your hat with just a shortcut key, is there? But, if you still want to go ahead with this, have a look at the javadocs for spout.
    http://jd.spout.org/platform/
    Especially at the input package. You can bind a command to the F5 key, so that's what you're looking for.
     
  11. Do you want to help or....? I mean you are helping but I can give you the source or you can go on bitbukkit or github and we can work on it together and I can add you to the members on the plugin if you want. You choose though :)

    I already have the source for the plugin and the keyboard

    I mean. I don't really understand and I'm sorting of teaching myself or going along with it but you seem like you could help me through the project!

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

    tuskiomi

    So, if you're in f5, you can put a block on your head? Else, you can't?
     
  13. Yes, that's waht I want it to do because if your in f5 and you click the block will be placed on your head

    Can you help me with that?

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

    Nitnelave

    You would need something like:
    Code:
    if (Spout.getPlatform() == Platform.CLIENT)
    {
        Client c = (Client) Spout.getEngine();
        InputManager in = c.getInputManager();
        in.bind (new Binding ("my command", Keyboard.KEY_F5));
    }
    I don't guarantee this even compiles, but it should be a start.
     
  15. Do you have a github so we can work on this together?
     
  16. Offline

    Nitnelave

    Yes I do have a github account, but my aim is not to provide you with the solution, but to help you find it yourself, so you can learn how to do it, and not need help next time. Programming takes a lot of time, and most of the time, there isn't someone to watch over your shoulder, so you should take the opportunity to try and gain as much independence as you can. Have you had a look at the javadoc I linked you? You're gong to have to look at a lot of those in the next years if you continue coding. So learn to use them!
    I'm here for any questions you might have, but NOT to code your plugin.
     
  17. Yes, I understand that but I meant like I would write some code, and you would give me some tips on if it's right or not. Ok, well I'll keep trying. :l

    And that code would work but it brings up an error saying:
    java.lang.error: Unresolved compilation problems:
    The method getPlatform() is undefined for the type Spout
    CLIENT cannot be resolved or is not a field
    Client cannot be resolved to a type
    Client cannot be resolved to a type
    The method getEngine() is undefined for the type Spout
    InputManager cannot be resolved to a type

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

    Nitnelave

    Well, you have to import the classes. If you're using Eclipse, you can usually add the import from the hover you get from putting your cursor on the error. Make sure you included the right spout package in your build path, though.

    If you want me to have a look at your code, put it on github, and send me the link, I'll add some comments.
     
  19. Ok

    https://github.com/Flyingcreeper247/BlockHat/
    That's the github

    Oh, I just added the whole jar file to the build path. How do I just add a package

    I just have the spoutplugin as the build path

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

    Nitnelave

    You can just edit your posts, if you're adding some info shortly after, instead of creating a new post every time. And quoting is only useful if the quoted part is relevant.

    Indeed, you need the whole jar in the build path, not just a package (it doesn't really change anything, as Java will only use what is needed by the imports you make).

    Experiment by yourself. Now that you have the jar added to the path, have you tried importing the packages? Have you had a look at the javadocs? have you tried letting Eclipse manage your packages for you (it's surprisingly accurate most of the time, but you still have to do some imports manually)? You can do that in Window->Preference->Java->Editor->Save Actions->Organize imports.

    Please some back with a specific question, and after having done some research on Google. Learning to find info on Google is a big part of learning to program.
     
  21. Ok, I have imported everything and I only get the little red line under CLIENT and in.bind
     
  22. Offline

    Nitnelave

    import org.spout.api.Platform and check what error it gives you for in.bind.
     
  23. Offline

    Nitnelave

    You're missing the ; at the end of the import line.
    What is the error reported for in.bind ? Instead of a screenshot, you can just give me the text (e. g. for CLIENT it would have been "CLIENT cannnot be resolved or is not a field"). By the way, you probably could have solved the ; problem if you had a look at the error message for the import.
     
  24. Ok, the Platform import just say remove unused port even with a ; and the in.bind error is "Change to 'unbind(...)' "
     
  25. Offline

    Nitnelave

    When you have the ';' at the end of the import, do you still have an error with the Platform.CLIENT?
    You could try this : static import org.spout.api.Platform.CLIENT;
    And then you change from Platform.CLIENT to simply CLIENT.

    For bind, this is not the error in the hover, it is the suggested solution. The error is the text above, that is not a link. What does it say?
     
  26. Ok, I have worked a lot on the code. I just need to know how I add it before the action. I see we used it like a command but still!
     
  27. Offline

    Minnymin3

  28. Code:java
    1. package whoppercraft;
    2.  
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.EventPriority;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.entity.EntityDamageEvent;
    12. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.event.player.PlayerJoinEvent;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.PlayerInventory;
    17. import org.spout.api.Client;
    18. import org.spout.api.Platform;
    19. import org.spout.api.Spout;
    20. import org.spout.api.input.Binding;
    21. import org.spout.api.input.InputManager;
    22. import org.spout.api.input.Keyboard;
    23.  
    24. public class Hat implements Listener {
    25. private static Main pl;
    26.  
    27. public Hat(Main instance) {
    28. pl = instance;
    29. }
    30.  
    31. public Hat() {
    32.  
    33. }
    34. @EventHandler(priority = EventPriority.HIGH)
    35. public void onPlayerInteract(PlayerInteractEvent event) {
    36. Player player = event.getPlayer();
    37. Action action = event.getAction();
    38. if (Spout.getPlatform() == Platform.CLIENT)
    39. {
    40. Client c = (Client) Spout.getEngine();
    41. InputManager in = c.getInputManager();
    42. in.bind (new Binding ("my command", Keyboard.KEY_F5));
    43. if (action == Action.LEFT_CLICK_AIR) {
    44. setHat(player);
    45. }
    46. }
    47. }
    48.  
    49. public static void setHat(Player player) {
    50. ItemStack hand = player.getItemInHand().clone();
    51. PlayerInventory inv = player.getInventory();
    52. ItemStack head = inv.getHelmet();
    53. hand.setAmount(1);
    54. inv.remove(hand);
    55. inv.setHelmet(hand);
    56. inv.setItemInHand(head);
    57. player.sendMessage(ChatColor.AQUA + "Enjoy your hat!");
    58. }
    59.  
    60. public static void setHat(Player player, ItemStack item) {
    61. PlayerInventory inv = player.getInventory();
    62. inv.setHelmet(item);
    63. player.sendMessage(ChatColor.AQUA + "Enjoy your hat!");
    64. }
    65.  
    66. @EventHandler
    67. public void onPlayerJoin(PlayerJoinEvent event) {
    68. setHat(event.getPlayer(), new ItemStack(pl.getConfig().getInt("startID")));
    69. }
    70. @EventHandler
    71. public void getHelmet(EntityDamageEvent event){
    72. DamageCause drowning = DamageCause.DROWNING;
    73. DamageCause cause = event.getCause();
    74.  
    75. if(cause == drowning && event.getEntity() instanceof Player){
    76. Player player = (Player) event.getEntity();
    77. ItemStack helmet = player.getInventory().getHelmet();
    78. if(helmet.getType().equals(Material.GLASS));
    79. event.setCancelled(true);
    80. }
    81. }
    82. }


    Lol, I know I suck XD

    I am totally lost and I really need help

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

    Minnymin3

    What do you need help with? Key pressed events are described in great detail in the link. Make a class that implements InputListener and add the onKeyPressedEvent(KeyPressedEvent event). Then put this in your onEnable:
    Code:java
    1. PluginManager pm = Bukkit.getServer().getPluginManager(); pm.registerEvent(Event.Type.CUSTOM_EVENT, new [B]TheListenerClassThatImplementsInputListene[/B]r(), Priority.Normal, this);


    Also you should ask this question here: http://forums.spout.org/view/plugin-development.58/

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

Share This Page