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;
}
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;
}
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;
}
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;
}
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());
}
Aggregations