Skip to content

Commit

Permalink
Introduce new (optional) item-syntax for 1.20.5+
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakllp committed Nov 23, 2024
1 parent 454c91e commit ef6d144
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
10 changes: 10 additions & 0 deletions modules/API/src/main/java/de/Keyle/MyPet/api/util/ConfigItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public ConfigItem(ItemStack item, DurabilityMode durabilityMode) {
}

public ConfigItem(String data) {
if(data.startsWith(".")) {
// Assumption: This is a 1.20.5+ Item
load(data.replace(". ", ""));
return;
}

String[] splitData = data.split("\\s+", 2);

if (splitData.length == 0) {
Expand Down Expand Up @@ -117,5 +123,9 @@ public String toString() {

public abstract boolean compare(Object compareItem);

public void load(String data) {
MyPetApi.getLogger().warning("You are trying to use 1.20.5+ item-NBT! You need to use 1.13 item IDs and NBT for this version.");
}

public abstract void load(MaterialHolder material, String data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import de.Keyle.MyPet.api.player.MyPetPlayer;
import de.Keyle.MyPet.api.util.Compat;
import de.Keyle.MyPet.api.util.ReflectionUtil;
import de.Keyle.MyPet.api.util.inventory.material.ItemDatabase;
import de.Keyle.MyPet.compat.v1_20_R4.entity.EntityMyAquaticPet;
import de.Keyle.MyPet.compat.v1_20_R4.util.NBTHelper;
import de.Keyle.MyPet.compat.v1_20_R4.util.inventory.ItemStackNBTConverter;
import de.keyle.knbt.TagCompound;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
Expand Down Expand Up @@ -416,12 +414,8 @@ public String getLastDamageSource(LivingEntity e) {

@Override
public String itemstackToString(org.bukkit.inventory.ItemStack itemStack) {
ItemDatabase itemDatabase = MyPetApi.getServiceManager().getService(ItemDatabase.class).get();
String itemstack = itemDatabase.getByID(itemStack.getType().getKey().getKey()).getId();
if (itemStack.hasItemMeta()) {
itemstack += " " + NBTHelper.getTag(CraftItemStack.asNMSCopy(itemStack)).toString();
}
return itemstack;
ItemStack stack = CraftItemStack.asNMSCopy(itemStack);
return ItemStackNBTConverter.itemStackToVanillaCompound(stack).toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.TagParser;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.datafix.fixes.References;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -58,6 +57,19 @@ public boolean compare(Object o) {
return this.compare(CraftItemStack.asCraftMirror(compareItem));
}

@Override
public void load(String data) {
// Assumption: This is just an item
try {
net.minecraft.world.item.ItemStack stack = ItemStackNBTConverter.vanillaCompoundToItemStack(TagParser.parseTag(data));
this.item = CraftItemStack.asCraftMirror(stack);

} catch (Exception e) {
MyPetApi.getLogger().warning("Error" + ChatColor.RESET + " in config: " + ChatColor.UNDERLINE + e.getLocalizedMessage() + ChatColor.RESET + " caused by:");
MyPetApi.getLogger().warning(data);
}
}

@Override
public void load(MaterialHolder material, String data) {
ResourceLocation key = ResourceLocation.tryParse(material.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import de.Keyle.MyPet.api.player.MyPetPlayer;
import de.Keyle.MyPet.api.util.Compat;
import de.Keyle.MyPet.api.util.ReflectionUtil;
import de.Keyle.MyPet.api.util.inventory.material.ItemDatabase;
import de.Keyle.MyPet.compat.v1_21_R1.entity.EntityMyAquaticPet;
import de.Keyle.MyPet.compat.v1_21_R1.util.NBTHelper;
import de.Keyle.MyPet.compat.v1_21_R1.util.inventory.ItemStackNBTConverter;
import de.keyle.knbt.TagCompound;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
Expand Down Expand Up @@ -418,12 +416,8 @@ public String getLastDamageSource(LivingEntity e) {

@Override
public String itemstackToString(org.bukkit.inventory.ItemStack itemStack) {
ItemDatabase itemDatabase = MyPetApi.getServiceManager().getService(ItemDatabase.class).get();
String itemstack = itemDatabase.getByID(itemStack.getType().getKey().getKey()).getId();
if (itemStack.hasItemMeta()) {
itemstack += " " + NBTHelper.getTag(CraftItemStack.asNMSCopy(itemStack)).toString();
}
return itemstack;
ItemStack stack = CraftItemStack.asNMSCopy(itemStack);
return ItemStackNBTConverter.itemStackToVanillaCompound(stack).toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ public boolean compare(Object o) {
return this.compare(CraftItemStack.asCraftMirror(compareItem));
}

@Override
public void load(String data) {
// Assumption: This is just an item
try {
net.minecraft.world.item.ItemStack stack = ItemStackNBTConverter.vanillaCompoundToItemStack(TagParser.parseTag(data));
this.item = CraftItemStack.asCraftMirror(stack);

} catch (Exception e) {
MyPetApi.getLogger().warning("Error" + ChatColor.RESET + " in config: " + ChatColor.UNDERLINE + e.getLocalizedMessage() + ChatColor.RESET + " caused by:");
MyPetApi.getLogger().warning(data);
}
}

@Override
public void load(MaterialHolder material, String data) {
ResourceLocation key = ResourceLocation.tryParse(material.getId());
Expand Down

0 comments on commit ef6d144

Please sign in to comment.