Search in sources :

Example 1 with ISecurityItem

use of mekanism.common.lib.security.ISecurityItem in project Mekanism by mekanism.

the class BlockMekanism method getPickBlock.

@Nonnull
@Override
public ItemStack getPickBlock(@Nonnull BlockState state, RayTraceResult target, @Nonnull IBlockReader world, @Nonnull BlockPos pos, PlayerEntity player) {
    ItemStack itemStack = new ItemStack(this);
    TileEntityMekanism tile = WorldUtils.getTileEntity(TileEntityMekanism.class, world, pos);
    if (tile == null) {
        return itemStack;
    }
    // TODO: Some of the data doesn't get properly "picked", because there are cases such as before opening the GUI where
    // the server doesn't bother syncing the data to the client. For example with what frequencies there are
    Item item = itemStack.getItem();
    if (tile.getFrequencyComponent().hasCustomFrequencies()) {
        tile.getFrequencyComponent().write(ItemDataUtils.getDataMap(itemStack));
    }
    if (item instanceof ISecurityItem && tile.hasSecurity()) {
        ISecurityItem securityItem = (ISecurityItem) item;
        securityItem.setOwnerUUID(itemStack, tile.getOwnerUUID());
        securityItem.setSecurity(itemStack, tile.getSecurityMode());
    }
    if (tile.supportsUpgrades()) {
        tile.getComponent().write(ItemDataUtils.getDataMap(itemStack));
    }
    if (tile instanceof ISideConfiguration) {
        ISideConfiguration config = (ISideConfiguration) tile;
        config.getConfig().write(ItemDataUtils.getDataMap(itemStack));
        config.getEjector().write(ItemDataUtils.getDataMap(itemStack));
    }
    if (tile instanceof ISustainedData) {
        ((ISustainedData) tile).writeSustainedData(itemStack);
    }
    if (tile.supportsRedstone()) {
        ItemDataUtils.setInt(itemStack, NBTConstants.CONTROL_TYPE, tile.getControlType().ordinal());
    }
    for (SubstanceType type : EnumUtils.SUBSTANCES) {
        if (tile.handles(type)) {
            ItemDataUtils.setList(itemStack, type.getContainerTag(), DataHandlerUtils.writeContainers(type.getContainers(tile)));
        }
    }
    if (item instanceof ISustainedInventory && tile.persistInventory() && tile.getSlots() > 0) {
        ((ISustainedInventory) item).setInventory(tile.getInventory(), itemStack);
    }
    return itemStack;
}
Also used : TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) Item(net.minecraft.item.Item) ISecurityItem(mekanism.common.lib.security.ISecurityItem) ISecurityItem(mekanism.common.lib.security.ISecurityItem) ISustainedInventory(mekanism.common.tile.interfaces.ISustainedInventory) SubstanceType(mekanism.common.tile.base.SubstanceType) ItemStack(net.minecraft.item.ItemStack) ISustainedData(mekanism.common.tile.interfaces.ISustainedData) ISideConfiguration(mekanism.common.tile.interfaces.ISideConfiguration) Nonnull(javax.annotation.Nonnull)

Example 2 with ISecurityItem

use of mekanism.common.lib.security.ISecurityItem in project Mekanism by mekanism.

the class RecipeUpgradeData method getSupportedTypes.

@Nonnull
static Set<RecipeUpgradeType> getSupportedTypes(ItemStack stack) {
    // TODO: Add more types of data that can be transferred such as side configs, auto sort, bucket mode, dumping mode
    if (stack.isEmpty()) {
        return Collections.emptySet();
    }
    Set<RecipeUpgradeType> supportedTypes = EnumSet.noneOf(RecipeUpgradeType.class);
    Item item = stack.getItem();
    TileEntityMekanism tile = null;
    if (item instanceof BlockItem) {
        Block block = ((BlockItem) item).getBlock();
        if (block instanceof IHasTileEntity) {
            TileEntity tileEntity = ((IHasTileEntity<?>) block).getTileType().create();
            if (tileEntity instanceof TileEntityMekanism) {
                tile = (TileEntityMekanism) tileEntity;
            }
        }
        if (Attribute.has(block, AttributeUpgradeSupport.class)) {
            supportedTypes.add(RecipeUpgradeType.UPGRADE);
        }
    }
    if (stack.getCapability(Capabilities.STRICT_ENERGY_CAPABILITY).isPresent() || tile != null && tile.handles(SubstanceType.ENERGY)) {
        // If we are for a block that handles energy, or we have an energy handler capability
        supportedTypes.add(RecipeUpgradeType.ENERGY);
    }
    if (FluidUtil.getFluidHandler(stack).isPresent() || tile != null && tile.handles(SubstanceType.FLUID)) {
        // If we are for a block that handles fluid, or we have a fluid handler capability
        supportedTypes.add(RecipeUpgradeType.FLUID);
    }
    if (stack.getCapability(Capabilities.GAS_HANDLER_CAPABILITY).isPresent() || tile != null && tile.handles(SubstanceType.GAS)) {
        // If we are for a block that handles gas, or we have a gas handler capability
        supportedTypes.add(RecipeUpgradeType.GAS);
    }
    if (stack.getCapability(Capabilities.INFUSION_HANDLER_CAPABILITY).isPresent() || tile != null && tile.handles(SubstanceType.INFUSION)) {
        // If we are for a block that handles infusion, or we have an infusion handler capability
        supportedTypes.add(RecipeUpgradeType.INFUSION);
    }
    if (stack.getCapability(Capabilities.PIGMENT_HANDLER_CAPABILITY).isPresent() || tile != null && tile.handles(SubstanceType.PIGMENT)) {
        // If we are for a block that handles pigment, or we have a pigment handler capability
        supportedTypes.add(RecipeUpgradeType.PIGMENT);
    }
    if (stack.getCapability(Capabilities.SLURRY_HANDLER_CAPABILITY).isPresent() || tile != null && tile.handles(SubstanceType.SLURRY)) {
        // If we are for a block that handles slurry, or we have a slurry handler capability
        supportedTypes.add(RecipeUpgradeType.SLURRY);
    }
    if (item instanceof ISustainedInventory || tile != null && tile.persistInventory()) {
        supportedTypes.add(RecipeUpgradeType.ITEM);
    }
    if (item instanceof ISecurityItem) {
        supportedTypes.add(RecipeUpgradeType.SECURITY);
    }
    if (item instanceof IQIODriveItem) {
        supportedTypes.add(RecipeUpgradeType.QIO_DRIVE);
    }
    return supportedTypes;
}
Also used : IHasTileEntity(mekanism.common.block.interfaces.IHasTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) IQIODriveItem(mekanism.common.content.qio.IQIODriveItem) Item(net.minecraft.item.Item) ISecurityItem(mekanism.common.lib.security.ISecurityItem) BlockItem(net.minecraft.item.BlockItem) TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) ISustainedInventory(mekanism.common.tile.interfaces.ISustainedInventory) ISecurityItem(mekanism.common.lib.security.ISecurityItem) IHasTileEntity(mekanism.common.block.interfaces.IHasTileEntity) Block(net.minecraft.block.Block) BlockItem(net.minecraft.item.BlockItem) IQIODriveItem(mekanism.common.content.qio.IQIODriveItem) Nonnull(javax.annotation.Nonnull)

Example 3 with ISecurityItem

use of mekanism.common.lib.security.ISecurityItem in project Mekanism by mekanism.

the class RecipeUpgradeData method getUpgradeData.

/**
 * Make sure to validate with getSupportedTypes before calling this
 */
@Nullable
static RecipeUpgradeData<?> getUpgradeData(@Nonnull RecipeUpgradeType type, @Nonnull ItemStack stack) {
    Item item = stack.getItem();
    switch(type) {
        case ENERGY:
            return new EnergyRecipeData(ItemDataUtils.getList(stack, NBTConstants.ENERGY_CONTAINERS));
        case FLUID:
            return new FluidRecipeData(ItemDataUtils.getList(stack, NBTConstants.FLUID_TANKS));
        case GAS:
            return new GasRecipeData(ItemDataUtils.getList(stack, NBTConstants.GAS_TANKS));
        case INFUSION:
            return new InfusionRecipeData(ItemDataUtils.getList(stack, NBTConstants.INFUSION_TANKS));
        case PIGMENT:
            return new PigmentRecipeData(ItemDataUtils.getList(stack, NBTConstants.PIGMENT_TANKS));
        case SLURRY:
            return new SlurryRecipeData(ItemDataUtils.getList(stack, NBTConstants.SLURRY_TANKS));
        case ITEM:
            return new ItemRecipeData(((ISustainedInventory) item).getInventory(stack));
        case SECURITY:
            ISecurityItem securityItem = (ISecurityItem) item;
            UUID ownerUUID = securityItem.getOwnerUUID(stack);
            return ownerUUID == null ? null : new SecurityRecipeData(ownerUUID, securityItem.getSecurity(stack));
        case UPGRADE:
            CompoundNBT componentUpgrade = ItemDataUtils.getCompound(stack, NBTConstants.COMPONENT_UPGRADE);
            return componentUpgrade.isEmpty() ? null : new UpgradesRecipeData(Upgrade.buildMap(componentUpgrade));
        case QIO_DRIVE:
            DriveMetadata data = DriveMetadata.load(stack);
            if (data.getCount() > 0 && ((IQIODriveItem) item).hasStoredItemMap(stack)) {
                // If we don't have any stored items don't actually grab any recipe data
                return new QIORecipeData(data, ItemDataUtils.getList(stack, NBTConstants.QIO_ITEM_MAP));
            }
            break;
    }
    return null;
}
Also used : DriveMetadata(mekanism.common.content.qio.IQIODriveItem.DriveMetadata) CompoundNBT(net.minecraft.nbt.CompoundNBT) GasRecipeData(mekanism.common.recipe.upgrade.chemical.GasRecipeData) SlurryRecipeData(mekanism.common.recipe.upgrade.chemical.SlurryRecipeData) IQIODriveItem(mekanism.common.content.qio.IQIODriveItem) Item(net.minecraft.item.Item) ISecurityItem(mekanism.common.lib.security.ISecurityItem) BlockItem(net.minecraft.item.BlockItem) ISecurityItem(mekanism.common.lib.security.ISecurityItem) PigmentRecipeData(mekanism.common.recipe.upgrade.chemical.PigmentRecipeData) UUID(java.util.UUID) InfusionRecipeData(mekanism.common.recipe.upgrade.chemical.InfusionRecipeData) Nullable(javax.annotation.Nullable)

Example 4 with ISecurityItem

use of mekanism.common.lib.security.ISecurityItem in project Mekanism by mekanism.

the class SecurityRecipeData method applyToStack.

@Override
public boolean applyToStack(ItemStack stack) {
    ISecurityItem securityItem = (ISecurityItem) stack.getItem();
    securityItem.setOwnerUUID(stack, owner);
    securityItem.setSecurity(stack, mode);
    return true;
}
Also used : ISecurityItem(mekanism.common.lib.security.ISecurityItem)

Example 5 with ISecurityItem

use of mekanism.common.lib.security.ISecurityItem in project Mekanism by mekanism.

the class SecurityUtils method canAccess.

public static boolean canAccess(PlayerEntity player, Object object) {
    ISecurityObject security;
    if (object instanceof ItemStack) {
        ItemStack stack = (ItemStack) object;
        if (!(stack.getItem() instanceof ISecurityItem) && stack.getItem() instanceof IOwnerItem) {
            // If it is an owner item but not a security item make sure the owner matches
            if (!MekanismConfig.general.allowProtection.get() || isOp(player)) {
                // If protection is disabled or the player is an op and bypass restrictions are enabled, access is always granted
                return true;
            }
            UUID owner = ((IOwnerItem) stack.getItem()).getOwnerUUID(stack);
            return owner == null || owner.equals(player.getUUID());
        }
        security = wrapSecurityItem(stack);
    } else if (object instanceof ISecurityObject) {
        security = (ISecurityObject) object;
    } else {
        // The object doesn't have security so there are no security restrictions
        return true;
    }
    return !security.hasSecurity() || canAccess(security.getSecurityMode(), player, security.getOwnerUUID());
}
Also used : ISecurityObject(mekanism.common.lib.security.ISecurityObject) ISecurityItem(mekanism.common.lib.security.ISecurityItem) IOwnerItem(mekanism.common.lib.security.IOwnerItem) ItemStack(net.minecraft.item.ItemStack) UUID(java.util.UUID)

Aggregations

ISecurityItem (mekanism.common.lib.security.ISecurityItem)8 UUID (java.util.UUID)5 Item (net.minecraft.item.Item)4 IOwnerItem (mekanism.common.lib.security.IOwnerItem)3 TileEntityMekanism (mekanism.common.tile.base.TileEntityMekanism)3 ISustainedInventory (mekanism.common.tile.interfaces.ISustainedInventory)3 Nonnull (javax.annotation.Nonnull)2 IQIODriveItem (mekanism.common.content.qio.IQIODriveItem)2 SubstanceType (mekanism.common.tile.base.SubstanceType)2 ISideConfiguration (mekanism.common.tile.interfaces.ISideConfiguration)2 ISustainedData (mekanism.common.tile.interfaces.ISustainedData)2 BlockItem (net.minecraft.item.BlockItem)2 ItemStack (net.minecraft.item.ItemStack)2 Nullable (javax.annotation.Nullable)1 IHasTileEntity (mekanism.common.block.interfaces.IHasTileEntity)1 DriveMetadata (mekanism.common.content.qio.IQIODriveItem.DriveMetadata)1 ISecurityObject (mekanism.common.lib.security.ISecurityObject)1 PacketSecurityUpdate (mekanism.common.network.to_client.PacketSecurityUpdate)1 GasRecipeData (mekanism.common.recipe.upgrade.chemical.GasRecipeData)1 InfusionRecipeData (mekanism.common.recipe.upgrade.chemical.InfusionRecipeData)1