use of mekanism.api.IConfigCardAccess in project Mekanism by mekanism.
the class ItemConfigurationCard method useOn.
@Nonnull
@Override
public ActionResultType useOn(ItemUseContext context) {
PlayerEntity player = context.getPlayer();
if (player == null) {
return ActionResultType.PASS;
}
World world = context.getLevel();
BlockPos pos = context.getClickedPos();
Direction side = context.getClickedFace();
TileEntity tile = WorldUtils.getTileEntity(world, pos);
Optional<IConfigCardAccess> configCardSupport = CapabilityUtils.getCapability(tile, Capabilities.CONFIG_CARD_CAPABILITY, side).resolve();
if (configCardSupport.isPresent()) {
if (SecurityUtils.canAccess(player, tile)) {
ItemStack stack = context.getItemInHand();
if (player.isShiftKeyDown()) {
if (!world.isClientSide) {
IConfigCardAccess configCardAccess = configCardSupport.get();
String translationKey = configCardAccess.getConfigCardName();
CompoundNBT data = configCardAccess.getConfigurationData(player);
data.putString(NBTConstants.DATA_NAME, translationKey);
data.putString(NBTConstants.DATA_TYPE, configCardAccess.getConfigurationDataType().getRegistryName().toString());
ItemDataUtils.setCompound(stack, NBTConstants.DATA, data);
player.sendMessage(MekanismUtils.logFormat(MekanismLang.CONFIG_CARD_GOT.translate(EnumColor.INDIGO, TextComponentUtil.translate(translationKey))), Util.NIL_UUID);
}
} else {
CompoundNBT data = getData(stack);
TileEntityType<?> storedType = getStoredTileType(data);
if (storedType == null) {
return ActionResultType.PASS;
}
if (!world.isClientSide) {
IConfigCardAccess configCardAccess = configCardSupport.get();
if (configCardAccess.isConfigurationDataCompatible(storedType)) {
configCardAccess.setConfigurationData(player, data);
configCardAccess.configurationDataSet();
player.sendMessage(MekanismUtils.logFormat(EnumColor.DARK_GREEN, MekanismLang.CONFIG_CARD_SET.translate(EnumColor.INDIGO, getConfigCardName(data))), Util.NIL_UUID);
} else {
player.sendMessage(MekanismUtils.logFormat(EnumColor.RED, MekanismLang.CONFIG_CARD_UNEQUAL), Util.NIL_UUID);
}
}
}
return ActionResultType.SUCCESS;
} else {
SecurityUtils.displayNoAccess(player);
}
}
return ActionResultType.PASS;
}
Aggregations