-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
454 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.reider745.behavior; | ||
|
||
import org.json.JSONObject; | ||
|
||
public abstract class BehaviorContent { | ||
public abstract String getTagName(); | ||
public abstract String getDefaultDirectory(); | ||
public abstract void load(JSONObject json); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.reider745.behavior; | ||
|
||
import com.reider745.behavior.entities.EntitiesContent; | ||
import com.zhekasmirnov.horizon.runtime.logger.Logger; | ||
import com.zhekasmirnov.horizon.util.FileUtils; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
|
||
public class BehaviorPack { | ||
private static final ArrayList<BehaviorContent> contents_api = new ArrayList<>(); | ||
|
||
static { | ||
contents_api.add(new EntitiesContent()); | ||
|
||
} | ||
|
||
private final String path; | ||
|
||
public BehaviorPack(String path){ | ||
this.path = path + "/"; | ||
} | ||
|
||
public void load(){ | ||
Logger.info("Loaded behavior-pack"); | ||
|
||
final File contentsFile = new File(path + "contents.json"); | ||
if(contentsFile.exists()) { | ||
try { | ||
final JSONObject contents = FileUtils.readJSON(contentsFile); | ||
if (contents.has("content")) { | ||
JSONArray content = contents.getJSONArray("content"); | ||
|
||
for (int i = 0; i < content.length(); i++) { | ||
JSONObject json = content.getJSONObject(i); | ||
|
||
loadFileContext(this.path + json.getString("path")); | ||
} | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
for(BehaviorContent content : contents_api){ | ||
final String pathFolder = this.path + content.getDefaultDirectory(); | ||
final String[] files = new File(pathFolder).list(); | ||
for(String file : files) | ||
loadFileContext(pathFolder+"/"+file); | ||
} | ||
} | ||
|
||
|
||
public void loadFileContext(String path){ | ||
try{ | ||
final JSONObject contents = FileUtils.readJSON(new File(path)); | ||
|
||
for(BehaviorContent content : contents_api){ | ||
if(contents.has(content.getTagName())){ | ||
content.load(contents); | ||
return; | ||
} | ||
} | ||
|
||
throw new RuntimeException("Not support "+path); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/reider745/behavior/entities/ComponentsTag.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.reider745.behavior.entities; | ||
|
||
public abstract class ComponentsTag { | ||
public abstract String getNameTag(); | ||
|
||
public void load(EntityContentFactory factory, Object json){ | ||
|
||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/com/reider745/behavior/entities/CustomEntity.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.reider745.behavior.entities; | ||
|
||
import cn.nukkit.entity.Entity; | ||
import cn.nukkit.entity.custom.EntityDefinition; | ||
import cn.nukkit.level.format.FullChunk; | ||
import cn.nukkit.nbt.tag.CompoundTag; | ||
import cn.nukkit.network.protocol.AddEntityPacket; | ||
import cn.nukkit.network.protocol.DataPacket; | ||
|
||
|
||
public class CustomEntity extends Entity implements cn.nukkit.entity.custom.CustomEntity { | ||
public CustomEntity(FullChunk chunk, CompoundTag nbt) { | ||
super(chunk, nbt); | ||
} | ||
|
||
protected EntityContentFactory factory; | ||
public EntityContentFactory getFactory(){ | ||
if(factory != null) | ||
return factory; | ||
|
||
factory = EntityContentFactory.factoryMap.get(this.namedTag.getString("___id___")); | ||
return factory; | ||
} | ||
|
||
|
||
@Override | ||
public float getWidth() { | ||
return getFactory().collision.width; | ||
} | ||
|
||
@Override | ||
public float getLength() { | ||
return this.getWidth(); | ||
} | ||
|
||
@Override | ||
public float getHeight() { | ||
return getFactory().collision.height; | ||
} | ||
|
||
@Override | ||
public EntityDefinition getEntityDefinition() { | ||
return getFactory().getEntityDefinition(); | ||
} | ||
|
||
@Override | ||
protected DataPacket createAddEntityPacket() { | ||
AddEntityPacket packet = (AddEntityPacket) super.createAddEntityPacket(); | ||
packet.id = getFactory().description.identifier; | ||
return packet; | ||
} | ||
|
||
@Override | ||
public int getNetworkId() { | ||
return -1; | ||
} | ||
|
||
@Override | ||
public void setOnFire(int seconds) { | ||
if(!factory.isFireImmune) | ||
super.setOnFire(seconds); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/reider745/behavior/entities/EntitiesContent.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.reider745.behavior.entities; | ||
|
||
import com.reider745.behavior.BehaviorContent; | ||
import com.reider745.behavior.entities.formats.EntitiesVersion113; | ||
import org.json.JSONObject; | ||
|
||
import java.util.HashMap; | ||
|
||
public class EntitiesContent extends BehaviorContent { | ||
private static final HashMap<String, EntitiesVersion> versions = new HashMap<>(); | ||
|
||
static { | ||
versions.put("1.13.0", new EntitiesVersion113()); | ||
} | ||
|
||
@Override | ||
public String getTagName() { | ||
return "minecraft:entity"; | ||
} | ||
|
||
@Override | ||
public String getDefaultDirectory() { | ||
return "entities"; | ||
} | ||
|
||
@Override | ||
public void load(JSONObject json) { | ||
final String version_name = json.getString("format_version"); | ||
final EntitiesVersion version = versions.get(version_name); | ||
if(version == null) | ||
throw new RuntimeException("not support version "+version_name); | ||
|
||
version.load(json.getJSONObject(getTagName())).build(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/reider745/behavior/entities/EntitiesVersion.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.reider745.behavior.entities; | ||
|
||
import org.json.JSONObject; | ||
|
||
public abstract class EntitiesVersion { | ||
public abstract EntityContentFactory load(JSONObject json); | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/reider745/behavior/entities/EntityContentFactory.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.reider745.behavior.entities; | ||
|
||
import cn.nukkit.entity.Entity; | ||
import cn.nukkit.entity.custom.EntityDefinition; | ||
import cn.nukkit.entity.custom.EntityManager; | ||
import cn.nukkit.level.Position; | ||
import com.reider745.behavior.entities.formats.tags.DescriptionsFactory; | ||
import com.reider745.behavior.entities.formats.tags.components.CollisionComponent; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
public class EntityContentFactory { | ||
public DescriptionsFactory.DescriptionTag description; | ||
public CollisionComponent collision; | ||
private ArrayList<ComponentsTag> components = new ArrayList<>(); | ||
|
||
public boolean isFireImmune = false; | ||
|
||
public static final HashMap<String, EntityContentFactory> factoryMap = new HashMap<>(); | ||
|
||
public EntityDefinition getEntityDefinition() { | ||
return new EntityDefinition( | ||
description.identifier, | ||
null, | ||
false, | ||
description.identifier, | ||
CustomEntity.class | ||
); | ||
} | ||
|
||
public void build(){ | ||
factoryMap.put(description.identifier, this); | ||
Entity.registerEntity(description.identifier, CustomEntity.class); | ||
EntityManager.get() | ||
.registerDefinition(this.getEntityDefinition()); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/reider745/behavior/entities/TagContentEntities.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.reider745.behavior.entities; | ||
|
||
public abstract class TagContentEntities { | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/reider745/behavior/entities/TagContentFactory.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.reider745.behavior.entities; | ||
|
||
import com.reider745.behavior.entities.EntityContentFactory; | ||
import org.json.JSONObject; | ||
|
||
public abstract class TagContentFactory { | ||
public abstract String getNameTag(); | ||
public abstract void loadJSON(EntityContentFactory factory, JSONObject json); | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/reider745/behavior/entities/formats/EntitiesVersion113.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.reider745.behavior.entities.formats; | ||
|
||
import com.reider745.behavior.entities.EntitiesVersion; | ||
import com.reider745.behavior.entities.EntityContentFactory; | ||
import com.reider745.behavior.entities.TagContentFactory; | ||
import com.reider745.behavior.entities.formats.tags.ComponentsFactory; | ||
import com.reider745.behavior.entities.formats.tags.DescriptionsFactory; | ||
import org.json.JSONObject; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class EntitiesVersion113 extends EntitiesVersion { | ||
private static final ArrayList<TagContentFactory> tags = new ArrayList<>(); | ||
|
||
static { | ||
tags.add(new DescriptionsFactory()); | ||
tags.add(new ComponentsFactory()); | ||
} | ||
|
||
@Override | ||
public EntityContentFactory load(JSONObject json) { | ||
final EntityContentFactory entityFactory = new EntityContentFactory(); | ||
|
||
for(TagContentFactory factory : tags){ | ||
if(json.has(factory.getNameTag())){ | ||
factory.loadJSON(entityFactory, json.getJSONObject(factory.getNameTag())); | ||
} | ||
} | ||
|
||
return entityFactory; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/reider745/behavior/entities/formats/tags/ComponentsFactory.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.reider745.behavior.entities.formats.tags; | ||
|
||
import com.reider745.behavior.entities.ComponentsTag; | ||
import com.reider745.behavior.entities.EntityContentFactory; | ||
import com.reider745.behavior.entities.TagContentFactory; | ||
import com.reider745.behavior.entities.formats.tags.components.CollisionComponent; | ||
import com.reider745.behavior.entities.formats.tags.components.FireImmuneComponent; | ||
import org.json.JSONObject; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class ComponentsFactory extends TagContentFactory { | ||
public static ArrayList<ComponentsTag> tags = new ArrayList<>(); | ||
|
||
static { | ||
tags.add(new FireImmuneComponent()); | ||
tags.add(new CollisionComponent()); | ||
} | ||
|
||
@Override | ||
public String getNameTag() { | ||
return "components"; | ||
} | ||
|
||
@Override | ||
public void loadJSON(EntityContentFactory entityFactory, JSONObject json) { | ||
for(ComponentsTag tag : tags){ | ||
if(json.has(tag.getNameTag())){ | ||
tag.load(entityFactory, json.get(tag.getNameTag())); | ||
} | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/reider745/behavior/entities/formats/tags/DescriptionsFactory.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.reider745.behavior.entities.formats.tags; | ||
|
||
import com.reider745.behavior.entities.EntityContentFactory; | ||
import com.reider745.behavior.entities.TagContentEntities; | ||
import com.reider745.behavior.entities.TagContentFactory; | ||
import org.json.JSONObject; | ||
|
||
public class DescriptionsFactory extends TagContentFactory { | ||
public static class DescriptionTag extends TagContentEntities{ | ||
public String identifier; | ||
public boolean isSpawnable, isSummonable, isExperimental; | ||
|
||
private DescriptionTag(JSONObject json){ | ||
identifier = json.getString("identifier"); | ||
isSpawnable = json.getBoolean("is_spawnable"); | ||
isSummonable = json.getBoolean("is_summonable"); | ||
isExperimental = json.getBoolean("is_experimental"); | ||
|
||
identifier = Character.toUpperCase(identifier.charAt(0)) + identifier.substring(1); | ||
} | ||
} | ||
|
||
@Override | ||
public String getNameTag() { | ||
return "description"; | ||
} | ||
|
||
@Override | ||
public void loadJSON(EntityContentFactory entityFactory, JSONObject json) { | ||
entityFactory.description = new DescriptionTag(json); | ||
} | ||
} |
Oops, something went wrong.