get item category

Discussion in 'Plugin Development' started by rushr93, Oct 15, 2016.

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

    rushr93

    hello
    i will try to have the item gategory same as the creative inventory
    for exemple : diamond sword -> combat ; stick -> materials

    is it possible ? if is it how can i get it ?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @rushr93 I believe that this is pure client side, not server side.
     
  3. Offline

    oriamrm

    I Don't Think There Is A Method For This...
    But if you have enough free time, you can just make a switch statment for each item That exists in the game.

    Not a very efficient way, but still the only one i can think of.
     
  4. Offline

    I Al Istannen

    @oriamrm @timtower @rushr93
    Or you use lovely Reflection. The calls are not optimized, rely on version dependend imports and are only as a proof-of-concept.

    Code:java
    1. @EventHandler
    2. public void onPlayerHit(PlayerInteractEvent event) {
    3. if (!event.hasItem()) {
    4. return;
    5. }
    6. ReflectResponse<Object> value;
    7. if (event.getItem().getType().isBlock()) {
    8. // Block
    9. Block block = CraftMagicNumbers.getBlock(event.getMaterial());
    10. value = ReflectionUtil.getFieldValue(Block.class, block, (field) -> field.getType() == CreativeModeTab.class);
    11. } else {
    12. // Item
    13. Item item = CraftMagicNumbers.getItem(event.getItem().getType());
    14. value = ReflectionUtil.getFieldValue(Item.class, item, (field) -> field.getType() == CreativeModeTab.class);
    15. }
    16. if (value == null || !value.isValuePresent()) {
    17. event.getPlayer().sendMessage(TextUtils.colorize("&cTab not found!"));
    18. return;
    19. }
    20. String creativeTabName = getCreativeTabName(value);
    21. if (creativeTabName == null) {
    22. event.getPlayer().sendMessage(TextUtils.colorize("&cTab name not found!"));
    23. } else {
    24. event.getPlayer().sendMessage(TextUtils.colorize("&7The tab is: &a" + creativeTabName + "&7."));
    25. }
    26. }
    27.  
    28. private String getCreativeTabName(ReflectResponse<Object> value) {
    29. if (value.isValuePresent()) {
    30. CreativeModeTab tab = (CreativeModeTab) value.getValue();
    31. ReflectResponse<Object> name = ReflectionUtil.getFieldValue(CreativeModeTab.class, tab, new MemberPredicate<Field>()
    32. .withModifiers(Modifier.FINAL, Modifier.PRIVATE).and(field -> field.getType() == String.class));
    33. if (name.isValuePresent()) {
    34. String category = (String) name.getValue();
    35. return category;
    36. }
    37. }
    38. return null;
    39. }


    Output:
    [​IMG]

    (@timtower Does the file upload here only doesn't work for me? Could easily be the case.)
     
    Last edited: Oct 15, 2016
  5. Offline

    timtower Administrator Administrator Moderator

    @I Al Istannen That is actually amazing.
    And not sure, never use it :p
     
  6. Offline

    I Al Istannen

    @timtower
    Nah, just a bit of digging in the client and server code. Thanks though ;)
    It is a nice concept, but I can't really find any application for it xD

    Ah, I see. It has worked but since a month or so it doesn't anymore. I will check with a clean browser and if it doesn't work make a thread about it. Thanks :)


    @rushr93
    If you don't really understand what I did, I can probably make a SSCCE for it, you can use.
     
Thread Status:
Not open for further replies.

Share This Page