Skip to content

Commit

Permalink
refactor: call AxiomManipulateEntityEvent before merging nbt for entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Apr 28, 2024
1 parent ba2d7f9 commit 9364d7c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.moulberry.axiom.event;

import net.minecraft.nbt.CompoundTag;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
Expand All @@ -18,50 +19,21 @@ public class AxiomManipulateEntityEvent extends Event implements Cancellable {
private static final HandlerList HANDLERS = new HandlerList();

private final Player player;
private final UUID entityUUID;
private final Location fromLocation;
private final Location toLocation;
private final Entity entity;

private boolean cancelled = false;

public AxiomManipulateEntityEvent(Player player, Entity entity, Location toLocation) {
public AxiomManipulateEntityEvent(Player player, @NotNull Entity entity) {
this.player = player;
this.entityUUID = entity.getUniqueId();
this.fromLocation = entity.getLocation();
this.toLocation = toLocation;
}

public AxiomManipulateEntityEvent(Player player, UUID entityUUID, Location fromLocation, Location toLocation) {
this.player = player;
this.entityUUID = entityUUID;
this.fromLocation = fromLocation;
this.toLocation = toLocation;
this.entity = entity;
}

public Player getPlayer() {
return player;
}

public Location getFromLocation() {
return fromLocation;
}

public Location getToLocation() {
return toLocation;
}

/**
* Gets a BukkitEntity from the given UUID.
* Do note this might return null in cases where the UUID was from a client-side entity
* @return The BukkitEntity, if it exists
*/
@Nullable
public Entity getEntity() {
return Bukkit.getEntity(entityUUID);
}

public UUID getUUID() {
return entityUUID;
return entity;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import com.moulberry.axiom.NbtSanitization;
import com.moulberry.axiom.event.AxiomManipulateEntityEvent;
import com.moulberry.axiom.integration.Integration;
import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration;
import io.netty.buffer.Unpooled;
import net.kyori.adventure.text.Component;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -119,17 +117,15 @@ public void onPluginMessageReceived(@NotNull String channel, @NotNull Player pla
continue;
}

AxiomManipulateEntityEvent manipulateEvent = new AxiomManipulateEntityEvent(player, entity.getBukkitEntity());
if (!manipulateEvent.callEvent()) continue;

if (entry.merge != null && !entry.merge.isEmpty()) {
final Location oldLocation = entity.getBukkitEntity().getLocation();
NbtSanitization.sanitizeEntity(entry.merge);

CompoundTag compoundTag = entity.saveWithoutId(new CompoundTag());
compoundTag = merge(compoundTag, entry.merge);
entity.load(compoundTag);

Location newLocation = new Location(serverLevel.getWorld(), position.x, position.y, position.z, entity.getBukkitYaw(), entity.getXRot());
AxiomManipulateEntityEvent manipulateEvent = new AxiomManipulateEntityEvent(player, entry.uuid, oldLocation, newLocation);
if (!manipulateEvent.callEvent()) continue;
}

entity.setPosRaw(position.x, position.y, position.z);
Expand All @@ -141,10 +137,6 @@ public void onPluginMessageReceived(@NotNull String channel, @NotNull Player pla
double newZ = entry.relativeMovementSet.contains(RelativeMovement.Z) ? entity.position().z + entryPos.z : entryPos.z;
float newYaw = entry.relativeMovementSet.contains(RelativeMovement.Y_ROT) ? entity.getYRot() + entry.yaw : entry.yaw;
float newPitch = entry.relativeMovementSet.contains(RelativeMovement.X_ROT) ? entity.getXRot() + entry.pitch : entry.pitch;
Location newLocation = new Location(serverLevel.getWorld(), newX, newY, newZ, newYaw, newPitch);

AxiomManipulateEntityEvent manipulateEvent = new AxiomManipulateEntityEvent(player, entity.getBukkitEntity(), newLocation);
if (!manipulateEvent.callEvent()) continue;

if (entity instanceof HangingEntity hangingEntity) {
float changedYaw = newYaw - entity.getYRot();
Expand Down

0 comments on commit 9364d7c

Please sign in to comment.