Search in sources :

Example 1 with IConfigCardAccess

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;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) CompoundNBT(net.minecraft.nbt.CompoundNBT) IConfigCardAccess(mekanism.api.IConfigCardAccess) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) Direction(net.minecraft.util.Direction) PlayerEntity(net.minecraft.entity.player.PlayerEntity) Nonnull(javax.annotation.Nonnull)

Aggregations

Nonnull (javax.annotation.Nonnull)1 IConfigCardAccess (mekanism.api.IConfigCardAccess)1 PlayerEntity (net.minecraft.entity.player.PlayerEntity)1 ItemStack (net.minecraft.item.ItemStack)1 CompoundNBT (net.minecraft.nbt.CompoundNBT)1 TileEntity (net.minecraft.tileentity.TileEntity)1 Direction (net.minecraft.util.Direction)1 BlockPos (net.minecraft.util.math.BlockPos)1 World (net.minecraft.world.World)1