Skip to content

Commit

Permalink
Feat: Основа для набора параметров
Browse files Browse the repository at this point in the history
  • Loading branch information
Reider745 committed Jun 18, 2024
1 parent 1744971 commit 737d7cb
Show file tree
Hide file tree
Showing 18 changed files with 454 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/reider745/InnerCoreServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.google.common.base.Preconditions;
import com.reider745.api.CallbackHelper;
import com.reider745.behavior.BehaviorPack;
import com.reider745.block.CustomBlock;
import com.reider745.commands.CommandsHelper;
import com.reider745.event.EventListener;
Expand Down Expand Up @@ -342,6 +343,8 @@ public void reload() {
}

public void start() {
/*BehaviorPack pack = new BehaviorPack(new File(InnerCoreServer.dataPath, "pack").getAbsolutePath());
pack.load();*/
}

public static Object getProperty(String variable) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/reider745/api/ReflectHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.reider745.api;

import cn.nukkit.entity.Entity;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.nbt.tag.CompoundTag;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/reider745/behavior/BehaviorContent.java
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);
}
73 changes: 73 additions & 0 deletions src/main/java/com/reider745/behavior/BehaviorPack.java
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);
}
}
}
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 src/main/java/com/reider745/behavior/entities/CustomEntity.java
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 src/main/java/com/reider745/behavior/entities/EntitiesContent.java
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();
}
}
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);
}
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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.reider745.behavior.entities;

public abstract class TagContentEntities {
}
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);
}
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;
}
}
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()));
}
}
}
}
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);
}
}
Loading

0 comments on commit 737d7cb

Please sign in to comment.