Skip to content

Commit

Permalink
Reduce default values of the quest file redux (#117)
Browse files Browse the repository at this point in the history
* Create and migrate to NBTUtil
* Reduce default values
* reset quest database to prevent errors on loading
* add quest ID and lineID/order to quest files
* use constants instead of numbers
---------

Co-authored-by: KatatsumuriPan <[email protected]>
  • Loading branch information
WaitingIdly and KatatsumuriPan authored Oct 7, 2024
1 parent 5c4a097 commit deb00b9
Show file tree
Hide file tree
Showing 93 changed files with 952 additions and 458 deletions.
28 changes: 2 additions & 26 deletions src/main/java/betterquesting/NBTReplaceUtil.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
package betterquesting;

import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;

@Deprecated
public class NBTReplaceUtil {
@SuppressWarnings("unchecked")
public static <T extends NBTBase> T replaceStrings(T baseTag, String key, String replace) {
if (baseTag == null) {
return null;
}

if (baseTag instanceof NBTTagCompound) {
NBTTagCompound compound = (NBTTagCompound) baseTag;

for (String k : compound.getKeySet()) {
compound.setTag(k, replaceStrings(compound.getTag(k), key, replace));
}
} else if (baseTag instanceof NBTTagList) {
NBTTagList list = (NBTTagList) baseTag;

for (int i = 0; i < list.tagCount(); i++) {
list.set(i, replaceStrings(list.get(i), key, replace));
}
} else if (baseTag instanceof NBTTagString) {
NBTTagString tString = (NBTTagString) baseTag;
return (T) new NBTTagString(tString.getString().replaceAll(key, replace));
}

return baseTag; // Either isn't a string or doesn't contain one
return NBTUtil.replaceStrings(baseTag, key, replace);
}
}
92 changes: 92 additions & 0 deletions src/main/java/betterquesting/NBTUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package betterquesting;

import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraftforge.common.util.Constants;

import javax.annotation.Nullable;

public class NBTUtil {
@SuppressWarnings("unchecked")
public static <T extends NBTBase> T replaceStrings(T baseTag, String key, String replace) {
if (baseTag == null) {
return null;
}

if (baseTag instanceof NBTTagCompound) {
NBTTagCompound compound = (NBTTagCompound) baseTag;

for (String k : compound.getKeySet()) {
compound.setTag(k, replaceStrings(compound.getTag(k), key, replace));
}
} else if (baseTag instanceof NBTTagList) {
NBTTagList list = (NBTTagList) baseTag;

for (int i = 0; i < list.tagCount(); i++) {
list.set(i, replaceStrings(list.get(i), key, replace));
}
} else if (baseTag instanceof NBTTagString) {
NBTTagString tString = (NBTTagString) baseTag;
return (T) new NBTTagString(tString.getString().replaceAll(key, replace));
}

return baseTag; // Either isn't a string or doesn't contain one
}

public static void setTag(NBTTagCompound tag, String key, NBTBase value, boolean reduce) {
if (reduce && value.isEmpty()) return;
tag.setTag(key, value);
}

public static boolean getBoolean(NBTTagCompound tag, String key, boolean defaultValue) {
return tag.hasKey(key, Constants.NBT.TAG_ANY_NUMERIC) ? tag.getBoolean(key) : defaultValue;
}

public static void setBoolean(NBTTagCompound tag, String key, boolean value, boolean defaultValue, boolean reduce) {
if (reduce && value == defaultValue) return;
tag.setBoolean(key, value);
}

public static int getInteger(NBTTagCompound tag, String key, int defaultValue) {
return tag.hasKey(key, Constants.NBT.TAG_ANY_NUMERIC) ? tag.getInteger(key) : defaultValue;
}

public static void setInteger(NBTTagCompound tag, String key, int value, int defaultValue, boolean reduce) {
if (reduce && value == defaultValue) return;
tag.setInteger(key, value);
}

public static float getFloat(NBTTagCompound tag, String key, float defaultValue) {
return tag.hasKey(key, Constants.NBT.TAG_ANY_NUMERIC) ? tag.getFloat(key) : defaultValue;
}

public static void setFloat(NBTTagCompound tag, String key, float value, float defaultValue, boolean reduce) {
if (reduce && value == defaultValue) return;
tag.setFloat(key, value);
}

public static String getString(NBTTagCompound tag, String key, String defaultValue) {
return tag.hasKey(key, Constants.NBT.TAG_STRING) ? tag.getString(key) : defaultValue;
}

public static void setString(NBTTagCompound tag, String key, String value, String defaultValue, boolean reduce) {
if (reduce && value.equals(defaultValue)) return;
tag.setString(key, value);
}

public static <E extends Enum<E>> E getEnum(NBTTagCompound tag, String key, Class<E> enumClass, boolean ignoreCases, @Nullable E defaultValue) {
if (tag.hasKey(key, Constants.NBT.TAG_STRING)) {
String valueStr = tag.getString(key);
for (E value : enumClass.getEnumConstants()) {
boolean equals = ignoreCases ? value.name().equalsIgnoreCase(valueStr) : value.name().equals(valueStr);
if (equals) {
return value;
}
}
}
return defaultValue;
}

}
32 changes: 19 additions & 13 deletions src/main/java/betterquesting/NbtBlockType.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,27 @@ public NbtBlockType(IBlockState state) {
this.tags = new NBTTagCompound();
}

public NBTTagCompound writeToNBT(NBTTagCompound json) {
json.setString("blockID", b.getRegistryName().toString());
json.setInteger("meta", m);
json.setTag("nbt", tags);
json.setInteger("amount", n);
json.setString("oreDict", oreDict);
return json;
@Deprecated
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
return writeToNBT(nbt, false);
}

public void readFromNBT(NBTTagCompound json) {
b = Block.REGISTRY.getObject(new ResourceLocation(json.getString("blockID")));
m = json.getInteger("meta");
tags = json.getCompoundTag("nbt");
n = json.getInteger("amount");
oreDict = json.getString("oreDict");
public NBTTagCompound writeToNBT(NBTTagCompound nbt, boolean reduce) {
nbt.setString("blockID", b.getRegistryName().toString());
NBTUtil.setInteger(nbt, "meta", m, -1, reduce);
NBTUtil.setTag(nbt, "nbt", tags, reduce);
NBTUtil.setInteger(nbt, "amount", n, 1, reduce);
NBTUtil.setString(nbt, "oreDict", oreDict, "", reduce);
return nbt;
}

public void readFromNBT(NBTTagCompound nbt) {
Block targetBlock = Block.REGISTRY.getObject(new ResourceLocation(nbt.getString("blockID")));
b = targetBlock != Blocks.AIR ? targetBlock : Blocks.LOG;
m = NBTUtil.getInteger(nbt, "meta", -1);
tags = nbt.getCompoundTag("nbt");
n = NBTUtil.getInteger(nbt, "amount", 1);
oreDict = NBTUtil.getString(nbt, "oreDict", "");
}

public BigItemStack getItemStack() {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/betterquesting/ScoreBQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ public synchronized boolean hasEntry(@Nonnull UUID uuid) {
return playerScores.containsKey(uuid);
}

@Deprecated
@Override
public synchronized NBTTagList writeToNBT(NBTTagList nbt, @Nullable List<UUID> subset) {
return writeToNBT(nbt, subset, false);
}

@Override
public synchronized NBTTagList writeToNBT(NBTTagList nbt, @Nullable List<UUID> subset, boolean reduce) {
for (Entry<UUID, Integer> entry : playerScores.entrySet()) {
if (subset != null && !subset.contains(entry.getKey())) continue;
NBTTagCompound jObj = new NBTTagCompound();
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/betterquesting/ScoreboardBQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ public synchronized void readFromNBT(NBTTagList nbt, boolean merge) {
}
}

@Deprecated
@Override
public synchronized NBTTagList writeToNBT(NBTTagList nbt, @Nullable List<UUID> users) {
return writeToNBT(nbt, users, false);
}

@Override
public synchronized NBTTagList writeToNBT(NBTTagList nbt, @Nullable List<UUID> users, boolean reduce) {
for (Entry<String, ScoreBQ> entry : objectives.entrySet()) {
NBTTagCompound jObj = new NBTTagCompound();
jObj.setString("name", entry.getKey());
jObj.setTag("scores", entry.getValue().writeToNBT(new NBTTagList(), users));
jObj.setTag("scores", entry.getValue().writeToNBT(new NBTTagList(), users, reduce));
nbt.appendTag(jObj);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ public NBTTagCompound getRewardConfigData() {
return nbtSaved;
}

@Deprecated
@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
return writeToNBT(nbt, false);
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt, boolean reduce) {
nbt.setTag("orig_data", nbtSaved);

return nbt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ public NBTTagCompound getTaskProgressData() {
return nbtData.getCompoundTag("orig_prog");
}

@Deprecated
@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
return writeToNBT(nbt, false);
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt, boolean reduce) {
nbt.setTag("orig_data", nbtData.getCompoundTag("orig_data"));
return nbt;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package betterquesting.api.properties;

import net.minecraft.nbt.NBTBase;

public interface IPropertyReducible<T> extends IPropertyType<T> {

NBTBase reduceNBT(NBTBase nbt);

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package betterquesting.api.properties.basic;

import betterquesting.api.properties.IPropertyReducible;
import betterquesting.api.utils.BigItemStack;
import betterquesting.api.utils.JsonHelper;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.util.Constants;

public class PropertyTypeItemStack extends PropertyTypeBase<BigItemStack> implements IPropertyReducible<BigItemStack> {

public class PropertyTypeItemStack extends PropertyTypeBase<BigItemStack> {
public PropertyTypeItemStack(ResourceLocation key, BigItemStack def) {
super(key, def);
}
Expand All @@ -25,11 +28,23 @@ public NBTBase writeValue(BigItemStack value) {
NBTTagCompound nbt = new NBTTagCompound();

if (value == null || value.getBaseStack() == null) {
getDefault().writeToNBT(nbt);
getDefault().writeToNBT(nbt, false);
} else {
value.writeToNBT(nbt);
value.writeToNBT(nbt, false);
}

return nbt;
}

@Override
public NBTBase reduceNBT(NBTBase nbt) {
BigItemStack value;
if (nbt == null || nbt.getId() != Constants.NBT.TAG_COMPOUND) {
value = this.getDefault();
} else {
value = JsonHelper.JsonToItemStack((NBTTagCompound) nbt);
}
return value.writeToNBT(new NBTTagCompound(), true);
}

}
25 changes: 18 additions & 7 deletions src/main/java/betterquesting/api/utils/BigItemStack.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package betterquesting.api.utils;

import betterquesting.NBTUtil;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
Expand All @@ -17,10 +18,14 @@
* <b>For storage purposes only!
*/
public class BigItemStack {
private static final OreIngredient NO_ORE = new OreIngredient("");

private static final short DEFAULT_DAMAGE = 0;
private static final String DEFAULT_OREDICT = "";
private static final OreIngredient NO_ORE = new OreIngredient(DEFAULT_OREDICT);

private final ItemStack baseStack;
public int stackSize;
private String oreDict = "";
private String oreDict = DEFAULT_OREDICT;
private OreIngredient oreIng = NO_ORE;

public BigItemStack(ItemStack stack) {
Expand All @@ -34,7 +39,7 @@ public BigItemStack(@Nonnull Block block) {
}

public BigItemStack(@Nonnull Block block, int amount) {
this(block, amount, 0);
this(block, amount, DEFAULT_DAMAGE);
}

public BigItemStack(@Nonnull Block block, int amount, int damage) {
Expand All @@ -46,7 +51,7 @@ public BigItemStack(@Nonnull Item item) {
}

public BigItemStack(@Nonnull Item item, int amount) {
this(item, amount, 0);
this(item, amount, DEFAULT_DAMAGE);
}

public BigItemStack(@Nonnull Item item, int amount, int damage) {
Expand Down Expand Up @@ -142,18 +147,24 @@ public BigItemStack(@Nonnull NBTTagCompound tags) // Can load normal ItemStack N
NBTTagCompound itemNBT = tags.copy();
itemNBT.setInteger("Count", 1);
if (tags.hasKey("id", 99)) {
itemNBT.setString("id", "" + tags.getShort("id"));
itemNBT.setString("id", String.valueOf(tags.getShort("id")));
}
this.stackSize = tags.getInteger("Count");
this.stackSize = NBTUtil.getInteger(tags, "Count", 1);
this.setOreDict(tags.getString("OreDict"));
this.baseStack = new ItemStack(itemNBT); // Minecraft does the ID conversions for me
if (tags.getShort("Damage") < 0) this.baseStack.setItemDamage(OreDictionary.WILDCARD_VALUE);
}

@Deprecated
public NBTTagCompound writeToNBT(NBTTagCompound tags) {
return writeToNBT(tags, false);
}

public NBTTagCompound writeToNBT(NBTTagCompound tags, boolean reduce) {
baseStack.writeToNBT(tags);
tags.setInteger("Count", stackSize);
tags.setString("OreDict", oreDict);
if (reduce && baseStack.getItemDamage() == DEFAULT_DAMAGE) tags.removeTag("Damage");
NBTUtil.setString(tags, "OreDict", oreDict, "", reduce);
return tags;
}
}
10 changes: 8 additions & 2 deletions src/main/java/betterquesting/api/utils/JsonHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package betterquesting.api.utils;

import betterquesting.NBTUtil;
import betterquesting.api.api.QuestingAPI;
import betterquesting.api.placeholders.ItemPlaceholder;
import betterquesting.api.placeholders.PlaceholderConverter;
Expand Down Expand Up @@ -279,14 +280,19 @@ public static boolean isEntity(NBTTagCompound tags) {
public static BigItemStack JsonToItemStack(NBTTagCompound nbt) {
Item preCheck = Item.getByNameOrId(nbt.hasKey("id", 99) ? "" + nbt.getShort("id") : nbt.getString("id"));
if (preCheck != null && preCheck != ItemPlaceholder.placeholder) return new BigItemStack(nbt);
return PlaceholderConverter.convertItem(preCheck, nbt.getString("id"), nbt.getInteger("Count"), nbt.getShort("Damage"), nbt.getString("OreDict"), !nbt.hasKey("tag", 10) ? null : nbt.getCompoundTag("tag"));
return PlaceholderConverter.convertItem(preCheck, nbt.getString("id"), NBTUtil.getInteger(nbt, "Count", 1), nbt.getShort("Damage"), nbt.getString("OreDict"), !nbt.hasKey("tag", 10) ? null : nbt.getCompoundTag("tag"));
}

/**
* Use this for quests instead of converter NBT because this doesn't use ID numbers
*/
public static NBTTagCompound ItemStackToJson(BigItemStack stack, NBTTagCompound nbt) {
if (stack != null) stack.writeToNBT(nbt);
return ItemStackToJson(stack, nbt, false);
}

public static NBTTagCompound ItemStackToJson(BigItemStack stack, NBTTagCompound nbt, boolean reduce) {
if (stack != null)
stack.writeToNBT(nbt, reduce);
return nbt;
}

Expand Down
Loading

0 comments on commit deb00b9

Please sign in to comment.