-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
copy NBTModifier temporary to start working on a fix for 1.20.6
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
src/main/java/me/hsgamer/bettergui/builder/ItemModifierBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/me/hsgamer/bettergui/modifier/NBTModifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ""); | ||
} | ||
} | ||
} |