Solved How to get an item's type with getMaterial()?

Discussion in 'Spigot Plugin Development' started by Askyyy, Sep 16, 2020.

  1. upload_2020-9-15_19-47-46.png
    doesn't work
  2. what are you trying to do instead? This is the proper way (and only way I think) to do it in the version you're in unless you maybe used some type of util class like XSeries (if that does support 1.8.8).

    You can't get many things like any colored blocks or spawn eggs using just .getType() or in this case a Material method.
    Though I'm not sure I understand your question clearly to give you an answer.. I'm not sure what you're trying to do since no spawn egg has a subid value of 0.
  3. Make an instanceof check and cast ItemStack#getItemMeta to SpawnEggMeta. Don't forget to set the modified item meta to the item again.
    EDIT:// that's for trying to set the spawn type, oops. For checking the type, you don't need to set the item meta back of course.
    #3 Escad, Sep 16, 2020
    Last edited: Sep 17, 2020
    • Agree Agree x 1
  4. Item ID's have been deprecated for quite a while now. Every item has a Material enumeration value.
    Instead of checking to see if the item is a spawn egg, and then checking if it has the proper ID, just check if it's the correct spawn egg material.

    It would look something like Material.COW_SPAWN_EGG


    Code (Java):
    if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK) && e.getMaterial().equals(Material.COW_SPAWN_EGG)) {
                        //do stuff
    • Funny Funny x 1
  5. Did you even look at what version he's in?.. This is only true in 1.13+ not in 1.8.8..

    Also yes I forgot you can cast the e.getItem() method from that event to a SpawnEggMeta since at this point that's still useful which you can then use the spawnEggMeta.getSpawnedType() to get the correct entityType instead of relying on subids.
  6. My apologies that I didn't see this was for a version 5 years old. My serious suggestion is to stop using 1.8.8 and update to a later version. There are plugins that revert the PVP to 1.8, the spigot API has increased drastically in quality and efficiency. Not to mention if you don't have plugins patching exploits, 1.8.8 is severely insecure.

    Also, developing for 1.8 requires a very fair understanding of the spigot API at that time, and if you don't have it, it can be VERY hard to find help on the forums, because many of the developers here didn't even play 1.8, let alone code plugins for it.

    I'm being serious. I simply suggest you upgrade. What you're trying to achieve can be done with a single line of code in the current API version 1.16.3, but even 1.15 has all the support you could need, while having basically any plugin you could really need up-to-date.
    • Agree Agree x 2
  7. Get the item in the player's hand via (I believe e.getItem()) or e.getPlayer().getItemInHand(). Make sure to check that it's not null first. Then use the "getDurability" method. That's what you're looking for, despite the "durability" part.
  8. What do you mean by item type? .getType not working?

    I agree with you, but I saw several requests to find a developer (recently), and when I responded and asked why 1.8.8, I was told that this is the best, most popular version for pvp lol
  9. I believe you need to check the durability of an ItemStack, the Material enum doesn’t have anything to do with extension ID’s, only the durability of an ItemStack does.


    In all seriousness, plugins that revert PvP are NOT the same and never will be. To truly revert, client modification is required.
  10. Yes of course, it's an alteration on a mechanic of the game to try to emulate the previous mechanic that was removed. So of course, since we can't literally change the mechanic, only how it's used, it wont be the same. However, getting pretty close to those mechanics while also being able to get support from so many broad horizons when you use a current version is worth it alone. considering you're close to the mechanic, and you'd have way more security, way more support, and way more functionality / features within the API itself, I would say the good heavily outweighs the bad.

    However, this can entirely be avoided by running a 1.8 server responsibly. That is, with your own spigot fork, and a very solid understanding of Java as well as how the API worked at that time and the full acceptance that literally every version that comes out past the version you're using increases the inability to find support for problems you encounter as well. For example, the bug tracker for 1.8.8 is unsupported by Spigot themselves. This is my honest advice regardless.
  11. I do agree, but even if the PvP was the same on newer versions, you run into more problems, like performance. 1.7.1-8 were the last stable server versions, 1.12 is stable but I believe it's not better than 1.7/1.8. If you have your own Spigot fork and optimize it well enough you can hold 1,000+ concurrent players on a 8 GB server with *20 TPS. Apparently Mojang is making a new server engine but I'm not sure.