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:
- Breaking up the comparisons
- 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.
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 2 common methods of creating an ID system:
- Custom Model Data (Vanilla Skript, doesn't need addons, super simple)
- NBT (Requires SkBee, a little more complex but not really)
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:
# Minecraft 1.20.4 and below
set {CoolSword} to diamond sword named "Cool Sword" with nbt from "{customitem:""cool_sword""}"
# Minecraft 1.20.5 and above
set {CoolSword} to diamond sword named "Cool Sword" with custom nbt from "{customitem:""cool_sword""}"
# or (for all MC versions)
set {CoolSword} to diamond sword named "Cool Sword"
set string tag "customitem" of 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 string tag "customitem" of nbt of player's tool = "cool_sword":
# do stuff
Pretty simple yeah?
We can easily verify that the item is in fact our cool sword.