Skip to content

Instantly share code, notes, and snippets.

@jamierocks jamierocks/Blocks.java
Last active Sep 21, 2015

Embed
What would you like to do?
class Block implements MetaOwner<Block> {
}
interface Meta<T> {
void init(Meta<T> meta);
void apply(MetaOwner<T> owner);
void update();
void remove(MetaOwner<T> owner);
}
interface MetaOwner<U> {
<M extends Meta<U>> M obtainMeta(Class<M> cls);
<M extends Meta<U>> M applyMeta(Class<M>... cls);
}
interface DefaultMeta<T> extends Meta<T> {
default void init(Meta<T> meta){
}
default void apply(MetaOwner<T> owner) {
}
default void update() {
}
default void remove(MetaOwner<T> owner) {
}
}
interface EntityMeta extends DefaultMeta<Entity> {
EntityType[] ownedBy();
}
interface BlockMeta extends DefaultMeta<Block> {
Substance[] ownedBy();
}
interface ItemMeta extends DefaultMeta<Item> {
Substance[] ownedBy();
}
class Entity implements MetaOwner<Entity> {
}
class AgeImpl implements Age {
private volatile int age;
@Override public boolean isBaby() {
return age < 68;
}
@Override public int get() {
return age;
}
@Override public void set(int age) {
this.age = age;
}
}
class DisplayNameImpl implements DisplayName {
private volatile String name;
@Override public boolean hasName() {
return name != null;
}
@Override public String get() {
return this.name;
}
@Override public void set(String name) {
this.name = name;
}
}
interface Age extends EntityMeta {
boolean isBaby();
int get();
void set(int age);
}
interface DisplayName extends EntityMeta, ItemMeta {
boolean hasName();
String get();
void set(String name);
}
class Item implements MetaOwner<Item> {
}
// Entities
Entity entity = world.spawn(EntityType.ZOMBIE, position);
Age a = entity.obtainMeta(Age.class);
DisplayName name = entity.obtainMeta(DisplayName.class);
if (a.isBaby()) {
a.set(69);
name.set("Adult");
}
entity.applyMeta(a, name);
// Items
Item item = new Item(Substance.STICK);
DisplayName name = item.obtainMeta(DisplayName.class);
name.set("ayy lmao");
item.applyMeta(name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.