<commit_before>package boq.cctags.cc;

import static boq.utils.misc.Utils.*;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.Vec3;
import net.minecraftforge.common.ForgeDirection;
import boq.cctags.tag.TagData;
import boq.cctags.tag.TagSize;
import boq.cctags.tag.access.*;
import boq.cctags.tag.access.EntityAccess.IPositionProvider;
import boq.cctags.tag.access.ItemAccess.IStackProvider;
import dan200.computer.api.*;
import dan200.turtle.api.ITurtleAccess;

public abstract class TurtlePeripheral implements IHostedPeripheral {

    protected final ITurtleAccess turtle;

    protected ITagAccess tagAccess = AccessUtils.NULL;

    public TurtlePeripheral(ITurtleAccess turtle) {
        this.turtle = turtle;
    }

    protected ForgeDirection getDirection(String direction) {
        int turtleDir = turtle.getFacingDir();
        ForgeDirection front = ForgeDirection.VALID_DIRECTIONS[turtleDir];
        return SidesHelper.localToWorld(front, direction);
    }

    protected final static String[] commonMethods = { "isTagValid", "scanForTag", "selectFromSlot", "contents", "write", "size", "serial", "library", "source" };

    @Override
    public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception {
        switch (method) {
            case 0: // isTagValid
                return wrap(tagAccess.isValid());
            case 1: { // scanForTag
                String directionName = checkArg(arguments, 0) ? arguments[0].toString() : null;
                ForgeDirection dir = getDirection(directionName);
                ITagAccess access = AccessUtils.selectTag(turtle.getWorld(), dir, new IPositionProvider() {
                    @Override
                    public Vec3 getPosition() {
                        return turtle.getPosition();
                    }
                });

                if (!access.isValid())
                    return FALSE;

                tagAccess = access;
                return TRUE;
            }
            case 2: {// selectFromSlot
                final int slotId = checkArg(arguments, 0) ? toInt(arguments[0]) : turtle.getSelectedSlot();

                ITagAccess access = new ItemAccess(new IStackProvider() {
                    @Override
                    public ItemStack getStack() {
                        return turtle.getSlotContents(slotId);
                    }
                }, AccessUtils.mergedAccess);

                if (access.isValid()) {
                    tagAccess = access;
                    return TRUE;
                }

                return wrap(false, "Invalid or missing tag");

            }
            case 3: {// contents
                if (!tagAccess.isValid())<commit_msg>Reset tag acces after failed scan<commit_after>package boq.cctags.cc;

import static boq.utils.misc.Utils.*;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.Vec3;
import net.minecraftforge.common.ForgeDirection;
import boq.cctags.tag.TagData;
import boq.cctags.tag.TagSize;
import boq.cctags.tag.access.*;
import boq.cctags.tag.access.EntityAccess.IPositionProvider;
import boq.cctags.tag.access.ItemAccess.IStackProvider;
import dan200.computer.api.*;
import dan200.turtle.api.ITurtleAccess;

public abstract class TurtlePeripheral implements IHostedPeripheral {

    protected final ITurtleAccess turtle;

    protected ITagAccess tagAccess = AccessUtils.NULL;

    public TurtlePeripheral(ITurtleAccess turtle) {
        this.turtle = turtle;
    }

    protected ForgeDirection getDirection(String direction) {
        int turtleDir = turtle.getFacingDir();
        ForgeDirection front = ForgeDirection.VALID_DIRECTIONS[turtleDir];
        return SidesHelper.localToWorld(front, direction);
    }

    protected final static String[] commonMethods = { "isTagValid", "scanForTag", "selectFromSlot", "contents", "write", "size", "serial", "library", "source" };

    @Override
    public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) throws Exception {
        switch (method) {
            case 0: // isTagValid
                return wrap(tagAccess.isValid());
            case 1: { // scanForTag
                String directionName = checkArg(arguments, 0) ? arguments[0].toString() : null;
                ForgeDirection dir = getDirection(directionName);
                tagAccess = AccessUtils.selectTag(turtle.getWorld(), dir, new IPositionProvider() {
                    @Override
                    public Vec3 getPosition() {
                        return turtle.getPosition();
                    }
                });

                return wrap(tagAccess.isValid());
            }
            case 2: {// selectFromSlot
                final int slotId = checkArg(arguments, 0) ? toInt(arguments[0]) : turtle.getSelectedSlot();

                tagAccess = new ItemAccess(new IStackProvider() {
                    @Override
                    public ItemStack getStack() {
                        return turtle.getSlotContents(slotId);
                    }
                }, AccessUtils.mergedAccess);

                return wrap(tagAccess.isValid());
            }
            case 3: {// contents
                if (!tagAccess.isValid())
{'fraction_non_alphanumeric': 0.09665427509293681, 'fraction_numerical': 0.005947955390334572, 'mean_word_length': 2.2357185808779314, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4769322', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1372, 'n_words': 314}