Skip to content

Instantly share code, notes, and snippets.

@towerofnix
Last active October 22, 2015 18:21
Show Gist options
  • Save towerofnix/f4ef08ce019874ecfe61 to your computer and use it in GitHub Desktop.
Save towerofnix/f4ef08ce019874ecfe61 to your computer and use it in GitHub Desktop.
Tad bit of hacking around Minecraft led me to this
import com.google.common.base.Charsets;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.io.Files;
import com.google.common.io.Resources;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class azo
{
private static final Logger a = LogManager.getLogger();
private static final Gson b = new GsonBuilder().registerTypeAdapter(azp.class, new azp.a()).registerTypeAdapter(azl.class, new azl.a()).registerTypeAdapter(azm.class, new azm.a()).registerTypeAdapter(azk.class, new azk.a()).registerTypeHierarchyAdapter(azr.class, new azs.a()).registerTypeHierarchyAdapter(bab.class, new bac.a()).registerTypeHierarchyAdapter(azn.b.class, new azn.b.a()).create();
private final LoadingCache<kl, azm> c = CacheBuilder.newBuilder().build(new azo.a(null));
private final File d;
public azo(File ☃)
{
this.d = ☃;
a();
}
public azm a(kl ☃) {
return (azm)this.c.getUnchecked(☃);
}
public void a() {
this.c.invalidateAll();
for (kl ☃ : azj.a())
a(☃);
}
class a extends CacheLoader<kl, azm> {
private a() {
}
public azm a(kl ☃) throws Exception {
if (☃.a().contains(".")) {
throw new IllegalArgumentException("Invalid loot table name '{}' (can't contain periods)" + ☃);
}
azm ☃ = b(☃);
if (☃ == null) {
☃ = c(☃);
}
if (☃ == null) {
☃ = azm.a;
azo.b().warn("Couldn't find resource table {}", new Object[] { ☃ });
} else {
azo.b().debug("Loaded resource table {}", new Object[] { ☃ });
}
return ☃;
}
private azm b(kl ☃)
{
File ☃ = new File(new File(azo.a(azo.this), ☃.b()), ☃.a() + ".json");
if (☃.exists()) {
if (☃.isFile()) {
String ☃;
try {
☃ = Files.toString(☃, Charsets.UTF_8);
} catch (IOException ☃) {
azo.b().warn("Couldn't load loot table " + ☃ + " from " + ☃, ☃);
return azm.a;
}
try {
return (azm)azo.c().fromJson(☃, azm.class);
} catch (JsonParseException ☃) {
azo.b().error("Couldn't load loot table " + ☃ + " from " + ☃, ☃);
return azm.a;
}
}
azo.b().warn("Expected to find loot table " + ☃ + " at " + ☃ + " but it was a folder.");
return azm.a;
}
return null;
}
private azm c(kl ☃)
{
URL ☃ = azo.class.getResource("/assets/" + ☃.b() + "/loot_tables/" + ☃.a() + ".json");
if (☃ != null) {
String ☃;
try {
☃ = Resources.toString(☃, Charsets.UTF_8);
} catch (IOException ☃) {
azo.b().warn("Couldn't load loot table " + ☃ + " from " + ☃.toString(), ☃);
return azm.a;
}
try {
return (azm)azo.c().fromJson(☃, azm.class);
} catch (JsonParseException ☃) {
azo.b().error("Couldn't load loot table " + ☃ + " from " + ☃.toString(), ☃);
return azm.a;
}
}
return null;
}
}
}
--------------------------------------------
import com.google.common.collect.Maps;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonSyntaxException;
import java.lang.reflect.Type;
import java.util.Map;
public class azs
{
private static final Map<kl, azr.a<?>> a = Maps.newHashMap();
private static final Map<Class<? extends azr>, azr.a<?>> b = Maps.newHashMap();
public static <T extends azr> void a(azr.a<? extends T> ☃)
{
kl ☃ = ☃.a();
Class ☃ = ☃.b();
if (a.containsKey(☃)) {
throw new IllegalArgumentException("Can't re-register item function name " + ☃);
}
if (b.containsKey(☃)) {
throw new IllegalArgumentException("Can't re-register item function class " + ☃.getName());
}
a.put(☃, ☃);
b.put(☃, ☃);
}
public static azr.a a(kl ☃) {
azr.a ☃ = (azr.a)a.get(☃);
if (☃ == null) {
throw new IllegalArgumentException("Unknown loot item function '" + ☃ + "'");
}
return ☃;
}
public static <T extends azr> azr.a a(T ☃) {
azr.a ☃ = (azr.a)b.get(☃.getClass());
if (☃ == null) {
throw new IllegalArgumentException("Unknown loot item function " + ☃);
}
return ☃;
}
static
{
a(new azu.a());
a(new azv.a());
a(new azq.a());
a(new azw.a());
a(new azx.a());
a(new azt.a());
}
public static class a
implements JsonDeserializer<azr>, JsonSerializer<azr>
{
public azr a(JsonElement ☃, Type ☃, JsonDeserializationContext ☃)
throws JsonParseException
{
JsonObject ☃ = od.m(☃, "function");
kl ☃ = new kl(od.h(☃, "function"));
azr.a ☃;
try
{
☃ = azs.a(☃);
} catch (IllegalArgumentException ☃) {
throw new JsonSyntaxException("Unknown function '" + ☃ + "'");
}
return ☃.b(☃, ☃, (bab[])od.a(☃, "conditions", new bab[0], ☃, [Lbab.class));
}
public JsonElement a(azr ☃, Type ☃, JsonSerializationContext ☃)
{
azr.a ☃ = azs.a(☃);
JsonObject ☃ = new JsonObject();
☃.a(☃, ☃, ☃);
☃.addProperty("function", ☃.a().toString());
if ((☃.a() != null) && (☃.a().length > 0)) {
☃.add("conditions", ☃.serialize(☃.a()));
}
return ☃;
}
}
}
--------------------------------------------
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import java.util.Random;
public class azk
{
private final adg a;
private final azr[] b;
private final bab[] c;
private final int d;
private final float e;
public azk(adg ☃, int ☃, float ☃, azr[] ☃, bab[] ☃)
{
this.a = ☃;
this.d = ☃;
this.e = ☃;
this.b = ☃;
this.c = ☃;
}
public azk(int ☃, float ☃) {
this.a = null;
this.d = ☃;
this.e = ☃;
this.b = null;
this.c = null;
}
public int a(float ☃) {
return on.d(this.d + this.d * this.e * ☃);
}
public adi a(Random ☃, azn ☃)
{
if ((this.a == null) || (this.b == null)) {
return null;
}
if (!bac.a(this.c, ☃, ☃)) {
return null;
}
adi ☃ = new adi(this.a);
int ☃ = 0; for (int ☃ = this.b.length; ☃ < ☃; ☃++) {
azr ☃ = this.b[☃];
if (bac.a(☃.a(), ☃, ☃)) {
☃ = ☃.a(☃, ☃, ☃);
}
}
return ☃;
}
public static class a implements JsonDeserializer<azk>, JsonSerializer<azk>
{
public azk a(JsonElement ☃, Type ☃, JsonDeserializationContext ☃) throws JsonParseException {
JsonObject ☃ = od.m(☃, "loot item");
int ☃ = od.n(☃, "weight");
float ☃ = od.a(☃, "luck_multiplier", 0.0F);
if (☃.has("item"))
{
adg ☃ = od.i(☃, "item");
azr[] ☃;
azr[] ☃;
if (☃.has("functions"))
☃ = (azr[])od.a(☃, "functions", ☃, [Lazr.class);
else
☃ = new azr[0];
bab[] ☃;
bab[] ☃;
if (☃.has("conditions"))
☃ = (bab[])od.a(☃, "conditions", ☃, [Lbab.class);
else {
☃ = new bab[0];
}
return new azk(☃, ☃, ☃, ☃, ☃);
}
if (☃.has("functions")) {
throw new JsonParseException("Unexpected 'functions' for empty loot item");
}
if (☃.has("conditions")) {
throw new JsonParseException("Unexpected 'conditions' for empty loot item");
}
return new azk(☃, ☃);
}
public JsonElement a(azk ☃, Type ☃, JsonSerializationContext ☃)
{
JsonObject ☃ = new JsonObject();
if (azk.a(☃) != null) {
kl ☃ = (kl)adg.e.b(azk.a(☃));
if (☃ == null) {
throw new IllegalArgumentException("Can't serialize unknown item " + azk.a(☃));
}
☃.addProperty("item", ☃.toString());
}
if ((azk.b(☃) != null) && (azk.b(☃).length > 0)) {
☃.add("functions", ☃.serialize(azk.b(☃)));
}
if ((azk.c(☃) != null) && (azk.c(☃).length > 0)) {
☃.add("conditions", ☃.serialize(azk.c(☃)));
}
☃.addProperty("weight", Integer.valueOf(azk.d(☃)));
☃.addProperty("luck_multiplier", Float.valueOf(azk.e(☃)));
return ☃;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment