Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce default values of the quest file #109

Open
wants to merge 8 commits into
base: 1.12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Rongmario marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
66 changes: 66 additions & 0 deletions src/main/java/betterquesting/NBTUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package betterquesting;

import javax.annotation.Nullable;

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

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 boolean getBoolean(NBTTagCompound tag, String key, boolean defaultValue) {
return tag.hasKey(key, 99) ? tag.getBoolean(key) : defaultValue;
}

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

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

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

public static <E extends Enum<E>> E getEnum(NBTTagCompound tag, String key, Class<E> enumClass, boolean ignoreCases, @Nullable E defaultValue) {
if (tag.hasKey(key, 8)) {
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;
}

}
35 changes: 22 additions & 13 deletions src/main/java/betterquesting/NbtBlockType.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,30 @@ 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());
if (!reduce || m != -1)
nbt.setInteger("meta", m);
if (!reduce || !tags.isEmpty())
nbt.setTag("nbt", tags);
if (!reduce || n != 1)
nbt.setInteger("amount", n);
if (!reduce || !oreDict.isEmpty())
nbt.setString("oreDict", oreDict);
return nbt;
}

public void readFromNBT(NBTTagCompound nbt) {
b = Block.REGISTRY.getObject(new ResourceLocation(nbt.getString("blockID")));
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,14 @@
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;

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

public PropertyTypeItemStack(ResourceLocation key, BigItemStack def) {
super(key, def);
}
Expand All @@ -25,11 +27,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() != 10) {
value = this.getDefault();
} else {
value = JsonHelper.JsonToItemStack((NBTTagCompound) nbt);
}
return value.writeToNBT(new NBTTagCompound(), true);
}

}
18 changes: 15 additions & 3 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 Down Expand Up @@ -144,16 +145,27 @@ public BigItemStack(@Nonnull NBTTagCompound tags) // Can load normal ItemStack N
if (tags.hasKey("id", 99)) {
itemNBT.setString("id", "" + 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 && stackSize == 1)
tags.removeTag("Count");
else
tags.setInteger("Count", stackSize);
if (!reduce || !oreDict.isEmpty())
tags.setString("OreDict", oreDict);
if (reduce && tags.getShort("Damage") == 0)
tags.removeTag("Damage");
return tags;
}
}
24 changes: 18 additions & 6 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 @@ -156,7 +157,7 @@ public static Future<Void> WriteToFile(File file, JsonObject jObj) {
QuestingAPI.getLogger().error("An error occured while saving JSON to file (Directory setup):", e);
return null;
}

// NOTE: These are now split due to an edge case in the previous implementation where resource leaking can occur should the outer constructor fail
try (FileOutputStream fos = new FileOutputStream(tmp);
OutputStreamWriter fw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
Expand All @@ -168,7 +169,7 @@ public static Future<Void> WriteToFile(File file, JsonObject jObj) {
QuestingAPI.getLogger().error("An error occurred while saving JSON to file (File write):", e);
return null;
}

// NOTE: These are now split due to an edge case in the previous implementation where resource leaking can occur should the outer constructor fail
try(FileInputStream fis = new FileInputStream(tmp); InputStreamReader fr = new InputStreamReader(fis, StandardCharsets.UTF_8))
{
Expand All @@ -179,7 +180,7 @@ public static Future<Void> WriteToFile(File file, JsonObject jObj) {
QuestingAPI.getLogger().error("An error occured while saving JSON to file (Validation check):", e);
return null;
}

try
{
if(file.exists()) file.delete();
Expand Down Expand Up @@ -278,15 +279,26 @@ 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"));
if (preCheck != null && preCheck != ItemPlaceholder.placeholder)
return new BigItemStack(nbt);
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
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,14 @@ public void onButtonClick() {
public void onRightButtonClick() {
}

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

@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt, boolean reduce) {
// TODO: Fix me
return nbt;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/betterquesting/api2/storage/INBTPartial.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

// Used when the base data set can safely be split. Can be used in place of INBTSaveLoad
public interface INBTPartial<T extends NBTBase, K> {
@Deprecated
T writeToNBT(T nbt, @Nullable List<K> subset);

default T writeToNBT(T nbt, @Nullable List<K> subset, boolean reduce) {
return writeToNBT(nbt, subset);
}

void readFromNBT(T nbt, boolean merge);
}
Loading