Skip to content

Commit

Permalink
Merge pull request #695 from zyxkad/dev/0.8
Browse files Browse the repository at this point in the history
fix saddle turtle teleport between dimesions
  • Loading branch information
SirEndii authored Jan 20, 2025
2 parents b5230f1 + 5d6ce68 commit 016c6c2
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ public static void renderingHuds(RenderGuiOverlayEvent.Pre event) {
@SubscribeEvent
public static void playerTryDismount(InputEvent.Key event) {
Minecraft minecraft = Minecraft.getInstance();
if (!minecraft.options.keyShift.matches(event.getKey(), event.getScanCode())) {
boolean isShift = minecraft.options.keyShift.matches(event.getKey(), event.getScanCode());
if (!isShift) {
return;
}
switch (event.getAction()) {
case InputConstants.PRESS:
sneaking = true;
if (ClientRegistry.SADDLE_TURTLE_OVERLAY.isPlayerMountedOnTurtle()) {
if (ClientRegistry.SADDLE_TURTLE_OVERLAY.isPlayerControllingTurtle()) {
minecraft.options.keyShift.setDown(false);
}
break;
Expand Down Expand Up @@ -65,7 +66,7 @@ public static void playerMounting(EntityMountEvent event) {

@SubscribeEvent
public static void playerMove(MovementInputUpdateEvent event) {
if (ClientRegistry.SADDLE_TURTLE_OVERLAY.isPlayerMountedOnTurtle()) {
if (ClientRegistry.SADDLE_TURTLE_OVERLAY.isPlayerControllingTurtle()) {
Input input = event.getInput();
if (sneaking == lastSneak && lastInput != null) {
if (lastInput.up == input.up && lastInput.down == input.down && lastInput.left == input.left && lastInput.right == input.right && lastInput.jumping == input.jumping) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ protected int textWidth(FormattedCharSequence text) {
return getFont().width(text);
}

public static boolean isPlayerControllingTurtle() {
LocalPlayer player = Minecraft.getInstance().player;
return player != null && player.getVehicle() instanceof TurtleSeatEntity;
}

public static boolean isPlayerMountedOnTurtle() {
LocalPlayer player = Minecraft.getInstance().player;
return player != null && player.getRootVehicle() instanceof TurtleSeatEntity;
Expand Down Expand Up @@ -131,6 +136,7 @@ private void renderDismountHint(PoseStack stack) {
getFont().drawShadow(stack, text, x, top, 0xffffff);
}

@Override
public void render(ForgeGui gui, PoseStack poseStack, float partialTick, int screenWidth, int screenHeight) {
if (!isPlayerMountedOnTurtle()) {
return;
Expand All @@ -143,6 +149,8 @@ public void render(ForgeGui gui, PoseStack poseStack, float partialTick, int scr
if (this.shouldRenderFuelBar()) {
this.renderFuelBar(poseStack);
}
this.renderDismountHint(poseStack);
if (isPlayerControllingTurtle()) {
this.renderDismountHint(poseStack);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.srendi.advancedperipherals.common.addons.computercraft.peripheral;

import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.peripheral.IComputerAccess;
import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.TurtleSide;
Expand Down Expand Up @@ -44,6 +45,11 @@ public boolean isEnabled() {
return APConfig.PERIPHERALS_CONFIG.enableChunkyTurtle.get();
}

@LuaFunction
public int getRadius() {
return ChunkManager.getMaxLoadRadius();
}

public void updateChunkState() {
// TODO: should find someway to update after turtle moved or while moving, but not every tick
ServerLevel level = (ServerLevel) getLevel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

public class MeBridgePeripheral extends BasePeripheral<BlockEntityPeripheralOwner<MeBridgeEntity>> implements IStorageSystemPeripheral {

public static final String PERIPHERAL_TYPE = "meBridge";
public static final String PERIPHERAL_TYPE = "me_bridge";
private final MeBridgeEntity tile;
private IGridNode node;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@
import de.srendi.advancedperipherals.common.network.APNetworking;
import de.srendi.advancedperipherals.common.network.toclient.SaddleTurtleInfoPacket;
import de.srendi.advancedperipherals.common.util.LuaConverter;
import de.srendi.advancedperipherals.common.util.TeleportUtil;
import de.srendi.advancedperipherals.common.util.fakeplayer.APFakePlayer;
import de.srendi.advancedperipherals.lib.peripherals.BasePeripheral;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntitySelector;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.TamableAnimal;
import net.minecraft.world.entity.vehicle.AbstractMinecart;
import net.minecraft.world.entity.vehicle.Boat;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.EntityHitResult;
Expand Down Expand Up @@ -68,7 +74,7 @@ public boolean isEntityRiding() {
@Override
public void attach(@NotNull IComputerAccess computer) {
super.attach(computer);
this.lastPos = owner.getPos();
this.lastPos = this.owner.getPos();
}

@Override
Expand All @@ -84,19 +90,29 @@ public void update() {
return;
}
this.seat.keepAlive();
BlockPos pos = owner.getPos();
BlockPos dir = pos.subtract(this.lastPos);
int dist = Math.abs(dir.getX()) + Math.abs(dir.getY()) + Math.abs(dir.getZ());
if (dist != 0) {
Vec3 newPos = new Vec3(pos.getX() + 0.5, pos.getY() + 0.4, pos.getZ() + 0.5);
if (dist == 1 && ++this.moveProg < ANIM_DURATION) {
float step = ((float) this.moveProg) / ANIM_DURATION;
newPos = newPos.add(Vec3.atLowerCornerOf(dir).scale(step - 1));
} else {
this.moveProg = 0;
this.lastPos = pos;
BlockPos pos = this.owner.getPos();
Level level = this.owner.getLevel();
if (this.seat.getLevel() != this.owner.getLevel()) {
this.seat = TeleportUtil.teleportToWithPassengers(this.seat, (ServerLevel) level, this.seat.getTurtlePos());
this.seat.setTurtle(this.owner.getTurtle());
this.seat.keepAlive();
this.rider = this.seat.getFirstPassenger();
this.moveProg = 0;
this.lastPos = pos;
} else {
BlockPos dir = pos.subtract(this.lastPos);
int dist = Math.abs(dir.getX()) + Math.abs(dir.getY()) + Math.abs(dir.getZ());
if (dist != 0) {
Vec3 newPos = this.seat.getTurtlePos();
if (dist == 1 && ++this.moveProg < ANIM_DURATION) {
float step = ((float) this.moveProg) / ANIM_DURATION;
newPos = newPos.add(Vec3.atLowerCornerOf(dir).scale(step - 1));
} else {
this.moveProg = 0;
this.lastPos = pos;
}
this.seat.moveTo(newPos);
}
this.seat.moveTo(newPos.x(), newPos.y(), newPos.z());
}
this.tickCount++;
if (this.tickCount > 40) {
Expand All @@ -108,21 +124,20 @@ public void update() {

private void sendHUD() {
if (this.rider instanceof ServerPlayer player) {
ITurtleAccess turtle = owner.getTurtle();
ITurtleAccess turtle = this.owner.getTurtle();
SaddleTurtleInfoPacket packet = new SaddleTurtleInfoPacket(turtle.getFuelLevel(), turtle.getFuelLimit(), barColor);
APNetworking.sendTo(packet, player);
}
}

private boolean sitDown(@NotNull Entity entity) {
Level world = owner.getLevel();
BlockPos pos = owner.getPos();
this.seat = new TurtleSeatEntity(owner.getTurtle());
this.seat.setPos(pos.getX() + 0.5, pos.getY() + 0.4, pos.getZ() + 0.5);
Level world = this.owner.getLevel();
this.seat = new TurtleSeatEntity(this.owner.getTurtle());
this.seat.setPos(this.seat.getTurtlePos());
if (!world.addFreshEntity(this.seat)) {
return false;
}
if (!entity.startRiding(this.seat, true)) {
if (!entity.startRiding(this.seat)) {
return false;
}
if (entity instanceof TamableAnimal tamable) {
Expand All @@ -138,14 +153,19 @@ private boolean standUp() {
if (this.seat == null) {
return false;
}
boolean isVehicle = this.seat.isVehicle();
Entity passenger = this.seat.getFirstPassenger();
if (passenger != null) {
this.seat.ejectPassengers();
BlockPos pos = this.owner.getPos();
passenger.dismountTo(pos.getX() + 0.5, pos.getY() + 0.9, pos.getZ() + 0.5);
}
this.seat.discard();
this.seat = null;
this.rider = null;
if (owner.getTurtle() instanceof TurtleBrain brain) {
brain.getOwner().createServerComputer().queueEvent("saddle_release");
}
return isVehicle;
return passenger != null;
}

@LuaFunction(mainThread = true)
Expand All @@ -154,16 +174,22 @@ public MethodResult capture() throws LuaException {
return MethodResult.of(null, "Another entity is riding");
}
return withOperation(SADDLE_CAPTURE, null, null, context -> {
Predicate<Entity> suitableEntity = (entity) -> entity.isAlive();
Predicate<Entity> suitableEntity = EntitySelector.NO_SPECTATORS
.and((entity) -> entity instanceof LivingEntity || entity instanceof AbstractMinecart || entity instanceof Boat)
.and((entity) -> !entity.isPassenger());
if (!APConfig.PERIPHERALS_CONFIG.allowSaddleTurtleCapturePlayer.get()) {
suitableEntity = suitableEntity.and((entity) -> !(entity instanceof Player));
}
final Predicate<Entity> finalSuitableEntity = suitableEntity;
HitResult entityHit = owner.withPlayer(player -> player.findHit(false, true, finalSuitableEntity));
final APFakePlayer.Action<HitResult> action = (player) -> player.findHit(false, true, finalSuitableEntity);
HitResult entityHit = owner.withPlayer(action);
if (entityHit.getType() == HitResult.Type.MISS) {
return MethodResult.of(null, "Nothing found");
entityHit = owner.withPlayer(APFakePlayer.wrapActionWithReachRange(1, APFakePlayer.wrapActionWithRot(0, -90, action)));
if (entityHit.getType() == HitResult.Type.MISS) {
return MethodResult.of(null, "Nothing found");
}
}
LivingEntity entity = (LivingEntity) ((EntityHitResult) entityHit).getEntity();
Entity entity = ((EntityHitResult) entityHit).getEntity();
if (!sitDown(entity)) {
return MethodResult.of(null, "Entity cannot sit");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public AutomataBlockHandPlugin(AutomataCorePeripheral automataCore) {

@Override
public @Nullable IPeripheralOperation<?>[] getOperations() {
return new IPeripheralOperation[]{DIG, USE_ON_BLOCK};
return new IPeripheralOperation[]{ACCURE_PLACE, DIG, UPDATE_BLOCK, USE_ON_BLOCK};
}

@LuaFunction(mainThread = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public double calculateDistance() {
Vec3 from = center;
Vec3 to = from.add(direction.x * maxRange, direction.y * maxRange, direction.z * maxRange);

HitResult result = this.getHitResult(to, from);
HitResult result = this.getHitResult(from, to);
if (result.getType() == HitResult.Type.MISS) {
return -1;
}
Expand All @@ -225,12 +225,12 @@ public double calculateAndUpdateDistance() {
return distance;
}

private HitResult getHitResult(Vec3 to, Vec3 from) {
private HitResult getHitResult(Vec3 from, Vec3 to) {
Level level = this.getLevel();
return switch (this.detectionType) {
case ENTITY -> HitResultUtil.getEntityHitResult(to, from, level);
case BLOCK -> HitResultUtil.getBlockHitResult(to, from, level, this.ignoreTransparent ? HitResultUtil.IgnoreNoOccludedContext.INSTANCE : ClipContext.Block.COLLIDER, this.getBlockPos());
case BOTH -> HitResultUtil.getHitResult(to, from, level, this.ignoreTransparent ? HitResultUtil.IgnoreNoOccludedContext.INSTANCE : ClipContext.Block.COLLIDER, this.getBlockPos());
case ENTITY -> HitResultUtil.getEntityHitResult(from, to, level);
case BLOCK -> HitResultUtil.getBlockHitResult(from, to, level, this.ignoreTransparent ? HitResultUtil.IgnoreNoOccludedContext.INSTANCE : ClipContext.Block.COLLIDER, this.getBlockPos());
case BOTH -> HitResultUtil.getHitResult(from, to, level, this.ignoreTransparent ? HitResultUtil.IgnoreNoOccludedContext.INSTANCE : ClipContext.Block.COLLIDER, this.getBlockPos());
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import dan200.computercraft.shared.computer.core.ServerContext;
import de.srendi.advancedperipherals.AdvancedPeripherals;
import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.ChunkyPeripheral;
import de.srendi.advancedperipherals.common.util.ChunkManager;
import de.srendi.advancedperipherals.common.util.inventory.ItemUtil;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
Expand Down Expand Up @@ -102,6 +103,8 @@ private static int forceloadDump(CommandSourceStack source) throws CommandSyntax
);
}

ChunkManager manager = ChunkManager.get(source.getServer().overworld());
source.sendSuccess(Component.literal("Forced " + manager.getForcedChunksCount() + " chunks"), true);
table.display(source);
return computers.length;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.HasCustomInventoryScreen;
Expand All @@ -26,6 +27,7 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.PushReaction;
import net.minecraft.world.level.portal.PortalInfo;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Nullable;
Expand All @@ -34,8 +36,8 @@ public class TurtleSeatEntity extends Entity implements HasCustomInventoryScreen

// TODO: better rendering

private ITurtleAccess turtle;
private int life;
private ITurtleAccess turtle = null;
private int life = 0;

private boolean forwardKey = false;
private boolean backKey = false;
Expand All @@ -58,7 +60,10 @@ public TurtleSeatEntity(EntityType<?> type, Level world) {
public TurtleSeatEntity(ITurtleAccess turtle) {
this(APEntities.TURTLE_SEAT.get(), turtle.getLevel());
this.turtle = turtle;
this.life = 0;
}

public void setTurtle(ITurtleAccess turtle) {
this.turtle = turtle;
}

public ITurtleAccess getOwner() {
Expand All @@ -81,6 +86,11 @@ public void keepAlive() {
this.life = 2;
}

public Vec3 getTurtlePos() {
BlockPos pos = this.turtle.getPosition();
return Vec3.atCenterOf(pos);
}

@Override
public Packet<?> getAddEntityPacket() {
return new ClientboundAddEntityPacket(this);
Expand Down Expand Up @@ -124,14 +134,19 @@ protected void removePassenger(Entity entity) {

@Override
public Vec3 getDismountLocationForPassenger(LivingEntity entity) {
return super.getDismountLocationForPassenger(entity).add(0, 0.5, 0);
return this.getTurtlePos().add(0, 0.4, 0);
}

@Override
public Entity getControllingPassenger() {
return null; // this.getFirstPassenger();
}

@Override
public double getPassengersRidingOffset() {
return 0.05;
}

@Override
public void tick() {
if (this.level.isClientSide) {
Expand Down Expand Up @@ -221,6 +236,11 @@ public boolean canChangeDimensions() {
return false;
}

@Override
public PortalInfo findDimensionEntryPoint(ServerLevel newLevel) {
return new PortalInfo(this.getTurtlePos(), Vec3.ZERO, 0, 0);
}

@Override
public boolean shouldBlockExplode(net.minecraft.world.level.Explosion a0, net.minecraft.world.level.BlockGetter a1, BlockPos a2, BlockState a3, float a4) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public class APEntities {
() -> EntityType.Builder.<TurtleEnderPearl>of(TurtleEnderPearl::new, MobCategory.MISC)
.sized(0.5F, 0.5F)
.clientTrackingRange(4)
.updateInterval(5)
.updateInterval(4)
.fireImmune()
.build("turtle_ender_pearl"));
public static final RegistryObject<EntityType<TurtleSeatEntity>> TURTLE_SEAT = APRegistration.ENTITIES.register("turtle_seat",
() -> EntityType.Builder.<TurtleSeatEntity>of(TurtleSeatEntity::new, MobCategory.MISC)
.sized(0.8F, 0.2F)
.sized(0.8F, 0.8F)
.clientTrackingRange(4)
.updateInterval(4)
.updateInterval(1)
.fireImmune()
.build("turtle_seat"));

Expand Down
Loading

0 comments on commit 016c6c2

Please sign in to comment.