Last active
September 21, 2015 21:44
-
-
Save jamierocks/4f05fee484b6211c81f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Block implements MetaOwner<Block> { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Entity implements MetaOwner<Entity> { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Item implements MetaOwner<Item> { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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