From 16fe5463a226baf9e114fe8c888d6d5f5e173f6d Mon Sep 17 00:00:00 2001 From: Reider745 <70357814+Reider745@users.noreply.github.com> Date: Wed, 19 Jun 2024 11:42:36 +0300 Subject: [PATCH] =?UTF-8?q?Feat:=20=D0=A0=D0=B0=D1=81=D1=88=D0=B8=D1=80?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80?= =?UTF-8?q?=D0=B6=D0=BA=D0=B8=20=D1=82=D0=B5=D0=B3=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Исправлена ошибка null exception от EntityDeathEvent --- .../java/com/reider745/InnerCoreServer.java | 4 +- .../com/reider745/behavior/BehaviorPack.java | 33 ++++- .../behavior/entities/ComponentsTag.java | 3 + .../behavior/entities/CustomEntity.java | 114 +++++++++++++++++- .../behavior/entities/EntitiesContent.java | 2 + .../behavior/entities/EntitiesVersion.java | 3 + .../entities/EntityContentFactory.java | 26 +++- .../entities/formats/EntitiesVersion113.java | 29 +---- .../entities/formats/EntitiesVersion116.java | 4 + .../entities/formats/EntitiesVersion18.java | 38 ++++++ .../formats/tags/ComponentsFactory.java | 9 +- .../formats/tags/DescriptionsFactory.java | 4 +- .../tags/components/CollisionComponent.java | 2 +- .../tags/components/GravityComponent.java | 16 +++ .../tags/components/HealthComponent.java | 27 +++++ .../tags/components/JumpComponent.java | 16 +++ .../components/MovementBasicComponent.java | 16 +++ .../tags/components/MovementComponent.java | 21 ++++ .../navigation/NavigationComponentsTag.java | 8 ++ .../navigation/NavigationRandomStroll.java | 39 ++++++ .../com/reider745/event/EventListener.java | 2 +- .../com/reider745/hooks/EntityOverrides.java | 8 +- 22 files changed, 380 insertions(+), 44 deletions(-) create mode 100644 src/main/java/com/reider745/behavior/entities/formats/EntitiesVersion116.java create mode 100644 src/main/java/com/reider745/behavior/entities/formats/EntitiesVersion18.java create mode 100644 src/main/java/com/reider745/behavior/entities/formats/tags/components/GravityComponent.java create mode 100644 src/main/java/com/reider745/behavior/entities/formats/tags/components/HealthComponent.java create mode 100644 src/main/java/com/reider745/behavior/entities/formats/tags/components/JumpComponent.java create mode 100644 src/main/java/com/reider745/behavior/entities/formats/tags/components/MovementBasicComponent.java create mode 100644 src/main/java/com/reider745/behavior/entities/formats/tags/components/MovementComponent.java create mode 100644 src/main/java/com/reider745/behavior/entities/formats/tags/components/navigation/NavigationComponentsTag.java create mode 100644 src/main/java/com/reider745/behavior/entities/formats/tags/components/navigation/NavigationRandomStroll.java diff --git a/src/main/java/com/reider745/InnerCoreServer.java b/src/main/java/com/reider745/InnerCoreServer.java index 63f309939..e03ca4001 100644 --- a/src/main/java/com/reider745/InnerCoreServer.java +++ b/src/main/java/com/reider745/InnerCoreServer.java @@ -343,8 +343,8 @@ public void reload() { } public void start() { - /*BehaviorPack pack = new BehaviorPack(new File(InnerCoreServer.dataPath, "pack").getAbsolutePath()); - pack.load();*/ + BehaviorPack pack = new BehaviorPack(new File(InnerCoreServer.dataPath, "pack").getAbsolutePath()); + pack.load(); } public static Object getProperty(String variable) { diff --git a/src/main/java/com/reider745/behavior/BehaviorPack.java b/src/main/java/com/reider745/behavior/BehaviorPack.java index e1c85176b..d2f5cb7b3 100644 --- a/src/main/java/com/reider745/behavior/BehaviorPack.java +++ b/src/main/java/com/reider745/behavior/BehaviorPack.java @@ -15,17 +15,46 @@ public class BehaviorPack { static { contents_api.add(new EntitiesContent()); - } private final String path; + public String name = "Not-load"; public BehaviorPack(String path){ this.path = path + "/"; } + protected void loadManifest2(JSONObject json){ + loadManifest1(json); + } + + protected void loadManifest1(JSONObject json){ + final JSONObject header = json.getJSONObject("header"); + + name = header.getString("name"); + } + + protected void loadManifest(){ + File file = new File(this.path+"manifest.json"); + if(file.exists()){ + try { + final JSONObject json = FileUtils.readJSON(file); + final int format = json.getInt("format_version"); + switch (format){ + case 1 -> loadManifest1(json); + case 2 -> loadManifest2(json);//Отличий не нашел + default -> throw new RuntimeException("Not support manifest version "+format); + } + }catch (Exception e){ + throw new RuntimeException(e); + } + } + } + public void load(){ - Logger.info("Loaded behavior-pack"); + loadManifest(); + + Logger.warning("Loaded behavior-pack "+name); final File contentsFile = new File(path + "contents.json"); if(contentsFile.exists()) { diff --git a/src/main/java/com/reider745/behavior/entities/ComponentsTag.java b/src/main/java/com/reider745/behavior/entities/ComponentsTag.java index e1e9671e4..528a3afe4 100644 --- a/src/main/java/com/reider745/behavior/entities/ComponentsTag.java +++ b/src/main/java/com/reider745/behavior/entities/ComponentsTag.java @@ -2,6 +2,9 @@ public abstract class ComponentsTag { public abstract String getNameTag(); + public int getPriority(){ + return 0; + } public void load(EntityContentFactory factory, Object json){ diff --git a/src/main/java/com/reider745/behavior/entities/CustomEntity.java b/src/main/java/com/reider745/behavior/entities/CustomEntity.java index c0d776396..69fc44007 100644 --- a/src/main/java/com/reider745/behavior/entities/CustomEntity.java +++ b/src/main/java/com/reider745/behavior/entities/CustomEntity.java @@ -1,27 +1,102 @@ package com.reider745.behavior.entities; -import cn.nukkit.entity.Entity; +import cn.nukkit.block.BlockID; +import cn.nukkit.entity.BaseEntity; import cn.nukkit.entity.custom.EntityDefinition; import cn.nukkit.level.format.FullChunk; +import cn.nukkit.math.Vector3; import cn.nukkit.nbt.tag.CompoundTag; import cn.nukkit.network.protocol.AddEntityPacket; import cn.nukkit.network.protocol.DataPacket; +import com.reider745.behavior.entities.formats.tags.components.navigation.NavigationComponentsTag; +import java.util.Optional; +import java.util.Random; -public class CustomEntity extends Entity implements cn.nukkit.entity.custom.CustomEntity { +public class CustomEntity extends BaseEntity implements cn.nukkit.entity.custom.CustomEntity { public CustomEntity(FullChunk chunk, CompoundTag nbt) { super(chunk, nbt); } + private Vector3 targetPos = null; + + @Override + public Vector3 updateMove(int tickDiff) { + if (this.isMovement() && !isImmobile()) { + this.motionX = 0; + this.motionY = 0; + this.motionZ = 0; + + final double gravity_value = this.getGravity() * 4; + final Vector3 pos = this.getPosition(); + + if(factory.isPhysics && this.level.getBlockIdAt((int) pos.x, (int) (pos.y - gravity_value), (int) pos.z) == BlockID.AIR) {// Гравитация + this.motionY -= gravity_value; + + this.move(this.motionX, this.motionY, this.motionZ); + this.updateMovement(); + return null; + } + + if(factory.isMovement){// Движение + if(targetPos == null) + targetPositionMovementUpdate(); + + + + if(pos.distance(targetPos) < .4){ + targetPos = null; + }else{ + final Vector3 move = pos.subtract(targetPos). + divide(targetPos.distance(pos) * 2); + + motionX += move.x * factory.movement; + motionZ += move.z * factory.movement; + } + } + + final Vector3 move = new Vector3(this.motionX, this.motionY, this.motionZ); + + this.move(this.motionX, this.motionY, this.motionZ); + this.updateMovement(); + + return pos.add(move); + } + return null; + } + + public void targetPositionMovementUpdate(){// Определение новой позиции + final Random random = new Random(); + final Vector3 target = this.getPosition().clone(); + + + + target.x += random.nextInt(5) - random.nextInt(5); + target.y += random.nextInt(5) - random.nextInt(5); + target.z += random.nextInt(5) - random.nextInt(5); + + this.targetPos = target; + } + + @Override + public int getKillExperience() { + return 0; + } + + protected EntityContentFactory factory; public EntityContentFactory getFactory(){ if(factory != null) return factory; factory = EntityContentFactory.factoryMap.get(this.namedTag.getString("___id___")); + initDef(); return factory; } + private void initDef(){ + this.setHealth(factory.health.defaultValue); + } @Override public float getWidth() { @@ -38,6 +113,11 @@ public float getHeight() { return getFactory().collision.height; } + @Override + public int getMaxHealth() { + return getFactory().health.max; + } + @Override public EntityDefinition getEntityDefinition() { return getFactory().getEntityDefinition(); @@ -50,6 +130,32 @@ protected DataPacket createAddEntityPacket() { return packet; } + @Override + public boolean onUpdate(int currentTick) { + if (this.closed) { + return false; + } + + for(NavigationComponentsTag tag : factory.navigations_compononets) + tag.update(this); + + if (!this.isAlive()) { + if (++this.deadTicks >= 23) { + this.close(); + return false; + } + return true; + } + + int tickDiff = currentTick - this.lastUpdate; + this.lastUpdate = currentTick; + + Optional.ofNullable(this.updateMove(tickDiff)) + .ifPresent(this::lookAt); + + return true; + } + @Override public int getNetworkId() { return -1; @@ -60,4 +166,8 @@ public void setOnFire(int seconds) { if(!factory.isFireImmune) super.setOnFire(seconds); } + + public boolean isInTickingRange() { + return true; + } } diff --git a/src/main/java/com/reider745/behavior/entities/EntitiesContent.java b/src/main/java/com/reider745/behavior/entities/EntitiesContent.java index 4237daa2e..38a78e1e3 100644 --- a/src/main/java/com/reider745/behavior/entities/EntitiesContent.java +++ b/src/main/java/com/reider745/behavior/entities/EntitiesContent.java @@ -10,7 +10,9 @@ public class EntitiesContent extends BehaviorContent { private static final HashMap versions = new HashMap<>(); static { + versions.put("1.8.0", new EntitiesVersion113()); versions.put("1.13.0", new EntitiesVersion113()); + versions.put("1.16.0", new EntitiesVersion113()); } @Override diff --git a/src/main/java/com/reider745/behavior/entities/EntitiesVersion.java b/src/main/java/com/reider745/behavior/entities/EntitiesVersion.java index 7e23578ae..65e73e6ec 100644 --- a/src/main/java/com/reider745/behavior/entities/EntitiesVersion.java +++ b/src/main/java/com/reider745/behavior/entities/EntitiesVersion.java @@ -2,6 +2,9 @@ import org.json.JSONObject; +import java.util.ArrayList; + public abstract class EntitiesVersion { + public abstract ArrayList getListTags(); public abstract EntityContentFactory load(JSONObject json); } diff --git a/src/main/java/com/reider745/behavior/entities/EntityContentFactory.java b/src/main/java/com/reider745/behavior/entities/EntityContentFactory.java index 17a38c259..eeff0dcad 100644 --- a/src/main/java/com/reider745/behavior/entities/EntityContentFactory.java +++ b/src/main/java/com/reider745/behavior/entities/EntityContentFactory.java @@ -6,19 +6,38 @@ import cn.nukkit.level.Position; import com.reider745.behavior.entities.formats.tags.DescriptionsFactory; import com.reider745.behavior.entities.formats.tags.components.CollisionComponent; +import com.reider745.behavior.entities.formats.tags.components.HealthComponent; +import com.reider745.behavior.entities.formats.tags.components.navigation.NavigationComponentsTag; +import com.zhekasmirnov.horizon.runtime.logger.Logger; import java.util.ArrayList; import java.util.HashMap; public class EntityContentFactory { public DescriptionsFactory.DescriptionTag description; - public CollisionComponent collision; - private ArrayList components = new ArrayList<>(); + public CollisionComponent collision = new CollisionComponent(); + public HealthComponent health = new HealthComponent(); + public ArrayList navigations_compononets = new ArrayList<>(); public boolean isFireImmune = false; + public boolean isJump = false; + public boolean isMovement = false; + public boolean isPhysics = false; + + public float movement = .5f; public static final HashMap factoryMap = new HashMap<>(); + public void addNavigationComponent(NavigationComponentsTag tag){ + for (int i = 0; i < navigations_compononets.size(); i++) { + if (navigations_compononets.get(i).getPriority() < tag.getPriority()) { + navigations_compononets.add(i, tag); + return; + } + } + navigations_compononets.add(tag); + } + public EntityDefinition getEntityDefinition() { return new EntityDefinition( description.identifier, @@ -30,8 +49,9 @@ public EntityDefinition getEntityDefinition() { } public void build(){ + Logger.info("Register entity "+description.identifier); + factoryMap.put(description.identifier, this); - Entity.registerEntity(description.identifier, CustomEntity.class); EntityManager.get() .registerDefinition(this.getEntityDefinition()); } diff --git a/src/main/java/com/reider745/behavior/entities/formats/EntitiesVersion113.java b/src/main/java/com/reider745/behavior/entities/formats/EntitiesVersion113.java index 3f3c74904..5766c2a3e 100644 --- a/src/main/java/com/reider745/behavior/entities/formats/EntitiesVersion113.java +++ b/src/main/java/com/reider745/behavior/entities/formats/EntitiesVersion113.java @@ -1,32 +1,5 @@ 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; +public class EntitiesVersion113 extends EntitiesVersion18 { -import java.util.ArrayList; - -public class EntitiesVersion113 extends EntitiesVersion { - private static final ArrayList 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; - } } diff --git a/src/main/java/com/reider745/behavior/entities/formats/EntitiesVersion116.java b/src/main/java/com/reider745/behavior/entities/formats/EntitiesVersion116.java new file mode 100644 index 000000000..32a22745b --- /dev/null +++ b/src/main/java/com/reider745/behavior/entities/formats/EntitiesVersion116.java @@ -0,0 +1,4 @@ +package com.reider745.behavior.entities.formats; + +public class EntitiesVersion116 extends EntitiesVersion113 { +} diff --git a/src/main/java/com/reider745/behavior/entities/formats/EntitiesVersion18.java b/src/main/java/com/reider745/behavior/entities/formats/EntitiesVersion18.java new file mode 100644 index 000000000..002a0e8f4 --- /dev/null +++ b/src/main/java/com/reider745/behavior/entities/formats/EntitiesVersion18.java @@ -0,0 +1,38 @@ +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 com.reider745.behavior.entities.formats.tags.components.HealthComponent; +import org.json.JSONObject; + +import java.util.ArrayList; + +public class EntitiesVersion18 extends EntitiesVersion { + private static final ArrayList tags = new ArrayList<>(); + + static { + tags.add(new DescriptionsFactory()); + tags.add(new ComponentsFactory()); + } + + @Override + public ArrayList getListTags() { + return tags; + } + + @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; + } +} \ No newline at end of file diff --git a/src/main/java/com/reider745/behavior/entities/formats/tags/ComponentsFactory.java b/src/main/java/com/reider745/behavior/entities/formats/tags/ComponentsFactory.java index 85d8c1742..28373b6f2 100644 --- a/src/main/java/com/reider745/behavior/entities/formats/tags/ComponentsFactory.java +++ b/src/main/java/com/reider745/behavior/entities/formats/tags/ComponentsFactory.java @@ -3,8 +3,7 @@ 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 com.reider745.behavior.entities.formats.tags.components.*; import org.json.JSONObject; import java.util.ArrayList; @@ -15,6 +14,12 @@ public class ComponentsFactory extends TagContentFactory { static { tags.add(new FireImmuneComponent()); tags.add(new CollisionComponent()); + tags.add(new HealthComponent()); + + tags.add(new GravityComponent()); + tags.add(new JumpComponent()); + tags.add(new MovementBasicComponent()); + tags.add(new MovementComponent()); } @Override diff --git a/src/main/java/com/reider745/behavior/entities/formats/tags/DescriptionsFactory.java b/src/main/java/com/reider745/behavior/entities/formats/tags/DescriptionsFactory.java index bc2969e4d..6ac9c7425 100644 --- a/src/main/java/com/reider745/behavior/entities/formats/tags/DescriptionsFactory.java +++ b/src/main/java/com/reider745/behavior/entities/formats/tags/DescriptionsFactory.java @@ -12,11 +12,11 @@ public static class DescriptionTag extends TagContentEntities{ private DescriptionTag(JSONObject json){ identifier = json.getString("identifier"); + + // TODO: Не поддерживается isSpawnable = json.getBoolean("is_spawnable"); isSummonable = json.getBoolean("is_summonable"); isExperimental = json.getBoolean("is_experimental"); - - identifier = Character.toUpperCase(identifier.charAt(0)) + identifier.substring(1); } } diff --git a/src/main/java/com/reider745/behavior/entities/formats/tags/components/CollisionComponent.java b/src/main/java/com/reider745/behavior/entities/formats/tags/components/CollisionComponent.java index 162d7ca09..b72c38928 100644 --- a/src/main/java/com/reider745/behavior/entities/formats/tags/components/CollisionComponent.java +++ b/src/main/java/com/reider745/behavior/entities/formats/tags/components/CollisionComponent.java @@ -6,7 +6,7 @@ import org.json.JSONObject; public class CollisionComponent extends ComponentsTag { - public int width, height; + public int width = 1, height = 2; @Override public String getNameTag() { diff --git a/src/main/java/com/reider745/behavior/entities/formats/tags/components/GravityComponent.java b/src/main/java/com/reider745/behavior/entities/formats/tags/components/GravityComponent.java new file mode 100644 index 000000000..57c443fb9 --- /dev/null +++ b/src/main/java/com/reider745/behavior/entities/formats/tags/components/GravityComponent.java @@ -0,0 +1,16 @@ +package com.reider745.behavior.entities.formats.tags.components; + +import com.reider745.behavior.entities.ComponentsTag; +import com.reider745.behavior.entities.EntityContentFactory; + +public class GravityComponent extends ComponentsTag { + @Override + public String getNameTag() { + return "minecraft:physics"; + } + + @Override + public void load(EntityContentFactory factory, Object json) { + factory.isPhysics = true; + } +} diff --git a/src/main/java/com/reider745/behavior/entities/formats/tags/components/HealthComponent.java b/src/main/java/com/reider745/behavior/entities/formats/tags/components/HealthComponent.java new file mode 100644 index 000000000..b9ba0bf54 --- /dev/null +++ b/src/main/java/com/reider745/behavior/entities/formats/tags/components/HealthComponent.java @@ -0,0 +1,27 @@ +package com.reider745.behavior.entities.formats.tags.components; + +import com.reider745.behavior.entities.ComponentsTag; +import com.reider745.behavior.entities.EntityContentFactory; +import com.zhekasmirnov.horizon.runtime.logger.Logger; +import org.json.JSONObject; + +public class HealthComponent extends ComponentsTag { + public int defaultValue = 20; + public int max = 20; + + @Override + public String getNameTag() { + return "minecraft:health"; + } + + @Override + public void load(EntityContentFactory factory, Object json) { + if(json instanceof JSONObject object){ + defaultValue = object.getInt("value"); + max = object.getInt("max"); + + factory.health = this; + }else + Logger.warning("Not support HealthComponent " + json.getClass()); + } +} diff --git a/src/main/java/com/reider745/behavior/entities/formats/tags/components/JumpComponent.java b/src/main/java/com/reider745/behavior/entities/formats/tags/components/JumpComponent.java new file mode 100644 index 000000000..dadc2843b --- /dev/null +++ b/src/main/java/com/reider745/behavior/entities/formats/tags/components/JumpComponent.java @@ -0,0 +1,16 @@ +package com.reider745.behavior.entities.formats.tags.components; + +import com.reider745.behavior.entities.ComponentsTag; +import com.reider745.behavior.entities.EntityContentFactory; + +public class JumpComponent extends ComponentsTag { + @Override + public String getNameTag() { + return "minecraft:jump.static"; + } + + @Override + public void load(EntityContentFactory factory, Object json) { + factory.isJump = true; + } +} diff --git a/src/main/java/com/reider745/behavior/entities/formats/tags/components/MovementBasicComponent.java b/src/main/java/com/reider745/behavior/entities/formats/tags/components/MovementBasicComponent.java new file mode 100644 index 000000000..0cd96c12f --- /dev/null +++ b/src/main/java/com/reider745/behavior/entities/formats/tags/components/MovementBasicComponent.java @@ -0,0 +1,16 @@ +package com.reider745.behavior.entities.formats.tags.components; + +import com.reider745.behavior.entities.ComponentsTag; +import com.reider745.behavior.entities.EntityContentFactory; + +public class MovementBasicComponent extends ComponentsTag { + @Override + public String getNameTag() { + return "minecraft:movement.basic"; + } + + @Override + public void load(EntityContentFactory factory, Object json) { + factory.isMovement = true; + } +} diff --git a/src/main/java/com/reider745/behavior/entities/formats/tags/components/MovementComponent.java b/src/main/java/com/reider745/behavior/entities/formats/tags/components/MovementComponent.java new file mode 100644 index 000000000..096df2668 --- /dev/null +++ b/src/main/java/com/reider745/behavior/entities/formats/tags/components/MovementComponent.java @@ -0,0 +1,21 @@ +package com.reider745.behavior.entities.formats.tags.components; + +import com.reider745.behavior.entities.ComponentsTag; +import com.reider745.behavior.entities.EntityContentFactory; +import com.zhekasmirnov.horizon.runtime.logger.Logger; +import org.json.JSONObject; + +public class MovementComponent extends ComponentsTag { + @Override + public String getNameTag() { + return "minecraft:movement"; + } + + @Override + public void load(EntityContentFactory factory, Object json) { + if(json instanceof JSONObject object){ + factory.movement = object.getFloat("value"); + }else + Logger.warning("Not support MovementComponent "+json.getClass()); + } +} diff --git a/src/main/java/com/reider745/behavior/entities/formats/tags/components/navigation/NavigationComponentsTag.java b/src/main/java/com/reider745/behavior/entities/formats/tags/components/navigation/NavigationComponentsTag.java new file mode 100644 index 000000000..344e65dd1 --- /dev/null +++ b/src/main/java/com/reider745/behavior/entities/formats/tags/components/navigation/NavigationComponentsTag.java @@ -0,0 +1,8 @@ +package com.reider745.behavior.entities.formats.tags.components.navigation; + +import com.reider745.behavior.entities.ComponentsTag; +import com.reider745.behavior.entities.CustomEntity; + +public abstract class NavigationComponentsTag extends ComponentsTag { + public abstract void update(CustomEntity entity); +} diff --git a/src/main/java/com/reider745/behavior/entities/formats/tags/components/navigation/NavigationRandomStroll.java b/src/main/java/com/reider745/behavior/entities/formats/tags/components/navigation/NavigationRandomStroll.java new file mode 100644 index 000000000..ec3d3d73d --- /dev/null +++ b/src/main/java/com/reider745/behavior/entities/formats/tags/components/navigation/NavigationRandomStroll.java @@ -0,0 +1,39 @@ +package com.reider745.behavior.entities.formats.tags.components.navigation; + +import cn.nukkit.block.Block; +import cn.nukkit.block.BlockID; +import cn.nukkit.level.Level; +import cn.nukkit.math.Vector3; +import com.reider745.behavior.entities.CustomEntity; +import com.reider745.behavior.entities.EntityContentFactory; +import com.zhekasmirnov.horizon.runtime.logger.Logger; +import org.json.JSONObject; + +public class NavigationRandomStroll extends NavigationComponentsTag { + public int priority; + public double speed_multiplier; + + @Override + public String getNameTag() { + return "minecraft:behavior.random_stroll"; + } + + @Override + public int getPriority() { + return priority; + } + + @Override + public void load(EntityContentFactory factory, Object json) { + if(json instanceof JSONObject object){ + priority = object.getInt("priority"); + speed_multiplier = object.getInt("speed_multiplier"); + }else + Logger.warning("Not support NavigationRandomStroll "+json.getClass()); + } + + @Override + public void update(CustomEntity entity) { + //entity.set + } +} diff --git a/src/main/java/com/reider745/event/EventListener.java b/src/main/java/com/reider745/event/EventListener.java index 68fcaa2e7..31c4286ba 100644 --- a/src/main/java/com/reider745/event/EventListener.java +++ b/src/main/java/com/reider745/event/EventListener.java @@ -314,7 +314,7 @@ public void onEntityDeath(EntityDeathEvent event) { ? damageByEntityEvent.getDamager() : null; - if(damageEvent.getCause() == DamageCause.ENTITY_EXPLOSION && + if(damageEvent != null && damageEvent.getCause() == DamageCause.ENTITY_EXPLOSION && entity instanceof EntityItem entityItem && !ItemMethod.canExplodable(entityItem.getItem().getId())){ event.setCancelled(); diff --git a/src/main/java/com/reider745/hooks/EntityOverrides.java b/src/main/java/com/reider745/hooks/EntityOverrides.java index 86733edb5..42d92f87d 100644 --- a/src/main/java/com/reider745/hooks/EntityOverrides.java +++ b/src/main/java/com/reider745/hooks/EntityOverrides.java @@ -61,13 +61,19 @@ public static void scheduleUpdate(Entity self){ signature = "(Ljava/lang/String;Lcn/nukkit/level/format/FullChunk;Lcn/nukkit/nbt/tag/CompoundTag;[Ljava/lang/Object;)Lcn/nukkit/entity/Entity;" ) public static Entity createEntity(String name, FullChunk chunk, CompoundTag nbt, Object... args) { + final String identifier = Character.toLowerCase(name.charAt(0)) + name.substring(1); final Map> knownEntities = ReflectHelper.getField(Entity.class, "knownEntities"); - final EntityContentFactory factory = EntityContentFactory.factoryMap.get(name); + EntityContentFactory factory = EntityContentFactory.factoryMap.get(name); if(factory != null) { nbt.putString("___id___", name); return new CustomEntity(chunk, nbt); } + factory = EntityContentFactory.factoryMap.get(identifier); + if(factory != null) { + nbt.putString("___id___", identifier); + return new CustomEntity(chunk, nbt); + } if (knownEntities.containsKey(name)) { return ReflectHelper.invoke(