mcjty.lib.typed.Key

Here are the examples of the java api mcjty.lib.typed.Key taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

5 Examples 7

19 Source : CommandHandler.java
with MIT License
from McJtyMods

public clreplaced CommandHandler {

    public static final String CMD_TESTRECIPE = "testRecipe";

    public static final String CMD_GETGRAPHICS = "getGraphics";

    public static final Key<BlockPos> PARAM_POS = new Key<>("pos", Type.BLOCKPOS);

    public static void registerCommands() {
        McJtyLib.registerCommand(RFToolsControl.MODID, CMD_TESTRECIPE, (player, arguments) -> {
            ItemStack heldItem = player.getHeldItem(EnumHand.MAIN_HAND);
            if (heldItem.isEmpty()) {
                return false;
            }
            if (heldItem.gereplacedem() instanceof CraftingCardItem) {
                CraftingCardItem.testRecipe(player.getEnreplacedyWorld(), heldItem);
            }
            return true;
        });
        McJtyLib.registerCommand(RFToolsControl.MODID, CMD_GETGRAPHICS, (player, arguments) -> {
            TileEnreplacedy te = player.getEnreplacedyWorld().getTileEnreplacedy(arguments.get(PARAM_POS));
            if (te instanceof ProcessorTileEnreplacedy) {
                ProcessorTileEnreplacedy processor = (ProcessorTileEnreplacedy) te;
                RFToolsCtrlMessages.INSTANCE.sendTo(new PacketGraphicsReady(processor), (EnreplacedyPlayerMP) player);
            }
            return true;
        });
    }
}

19 Source : CommandHandler.java
with MIT License
from McJtyMods

public clreplaced CommandHandler {

    public static final String CMD_CANCEL_PORTAL = "cancel_portal";

    public static final String CMD_DELETE_DESTINATION = "delete_dest";

    public static final String CMD_SET_CURRENT = "set_current";

    public static final String CMD_RESUME_ACTION = "resume_action";

    public static final String CMD_CANCEL_ACTION = "cancel_action";

    public static final Key<BlockPos> PARAM_POS = new Key<>("pos", Type.BLOCKPOS);

    public static final Key<Integer> PARAM_ID = new Key<>("id", Type.INTEGER);

    public static void registerCommands() {
        McJtyLib.registerCommand(MeeCreeps.MODID, CMD_CANCEL_PORTAL, (player, arguments) -> {
            ItemStack heldItem = PortalGunItem.getGun(player);
            // Something went wrong
            if (heldItem.isEmpty())
                return false;
            TeleportationTools.cancelPortalPair(player, arguments.get(PARAM_POS));
            return true;
        });
        McJtyLib.registerCommand(MeeCreeps.MODID, CMD_DELETE_DESTINATION, (player, arguments) -> {
            ItemStack heldItem = PortalGunItem.getGun(player);
            // Something went wrong
            if (heldItem.isEmpty())
                return false;
            PortalGunItem.addDestination(heldItem, null, arguments.get(PARAM_ID));
            return true;
        });
        McJtyLib.registerCommand(MeeCreeps.MODID, CMD_SET_CURRENT, (player, arguments) -> {
            ItemStack heldItem = PortalGunItem.getGun(player);
            // Something went wrong
            if (heldItem.isEmpty())
                return false;
            PortalGunItem.setCurrentDestination(heldItem, arguments.get(PARAM_ID));
            return true;
        });
        McJtyLib.registerCommand(MeeCreeps.MODID, CMD_RESUME_ACTION, (player, arguments) -> {
            ServerActionManager.getManager().resumeAction((EnreplacedyPlayerMP) player, arguments.get(PARAM_ID));
            return true;
        });
        McJtyLib.registerCommand(MeeCreeps.MODID, CMD_CANCEL_ACTION, (player, arguments) -> {
            ServerActionManager.getManager().cancelAction((EnreplacedyPlayerMP) player, arguments.get(PARAM_ID));
            return true;
        });
    }
}

18 Source : NodeTileEntity.java
with MIT License
from McJtyMods

public clreplaced NodeTileEnreplacedy extends GenericTileEnreplacedy {

    public static final String CMD_UPDATE = "node.update";

    public static final Key<String> PARAM_NODE = new Key<>("node", Type.STRING);

    public static final Key<String> PARAM_CHANNEL = new Key<>("channel", Type.STRING);

    private String channel;

    private String node;

    private BlockPos processor = null;

    // Bitmask for all six sides
    private int prevIn = 0;

    private int[] powerOut = new int[] { 0, 0, 0, 0, 0, 0 };

    public String getNodeName() {
        return node;
    }

    public String getChannelName() {
        return channel;
    }

    public BlockPos getProcessor() {
        return processor;
    }

    public void setProcessor(BlockPos processor) {
        this.processor = processor;
        markDirty();
    }

    @Override
    public void setPowerInput(int powered) {
        if (powerLevel != powered) {
            if (processor != null) {
                TileEnreplacedy te = getWorld().getTileEnreplacedy(processor);
                if (te instanceof ProcessorTileEnreplacedy) {
                    ProcessorTileEnreplacedy processorTileEnreplacedy = (ProcessorTileEnreplacedy) te;
                    processorTileEnreplacedy.redstoneNodeChange(prevIn, powered, node);
                }
            }
            prevIn = powered;
        }
        super.setPowerInput(powered);
    }

    public int getPowerOut(EnumFacing side) {
        return powerOut[side.ordinal()];
    }

    public void setPowerOut(EnumFacing side, int powerOut) {
        this.powerOut[side.ordinal()] = powerOut;
        markDirty();
        getWorld().neighborChanged(this.pos.offset(side), this.getBlockType(), this.pos);
    }

    @Override
    public void readFromNBT(NBTTagCompound tagCompound) {
        super.readFromNBT(tagCompound);
        prevIn = tagCompound.getInteger("prevIn");
        for (int i = 0; i < 6; i++) {
            powerOut[i] = tagCompound.getByte("p" + i);
        }
    }

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound tagCompound) {
        super.writeToNBT(tagCompound);
        tagCompound.setInteger("prevIn", prevIn);
        for (int i = 0; i < 6; i++) {
            tagCompound.setByte("p" + i, (byte) powerOut[i]);
        }
        return tagCompound;
    }

    @Override
    public void readRestorableFromNBT(NBTTagCompound tagCompound) {
        super.readRestorableFromNBT(tagCompound);
        channel = tagCompound.getString("channel");
        node = tagCompound.getString("node");
        processor = BlockPosTools.readFromNBT(tagCompound, "processor");
    }

    @Override
    public void writeRestorableToNBT(NBTTagCompound tagCompound) {
        super.writeRestorableToNBT(tagCompound);
        if (channel != null) {
            tagCompound.setString("channel", channel);
        }
        if (node != null) {
            tagCompound.setString("node", node);
        }
        if (processor != null) {
            BlockPosTools.writeToNBT(tagCompound, "processor", processor);
        }
    }

    @Override
    public boolean execute(EnreplacedyPlayerMP playerMP, String command, TypedMap args) {
        boolean rc = super.execute(playerMP, command, args);
        if (rc) {
            return true;
        }
        if (CMD_UPDATE.equals(command)) {
            this.node = args.get(PARAM_NODE);
            this.channel = args.get(PARAM_CHANNEL);
            markDirtyClient();
            return true;
        }
        return false;
    }
}

17 Source : CraftingStationTileEntity.java
with MIT License
from McJtyMods

public clreplaced CraftingStationTileEnreplacedy extends GenericTileEnreplacedy implements DefaultSidedInventory {

    public static final String CMD_GETCRAFTABLE = "getCraftable";

    public static final String CLIENTCMD_GETCRAFTABLE = "getCraftable";

    public static final String CMD_GETREQUESTS = "getRequests";

    public static final String CLIENTCMD_GETREQUESTS = "getRequests";

    public static final String CMD_REQUEST = "station.request";

    public static final Key<String> PARAM_ITEMNAME = new Key<>("itemname", Type.STRING);

    public static final Key<Integer> PARAM_META = new Key<>("meta", Type.INTEGER);

    public static final Key<String> PARAM_NBT = new Key<>("nbt", Type.STRING);

    public static final Key<Integer> PARAM_AMOUNT = new Key<>("amount", Type.INTEGER);

    public static final String CMD_CANCEL = "station.cancel";

    public static final Key<Integer> PARAM_INDEX = new Key<>("index", Type.INTEGER);

    private InventoryHelper inventoryHelper = new InventoryHelper(this, CraftingStationContainer.factory, 9);

    private List<BlockPos> processorList = new ArrayList<>();

    private int currentTicket = 0;

    private List<CraftingRequest> activeCraftingRequests = new ArrayList<>();

    private int cleanupCounter = 50;

    @Override
    protected boolean needsCustomInvWrapper() {
        return true;
    }

    public void registerProcessor(BlockPos pos) {
        if (!processorList.contains(pos)) {
            processorList.add(pos);
        }
        markDirty();
    }

    // @todo optimize finding requests on craftid!!!!
    public ItemStack getCraftResult(String craftId) {
        for (CraftingRequest request : activeCraftingRequests) {
            if (craftId.equals(request.getTicket())) {
                return request.getStack();
            }
        }
        return ItemStack.EMPTY;
    }

    private Pair<ProcessorTileEnreplacedy, ItemStack> findCraftableItem(int index) {
        for (BlockPos p : processorList) {
            TileEnreplacedy te = getWorld().getTileEnreplacedy(p);
            if (te instanceof ProcessorTileEnreplacedy) {
                ProcessorTileEnreplacedy processor = (ProcessorTileEnreplacedy) te;
                ItemStackList items = ItemStackList.create();
                processor.getCraftableItems(items);
                for (ItemStack item : items) {
                    if (index == 0) {
                        // found!
                        return Pair.of(processor, item);
                    }
                    index--;
                }
            }
        }
        return null;
    }

    public ItemStack craftOk(ProcessorTileEnreplacedy processor, String ticket, ItemStack stack) {
        CraftingRequest foundRequest = null;
        for (CraftingRequest request : activeCraftingRequests) {
            if (ticket.equals(request.getTicket())) {
                foundRequest = request;
                break;
            }
        }
        if (foundRequest != null) {
            markDirty();
            foundRequest.decrTodo();
            if (foundRequest.getTodo() <= 0) {
                foundRequest.setOk(System.currentTimeMillis() + 1000);
            } else {
                processor.fireCraftEvent(ticket, foundRequest.getStack());
            }
            if (!stack.isEmpty()) {
                Inventory inventory = getInventoryFromTicket(ticket);
                if (inventory != null) {
                    IItemHandler handlerAt = processor.gereplacedemHandlerAt(inventory);
                    if (handlerAt == null) {
                        throw new ProgException(ExceptionType.EXCEPT_INVALIDINVENTORY);
                    }
                    return ItemHandlerHelper.inserreplacedem(handlerAt, stack, false);
                } else {
                    return ItemHandlerHelper.inserreplacedem(getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null), stack, false);
                }
            }
        }
        return stack;
    }

    public void craftFail(String ticket) {
        for (CraftingRequest request : activeCraftingRequests) {
            if (ticket.equals(request.getTicket())) {
                request.setFailed(System.currentTimeMillis() + 2000);
                markDirty();
            }
        }
    }

    private void cancelCraft(int index) {
        try {
            activeCraftingRequests.remove(index);
        } catch (Exception e) {
        }
    }

    private void startCraft(int index, int amount) {
        Pair<ProcessorTileEnreplacedy, ItemStack> pair = findCraftableItem(index);
        if (pair == null) {
            // Somehow not possible
            System.out.println("What? Can't happen");
            return;
        }
        String ticket = getNewTicket(null);
        ItemStack stack = pair.getValue();
        int count = (amount + stack.getCount() - 1) / stack.getCount();
        CraftingRequest request = new CraftingRequest(ticket, stack, count);
        if (!checkRequestAmount()) {
            return;
        }
        activeCraftingRequests.add(request);
        pair.getKey().fireCraftEvent(ticket, stack);
        cleanupCounter--;
        if (cleanupCounter <= 0) {
            cleanupCounter = 50;
            cleanupStaleRequests();
        }
    }

    private boolean checkRequestAmount() {
        if (activeCraftingRequests.size() >= ConfigSetup.maxCraftRequests.get()) {
            cleanupCounter = 50;
            cleanupStaleRequests();
            if (activeCraftingRequests.size() >= ConfigSetup.maxCraftRequests.get()) {
                // To many requests
                return false;
            }
        }
        return true;
    }

    public boolean isRequested(ItemStack item) {
        for (CraftingRequest request : activeCraftingRequests) {
            long failed = request.getFailed();
            long ok = request.getOk();
            if ((failed == -1) && (ok == -1)) {
                if (request.getStack().isItemEqual(item)) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean request(ItemStack item, @Nullable Inventory destination) {
        for (BlockPos p : processorList) {
            TileEnreplacedy te = getWorld().getTileEnreplacedy(p);
            if (te instanceof ProcessorTileEnreplacedy) {
                ProcessorTileEnreplacedy processor = (ProcessorTileEnreplacedy) te;
                ItemStackList items = ItemStackList.create();
                processor.getCraftableItems(items);
                for (ItemStack i : items) {
                    if (item.isItemEqual(i)) {
                        String ticket = getNewTicket(destination);
                        if (!checkRequestAmount()) {
                            return false;
                        }
                        activeCraftingRequests.add(new CraftingRequest(ticket, i, 1));
                        processor.fireCraftEvent(ticket, i);
                        return true;
                    }
                }
            }
        }
        return false;
    }

    private String getNewTicket(@Nullable Inventory destInv) {
        currentTicket++;
        markDirty();
        if (destInv != null) {
            return destInv.serialize() + "#" + currentTicket;
        } else {
            return BlockPosTools.toString(pos) + ":" + currentTicket;
        }
    }

    // / Returns null if the ticket represents a crafting station instead
    @Nullable
    private Inventory getInventoryFromTicket(String ticket) {
        if (ticket.startsWith("#")) {
            return Inventory.deserialize(ticket);
        }
        return null;
    }

    // / Returns null if the ticket represents an inventory
    @Nullable
    private BlockPos getPositionFromTicket(String ticket) {
        if (ticket.startsWith("#")) {
            return null;
        }
        String[] splitted = StringUtils.split(ticket, ';');
        String[] poss = StringUtils.split(splitted[0], ',');
        return new BlockPos(Integer.parseInt(poss[0]), Integer.parseInt(poss[1]), Integer.parseInt(poss[2]));
    }

    public ItemStackList getCraftableItems() {
        ItemStackList items = ItemStackList.create();
        for (BlockPos p : processorList) {
            TileEnreplacedy te = getWorld().getTileEnreplacedy(p);
            if (te instanceof ProcessorTileEnreplacedy) {
                ProcessorTileEnreplacedy processor = (ProcessorTileEnreplacedy) te;
                processor.getCraftableItems(items);
            }
        }
        return items;
    }

    private void cleanupStaleRequests() {
        long time = System.currentTimeMillis();
        List<CraftingRequest> oldRequests = this.activeCraftingRequests;
        activeCraftingRequests = new ArrayList<>();
        for (CraftingRequest request : oldRequests) {
            long failed = request.getFailed();
            long ok = request.getOk();
            if ((failed == -1 || time <= failed) && (ok == -1 || time <= ok)) {
                activeCraftingRequests.add(request);
            }
        }
    }

    public List<CraftingRequest> getRequests() {
        cleanupStaleRequests();
        return new ArrayList<>(activeCraftingRequests);
    }

    @Override
    public void readFromNBT(NBTTagCompound tagCompound) {
        super.readFromNBT(tagCompound);
        readProcessorList(tagCompound);
        readRequests(tagCompound);
    }

    private void readRequests(NBTTagCompound tagCompound) {
        NBTTagList list = tagCompound.getTagList("requests", Constants.NBT.TAG_COMPOUND);
        activeCraftingRequests.clear();
        for (int i = 0; i < list.tagCount(); i++) {
            NBTTagCompound requestTag = list.getCompoundTagAt(i);
            String craftId = requestTag.getString("craftId");
            ItemStack stack = new ItemStack(requestTag.getCompoundTag("stack"));
            int count = requestTag.getInteger("count");
            CraftingRequest request = new CraftingRequest(craftId, stack, count);
            request.setFailed(requestTag.getLong("failed"));
            request.setOk(requestTag.getLong("ok"));
            activeCraftingRequests.add(request);
        }
    }

    private void readProcessorList(NBTTagCompound tagCompound) {
        NBTTagList list = tagCompound.getTagList("processors", Constants.NBT.TAG_COMPOUND);
        processorList.clear();
        for (int i = 0; i < list.tagCount(); i++) {
            NBTTagCompound tag = list.getCompoundTagAt(i);
            processorList.add(new BlockPos(tag.getInteger("x"), tag.getInteger("y"), tag.getInteger("z")));
        }
    }

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound tagCompound) {
        super.writeToNBT(tagCompound);
        writeProcessorList(tagCompound);
        writeRequests(tagCompound);
        return tagCompound;
    }

    private void writeRequests(NBTTagCompound tagCompound) {
        NBTTagList list = new NBTTagList();
        for (CraftingRequest request : activeCraftingRequests) {
            NBTTagCompound requestTag = new NBTTagCompound();
            requestTag.setString("craftId", request.getTicket());
            NBTTagCompound stackNbt = new NBTTagCompound();
            request.getStack().writeToNBT(stackNbt);
            requestTag.setTag("stack", stackNbt);
            requestTag.setInteger("count", request.getTodo());
            requestTag.setLong("failed", request.getFailed());
            requestTag.setLong("ok", request.getOk());
            list.appendTag(requestTag);
        }
        tagCompound.setTag("requests", list);
    }

    private void writeProcessorList(NBTTagCompound tagCompound) {
        NBTTagList list = new NBTTagList();
        for (BlockPos p : processorList) {
            NBTTagCompound tag = new NBTTagCompound();
            tag.setInteger("x", p.getX());
            tag.setInteger("y", p.getY());
            tag.setInteger("z", p.getZ());
            list.appendTag(tag);
        }
        tagCompound.setTag("processors", list);
    }

    @Override
    public void readRestorableFromNBT(NBTTagCompound tagCompound) {
        super.readRestorableFromNBT(tagCompound);
        readBufferFromNBT(tagCompound, inventoryHelper);
        currentTicket = tagCompound.getInteger("craftId");
    }

    @Override
    public void writeRestorableToNBT(NBTTagCompound tagCompound) {
        super.writeRestorableToNBT(tagCompound);
        writeBufferToNBT(tagCompound, inventoryHelper);
        tagCompound.setInteger("craftId", currentTicket);
    }

    @Override
    public InventoryHelper getInventoryHelper() {
        return inventoryHelper;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public boolean isUsableByPlayer(EnreplacedyPlayer player) {
        return canPlayerAccess(player);
    }

    private int findItem(String itemName, int meta, String nbtString) {
        int index = 0;
        for (BlockPos p : processorList) {
            TileEnreplacedy te = getWorld().getTileEnreplacedy(p);
            if (te instanceof ProcessorTileEnreplacedy) {
                ProcessorTileEnreplacedy processor = (ProcessorTileEnreplacedy) te;
                ItemStackList items = ItemStackList.create();
                processor.getCraftableItems(items);
                for (ItemStack item : items) {
                    if (item.gereplacedemDamage() == meta && itemName.equals(item.gereplacedem().getRegistryName().toString())) {
                        if (item.hasTagCompound()) {
                            if (nbtString.equalsIgnoreCase(item.serializeNBT().toString())) {
                                return index;
                            }
                        } else {
                            return index;
                        }
                    }
                    index++;
                }
            }
        }
        return -1;
    }

    @Override
    public boolean execute(EnreplacedyPlayerMP playerMP, String command, TypedMap params) {
        boolean rc = super.execute(playerMP, command, params);
        if (rc) {
            return true;
        }
        if (CMD_REQUEST.equals(command)) {
            String itemName = params.get(PARAM_ITEMNAME);
            int meta = params.get(PARAM_META);
            String nbtString = params.get(PARAM_NBT);
            int index = findItem(itemName, meta, nbtString);
            if (index == -1) {
                return true;
            }
            int amount = params.get(PARAM_AMOUNT);
            startCraft(index, amount);
            return true;
        } else if (CMD_CANCEL.equals(command)) {
            int index = params.get(PARAM_INDEX);
            cancelCraft(index);
            return true;
        }
        return false;
    }

    @Nonnull
    @Override
    public <T> List<T> executeWithResultList(String command, TypedMap args, Type<T> type) {
        List<T> rc = super.executeWithResultList(command, args, type);
        if (!rc.isEmpty()) {
            return rc;
        }
        if (CMD_GETCRAFTABLE.equals(command)) {
            return type.convert(getCraftableItems());
        } else if (CMD_GETREQUESTS.equals(command)) {
            return type.convert(getRequests());
        }
        return Collections.emptyList();
    }

    @Override
    public <T> boolean receiveListFromServer(String command, List<T> list, Type<T> type) {
        boolean rc = super.receiveListFromServer(command, list, type);
        if (rc) {
            return true;
        }
        if (CLIENTCMD_GETCRAFTABLE.equals(command)) {
            GuiCraftingStation.storeCraftableForClient(Type.create(ItemStack.clreplaced).convert(list));
            return true;
        } else if (CLIENTCMD_GETREQUESTS.equals(command)) {
            GuiCraftingStation.storeRequestsForClient(Type.create(CraftingRequest.clreplaced).convert(list));
            return true;
        }
        return false;
    }
}

13 Source : PacketUpdateNBTItemCard.java
with MIT License
from McJtyMods

public void handle(Supplier<Context> supplier) {
    Context ctx = supplier.get();
    ctx.enqueueWork(() -> {
        EnreplacedyPlayerMP playerEnreplacedy = ctx.getSender();
        ItemStack heldItem = playerEnreplacedy.getHeldItem(EnumHand.MAIN_HAND);
        if (heldItem.isEmpty()) {
            return;
        }
        // To avoid people messing with packets
        if (!isValidItem(heldItem)) {
            return;
        }
        NBTTagCompound tagCompound = heldItem.getTagCompound();
        if (tagCompound == null) {
            tagCompound = new NBTTagCompound();
            heldItem.setTagCompound(tagCompound);
        }
        for (Key<?> akey : args.getKeys()) {
            String key = akey.getName();
            if (Type.STRING.equals(akey.getType())) {
                tagCompound.setString(key, (String) args.get(akey));
            } else if (Type.INTEGER.equals(akey.getType())) {
                tagCompound.setInteger(key, (Integer) args.get(akey));
            } else if (Type.LONG.equals(akey.getType())) {
                tagCompound.setLong(key, (Long) args.get(akey));
            } else if (Type.DOUBLE.equals(akey.getType())) {
                tagCompound.setDouble(key, (Double) args.get(akey));
            } else if (Type.BOOLEAN.equals(akey.getType())) {
                tagCompound.setBoolean(key, (Boolean) args.get(akey));
            } else if (Type.BLOCKPOS.equals(akey.getType())) {
                throw new RuntimeException("BlockPos not supported for PacketUpdateNBreplacedem!");
            } else if (Type.ITEMSTACK.equals(akey.getType())) {
                throw new RuntimeException("ItemStack not supported for PacketUpdateNBreplacedem!");
            } else {
                throw new RuntimeException(akey.getType().getType().getSimpleName() + " not supported for PacketUpdateNBreplacedem!");
            }
        }
    });
    ctx.setPacketHandled(true);
}