Skip to content

Commit

Permalink
copy NBTModifier temporary to start working on a fix for 1.20.6
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Jun 8, 2024
1 parent 64e35fe commit a3cf27b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.hsgamer.bettergui.builder;

import me.hsgamer.bettergui.modifier.NBTModifier;
import me.hsgamer.hscore.builder.MassBuilder;
import me.hsgamer.hscore.bukkit.item.modifier.*;
import me.hsgamer.hscore.minecraft.item.ItemModifier;
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/me/hsgamer/bettergui/modifier/NBTModifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package me.hsgamer.bettergui.modifier;

import com.google.gson.Gson;
import me.hsgamer.hscore.common.StringReplacer;
import me.hsgamer.hscore.common.Validate;
import me.hsgamer.hscore.minecraft.item.ItemModifier;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

public class NBTModifier implements ItemModifier<ItemStack> {
private static final Gson GSON = new Gson();
private String nbtData = "";

@SuppressWarnings("deprecation")
@Override
public @NotNull ItemStack modify(@NotNull ItemStack original, UUID uuid, @NotNull Collection<StringReplacer> stringReplacers) {
if (Validate.isNullOrEmpty(nbtData)) {
return original;
}
try {
return Bukkit.getUnsafe().modifyItemStack(original, StringReplacer.replace(nbtData, uuid, stringReplacers));
} catch (Throwable throwable) {
return original;
}
}

@Override
public Object toObject() {
return nbtData;
}

@Override
public void loadFromObject(Object object) {
if (object instanceof Map) {
Map<?, ?> map = (Map<?, ?>) object;
this.nbtData = GSON.toJson(map);
} else {
this.nbtData = Objects.toString(object, "");
}
}
}

0 comments on commit a3cf27b

Please sign in to comment.