Skip to content

Commit

Permalink
Movement fixed. IGN is now semi-case-sensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
tanishisherewithhh committed Jun 3, 2023
1 parent 779910f commit dd84758
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 60 deletions.
55 changes: 55 additions & 0 deletions src/main/java/com/lumaa/act/command/ActorCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.lumaa.act.command;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.lumaa.act.entity.ActorEntity;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.builder.ArgumentBuilder;
Expand All @@ -21,15 +24,18 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;


public class ActorCommand {
public static final String name = "actor";
private List<ActorEntity> actors = new ArrayList<>();
private List<ServerPlayerEntity> players;

public int onRun(CommandContext<ServerCommandSource> command, String username) {
UUID uuid = lookForPlayer(username);
username=getUsername(username);

ServerCommandSource source = command.getSource();
Vec3d pos = source.getPosition();
Expand Down Expand Up @@ -78,7 +84,55 @@ public String getName() {
return ActorCommand.name;
}

public String getUsername(String arg) {
try {
URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + arg);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

// Parse the JSON response to extract the UUID
JsonObject jsonResponse = JsonParser.parseString(response.toString()).getAsJsonObject();
String name = jsonResponse.get("name").getAsString();
return name;
} catch (Exception e) {
return arg;
}
}


public UUID lookForPlayer(String arg) {
try {
URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + arg);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

// Parse the JSON response to extract the UUID
JsonObject jsonResponse = JsonParser.parseString(response.toString()).getAsJsonObject();
String uuidString = jsonResponse.get("id").getAsString();
return UUID.fromString(formatUuid(uuidString));
} catch (Exception e) {
return lookForPlayerUsingMinecraftAPI(arg);
}
}


public UUID lookForPlayerUsingMinecraftAPI(String arg) {
try {
URL url = new URL("https://minecraft-api.com/api/uuid/" + arg);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
Expand All @@ -103,6 +157,7 @@ public UUID lookForPlayer(String arg) {
}
}


public static String formatUuid(String uuidString) {
String formattedUuidString = uuidString.replaceAll(
"(.{8})(.{4})(.{4})(.{4})(.+)",
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/com/lumaa/act/entity/ActorEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public class ActorEntity extends ServerPlayerEntity {
public boolean isFollowingPlayer = false;
public boolean isFollowingBlock = false;
public boolean isStuck = false;
public volatile boolean keepMovingToPlayer = true;
public volatile boolean keepMovingToBlock = true;
public Thread playermovementThread = null;
public Thread positionmovementThread = null;
private final double runSpeed = 0.2;
private final double walkSpeed = 0.14;
private final double sneakSpeed = 0.04;
public final double swimSpeed = 0.30;

public ActorEntity(MinecraftServer server, ServerWorld world, GameProfile profile) {
super(server, world, profile);
Expand Down Expand Up @@ -92,7 +100,17 @@ public void checkAndTeleport(PlayerEntity player) {
}
}


public double getMovementSpeed(Movement.EMovementState movementState) {
if (movementState == Movement.EMovementState.WALK) {
return walkSpeed;
} else if (movementState == Movement.EMovementState.RUN) {
return runSpeed;
} else if (movementState == Movement.EMovementState.SNEAK) {
return sneakSpeed;
} else {
return 0.0d;
}
}


/**
Expand Down
75 changes: 24 additions & 51 deletions src/main/java/com/lumaa/act/entity/Movement.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.lumaa.act.entity;

import com.lumaa.act.item.stick.SpeedManagerStick;
import net.fabricmc.loader.impl.lib.sat4j.core.Vec;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.EntityPose;
Expand All @@ -13,37 +12,15 @@

public class Movement {
// Actor Movement Speed
private static final double runSpeed = 0.2;
private static final double walkSpeed = 0.14;
private static final double sneakSpeed = 0.04;
private static final double swimSpeed = 0.30;
private static volatile boolean keepMovingToPlayer = true;
private static volatile boolean keepMovingToBlock = true;
private static Vec3d goal;
private static Thread playermovementThread = null;
private static Thread positionmovementThread = null;

public static double getMovementSpeed(EMovementState movementState) {
if (movementState == EMovementState.WALK) {
return walkSpeed;
} else if (movementState == EMovementState.RUN) {
return runSpeed;
} else if (movementState == EMovementState.SNEAK) {
return sneakSpeed;
} else {
return 0.0d;
}
}


public static void moveToPlayer(PlayerEntity player, ActorEntity actor) {
keepMovingToPlayer = true;
actor.keepMovingToPlayer = true;
moveTowardsPlayer(actor, player);
if (actor.isDead() || actor.notInAnyWorld) stopMoving(actor);
}

public static void moveToBlockPos(BlockPos blockPos, ActorEntity actor) {
keepMovingToBlock = true;
actor.keepMovingToBlock = true;
moveTowardsPosition(actor, blockPos.toCenterPos());
if (actor.isDead() || actor.notInAnyWorld || actor.getBlockPos() == blockPos) stopMoving(actor);
}
Expand Down Expand Up @@ -81,26 +58,24 @@ else if (player.isTouchingWater())
}

public static void moveTowardsPosition(ActorEntity actor, Vec3d position) {
if (positionmovementThread!=null)positionmovementThread.interrupt();
positionmovementThread = new Thread(() -> {
if ( actor.positionmovementThread!=null) actor.positionmovementThread.interrupt();
actor.positionmovementThread = new Thread(() -> {
World world = actor.getEntityWorld();
double distance = actor.getPos().distanceTo(position);
if (distance < 0.5) {
stopMovingToBlock(actor);
return;
}
while (keepMovingToBlock) {
while ( actor.keepMovingToBlock) {
setCrouchingState(actor);
Vec3d direction2 = getDirection(actor, position);
goal = (position);
// System.out.println("Current Goal: " + goal);
direction2 = adjustDirectionForBlocks(actor, world, direction2);
setActorVelocity(actor, direction2);
actor.setSwimming(actor.isSubmergedInWater());
}
if (actor.getPos()==position)keepMovingToBlock=false;
if (actor.getPos()==position) actor.keepMovingToBlock=false;
});
positionmovementThread.start();
actor.positionmovementThread.start();
}


Expand All @@ -110,10 +85,10 @@ private static Vec3d getDirection(ActorEntity actor, Vec3d position) {
}

public static void moveTowardsPlayer(ActorEntity actor, PlayerEntity player) {
playermovementThread = new Thread(() -> {
actor.playermovementThread = new Thread(() -> {
actor.isStuck = false;
World world = actor.getEntityWorld();
while (keepMovingToPlayer && world.getDimension() == player.getWorld().getDimension()) {
while ( actor.keepMovingToPlayer && world.getDimension() == player.getWorld().getDimension()) {

actor.checkAndTeleport(player);

Expand All @@ -134,7 +109,7 @@ public static void moveTowardsPlayer(ActorEntity actor, PlayerEntity player) {
swim(actor, player);
}
});
playermovementThread.start();
actor.playermovementThread.start();
}

private static void setCrouchingState(ActorEntity actor) {
Expand All @@ -154,7 +129,7 @@ private static Vec3d getDirection(ActorEntity actor, PlayerEntity player) {
}

private static Vec3d adjustDirectionForBlocks(ActorEntity actor, World world, Vec3d direction2) {
double speed = getMovementSpeed(SpeedManagerStick.getState());
double speed = actor.getMovementSpeed(SpeedManagerStick.getState());
BlockPos blockPos = new BlockPos(BlockPos.ofFloored(actor.getPos().add(direction2.multiply(speed))));
BlockState blockState = world.getBlockState(blockPos);
if (blockState.isAir()) {
Expand All @@ -168,30 +143,28 @@ private static Vec3d adjustDirectionForBlocks(ActorEntity actor, World world, Ve
}

private static void setActorVelocity(ActorEntity actor, Vec3d direction2) {
double speed = getMovementSpeed(SpeedManagerStick.getState());
if (actor != null) {
synchronized (actor) {
if (actor.isSubmergedInWater()) speed = swimSpeed;
actor.setVelocity(direction2.multiply(speed));
// System.out.println("Velocity of actor: " + direction2.multiply(speed));
}
double speed = actor.getMovementSpeed(SpeedManagerStick.getState());
synchronized (actor) {
if (actor.isSubmergedInWater()) speed = actor.swimSpeed;
actor.setVelocity(direction2.multiply(speed));
// System.out.println("Velocity of actor: " + direction2.multiply(speed));
}
}
public static void stopMovingToBlock(ActorEntity actor)
{
keepMovingToBlock=false;
if (positionmovementThread != null) {
positionmovementThread.interrupt();
positionmovementThread = null;
actor.keepMovingToBlock=false;
if ( actor.positionmovementThread != null) {
actor.positionmovementThread.interrupt();
actor.positionmovementThread = null;
}
actor.isFollowingBlock = false;
}

public static void stopMoving(ActorEntity actor) {
keepMovingToPlayer = false;
if (playermovementThread != null) {
playermovementThread.interrupt();
playermovementThread = null;
actor.keepMovingToPlayer = false;
if ( actor.playermovementThread != null) {
actor. playermovementThread.interrupt();
actor.playermovementThread = null;
}
actor.isFollowingPlayer = false;
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/lumaa/act/item/stick/FollowStick.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ public FollowStick(Settings settings) {

@Override
public ActionResult useOnEntity(ItemStack stack, PlayerEntity user, LivingEntity entity, Hand hand) {
if (entity instanceof ActorEntity) {
ActorEntity actor = (ActorEntity) entity;
if (entity instanceof ActorEntity actor) {
boolean follows = actor.getAi().action.getAction().equals(ActorAction.Actions.FOLLOW);
actor.isFollowingPlayer = !actor.isFollowingPlayer; // Update the isFollowinglayer state for this actor
user.playSound(SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, 1f, follows ? 0.8f : 1f);

if (actor.isFollowingPlayer) {
Movement.moveToPlayer(user, actor);
} else {
actor.getAi().action.setAction(ActorAction.Actions.NONE);
Movement.stopMoving(actor);
}
return ActionResult.SUCCESS;
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/lumaa/act/item/stick/TravelStick.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,14 @@ public ActionResult useOnBlock(ItemUsageContext context) {
VecblockPos = context.getHitPos();
assert MinecraftClient.getInstance().player != null;

if (actor != null) actor.isFollowingBlock = !actor.isFollowingBlock; // Update the isFollowingBlock state for this actor
//if (actor != null) actor.isFollowingBlock = !actor.isFollowingBlock; // Update the isFollowingBlock state for this actor
if (actor == null) {
MinecraftClient.getInstance().player.sendMessage(Text.of("Right-Click on an Actor first").copy().formatted(Formatting.RED), false);
MinecraftClient.getInstance().player.playSound(SoundEvents.ENTITY_VILLAGER_NO, SoundCategory.PLAYERS, 1f, 1f);
} else if (!actor.isFollowingBlock) {
MinecraftClient.getInstance().player.playSound(SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, 1f, 1f);
System.out.println("BlockPos recieved: " + BlockPos.ofFloored(VecblockPos));
stopMovingToBlock(actor);
moveToBlockPos(BlockPos.ofFloored(VecblockPos), actor);
if (Objects.equals(actor.getBlockPos(), BlockPos.ofFloored(VecblockPos))) stopMoving(actor);
actor.getAi().moveTo(ActorMovement.MovementState.RUN,VecblockPos);
if (Objects.equals(actor.getBlockPos(), BlockPos.ofFloored(VecblockPos))) actor.getAi().movement.pathfinder.path.stop();
} else
stopMovingToBlock(actor);
}
Expand Down

0 comments on commit dd84758

Please sign in to comment.