Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added target level to teleport events #1531

Open
wants to merge 11 commits into
base: 1.21.x
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--- a/net/minecraft/server/commands/SpreadPlayersCommand.java
+++ b/net/minecraft/server/commands/SpreadPlayersCommand.java
@@ -259,15 +_,22 @@
@@ -259,15 +_,23 @@
spreadplayerscommand$position = p_138732_[i++];
}

Expand All @@ -12,14 +12,15 @@
- Set.of(),
- entity.getYRot(),
- entity.getXRot()
+ net.neoforged.neoforge.event.entity.EntityTeleportEvent.SpreadPlayersCommand event = net.neoforged.neoforge.event.EventHooks.onEntityTeleportSpreadPlayersCommand(entity,
+ net.neoforged.neoforge.event.entity.EntityTeleportEvent.SpreadPlayersCommand event = net.neoforged.neoforge.event.EventHooks.onEntityTeleportSpreadPlayersCommand(entity, new net.neoforged.neoforge.common.util.GlobalVec3(
+ p_138731_,
+ (double)Mth.floor(spreadplayerscommand$position.x) + 0.5,
+ spreadplayerscommand$position.getSpawnY(p_138731_, p_138733_),
+ (double)Mth.floor(spreadplayerscommand$position.z) + 0.5
+ (double)Mth.floor(spreadplayerscommand$position.z) + 0.5)
);
+ if (!event.isCanceled()) {
+ entity.teleportTo(
+ p_138731_,
+ event.getTargetServerLevelOr(p_138731_),
+ event.getTargetX(),
+ event.getTargetY(),
+ event.getTargetZ(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
--- a/net/minecraft/server/commands/TeleportCommand.java
+++ b/net/minecraft/server/commands/TeleportCommand.java
@@ -270,6 +_,14 @@
@@ -270,6 +_,15 @@
float p_139023_,
@Nullable TeleportCommand.LookAt p_139024_
) throws CommandSyntaxException {
+ net.neoforged.neoforge.event.entity.EntityTeleportEvent.TeleportCommand event = net.neoforged.neoforge.event.EventHooks.onEntityTeleportCommand(p_139016_, p_139018_, p_139019_, p_139020_);
+ net.neoforged.neoforge.event.entity.EntityTeleportEvent.TeleportCommand event = net.neoforged.neoforge.event.EventHooks.onEntityTeleportCommand(p_139016_, new net.neoforged.neoforge.common.util.GlobalVec3(p_139017_, p_139018_, p_139019_, p_139020_));
+ if (event.isCanceled()) {
+ return;
+ }
+ p_139017_ = event.getTargetServerLevelOr(p_139017_);
+ p_139018_ = event.getTargetX();
+ p_139019_ = event.getTargetY();
+ p_139020_ = event.getTargetZ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
boolean flag = blockstate.blocksMotion();
boolean flag1 = blockstate.getFluidState().is(FluidTags.WATER);
if (flag && !flag1) {
+ net.neoforged.neoforge.event.entity.EntityTeleportEvent.EnderEntity event = net.neoforged.neoforge.event.EventHooks.onEnderTeleport(this, p_32544_, p_32545_, p_32546_);
+ net.neoforged.neoforge.event.entity.EntityTeleportEvent.EnderEntity event = net.neoforged.neoforge.event.EventHooks.onEnderTeleport(this, new net.neoforged.neoforge.common.util.GlobalVec3(this.level(), p_32544_, p_32545_, p_32546_));
+ if (event.isCanceled()) return false;
Vec3 vec3 = this.position();
- boolean flag2 = this.randomTeleport(p_32544_, p_32545_, p_32546_, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
&& this.level().noCollision(this, new AABB(blockpos1).deflate(1.0E-6))) {
Direction direction = this.findAttachableSurface(blockpos1);
if (direction != null) {
+ net.neoforged.neoforge.event.entity.EntityTeleportEvent.EnderEntity event = net.neoforged.neoforge.event.EventHooks.onEnderTeleport(this, blockpos1.getX(), blockpos1.getY(), blockpos1.getZ());
+ net.neoforged.neoforge.event.entity.EntityTeleportEvent.EnderEntity event = net.neoforged.neoforge.event.EventHooks.onEnderTeleport(this, new net.neoforged.neoforge.common.util.GlobalVec3(this.level(), blockpos1));
+ if (event.isCanceled()) direction = null;
+ blockpos1 = BlockPos.containing(event.getTargetX(), event.getTargetY(), event.getTargetZ());
+ }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

if (entity instanceof ServerPlayer serverplayer) {
if (serverplayer.connection.isAcceptingMessages()) {
+ net.neoforged.neoforge.event.entity.EntityTeleportEvent.EnderPearl event = net.neoforged.neoforge.event.EventHooks.onEnderPearlLand(serverplayer, this.getX(), this.getY(), this.getZ(), this, 5.0F, p_37504_);
+ net.neoforged.neoforge.event.entity.EntityTeleportEvent.EnderPearl event = net.neoforged.neoforge.event.EventHooks.onEnderPearlLand(serverplayer, new net.neoforged.neoforge.common.util.GlobalVec3(this), this, 5.0F, p_37504_);
+ if (!event.isCanceled()) { // Don't indent to lower patch size
if (this.random.nextFloat() < 0.05F && serverlevel.getGameRules().getBoolean(GameRules.RULE_DOMOBSPAWNING)) {
Endermite endermite = EntityType.ENDERMITE.create(serverlevel);
Expand All @@ -14,14 +14,15 @@
entity.changeDimension(
new DimensionTransition(
- serverlevel, this.position(), entity.getDeltaMovement(), entity.getYRot(), entity.getXRot(), DimensionTransition.DO_NOTHING
+ serverlevel, event.getTarget(), entity.getDeltaMovement(), entity.getYRot(), entity.getXRot(), DimensionTransition.DO_NOTHING
+ event.getTargetServerLevelOr(serverlevel), event.getTarget(), entity.getDeltaMovement(), entity.getYRot(), entity.getXRot(), DimensionTransition.DO_NOTHING
)
);
entity.resetFallDistance();
serverplayer.resetCurrentImpulseContext();
- entity.hurt(this.damageSources().fall(), 5.0F);
- this.playSound(serverlevel, this.position());
+ entity.hurt(this.damageSources().fall(), event.getAttackDamage());
this.playSound(serverlevel, this.position());
+ this.playSound(event.getTargetServerLevelOr(serverlevel), this.position());
+ } //Forge: End
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Vec3 vec3 = p_40714_.position();
- if (p_40714_.randomTeleport(d0, d1, d2, true)) {
+ net.neoforged.neoforge.event.entity.EntityTeleportEvent.ChorusFruit event = net.neoforged.neoforge.event.EventHooks.onChorusFruitTeleport(p_40714_, d0, d1, d2);
+ net.neoforged.neoforge.event.entity.EntityTeleportEvent.ChorusFruit event = net.neoforged.neoforge.event.EventHooks.onChorusFruitTeleport(p_40714_, new net.neoforged.neoforge.common.util.GlobalVec3(p_40714_.level(), d0, d1, d2));
+ if (event.isCanceled()) return itemstack;
+ if (p_40714_.randomTeleport(event.getTargetX(), event.getTargetY(), event.getTargetZ(), true)) {
p_40713_.gameEvent(GameEvent.TELEPORT, vec3, GameEvent.Context.of(p_40714_));
Expand Down
299 changes: 299 additions & 0 deletions src/main/java/net/neoforged/neoforge/common/util/GlobalVec3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.common.util;

import java.lang.ref.WeakReference;
import java.util.EnumSet;
import java.util.Objects;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Direction.Axis;
import net.minecraft.core.GlobalPos;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector3f;

/**
* A subclass of {@link Vec3} that also holds a {@link Level}.
* <p>
* Note that the Level is held as a weak reference and is not required. Only the dimension ({@link ResourceKey}) is always available.
* <p>
* Methods that take a {@link Vec3} as parameter will throw an {@link IllegalStateException} if they are supplied with a {@link GlobalVec3}
* that has a different dimension. To avoid this, downgrade the parameter with {@link #toVec3()} if necessary.
*/
public class GlobalVec3 extends Vec3 {
public final ResourceKey<Level> dimension;
private final WeakReference<Level> level;

protected GlobalVec3(ResourceKey<Level> dimension, WeakReference<Level> level, double x, double y, double z) {
super(x, y, z);
this.dimension = dimension;
this.level = level;
}

public GlobalVec3(Level level, double x, double y, double z) {
this(level.dimension(), new WeakReference<>(level), x, y, z);
}

public GlobalVec3(ResourceKey<Level> dimension, double x, double y, double z) {
this(dimension, new WeakReference<>(null), x, y, z);
}

public GlobalVec3(Level level, Vector3f vector3f) {
this(level.dimension(), new WeakReference<>(level), vector3f.x, vector3f.y, vector3f.z);
}

public GlobalVec3(ResourceKey<Level> dimension, Vector3f vector3f) {
this(dimension, new WeakReference<>(null), vector3f.x, vector3f.y, vector3f.z);
}

protected GlobalVec3(ResourceKey<Level> dimension, WeakReference<Level> level, Vec3 vec3) {
this(dimension, level, vec3.x, vec3.y, vec3.z);
}

public GlobalVec3(Level level, Vec3 vec3) {
this(level.dimension(), new WeakReference<>(level), vec3);
}

public GlobalVec3(ResourceKey<Level> dimension, Vec3 vec3) {
this(dimension, new WeakReference<>(null), vec3);
}

public GlobalVec3(Entity entity) {
this(entity.level(), entity.position());
}

public GlobalVec3(Level level, BlockPos blockpos) {
this(level.dimension(), new WeakReference<>(level), blockpos.getX(), blockpos.getY(), blockpos.getZ());
}

public GlobalVec3(ResourceKey<Level> dimension, BlockPos blockpos) {
this(dimension, new WeakReference<>(null), blockpos.getX(), blockpos.getY(), blockpos.getZ());
}

public GlobalVec3(GlobalPos globalpos) {
this(globalpos.dimension(), globalpos.pos());
}

// setters

public GlobalVec3 withX(double x) {
return new GlobalVec3(dimension, level, x, y, z);
}

public GlobalVec3 withY(double y) {
return new GlobalVec3(dimension, level, x, y, z);
}

public GlobalVec3 withZ(double z) {
return new GlobalVec3(dimension, level, x, y, z);
}

public GlobalVec3 withLevel(Level level) {
return new GlobalVec3(level.dimension(), new WeakReference<>(level), x, y, z);
}

public GlobalVec3 withDimension(ResourceKey<Level> dimension) {
return new GlobalVec3(dimension, new WeakReference<>(null), x, y, z);
}

// getters

public ResourceKey<Level> getDimension() {
return dimension;
}

/**
* Returns the level of this global position if it is available.
* <p>
* Use {@link #getLevel(MinecraftServer)} or {@link #getLevel(Level)} if possible.
*
* @return The level.
*/
public @Nullable Level getLevel() {
return level.get();
}

public Level getLevel(Level anotherLevel) {
return getLevel(anotherLevel.getServer());
HenryLoenwind marked this conversation as resolved.
Show resolved Hide resolved
}

public Level getLevel(MinecraftServer server) {
final Level level2 = level.get();
if (level2 == null) {
return server.getLevel(dimension);
}
return level2;
}

// converters

public Vec3 toVec3() {
return super.add(0, 0, 0);
}

public BlockPos toBlockPos() {
return BlockPos.containing(this);
}

public GlobalPos toGlobalPos() {
return new GlobalPos(dimension, toBlockPos());
}

public GlobalVec3 copy() {
return new GlobalVec3(dimension, level, x, y, z);
}

// method overrides

protected void sameDim(Vec3 other) {
if (other instanceof GlobalVec3 other3 && other3.dimension != dimension) {
throw new IllegalStateException("Cannot compute with GlobalPoses that are in different dimensions");
HenryLoenwind marked this conversation as resolved.
Show resolved Hide resolved
}
}

@Override
public GlobalVec3 vectorTo(Vec3 other) {
sameDim(other);
return new GlobalVec3(dimension, level, super.vectorTo(other));
}

@Override
public GlobalVec3 cross(Vec3 other) {
sameDim(other);
return new GlobalVec3(dimension, level, super.cross(other));
}

@Override
public GlobalVec3 add(Vec3 other) {
sameDim(other);
return this.add(other.x, other.y, other.z);
}

@Override
public GlobalVec3 add(double otherX, double otherY, double otherZ) {
return new GlobalVec3(dimension, level, super.add(otherX, otherY, otherZ));
}

@Override
public double distanceTo(Vec3 other) {
sameDim(other);
return super.distanceTo(other);
}

@Override
public double distanceToSqr(Vec3 other) {
sameDim(other);
return super.distanceToSqr(other);
}

@Override
public boolean closerThan(Vec3 other, double horizontal, double vertical) {
sameDim(other);
return super.closerThan(other, horizontal, vertical);
}

@Override
public boolean equals(Object other) {
if (other instanceof GlobalVec3 other3 && other3.dimension == dimension) {
return super.equals(other);
}
return false;
}

@Override
public GlobalVec3 align(EnumSet<Axis> p_82518_) {
HenryLoenwind marked this conversation as resolved.
Show resolved Hide resolved
return new GlobalVec3(dimension, level, super.align(p_82518_));
}

@Override
public GlobalVec3 normalize() {
return new GlobalVec3(dimension, level, super.normalize());
}

@Override
public GlobalVec3 subtract(Vec3 other) {
sameDim(other);
return new GlobalVec3(dimension, level, super.subtract(other));
}

@Override
public GlobalVec3 subtract(double otherX, double otherY, double otherZ) {
return new GlobalVec3(dimension, level, super.subtract(otherX, otherY, otherZ));
}

@Override
public GlobalVec3 scale(double by) {
return new GlobalVec3(dimension, level, super.scale(by));
}

@Override
public GlobalVec3 reverse() {
return new GlobalVec3(dimension, level, super.reverse());
}

@Override
public GlobalVec3 multiply(Vec3 other) {
sameDim(other);
return new GlobalVec3(dimension, level, super.multiply(other));
}

@Override
public GlobalVec3 multiply(double otherX, double otherY, double otherZ) {
return new GlobalVec3(dimension, level, super.multiply(otherX, otherY, otherZ));
}

@Override
public GlobalVec3 offsetRandom(RandomSource rand, float factor) {
return new GlobalVec3(dimension, level, super.offsetRandom(rand, factor));
}

@Override
public int hashCode() {
return 31 * super.hashCode() + Objects.hash(dimension);
}

@Override
public String toString() {
return "(" + dimension + ", " + x + ", " + y + ", " + z + ")";
}

@Override
public GlobalVec3 lerp(Vec3 other, double scale) {
sameDim(other);
return new GlobalVec3(dimension, level, super.lerp(other, scale));
}

@Override
public GlobalVec3 xRot(float amount) {
return new GlobalVec3(dimension, level, super.xRot(amount));
}

@Override
public GlobalVec3 yRot(float amount) {
return new GlobalVec3(dimension, level, super.yRot(amount));
}

@Override
public GlobalVec3 zRot(float amount) {
return new GlobalVec3(dimension, level, super.zRot(amount));
}

@Override
public GlobalVec3 relative(Direction direction, double scale) {
return new GlobalVec3(dimension, level, super.relative(direction, scale));
}

@Override
public GlobalVec3 with(Axis axis, double value) {
return new GlobalVec3(dimension, level, super.with(axis, value));
}
}
Loading