Skip to content

Commit

Permalink
fix climbing
Browse files Browse the repository at this point in the history
don't assume entities are loaded yet
  • Loading branch information
TropheusJ committed Jul 8, 2023
1 parent d1a6ed4 commit 2902881
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private static void changeClimbingState(ServerPlayer player, @Nullable Vec3 clic

private static void addCupEntities(ServerPlayer player, ClimbingState state, Vec3 clickPos, Direction facing) {
Level level = player.level();
for (SuctionCupLimb limb : SuctionCupLimb.values()) {
for (SuctionCupLimb limb : SuctionCupLimb.VALUES) {
Vec3 cupPos = clickPos.add(SuccUtils.rotateVec(limb.cupOffset, facing.toYRot()));
ClimbingSuctionCupEntity entity = new ClimbingSuctionCupEntity(level, limb, cupPos, state, facing);
level.addFreshEntity(entity);
Expand Down
76 changes: 32 additions & 44 deletions src/main/java/one/devos/nautical/succ/LocalClimbingManager.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package one.devos.nautical.succ;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.tuple.Triple;
import org.quiltmc.loader.api.minecraft.ClientOnly;
import org.quiltmc.qsl.networking.api.PacketByteBufs;
import org.quiltmc.qsl.networking.api.client.ClientPlayNetworking;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

import org.jetbrains.annotations.Nullable;

import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -49,10 +43,10 @@ public class LocalClimbingManager {

public final ClimbingState state;
public final float minYaw, maxYaw, minPitch, maxPitch;
public final List<Triple<KeyMapping, SuctionCupLimb, ClimbingSuctionCupEntity>> cups = new ArrayList<>();
public final List<Triple<KeyMapping, SuctionCupLimb, ClimbingSuctionCupEntity>> liftedCups = new LinkedList<>();

Triple<KeyMapping, SuctionCupLimb, ClimbingSuctionCupEntity> movedCup = null;
public final List<SuctionCupLimb> liftedCups = new ArrayList<>();

SuctionCupLimb movedCup = null;
private int initialCooldown = 10;
private int ticksTillStop = 0;
SuctionCupMoveDirection lastInputDirection = null;
Expand All @@ -64,13 +58,8 @@ public class LocalClimbingManager {
@Nullable
public static LocalClimbingManager INSTANCE = null;

public LocalClimbingManager(Minecraft mc) {
this.state = GlobalClimbingManager.getState(mc.player);
this.state.entities.forEach((limb, entity) -> {
KeyMapping key = SuccKeybinds.LIMBS_TO_KEYS.get(limb);
cups.add(Triple.of(key, limb, entity));
});
LocalPlayer player = mc.player;
public LocalClimbingManager(LocalPlayer player, ClimbingState state) {
this.state = state;
player.yBodyRot = player.getYRot();
float playerYaw = player.getYRot();
minYaw = playerYaw - (DEFAULT_ROTATION_RANGE / 2f);
Expand All @@ -81,14 +70,14 @@ public LocalClimbingManager(Minecraft mc) {
}

public static void tick(Minecraft mc, ClientLevel level) {
if (INSTANCE != null) {
if (INSTANCE != null && INSTANCE.isValid()) {
INSTANCE.tickClimbing(mc, level);
}
}

// runs every frame
public static void clientTick(Minecraft mc) {
if (INSTANCE != null) {
if (INSTANCE != null && INSTANCE.isValid()) {
INSTANCE.frameTick(mc);
}
}
Expand All @@ -114,7 +103,7 @@ private void frameTick(Minecraft mc) {

private void moveSelectedCup(LocalPlayer player, ClientLevel level, Options options) {
if (movedCup != null) {
ClimbingSuctionCupEntity entity = movedCup.getRight();
ClimbingSuctionCupEntity entity = state.entities.get(movedCup);
SuctionCupMoveDirection direction = SuctionCupMoveDirection.findFromInputs(options);
SuctionCupMoveDirection currentDirection = entity.getMoveDirection();
if (currentDirection != direction) {
Expand Down Expand Up @@ -169,8 +158,8 @@ private Component getTimerComponent() {
private boolean newCupPosCloseEnough(LocalPlayer player, ClientLevel level, ClimbingSuctionCupEntity entity,
Vec3 newPos, SuctionCupMoveDirection direction) {
double largestDistance = 0;
for (Triple<KeyMapping, SuctionCupLimb, ClimbingSuctionCupEntity> cup : cups) {
ClimbingSuctionCupEntity otherEntity = cup.getRight();
for (SuctionCupLimb limb : SuctionCupLimb.VALUES) {
ClimbingSuctionCupEntity otherEntity = state.entities.get(limb);
if (entity != otherEntity) {
largestDistance = Math.max(largestDistance, otherEntity.position().distanceTo(newPos));
}
Expand All @@ -195,8 +184,8 @@ private boolean newCupPosNotObstructed(LocalPlayer player, ClientLevel level, Cl
sendNotification(player, level, direction, SuctionCupItem.OBSTRUCTED_LIQUID);
return false;
}
for (Triple<KeyMapping, SuctionCupLimb, ClimbingSuctionCupEntity> cup : cups) {
ClimbingSuctionCupEntity otherEntity = cup.getRight();
for (SuctionCupLimb limb : SuctionCupLimb.VALUES) {
ClimbingSuctionCupEntity otherEntity = this.state.entities.get(limb);
if (entity != otherEntity) {
AABB otherBounds = otherEntity.getBoundingBox();
AABB newBounds = entity.getDimensions(Pose.STANDING).makeBoundingBox(newPos);
Expand All @@ -222,36 +211,31 @@ private void sendNotification(LocalPlayer player, ClientLevel level, SuctionCupM
private void handleSuctionStateChanges() {
// give a short cooldown after starting to climb
if (initialCooldown > 0) {
cups.forEach(triple -> {
KeyMapping key = triple.getLeft();
for (KeyMapping key : SuccKeybinds.CLIMBING_KEYS) {
while (key.consumeClick()); // discard any clicks that happen here
});
}
return;
}
cups.forEach(triple -> {
KeyMapping key = triple.getLeft();
ClimbingSuctionCupEntity entity = triple.getRight();
for (SuctionCupLimb limb : SuctionCupLimb.VALUES) {
KeyMapping key = SuccKeybinds.LIMBS_TO_KEYS.get(limb);
ClimbingSuctionCupEntity entity = state.entities.get(limb);
if (key.consumeClick()) {
boolean suction = entity.getSuction();
entity.setSuctionFromClient(!suction);
}
while (key.consumeClick()); // get rid of extras
});
}
}

public void entitySuctionUpdated(ClimbingSuctionCupEntity entity) {
Optional<Triple<KeyMapping, SuctionCupLimb, ClimbingSuctionCupEntity>> maybeTriple =
cups.stream().filter(t -> t.getRight() == entity).findFirst();
maybeTriple.ifPresent(triple -> {
if (entity.getSuction()) { // placed down
liftedCups.remove(triple);
movedCup = liftedCups.isEmpty() ? null : liftedCups.get(liftedCups.size() - 1);
lastInputDirection = null;
} else { // picked up
movedCup = triple;
liftedCups.add(triple);
}
});
if (entity.getSuction()) { // placed down
liftedCups.remove(entity.limb);
movedCup = liftedCups.isEmpty() ? null : liftedCups.get(liftedCups.size() - 1);
lastInputDirection = null;
} else { // picked up
movedCup = entity.limb;
liftedCups.add(entity.limb);
}
}

private void handlePlayerMovement(LocalPlayer player, float partialTicks) {
Expand All @@ -265,8 +249,8 @@ private void handlePlayerMovement(LocalPlayer player, float partialTicks) {
double totalX = 0;
double totalY = 0;
double totalZ = 0;
for (Triple<KeyMapping, SuctionCupLimb, ClimbingSuctionCupEntity> triple : cups) {
ClimbingSuctionCupEntity cup = triple.getRight();
for (SuctionCupLimb limb : SuctionCupLimb.VALUES) {
ClimbingSuctionCupEntity cup = state.entities.get(limb);
Vec3 toUse = cup.isBeingPlaced() ? cup.getPosition(partialTicks) : cup.getStuckPos();
totalX += Mth.lerp(partialTicks, toUse.x, cup.xOld);
totalY += Mth.lerp(partialTicks, toUse.y, cup.yOld);
Expand All @@ -287,4 +271,8 @@ private void handlePlayerMovement(LocalPlayer player, float partialTicks) {
double finalZ = Mth.lerp(partialTicks * 2, oldPos.z, averageZ);
player.setPos(finalX, finalY, finalZ);
}

public boolean isValid() {
return state.entities.size() == 4;
}
}
4 changes: 2 additions & 2 deletions src/main/java/one/devos/nautical/succ/StateChangeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void handle(Minecraft mc, FriendlyByteBuf buf) {
GlobalClimbingManager.putState(state, true);
LocalPlayer player = Minecraft.getInstance().player;
if (state.isClimbing() && player != null && player.getUUID().equals(state.playerUuid)) {
LocalClimbingManager.INSTANCE = new LocalClimbingManager(mc);
LocalClimbingManager.INSTANCE = new LocalClimbingManager(player, state);
}
});
}
Expand All @@ -32,7 +32,7 @@ public void handle(Minecraft mc, FriendlyByteBuf buf) {
state.facing = facing;
LocalPlayer player = mc.player;
if (player != null && player.getUUID().equals(playerId)) {
LocalClimbingManager.INSTANCE = state.isClimbing() ? new LocalClimbingManager(mc) : null;
LocalClimbingManager.INSTANCE = state.isClimbing() ? new LocalClimbingManager(player, state) : null;
}
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/one/devos/nautical/succ/SuctionCupLimb.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public enum SuctionCupLimb {
LEFT_FOOT(true, false, PlayerModelPart.LEFT_PANTS_LEG),
RIGHT_FOOT(false, false, PlayerModelPart.RIGHT_PANTS_LEG);

public static final SuctionCupLimb[] VALUES = values();

public final Vec3 cupOffset; // the offset from the player of cup start positions
public final Vec3 offsetFromPlayer; // the offset from the player to where it connects to the torso
public final boolean left;
Expand Down

0 comments on commit 2902881

Please sign in to comment.