Skip to content

Commit

Permalink
No more warp lag!
Browse files Browse the repository at this point in the history
  • Loading branch information
WenXin20 committed Mar 5, 2025
1 parent 6299a2b commit ee50f77
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public void spawnParticles(Entity entity) {
double entityZ = entity.getZ();

Collection<ServerPlayer> players = ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayers();
for (ServerPlayer player : players) {
for (ServerPlayer player : players) { // TODO
for (int i = 0; i < 2; i++) {
player.connection.send(new ClientboundLevelParticlesPacket(
ParticleTypes.EFFECT, false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class BaseWarpBlockEntity extends BlockEntity {
public boolean isWaxed;
public UUID uuid;
public UUID warpUuid;
public static final Map<UUID, BlockPos> WARP_LOCATIONS = new HashMap<>();

public BaseWarpBlockEntity(final BlockEntityType<?> tileEntity, BlockPos pos, BlockState state) {
super(tileEntity, pos, state);
Expand Down Expand Up @@ -163,6 +164,18 @@ public void saveAdditional(CompoundTag tag, HolderLookup.Provider provider) {
tag.putUUID(WARP_UUID, this.getWarpUuid());
}

@Override
public void setRemoved() {
super.setRemoved();
WARP_LOCATIONS.remove(this.getWarpUuid());
}

@Override
public void onLoad() {
super.onLoad();
WARP_LOCATIONS.put(this.getWarpUuid(), this.getBlockPos());
}

@NotNull
@Override
public CompoundTag getUpdateTag(HolderLookup.Provider provider) {
Expand Down Expand Up @@ -198,34 +211,7 @@ public static void markEntityTeleported(Entity entity) {
}
}

public static BlockPos findMatchingUUID(UUID uuid, Level world, BlockPos pos) {
BlockPos closestPos = null;
double closestDistanceSq = Double.MAX_VALUE;
int maxDistance = 64; // How far it searches for warp pipes with a matching UUID

for (int x = -maxDistance; x <= maxDistance; x++) {
for (int y = Math.max(-maxDistance, world.getMinBuildHeight() - pos.getY()); y <= Math.min(maxDistance, world.getMaxBuildHeight() - pos.getY()); y++) {
for (int z = -maxDistance; z <= maxDistance; z++) {
BlockPos checkingPos = pos.offset(x, y, z);

if (world.getBlockEntity(checkingPos) instanceof BaseWarpBlockEntity) {
BlockEntity blockEntity = world.getBlockEntity(checkingPos);

if (blockEntity instanceof BaseWarpBlockEntity warpBlockEntity) {
UUID warpUUID = warpBlockEntity.getWarpUuid();

if (uuid.equals(warpUUID)) {
double distanceSq = pos.distToCenterSqr(checkingPos.getX(), checkingPos.getY(), checkingPos.getZ());
if (distanceSq < closestDistanceSq) {
closestPos = checkingPos.immutable();
closestDistanceSq = distanceSq;
}
}
}
}
}
}
}
return closestPos;
public static BlockPos findMatchingUUID(UUID uuid) {
return WARP_LOCATIONS.getOrDefault(uuid, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ protected AABB getTargetSearchArea(double followDistance) {
}

protected void findTarget() {
// if (this.target != null && this.target.isAlive()
// && this.mob.distanceToSqr(this.target) < this.getFollowDistance() * this.getFollowDistance()) {
// return;
// }

if (this.target == null || !this.target.isAlive()) {
List<LivingEntity> potentialTargets = this.mob.level().getEntitiesOfClass(
LivingEntity.class,
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/wenxin2/marioverse/mixin/EntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ public void modifyIsInWall(CallbackInfoReturnable<Boolean> cir) {
world.playSound(null, pos, SoundRegistry.PIPE_WARPS.get(), SoundSource.BLOCKS);
this.marioverse$updateDoor(pos, state, warpPos, warpState);
} else if (warpBE.getUuid() != null && warpBE.getWarpUuid() != null
&& BaseWarpBlockEntity.findMatchingUUID(warpBE.getUuid(), world, pos) != null) {
warpPos = BaseWarpBlockEntity.findMatchingUUID(warpBE.getUuid(), world, pos);
&& BaseWarpBlockEntity.findMatchingUUID(warpBE.getUuid()) != null) {
warpPos = BaseWarpBlockEntity.findMatchingUUID(warpBE.getUuid());
BlockState warpState = world.getBlockState(warpPos);

if (warpState.getBlock() instanceof DoorBlock doorblock)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import io.wispforest.accessories.api.AccessoriesContainer;
import io.wispforest.accessories.data.SlotTypeLoader;
import java.util.List;
import java.util.function.Supplier;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -891,8 +890,8 @@ else if (offhandStack.getItem() instanceof OneUpMushroomItem)
world.playSound(null, pos, SoundRegistry.PIPE_WARPS.get(), SoundSource.BLOCKS);
this.marioverse$updateDoor(pos, state, warpPos, warpState);
} else if (warpBE.getUuid() != null && warpBE.getWarpUuid() != null
&& BaseWarpBlockEntity.findMatchingUUID(warpBE.getUuid(), world, pos) != null) {
warpPos = BaseWarpBlockEntity.findMatchingUUID(warpBE.getUuid(), world, pos);
&& BaseWarpBlockEntity.findMatchingUUID(warpBE.getUuid()) != null) {
warpPos = BaseWarpBlockEntity.findMatchingUUID(warpBE.getUuid());
BlockState warpState = world.getBlockState(warpPos);

if (warpState.getBlock() instanceof DoorBlock doorblock)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/wenxin2/marioverse/mixin/PlayerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ public void baseTick() {
if (state.getBlock() instanceof WarpPipeBlock)
world.playSound(null, pos, SoundRegistry.PIPE_WARPS.get(), SoundSource.BLOCKS);
} else if (warpBE.getUuid() != null && warpBE.getWarpUuid() != null
&& BaseWarpBlockEntity.findMatchingUUID(warpBE.getUuid(), world, pos) != null) {
warpPos = BaseWarpBlockEntity.findMatchingUUID(warpBE.getUuid(), world, pos);
&& BaseWarpBlockEntity.findMatchingUUID(warpBE.getUuid()) != null) {
warpPos = BaseWarpBlockEntity.findMatchingUUID(warpBE.getUuid());
BlockState warpState = world.getBlockState(warpPos);

this.marioverse$updateDoor(pos, state, warpPos, warpState);
Expand Down

0 comments on commit ee50f77

Please sign in to comment.