Skip to content

Commit

Permalink
Feat: Расширение поддержки тегов
Browse files Browse the repository at this point in the history
- Исправлена ошибка null exception от EntityDeathEvent
  • Loading branch information
Reider745 committed Jun 19, 2024
1 parent 737d7cb commit 16fe546
Show file tree
Hide file tree
Showing 22 changed files with 380 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/reider745/InnerCoreServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
33 changes: 31 additions & 2 deletions src/main/java/com/reider745/behavior/BehaviorPack.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

public abstract class ComponentsTag {
public abstract String getNameTag();
public int getPriority(){
return 0;
}

public void load(EntityContentFactory factory, Object json){

Expand Down
114 changes: 112 additions & 2 deletions src/main/java/com/reider745/behavior/entities/CustomEntity.java
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -60,4 +166,8 @@ public void setOnFire(int seconds) {
if(!factory.isFireImmune)
super.setOnFire(seconds);
}

public boolean isInTickingRange() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public class EntitiesContent extends BehaviorContent {
private static final HashMap<String, EntitiesVersion> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.json.JSONObject;

import java.util.ArrayList;

public abstract class EntitiesVersion {
public abstract ArrayList<TagContentFactory> getListTags();
public abstract EntityContentFactory load(JSONObject json);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ComponentsTag> components = new ArrayList<>();
public CollisionComponent collision = new CollisionComponent();
public HealthComponent health = new HealthComponent();
public ArrayList<NavigationComponentsTag> 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<String, EntityContentFactory> 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,
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<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,4 @@
package com.reider745.behavior.entities.formats;

public class EntitiesVersion116 extends EntitiesVersion113 {
}
Original file line number Diff line number Diff line change
@@ -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<TagContentFactory> tags = new ArrayList<>();

static {
tags.add(new DescriptionsFactory());
tags.add(new ComponentsFactory());
}

@Override
public ArrayList<TagContentFactory> 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;
}
}
Loading

0 comments on commit 16fe546

Please sign in to comment.