Skip to content

Commit

Permalink
Merge pull request #7 from Hephaestus-Dev/scripting
Browse files Browse the repository at this point in the history
Adds scriptable BlockFibs and removes the need for manual block tracking
  • Loading branch information
Haven-King authored Apr 22, 2020
2 parents 765a3ca + 3f7c28e commit 1ee6da9
Show file tree
Hide file tree
Showing 29 changed files with 686 additions and 420 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ dependencies {
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

compileOnly "com.google.code.findbugs:jsr305:3.0.2"

// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ yarn_mappings=1.15.2+build.14
loader_version=0.7.8+build.184

# Mod Properties
mod_version = 0.0.7
mod_version = 0.0.9
maven_group = dev.hephaestus
archives_base_name = FibLib

Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.
87 changes: 87 additions & 0 deletions src/main/java/dev/hephaestus/fiblib/CompositeMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package dev.hephaestus.fiblib;

import java.io.InvalidObjectException;
import java.util.*;

public class CompositeMap<V> {
private final HashMap<Key, V> contents = new HashMap<>();
private final Class[] keySchema;

public CompositeMap(Class... keyTypes) {
keySchema = new Class[keyTypes.length];
int i = 0;
for (Class c : keyTypes) keySchema[i++] = c;
}

// Backwards from normal maps so we can use VarArgs :)
public V put(V entry, Object... keys) throws InvalidObjectException {
Key key = new Key(keys);
if (!key.fitsSchema(keySchema)) throw new InvalidObjectException("Illegal key: " + key.keys.toString());
contents.put(key, entry);
return contents.get(key);
}

public V put(V entry, Key key) {
contents.put(key, entry);
return contents.get(key);
}

public V get(Object... keys) throws InvalidObjectException {
Key key = new Key(keys);
if (!key.fitsSchema(keySchema)) throw new InvalidObjectException("Illegal key: " + key.keys.toString());
return contents.get(key);
}

public V getOrDefault(V d, Object... keys) throws InvalidObjectException {
Key key = new Key(keys);
if (!key.fitsSchema(keySchema)) throw new InvalidObjectException("Illegal key: " + key.keys.toString());
return contents.getOrDefault(key, d);
}

public Set<Map.Entry<Key, V>> entrySet() {
return contents.entrySet();
}

public void clear() {
this.contents.clear();
}

public boolean containsKey(Object... keys) {
return contents.containsKey(new Key(keys));
}

public static class Key {
private final ArrayList<Object> keys = new ArrayList<>();

private Key(Object... keys) {
this.keys.addAll(Arrays.asList(keys));
}

Object get(int index) {
return keys.get(index);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
return keys.equals(key.keys);
}

@Override
public int hashCode() {
return Objects.hash(keys);
}

private boolean fitsSchema(Class[] schema) {
if (schema.length != keys.size()) return false;

for (int i = 0; i < schema.length; ++i) {
if (!(keys.get(i).getClass() == schema[i])) return false;
}

return true;
}
}
}
Loading

0 comments on commit 1ee6da9

Please sign in to comment.