Teleport cooldown fix

This commit is contained in:
Aleksey 2021-05-24 10:14:32 +03:00
parent 990dccd1b5
commit c3a3b50438
2 changed files with 27 additions and 23 deletions

View file

@ -1,6 +1,7 @@
package ru.betterend.blocks; package ru.betterend.blocks;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Random; import java.util.Random;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
@ -9,6 +10,7 @@ import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.client.color.block.BlockColor; import net.minecraft.client.color.block.BlockColor;
import net.minecraft.client.color.item.ItemColor; import net.minecraft.client.color.item.ItemColor;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.core.Direction.Axis; import net.minecraft.core.Direction.Axis;
import net.minecraft.core.Direction.AxisDirection; import net.minecraft.core.Direction.AxisDirection;
@ -90,16 +92,13 @@ public class EndPortalBlock extends NetherPortalBlock implements IRenderTypeable
ServerLevel destination = isInEnd ? targetWorld : server.getLevel(Level.END); ServerLevel destination = isInEnd ? targetWorld : server.getLevel(Level.END);
BlockPos exitPos = findExitPos(currentWorld, destination, pos, entity); BlockPos exitPos = findExitPos(currentWorld, destination, pos, entity);
if (exitPos == null) return; if (exitPos == null) return;
if (entity instanceof ServerPlayer) { if (entity instanceof ServerPlayer && ((ServerPlayer) entity).isCreative()) {
ServerPlayer player = (ServerPlayer) entity; ((ServerPlayer) entity).teleportTo(destination, exitPos.getX() + 0.5, exitPos.getY(),
teleportPlayer(player, destination, exitPos); exitPos.getZ() + 0.5, entity.yRot, entity.xRot);
} else { } else {
TeleportingEntity teleEntity = (TeleportingEntity) entity; ((TeleportingEntity) entity).be_setExitPos(exitPos);
teleEntity.be_setExitPos(exitPos); Optional<Entity> teleported = Optional.ofNullable(entity.changeDimension(destination));
Entity teleported = entity.changeDimension(destination); teleported.ifPresent(Entity::setPortalCooldown);
if (teleported != null) {
teleported.setPortalCooldown();
}
} }
} }
@ -107,16 +106,6 @@ public class EndPortalBlock extends NetherPortalBlock implements IRenderTypeable
return !entity.isPassenger() && !entity.isVehicle() && return !entity.isPassenger() && !entity.isVehicle() &&
entity.canChangeDimensions() && !entity.isOnPortalCooldown(); entity.canChangeDimensions() && !entity.isOnPortalCooldown();
} }
private void teleportPlayer(ServerPlayer player, ServerLevel destination, BlockPos exitPos) {
if (player.isCreative()) {
player.teleportTo(destination, exitPos.getX() + 0.5, exitPos.getY(), exitPos.getZ() + 0.5, player.yRot, player.xRot);
} else {
TeleportingEntity teleEntity = (TeleportingEntity) player;
teleEntity.be_setExitPos(exitPos);
player.changeDimension(destination);
}
}
@Override @Override
public ERenderLayer getRenderLayer() { public ERenderLayer getRenderLayer() {
@ -131,8 +120,8 @@ public class EndPortalBlock extends NetherPortalBlock implements IRenderTypeable
double targetMultiplier = Objects.requireNonNull(registry.get(targetWorldId)).coordinateScale(); double targetMultiplier = Objects.requireNonNull(registry.get(targetWorldId)).coordinateScale();
double currentMultiplier = Objects.requireNonNull(registry.get(currentWorldId)).coordinateScale(); double currentMultiplier = Objects.requireNonNull(registry.get(currentWorldId)).coordinateScale();
double multiplier = targetMultiplier > currentMultiplier ? 1.0 / targetMultiplier : currentMultiplier; double multiplier = targetMultiplier > currentMultiplier ? 1.0 / targetMultiplier : currentMultiplier;
BlockPos.MutableBlockPos basePos = currentPos.mutable().set(currentPos.getX() * multiplier, currentPos.getY(), currentPos.getZ() * multiplier); MutableBlockPos basePos = currentPos.mutable().set(currentPos.getX() * multiplier, currentPos.getY(), currentPos.getZ() * multiplier);
BlockPos.MutableBlockPos checkPos = basePos.mutable(); MutableBlockPos checkPos = basePos.mutable();
BlockState currentState = currentWorld.getBlockState(currentPos); BlockState currentState = currentWorld.getBlockState(currentPos);
int radius = (EternalRitual.SEARCH_RADIUS >> 4) + 1; int radius = (EternalRitual.SEARCH_RADIUS >> 4) + 1;
checkPos = EternalRitual.findBlockPos(targetWorld, checkPos, radius, this, state -> state.is(this) && checkPos = EternalRitual.findBlockPos(targetWorld, checkPos, radius, this, state -> state.is(this) &&
@ -155,11 +144,11 @@ public class EndPortalBlock extends NetherPortalBlock implements IRenderTypeable
return null; return null;
} }
private BlockPos.MutableBlockPos findCenter(Level world, BlockPos.MutableBlockPos pos, Direction.Axis axis) { private MutableBlockPos findCenter(Level world, MutableBlockPos pos, Direction.Axis axis) {
return findCenter(world, pos, axis, 1); return findCenter(world, pos, axis, 1);
} }
private BlockPos.MutableBlockPos findCenter(Level world, BlockPos.MutableBlockPos pos, Direction.Axis axis, int step) { private MutableBlockPos findCenter(Level world, MutableBlockPos pos, Direction.Axis axis, int step) {
if (step > 8) return pos; if (step > 8) return pos;
BlockState right, left; BlockState right, left;
Direction rightDir, leftDir; Direction rightDir, leftDir;

View file

@ -53,6 +53,7 @@ public abstract class ServerPlayerMixin extends Player implements TeleportingEnt
private int lastSentExp; private int lastSentExp;
private BlockPos exitPos; private BlockPos exitPos;
private int be_teleportDelay = 0;
public ServerPlayerMixin(Level world, BlockPos pos, float yaw, GameProfile profile) { public ServerPlayerMixin(Level world, BlockPos pos, float yaw, GameProfile profile) {
super(world, pos, yaw, profile); super(world, pos, yaw, profile);
@ -111,11 +112,25 @@ public abstract class ServerPlayerMixin extends Player implements TeleportingEnt
lastSentHealth = -1.0F; lastSentHealth = -1.0F;
lastSentFood = -1; lastSentFood = -1;
} }
be_teleportDelay = 100;
be_resetExitPos(); be_resetExitPos();
info.setReturnValue(player); info.setReturnValue(player);
} }
} }
@Inject(method = "tick", at = @At("TAIL"))
public void be_decreaseCooldawn(CallbackInfo info) {
if (be_teleportDelay > 0) be_teleportDelay--;
}
@Override
public int getDimensionChangingDelay() {
if (be_teleportDelay > 0) {
return be_teleportDelay;
}
return super.getDimensionChangingDelay();
}
@Shadow @Shadow
abstract ServerLevel getLevel(); abstract ServerLevel getLevel();