Tricks Item Comparison - ShaneBeee/SkBee GitHub Wiki

If you've landed on this page, you're probably having issues comparing items.
Let's talk about some of the quirks/issues with item comparisons.

CREATING AN ITEM

For the sake of this wiki, we're going to make this simple.

on load:
	set {CoolSword} to diamond sword named "Cool Sword"

on some event:
	give player {CoolSword}

Simply put, we're creating a named item and giving it to the player.

COMPARISONS

Now comes the issue with comparing.
Common practice:

on some event:
	if player's tool = {CoolSword}:
		# do something

The big issue here is if the item isn't an exact match, the comparison will fail.
Let's pretend the item was damaged by 1, essentially this is what your code is:

on some event:
	   # being the player's tool                        # being the variable
	if diamond sword named "Cool Sword" damaged by 1 = diamond sword named "Cool Sword":
		# do something

Because the item is damaged, it no longer matches the item in your variable.
(the same thing goes when just comparing items and not using variables).

FIX

How can we fix this?
We're going to talk about 2 different methods:

  1. Breaking up the comparisons
  2. Using an ID system

BREAKING UP COMPARISONS

As we have learned, the main issue is comparing 2 items, and them not matching.
So rather than directly comparing the item, let's compare the parts that matter.

on some event:
	if all:
		type of player's tool = diamond sword
		name of player's tool = "Cool Sword"
	then:
		# do stuff

Simply put, we compare the item type and name separately.
This cuts away having to worry about other things modifying the tool (such as enchants, lore, damage) and breaking the comparison.
It's generally not recommended to compare things a player can change, ie: the name of the item.
Lore is also a bad alternative as it can get messy if you need to update lore.

ID SYSTEM

Another great practice for comparison is using an ID system.
This is essentially making your item's "custom" and comparing just the custom part.
We're going to go over a few common methods of creating an ID system:

  1. Custom Model Data (Vanilla Skript, doesn't need addons, super simple)
  2. NBT (Requires SkBee, a little more complex but not really)
  3. PDC (Requires Skript 2.15.0+, fairly simple/straight forward)

CUSTOM MODEL DATA METHOD

Custom model data is a system in Minecraft which allows you to apply custom models in a resource pack to an item.
If there are no custom models in a pack, this number is ignored... which allows for a great/simple method for creating an ID system.

Creating the Item

on load:
	set {CoolSword} to diamond sword named "Cool Sword" with custom model data 500

Yep, it's as simple as that. We just had to add the custom model data expression to our previous item.

Comparing

on some event:
	if custom model data of player's tool = 500:
		# do stuff

WOW SO SIMPLE!
Yes, it really is that easy.
Since a player can't change the custom model data, we can now easily verify they're holding that item.

NBT METHOD

Storing an ID in the NBT of an item is a great way to identify your custom items.
There are several ways we can create a custom item with a custom NBT tag, let's look at a few ways.

Creating the Item

on load:
	set {CoolSword} to diamond sword named "Cool Sword" with custom nbt from "{customitem:""cool_sword""}"
	# or
	set {CoolSword} to diamond sword named "Cool Sword"
	set string tag "customitem" of custom nbt of {CoolSword} to "cool_sword"

Fairly simple right?
Now our item has a custom NBT tag identifying its uniqueness.

Comparing

on some event:
    if all:
        custom nbt of player's tool has tag "customitem"
        string tag "customitem" of custom nbt of player = "cool_sword"
	then:
		# do stuff

Pretty simple yeah?
We can easily verify that the item is in fact our cool sword.

PDC METHOD

PDC allows you to create/read an ID on the item.

Creating the Item

Here we create our item just like before, but we also add a custom tag so we can compare it later.

on load:
	set {CoolSword} to diamond sword named "Cool Sword"
    set string data tag "customitem" of {CoolSword} to "cool_sword"

Comparing

Now we can compare the item's custom tag to our ID.

on some event:
    if string data tag "customitem" of player's tool = "cool_sword":
        # do stuff