Skip to content

Commit

Permalink
add saddle.getRider
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Apr 27, 2024
1 parent fe59567 commit dea2e60
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class SaddlePeripheral extends BasePeripheral<TurtlePeripheralOwner> {
private static final int ANIM_DURATION = 8; // Should be same as TurtleBrain.ANIM_DURATION

public static final String PERIPHERAL_TYPE = "saddle";
private TurtleSeatEntity seat = null;
private volatile TurtleSeatEntity seat = null;
private int moveProg = 0;
private BlockPos moveDir = null;

Expand All @@ -43,7 +43,7 @@ public boolean isEnabled() {

@Nullable
public Entity getRidingEntity() {
return this.seat;
return this.seat != null ? this.seat.getFirstPassenger() : null;
}

public boolean isEntityRiding() {
Expand All @@ -57,11 +57,8 @@ public void attach(@NotNull IComputerAccess computer) {

@Override
public void detach(@NotNull IComputerAccess computer) {
standUp();
super.detach(computer);
if (this.seat != null) {
this.seat.discard();
this.seat = null;
}
}

public void update() {
Expand Down Expand Up @@ -114,6 +111,16 @@ private boolean sitDown(@NotNull Entity entity) {
return true;
}

private boolean standUp() {
if (this.seat == null) {
return false;
}
boolean isVehicle = this.seat.isVehicle();
this.seat.discard();
this.seat = null;
return isVehicle;
}

@LuaFunction(mainThread = true)
public MethodResult capture() {
if (isEntityRiding()) {
Expand All @@ -135,4 +142,26 @@ public MethodResult capture() {
}
return MethodResult.of(true);
}

@LuaFunction(mainThread = true)
public MethodResult release() {
if (!standUp()) {
return MethodResult.of(null, "No entity is riding");
}
return MethodResult.of(true);
}

@LuaFunction(mainThread = true)
public boolean hasRider() {
return this.isEntityRiding();
}

@LuaFunction(mainThread = true)
public MethodResult getRider() {
Entity entity = getRidingEntity();
if (entity == null) {
return MethodResult.of(null);
}
return MethodResult.of(LuaConverter.completeEntityToLua(entity));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.tags.TagKey;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.properties.Property;
Expand Down Expand Up @@ -39,11 +41,15 @@ public static Map<String, Object> entityToLua(Entity entity) {
data.put("canFreeze", entity.canFreeze());
data.put("isGlowing", entity.isCurrentlyGlowing());
data.put("isInWall", entity.isInWall());
if (entity instanceof InventoryCarrier carrier) {
SimpleContainer inv = carrier.getInventory();
}
return data;
}

public static Map<String, Object> livingEntityToLua(LivingEntity entity) {
Map<String, Object> data = entityToLua(entity);
data.put("baby", entity.isBaby());
data.put("health", entity.getHealth());
data.put("maxHealth", entity.getMaxHealth());
data.put("lastDamageSource", entity.getLastDamageSource() == null ? null : entity.getLastDamageSource().toString());
Expand All @@ -52,7 +58,6 @@ public static Map<String, Object> livingEntityToLua(LivingEntity entity) {

public static Map<String, Object> animalToLua(Animal animal, ItemStack itemInHand) {
Map<String, Object> data = livingEntityToLua(animal);
data.put("baby", animal.isBaby());
data.put("inLove", animal.isInLove());
data.put("aggressive", animal.isAggressive());
if (animal instanceof IForgeShearable shareable && !itemInHand.isEmpty()) {
Expand All @@ -61,7 +66,15 @@ public static Map<String, Object> animalToLua(Animal animal, ItemStack itemInHan
return data;
}

public static Map<String, Object> playerToLua(Player player) {
Map<String, Object> data = livingEntityToLua(animal);
data.put("score", player.getScore());
data.put("luck", player.getLuck());
return data;
}

public static Map<String, Object> completeEntityToLua(Entity entity, ItemStack itemInHand) {
if (entity instanceof Player player) return playerToLua(player);
if (entity instanceof Animal animal) return animalToLua(animal, itemInHand);
if (entity instanceof LivingEntity livingEntity) return livingEntityToLua(livingEntity);
return entityToLua(entity);
Expand Down

0 comments on commit dea2e60

Please sign in to comment.