Skip to content

Commit

Permalink
Add display entities
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4722202468 committed Jun 25, 2023
1 parent 81267ea commit 2294415
Show file tree
Hide file tree
Showing 19 changed files with 213 additions and 41 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ publishing {
publications.create<MavenPublication>("maven") {
groupId = "net.worldseed.multipart"
artifactId = "WorldSeedEntityEngine"
version = "6.0.16"
version = "7.0.0"

from(components["java"])
}
Expand All @@ -38,7 +38,7 @@ publishing {
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1")
compileOnly("com.github.Minestom:Minestom:aebf72de90")
compileOnly("dev.hollowcube:minestom-ce:438338381e")
implementation("commons-io:commons-io:2.11.0")
implementation("org.zeroturnaround:zt-zip:1.8")

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/worldseed/multipart/GenericModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public interface GenericModel {
* @param entity the entity
*/
void setNametagEntity(LivingEntity entity);
LivingEntity getNametagEntity();
Entity getNametagEntity();

Instance getInstance();

Expand Down
12 changes: 9 additions & 3 deletions src/main/java/net/worldseed/multipart/GenericModelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import net.worldseed.multipart.model_bones.armour_stand.ModelBoneHeadArmourStandHand;
import net.worldseed.multipart.model_bones.armour_stand.ModelBonePartArmourStand;
import net.worldseed.multipart.model_bones.armour_stand.ModelBonePartArmourStandHand;
import net.worldseed.multipart.model_bones.display_entity.ModelBoneHeadDisplay;
import net.worldseed.multipart.model_bones.display_entity.ModelBonePartDisplay;
import net.worldseed.multipart.model_bones.misc.*;
import net.worldseed.multipart.model_bones.zombie.ModelBoneHeadZombie;
import net.worldseed.multipart.model_bones.zombie.ModelBonePartZombie;
Expand Down Expand Up @@ -144,8 +146,10 @@ protected void loadBones(JsonObject loadedModel, LivingEntity masterEntity) {
} else {
modelBonePart = new ModelBoneHeadArmourStandHand(pivotPos, name, boneRotation, this, config, masterEntity);
}
} else {
} else if (config.modelType() == ModelConfig.ModelType.ZOMBIE) {
modelBonePart = new ModelBoneHeadZombie(pivotPos, name, boneRotation, this, config, masterEntity);
} else {
modelBonePart = new ModelBoneHeadDisplay(pivotPos, name, boneRotation, this, config, masterEntity);
}
this.head = (ModelBoneHead) modelBonePart;
} else {
Expand All @@ -155,8 +159,10 @@ protected void loadBones(JsonObject loadedModel, LivingEntity masterEntity) {
} else {
modelBonePart = new ModelBonePartArmourStandHand(pivotPos, name, boneRotation, this, config, masterEntity);
}
} else {
} else if (config.modelType() == ModelConfig.ModelType.ZOMBIE) {
modelBonePart = new ModelBonePartZombie(pivotPos, name, boneRotation, this, config, masterEntity);
} else {
modelBonePart = new ModelBonePartDisplay(pivotPos, name, boneRotation, this, config, masterEntity);
}
}

Expand Down Expand Up @@ -198,7 +204,7 @@ public void setNametagEntity(LivingEntity entity) {
if (this.nametag != null) this.nametag.linkEntity(entity);
}

public LivingEntity getNametagEntity() {
public Entity getNametagEntity() {
if (this.nametag != null) return this.nametag.getStand();
return null;
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/worldseed/multipart/ModelConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public enum ModelType {
/**
* X, Y rotation. No Z
*/
ZOMBIE
ZOMBIE,
DISPLAY_ITEM
}

public enum InterpolationType {
Expand Down Expand Up @@ -78,7 +79,7 @@ public ItemSlot itemSlot() {
}

public static ModelConfig defaultConfig =
new ModelConfig(ModelType.ARMOUR_STAND,
new ModelConfig(ModelType.DISPLAY_ITEM,
InterpolationType.POSITION_INTERPOLATION,
Size.NORMAL,
ItemSlot.HEAD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,16 @@ public void playRepeat(String animation, AnimationDirection direction) {
this.repeating.put(this.animationPriorities().get(animation), animation);
var top = this.repeating.firstEntry();

if (playingOnce != null) {
if (this.callbacks.get(playingOnce) != null) {
this.callbacks.get(playingOnce).accept(null);
this.callbacks.remove(playingOnce);
this.callbackTimers.remove(playingOnce);
}

this.animations.get(playingOnce).forEach(ModelAnimation::stop);
}

if (top != null && animation.equals(top.getValue())) {
this.repeating.values().forEach(v -> {
if (!v.equals(animation)) {
this.animations.get(v).forEach(ModelAnimation::stop);
}
});
this.animations.get(animation).forEach(a -> a.setDirection(direction));
this.animations.get(animation).forEach(ModelAnimation::play);
if (playingOnce == null) {
this.animations.get(animation).forEach(ModelAnimation::play);
}
}
}

Expand All @@ -118,7 +110,7 @@ public void stopRepeat(String animation) {

Map.Entry<Integer, String> firstEntry = this.repeating.firstEntry();

if (firstEntry != null && currentTop != null && !firstEntry.getKey().equals(currentTop.getKey())) {
if (this.playingOnce == null && firstEntry != null && currentTop != null && !firstEntry.getKey().equals(currentTop.getKey())) {
this.animations.get(firstEntry.getValue()).forEach(ModelAnimation::play);
}
}
Expand All @@ -138,7 +130,6 @@ public void playOnce(String animation, AnimationDirection direction, Consumer<Vo

int callbackTimer = this.callbackTimers.getOrDefault(animation, 0);


if (animation.equals(this.playingOnce) && direction == AnimationDirection.PAUSE && callbackTimer > 0) {
// Pause. Only call if we're not stopped
playingOnce = animation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class ModelBoneImpl implements ModelBone {
protected Point offset;
protected Point rotation;
ModelBone parent;
protected LivingEntity stand;
protected Entity stand;

protected final ArrayList<ModelBone> children = new ArrayList<>();
protected final GenericModel model;
Expand Down Expand Up @@ -184,12 +184,13 @@ public void addChild(ModelBone child) {

@Override
public void destroy() {
this.children.forEach(c -> c.destroy());
this.children.clear();

if (this.stand != null) {
this.stand.setInvisible(true);
this.stand.remove();
}

this.children.clear();
}

public CompletableFuture<Void> spawn(Instance instance, Point position) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,16 @@ public void draw() {

@Override
public void setState(String state) {
if (this.stand != null) {
if (this.stand != null && this.stand instanceof LivingEntity e) {
if (state.equals("invisible")) {
this.stand.setHelmet(ItemStack.AIR);
e.setHelmet(ItemStack.AIR);
return;
}

var item = this.items.get(state);

if (item != null) {
this.stand.setHelmet(item);
e.setHelmet(item);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,16 @@ public void draw() {

@Override
public void setState(String state) {
if (this.stand != null) {
if (this.stand != null && this.stand instanceof LivingEntity e) {
if (state.equals("invisible")) {
this.stand.setItemInMainHand(ItemStack.AIR);
e.setItemInMainHand(ItemStack.AIR);
return;
}

var item = this.items.get(state);

if (item != null) {
this.stand.setItemInMainHand(item);
e.setItemInMainHand(item);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.worldseed.multipart.model_bones.display_entity;

import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.LivingEntity;
import net.worldseed.multipart.GenericModel;
import net.worldseed.multipart.ModelConfig;
import net.worldseed.multipart.ModelLoader;
import net.worldseed.multipart.animations.ModelAnimation;
import net.worldseed.multipart.model_bones.ModelBoneHead;
import net.worldseed.multipart.model_bones.ModelBoneViewable;
import net.worldseed.multipart.model_bones.zombie.ModelBonePartZombie;

public class ModelBoneHeadDisplay extends ModelBonePartDisplay implements ModelBoneHead, ModelBoneViewable {
private double headRotation;

public ModelBoneHeadDisplay(Point pivot, String name, Point rotation, GenericModel model, ModelConfig config, LivingEntity forwardTo) {
super(pivot, name, rotation, model, config, forwardTo);
}

@Override
public Point getPropogatedRotation() {
Point netTransform = Vec.ZERO;

for (ModelAnimation currentAnimation : this.allAnimations) {
if (currentAnimation != null && currentAnimation.isPlaying()) {
if (currentAnimation.getType() == ModelLoader.AnimationType.ROTATION) {
Point calculatedTransform = currentAnimation.getTransform();
netTransform = netTransform.add(calculatedTransform);
}
}
}

return this.rotation.add(netTransform).add(0, this.headRotation, 0);
}

public void setRotation(double rotation) {
this.headRotation = rotation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package net.worldseed.multipart.model_bones.display_entity;

import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.EntityType;
import net.minestom.server.entity.LivingEntity;
import net.minestom.server.entity.metadata.display.ItemDisplayMeta;
import net.minestom.server.item.ItemStack;
import net.worldseed.multipart.GenericModel;
import net.worldseed.multipart.ModelConfig;
import net.worldseed.multipart.Quaternion;
import net.worldseed.multipart.model_bones.ModelBone;
import net.worldseed.multipart.model_bones.ModelBoneImpl;
import net.worldseed.multipart.model_bones.ModelBoneViewable;

public class ModelBonePartDisplay extends ModelBoneImpl implements ModelBoneViewable {
int sendTick = 0;

public ModelBonePartDisplay(Point pivot, String name, Point rotation, GenericModel model, ModelConfig config, LivingEntity forwardTo) {
super(pivot, name, rotation, model);

if (this.offset != null) {
this.stand = new Entity(EntityType.ITEM_DISPLAY) {
@Override
public void tick(long time) {}
};

var meta = (ItemDisplayMeta) this.stand.getEntityMeta();

meta.setScale(new Vec(1, 1, 1));
meta.setDisplayContext(ItemDisplayMeta.DisplayContext.FIXED);
meta.setInterpolationDuration(3);
ModelBoneImpl.hookPart(this, forwardTo);
}
}

@Override
public Pos calculatePosition() {
if (this.offset == null) return Pos.ZERO;

Point p = this.offset;
p = applyTransform(p);
p = calculateGlobalRotation(p);

Pos endPos = Pos.fromPoint(p);

return Pos.fromPoint(endPos).div(4);
}

@Override
public Point calculateRotation() {
Quaternion q = calculateFinalAngle(new Quaternion(getPropogatedRotation()));
if (model.getGlobalRotation() != 0) {
Quaternion pq = new Quaternion(new Vec(0, this.model.getGlobalRotation(), 0));
q = pq.multiply(q);
}

return q.toEulerYZX();
}

public void draw() {
this.children.forEach(ModelBone::draw);
if (this.offset == null) return;

sendTick++;

if (sendTick % 2 == 0 && this.stand != null && this.stand.getEntityMeta() instanceof ItemDisplayMeta meta) {
var position = calculatePosition();
Quaternion q = calculateFinalAngle(new Quaternion(getPropogatedRotation()));
if (model.getGlobalRotation() != 0) {
Quaternion pq = new Quaternion(new Vec(0, this.model.getGlobalRotation(), 0));
q = pq.multiply(q);
}

meta.setNotifyAboutChanges(false);
meta.setInterpolationStartDelta(0);
meta.setRightRotation(new float[] {(float) q.x(), (float) q.y(), (float) q.z(), (float) q.w()});
meta.setTranslation(position);
meta.setNotifyAboutChanges(true);
}

stand.teleport(Pos.fromPoint(model.getPosition()));
}

@Override
public void setState(String state) {
if (this.stand != null && this.stand.getEntityMeta() instanceof ItemDisplayMeta meta) {
if (state.equals("invisible")) {
meta.setItemStack(ItemStack.AIR);
return;
}

var item = this.items.get(state);
if (item != null) {
meta.setItemStack(item);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,20 @@ public void tick(long time) {}
public void updateNewViewer(@NotNull Player player) {
super.updateNewViewer(player);

if (holder != null) {
if (holder != null && !this.isRemoved()) {
holder.addViewer(player);
holder.updateNewViewer(player);
MinecraftServer.getSchedulerManager().scheduleNextTick(() -> player.sendPacket(new AttachEntityPacket(holder, this)));
MinecraftServer.getSchedulerManager().scheduleNextTick(() -> {
player.sendPacket(new AttachEntityPacket(holder, this));
});
}
}

@Override
public void updateOldViewer(@NotNull Player player) {
super.updateOldViewer(player);

if (holder != null) {
if (holder != null && !this.isRemoved()) {
holder.removeViewer(player);
holder.updateOldViewer(player);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.Entity;
import net.minestom.server.entity.LivingEntity;
import net.minestom.server.tag.Tag;
import net.worldseed.multipart.GenericModel;
Expand Down Expand Up @@ -33,7 +34,7 @@ public void draw() {
stand.teleport(calculatePosition());
}

public LivingEntity getStand() {
public Entity getStand() {
return stand;
}

Expand Down
Loading

0 comments on commit 2294415

Please sign in to comment.