Skip to content

Commit

Permalink
fix being teleported in blocks and improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
YanisBft committed Jul 7, 2021
1 parent f83f3ce commit cd6d3c9
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/main/java/com/yanisbft/aroundtheworld/block/TravelerBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ChunkTicketType;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.ItemScatterer;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;
Expand Down Expand Up @@ -107,18 +109,23 @@ private void teleportToBiome(ServerWorld world, ServerPlayerEntity player, Biome
BlockPos biomePos = world.locateBiome(biome, blockEntityPos, 6400, 8);

if (biomePos != null) {
player.teleport(world, biomePos.getX(), this.getSurfacePos(world, blockEntityPos).getY(), biomePos.getZ(), player.getYaw(), player.getPitch());
blockEntity.setLastUsed(System.currentTimeMillis());
ChunkPos chunkPos = new ChunkPos(new BlockPos(biomePos.getX(), biomePos.getY(), biomePos.getZ()));
world.getChunkManager().addTicket(ChunkTicketType.POST_TELEPORT, chunkPos, 1, player.getId());
player.teleport(world, biomePos.getX(), this.getSurfaceHeight(world, biomePos), biomePos.getZ(), player.getYaw(), player.getPitch());
} else {
player.sendMessage(new TranslatableText("block.aroundtheworld.traveler.no_biome_found"), true);
}
}

private BlockPos getSurfacePos(ServerWorld world, BlockPos pos) {
/**
* Returns the Y coordinate of the first block position containing air.
*/
private int getSurfaceHeight(ServerWorld world, BlockPos pos) {
if (world.getBlockState(pos).isAir()) {
return pos;
return pos.getY();
}
return this.getSurfacePos(world, pos.up());
return this.getSurfaceHeight(world, pos.up());
}

@Override
Expand Down

0 comments on commit cd6d3c9

Please sign in to comment.