Skip to content

Commit

Permalink
implement shape on generic model
Browse files Browse the repository at this point in the history
  • Loading branch information
iam4722202468 committed Mar 2, 2024
1 parent c41045e commit 71941c7
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/main/java/net/worldseed/multipart/GenericModel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.worldseed.multipart;

import net.minestom.server.Viewable;
import net.minestom.server.collision.Shape;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.Entity;
Expand All @@ -15,7 +16,7 @@
import java.util.List;
import java.util.Set;

public interface GenericModel extends Viewable, EventHandler<ModelEvent> {
public interface GenericModel extends Viewable, EventHandler<ModelEvent>, Shape {
/**
* Get the ID of the model
* @return the model ID
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/net/worldseed/multipart/GenericModelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import net.kyori.adventure.audience.Audience;
import net.minestom.server.MinecraftServer;
import net.minestom.server.ServerProcess;
import net.minestom.server.collision.BoundingBox;
import net.minestom.server.collision.Shape;
import net.minestom.server.collision.SweepResult;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.coordinate.Vec;
Expand All @@ -14,6 +17,7 @@
import net.minestom.server.event.EventFilter;
import net.minestom.server.event.EventNode;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.network.packet.server.SendablePacket;
import net.worldseed.gestures.EmoteModel;
import net.worldseed.multipart.animations.AnimationHandlerImpl;
Expand All @@ -27,6 +31,7 @@
import net.worldseed.multipart.model_bones.misc.ModelBoneNametag;
import net.worldseed.multipart.model_bones.misc.ModelBoneSeat;
import net.worldseed.multipart.model_bones.misc.ModelBoneVFX;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -372,4 +377,71 @@ public boolean removeViewer(@NotNull Player player) {
public @NotNull Set<@NotNull Player> getViewers() {
return Set.copyOf(this.viewers);
}

// SHAPE IMPL
@Override
public boolean isFaceFull(@NotNull BlockFace face) {
return true;
}

@Override
public boolean isOccluded(@NotNull Shape shape, @NotNull BlockFace blockFace) {
return false;
}

@Override
public @NotNull Point relativeStart() {
Pos currentPosition = getPosition();
Point p = currentPosition;

for (var bone : this.hittableBones) {
for (var part : bone.getParts()) {
var entity = part.getEntity();
var absoluteStart = entity.relativeStart().add(entity.getPosition());

if (p.x() > absoluteStart.x()) p = p.withX(absoluteStart.x());
if (p.y() > absoluteStart.y()) p = p.withY(absoluteStart.y());
if (p.z() > absoluteStart.z()) p = p.withZ(absoluteStart.z());
}
}

return p.sub(currentPosition);
}

@Override
public @NotNull Point relativeEnd() {
Pos currentPosition = getPosition();
Point p = currentPosition;

for (var bone : this.hittableBones) {
for (var part : bone.getParts()) {
var entity = part.getEntity();
var absoluteStart = entity.relativeEnd().add(entity.getPosition());

if (p.x() < absoluteStart.x()) p = p.withX(absoluteStart.x());
if (p.y() < absoluteStart.y()) p = p.withY(absoluteStart.y());
if (p.z() < absoluteStart.z()) p = p.withZ(absoluteStart.z());
}
}

return p.sub(currentPosition);
}

@Override
public boolean intersectBox(@NotNull Point point, @NotNull BoundingBox boundingBox) {
var pos = getPosition();

for (var bone : this.hittableBones) {
for (var part : bone.getParts()) {
if (boundingBox.intersectEntity(point.add(pos), part.getEntity())) return true;
}
}
return false;
}

@Override
@ApiStatus.Experimental
public boolean intersectBoxSwept(@NotNull Point rayStart, @NotNull Point rayDirection, @NotNull Point shapePos, @NotNull BoundingBox moving, @NotNull SweepResult finalResult) {
throw new UnsupportedOperationException("Not implemented");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void setParent(ModelBone parent) {

@Override
public Point getPosition() {
return this.actualPosition;
return this.actualPosition.add(this.model.getPosition());
}

public Collection<ModelBoneHitbox> getParts() {
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/demo_models/bulbasaur/BulbasaurMob.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import net.minestom.server.instance.Instance;
import net.minestom.server.network.packet.server.play.ParticlePacket;
import net.minestom.server.particle.Particle;
import net.minestom.server.particle.ParticleCreator;
import net.minestom.server.timer.Task;
import net.minestom.server.utils.position.PositionUtils;
import net.minestom.server.utils.time.TimeUnit;
Expand Down Expand Up @@ -116,7 +115,7 @@ public void remove() {
this.animationHandler.playOnce("animation.bulbasaur.faint", (cb) -> {
this.model.destroy();
this.animationHandler.destroy();
ParticlePacket packet = ParticleCreator.createParticlePacket(Particle.POOF, position.x(), position.y() + 1, position.z(), 1, 1, 1, 50);
ParticlePacket packet = new ParticlePacket(Particle.POOF, position.x(), position.y() + 1, position.z(), 1, 1, 1, 0, 50);
viewers.forEach(v -> v.sendPacket(packet));

super.remove();
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/demo_models/gem_golem/GemGolemAttackGoal.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minestom.server.entity.ai.GoalSelector;
import net.minestom.server.network.packet.server.play.ParticlePacket;
import net.minestom.server.particle.Particle;
import net.minestom.server.particle.ParticleCreator;
import net.minestom.server.timer.ExecutionType;
import net.minestom.server.timer.TaskSchedule;
import net.worldseed.multipart.GenericModel;
Expand Down Expand Up @@ -76,13 +75,13 @@ private void attackNear() {
Point p = model.getVFX("hit_vfx");

if (p != null) {
ParticlePacket packet = ParticleCreator.createParticlePacket(Particle.FLAME, p.x(), p.y(), p.z(), 0, 0, 0, 1);
ParticlePacket packet = new ParticlePacket(Particle.FLAME, p.x(), p.y(), p.z(), 0, 0, 0, 0, 1);
entityCreature.sendPacketToViewers(packet);
}

if (counter.addAndGet(1) > sections) {
if (p != null) {
ParticlePacket packet = ParticleCreator.createParticlePacket(Particle.EXPLOSION, p.x(), p.y(), p.z(), 0, 0, 0, 1);
ParticlePacket packet = new ParticlePacket(Particle.EXPLOSION, p.x(), p.y(), p.z(), 0, 0, 0, 0, 1);
entityCreature.sendPacketToViewers(packet);
}

Expand Down
3 changes: 1 addition & 2 deletions src/test/java/demo_models/gem_golem/GemGolemMob.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import net.minestom.server.instance.Instance;
import net.minestom.server.network.packet.server.play.ParticlePacket;
import net.minestom.server.particle.Particle;
import net.minestom.server.particle.ParticleCreator;
import net.minestom.server.timer.Task;
import net.minestom.server.utils.position.PositionUtils;
import net.minestom.server.utils.time.TimeUnit;
Expand Down Expand Up @@ -143,7 +142,7 @@ public void remove() {
this.animationHandler.playOnce("death", (cb) -> {
this.model.destroy();
this.animationHandler.destroy();
ParticlePacket packet = ParticleCreator.createParticlePacket(Particle.POOF, position.x(), position.y() + 1, position.z(), 1, 1, 1, 50);
ParticlePacket packet = new ParticlePacket(Particle.POOF, position.x(), position.y() + 1, position.z(), 1, 1, 1, 0, 50);
viewers.forEach(v -> v.sendPacket(packet));

super.remove();
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/demo_models/tuff_golem/TuffGolemMob.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.minestom.server.instance.Instance;
import net.minestom.server.network.packet.server.play.ParticlePacket;
import net.minestom.server.particle.Particle;
import net.minestom.server.particle.ParticleCreator;
import net.minestom.server.timer.Task;
import net.minestom.server.utils.time.TimeUnit;
import net.worldseed.multipart.animations.AnimationHandler;
Expand Down Expand Up @@ -92,7 +91,7 @@ public void remove() {
var viewers = Set.copyOf(this.getViewers());
this.model.destroy();
this.animationHandler.destroy();
ParticlePacket packet = ParticleCreator.createParticlePacket(Particle.POOF, position.x(), position.y() + 1, position.z(), 1, 1, 1, 50);
ParticlePacket packet = new ParticlePacket(Particle.POOF, position.x(), position.y() + 1, position.z(), 1, 1, 1, 0, 50);
viewers.forEach(v -> v.sendPacket(packet));

super.remove();
Expand Down

0 comments on commit 71941c7

Please sign in to comment.