Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package de.srendi.advancedperipherals.common.addons.computercraft.peripheral.plugins;

import dan200.computercraft.api.lua.IArguments;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.core.apis.TableHelper;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.TurtlePeripheralOwner;
import de.srendi.advancedperipherals.common.util.Pair;
import de.srendi.advancedperipherals.common.util.fakeplayer.APFakePlayer;
import de.srendi.advancedperipherals.lib.peripherals.AutomataCorePeripheral;
import de.srendi.advancedperipherals.lib.peripherals.IPeripheralOperation;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.ItemStack;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.Map;

import static de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperation.DIG;
import static de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperation.USE_ON_BLOCK;
Expand All @@ -27,31 +33,41 @@ public AutomataBlockHandPlugin(AutomataCorePeripheral automataCore) {
}

@LuaFunction(mainThread = true)
public final MethodResult digBlock() throws LuaException {
public final MethodResult digBlock(@NotNull IArguments arguments) throws LuaException {
Map<?, ?> opts = arguments.count() > 0 ? arguments.getTable(0) : Collections.emptyMap();
boolean sneak = TableHelper.optBooleanField(opts, "sneak", false);
float yaw = opts != null ? (float) TableHelper.optNumberField(opts, "yaw", 0) : 0;
float pitch = opts != null ? (float) TableHelper.optNumberField(opts, "pitch", 0) : 0;
return automataCore.withOperation(DIG, context -> {
TurtlePeripheralOwner owner = automataCore.getPeripheralOwner();
ItemStack selectedTool = owner.getToolInMainHand();
int previousDamageValue = selectedTool.getDamageValue();
Pair<Boolean, String> result = owner.withPlayer(apFakePlayer -> apFakePlayer.digBlock(owner.getFacing().getOpposite()));
Pair<Boolean, String> result = owner.withPlayer(APFakePlayer.wrapActionWithShiftKey(sneak, APFakePlayer.wrapActionWithRot(yaw, pitch, APFakePlayer::digBlock)));
if (!result.getLeft()) {
return MethodResult.of(null, result.getRight());
}
if (automataCore.hasAttribute(AutomataCorePeripheral.ATTR_STORING_TOOL_DURABILITY))
if (automataCore.hasAttribute(AutomataCorePeripheral.ATTR_STORING_TOOL_DURABILITY)) {
selectedTool.setDamageValue(previousDamageValue);
}
return MethodResult.of(true);
});
}

@LuaFunction(mainThread = true)
public final MethodResult useOnBlock() throws LuaException {
public final MethodResult useOnBlock(@NotNull IArguments arguments) throws LuaException {
Map<?, ?> opts = arguments.count() > 0 ? arguments.getTable(0) : Collections.emptyMap();
boolean sneak = TableHelper.optBooleanField(opts, "sneak", false);
float yaw = opts != null ? (float) TableHelper.optNumberField(opts, "yaw", 0) : 0;
float pitch = opts != null ? (float) TableHelper.optNumberField(opts, "pitch", 0) : 0;
return automataCore.withOperation(USE_ON_BLOCK, context -> {
TurtlePeripheralOwner owner = automataCore.getPeripheralOwner();
ItemStack selectedTool = owner.getToolInMainHand();
int previousDamageValue = selectedTool.getDamageValue();
InteractionResult result = owner.withPlayer(APFakePlayer::useOnBlock);
if (automataCore.hasAttribute(AutomataCorePeripheral.ATTR_STORING_TOOL_DURABILITY))
InteractionResult result = owner.withPlayer(APFakePlayer.wrapActionWithShiftKey(sneak, APFakePlayer.wrapActionWithRot(yaw, pitch, APFakePlayer::useOnBlock)));
if (automataCore.hasAttribute(AutomataCorePeripheral.ATTR_STORING_TOOL_DURABILITY)) {
selectedTool.setDamageValue(previousDamageValue);
return MethodResult.of(true, result.toString());
}
return MethodResult.of(result.consumesAction(), result.toString());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import de.srendi.advancedperipherals.lib.peripherals.AutomataCorePeripheral;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import org.jetbrains.annotations.NotNull;

import org.jetbrains.annotations.NotNull;
import java.util.Objects;

public class AutomataChargingPlugin extends AutomataCorePlugin {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package de.srendi.advancedperipherals.common.addons.computercraft.peripheral.plugins;

import dan200.computercraft.api.lua.IArguments;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.core.apis.TableHelper;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.TurtlePeripheralOwner;
import de.srendi.advancedperipherals.common.util.LuaConverter;
import de.srendi.advancedperipherals.common.util.fakeplayer.APFakePlayer;
import de.srendi.advancedperipherals.lib.peripherals.AutomataCorePeripheral;
import de.srendi.advancedperipherals.lib.peripherals.IPeripheralOperation;
import net.minecraft.core.BlockPos;
Expand All @@ -15,9 +18,10 @@
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
Expand All @@ -39,12 +43,16 @@ public AutomataEntityHandPlugin(AutomataCorePeripheral automataCore, Predicate<E
}

@LuaFunction(mainThread = true)
public final MethodResult useOnAnimal() throws LuaException {
public final MethodResult useOnAnimal(@NotNull IArguments arguments) throws LuaException {
Map<?, ?> opts = arguments.count() > 0 ? arguments.getTable(0) : Collections.emptyMap();
boolean sneak = TableHelper.optBooleanField(opts, "sneak", false);
float yaw = opts != null ? (float) TableHelper.optNumberField(opts, "yaw", 0) : 0;
float pitch = opts != null ? (float) TableHelper.optNumberField(opts, "pitch", 0) : 0;
return automataCore.withOperation(USE_ON_ANIMAL, context -> {
TurtlePeripheralOwner owner = automataCore.getPeripheralOwner();
ItemStack selectedTool = owner.getToolInMainHand();
int previousDamageValue = selectedTool.getDamageValue();
InteractionResult result = owner.withPlayer(player -> player.useOnFilteredEntity(suitableEntity));
InteractionResult result = owner.withPlayer(APFakePlayer.wrapActionWithShiftKey(sneak, APFakePlayer.wrapActionWithRot(yaw, pitch, p -> p.useOnFilteredEntity(suitableEntity))));
if (automataCore.hasAttribute(AutomataCorePeripheral.ATTR_STORING_TOOL_DURABILITY))
selectedTool.setDamageValue(previousDamageValue);

Expand All @@ -53,10 +61,14 @@ public final MethodResult useOnAnimal() throws LuaException {
}

@LuaFunction(mainThread = true)
public final MethodResult inspectAnimal() {
public final MethodResult inspectAnimal(@NotNull IArguments arguments) throws LuaException {
Map<?, ?> opts = arguments.count() > 0 ? arguments.getTable(0) : Collections.emptyMap();
float yaw = opts != null ? (float) TableHelper.optNumberField(opts, "yaw", 0) : 0;
float pitch = opts != null ? (float) TableHelper.optNumberField(opts, "pitch", 0) : 0;

automataCore.addRotationCycle();
TurtlePeripheralOwner owner = automataCore.getPeripheralOwner();
HitResult entityHit = owner.withPlayer(player -> player.findHit(false, true, suitableEntity));
HitResult entityHit = owner.withPlayer(APFakePlayer.wrapActionWithRot(yaw, pitch, p -> p.findHit(false, true, suitableEntity)));
if (entityHit.getType() == HitResult.Type.MISS)
return MethodResult.of(null, "Nothing found");

Expand All @@ -73,9 +85,8 @@ public final MethodResult searchAnimals() {
TurtlePeripheralOwner owner = automataCore.getPeripheralOwner();
BlockPos currentPos = owner.getPos();
AABB box = new AABB(currentPos);
List<Map<String, Object>> entities = new ArrayList<>();
ItemStack itemInHand = owner.getToolInMainHand();
owner.getLevel().getEntities((Entity) null, box.inflate(automataCore.getInteractionRadius()), suitableEntity).forEach(entity -> entities.add(LuaConverter.completeEntityWithPositionToLua(entity, itemInHand, currentPos)));
List<Map<String, Object>> entities = owner.getLevel().getEntities((Entity) null, box.inflate(automataCore.getInteractionRadius()), suitableEntity).stream().map(entity -> LuaConverter.completeEntityWithPositionToLua(entity, itemInHand, currentPos)).toList();
return MethodResult.of(entities);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package de.srendi.advancedperipherals.common.addons.computercraft.peripheral.plugins;

import dan200.computercraft.api.lua.IArguments;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.core.apis.TableHelper;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.TurtlePeripheralOwner;
import de.srendi.advancedperipherals.common.util.LuaConverter;
import de.srendi.advancedperipherals.common.util.fakeplayer.APFakePlayer;
import de.srendi.advancedperipherals.lib.peripherals.AutomataCorePeripheral;
import de.srendi.advancedperipherals.lib.peripherals.IPeripheralOperation;
import net.minecraft.core.BlockPos;
Expand All @@ -16,7 +19,10 @@
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.Map;
import java.util.function.Predicate;

import static de.srendi.advancedperipherals.common.addons.computercraft.operations.SingleOperation.CAPTURE_ANIMAL;
Expand Down Expand Up @@ -69,8 +75,12 @@ protected Entity extractEntity() {


@LuaFunction(mainThread = true)
public final MethodResult captureAnimal() throws LuaException {
HitResult entityHit = automataCore.getPeripheralOwner().withPlayer(player -> player.findHit(false, true, suitableEntity));
public final MethodResult captureAnimal(@NotNull IArguments arguments) throws LuaException {
Map<?, ?> opts = arguments.count() > 0 ? arguments.getTable(0) : Collections.emptyMap();
float yaw = opts != null ? (float) TableHelper.optNumberField(opts, "yaw", 0) : 0;
float pitch = opts != null ? (float) TableHelper.optNumberField(opts, "pitch", 0) : 0;

HitResult entityHit = automataCore.getPeripheralOwner().withPlayer(APFakePlayer.wrapActionWithRot(yaw, pitch, p -> p.findHit(false, true, suitableEntity)));
if (entityHit.getType() == HitResult.Type.MISS)
return MethodResult.of(null, "Nothing found");
return automataCore.withOperation(CAPTURE_ANIMAL, context -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.AABB;
import net.minecraftforge.registries.ForgeRegistries;
import org.jetbrains.annotations.NotNull;

import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package de.srendi.advancedperipherals.common.addons.computercraft.peripheral.plugins;

import dan200.computercraft.api.lua.IArguments;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.lua.MethodResult;
import dan200.computercraft.core.apis.TableHelper;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.TurtlePeripheralOwner;
import de.srendi.advancedperipherals.common.util.LuaConverter;
import de.srendi.advancedperipherals.common.util.fakeplayer.APFakePlayer;
import de.srendi.advancedperipherals.lib.peripherals.AutomataCorePeripheral;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.BlockState;
Expand All @@ -12,6 +16,8 @@
import net.minecraft.world.phys.HitResult;
import net.minecraftforge.registries.ForgeRegistries;

import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -22,10 +28,14 @@ public AutomataLookPlugin(AutomataCorePeripheral automataCore) {
}

@LuaFunction(mainThread = true)
public final MethodResult lookAtBlock() {
public final MethodResult lookAtBlock(@NotNull IArguments arguments) throws LuaException {
Map<?, ?> opts = arguments.count() > 0 ? arguments.getTable(0) : Collections.emptyMap();
float yaw = opts != null ? (float) TableHelper.optNumberField(opts, "yaw", 0) : 0;
float pitch = opts != null ? (float) TableHelper.optNumberField(opts, "pitch", 0) : 0;

automataCore.addRotationCycle();
TurtlePeripheralOwner owner = automataCore.getPeripheralOwner();
HitResult result = owner.withPlayer(apFakePlayer -> apFakePlayer.findHit(true, false));
HitResult result = owner.withPlayer(APFakePlayer.wrapActionWithRot(yaw, pitch, p -> p.findHit(true, false)));
if (result.getType() == HitResult.Type.MISS)
return MethodResult.of(null, "No block find");

Expand All @@ -40,9 +50,13 @@ public final MethodResult lookAtBlock() {
}

@LuaFunction(mainThread = true)
public final MethodResult lookAtEntity() {
public final MethodResult lookAtEntity(@NotNull IArguments arguments) throws LuaException {
Map<?, ?> opts = arguments.count() > 0 ? arguments.getTable(0) : Collections.emptyMap();
float yaw = opts != null ? (float) TableHelper.optNumberField(opts, "yaw", 0) : 0;
float pitch = opts != null ? (float) TableHelper.optNumberField(opts, "pitch", 0) : 0;

automataCore.addRotationCycle();
HitResult result = automataCore.getPeripheralOwner().withPlayer(apFakePlayer -> apFakePlayer.findHit(false, true));
HitResult result = automataCore.getPeripheralOwner().withPlayer(APFakePlayer.wrapActionWithRot(yaw, pitch, p -> p.findHit(false, true)));
if (result.getType() == HitResult.Type.MISS)
return MethodResult.of(null, "No entity find");

Expand Down
Loading