Search in sources :

Example 1 with IFrequencyItem

use of mekanism.common.lib.frequency.IFrequencyItem in project Mekanism by mekanism.

the class MekanismUtils method addFrequencyItemTooltip.

/**
 * @apiNote Only call on the client.
 */
public static void addFrequencyItemTooltip(ItemStack stack, List<ITextComponent> tooltip) {
    FrequencyIdentity frequency = ((IFrequencyItem) stack.getItem()).getFrequencyIdentity(stack);
    if (frequency != null) {
        tooltip.add(MekanismLang.FREQUENCY.translateColored(EnumColor.INDIGO, EnumColor.GRAY, frequency.getKey()));
        CompoundNBT frequencyCompound = ItemDataUtils.getCompound(stack, NBTConstants.FREQUENCY);
        if (frequencyCompound.hasUUID(NBTConstants.OWNER_UUID)) {
            String owner = OwnerDisplay.getOwnerName(MekanismClient.tryGetClientPlayer(), frequencyCompound.getUUID(NBTConstants.OWNER_UUID), null);
            if (owner != null) {
                tooltip.add(MekanismLang.OWNER.translateColored(EnumColor.INDIGO, EnumColor.GRAY, owner));
            }
        }
        tooltip.add(MekanismLang.MODE.translateColored(EnumColor.INDIGO, EnumColor.GRAY, frequency.isPublic() ? MekanismLang.PUBLIC : MekanismLang.PRIVATE));
    }
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT) FrequencyIdentity(mekanism.common.lib.frequency.Frequency.FrequencyIdentity) IFrequencyItem(mekanism.common.lib.frequency.IFrequencyItem)

Example 2 with IFrequencyItem

use of mekanism.common.lib.frequency.IFrequencyItem in project Mekanism by mekanism.

the class PacketGuiSetFrequency method handle.

@Override
public void handle(NetworkEvent.Context context) {
    ServerPlayerEntity player = context.getSender();
    if (player == null) {
        return;
    }
    if (updateType.isTile()) {
        TileEntity tile = WorldUtils.getTileEntity(player.level, tilePosition);
        if (SecurityUtils.canAccess(player, tile) && tile instanceof IFrequencyHandler) {
            if (updateType == FrequencyUpdate.SET_TILE) {
                ((IFrequencyHandler) tile).setFrequency(type, data, player.getUUID());
            } else if (updateType == FrequencyUpdate.REMOVE_TILE) {
                ((IFrequencyHandler) tile).removeFrequency(type, data, player.getUUID());
            }
        }
    } else {
        ItemStack stack = player.getItemInHand(currentHand);
        if (SecurityUtils.canAccess(player, stack) && stack.getItem() instanceof IFrequencyItem) {
            IFrequencyItem item = (IFrequencyItem) stack.getItem();
            FrequencyManager<FREQ> manager = type.getManager(data, player.getUUID());
            if (updateType == FrequencyUpdate.SET_ITEM) {
                // Note: We don't bother validating if the frequency is public or not here, as if it isn't then
                // a new private frequency will just be created for the player who sent a packet they shouldn't
                // have been able to send due to not knowing what private frequencies exist for other players
                item.setFrequency(stack, manager.getOrCreateFrequency(data, player.getUUID()));
            } else if (updateType == FrequencyUpdate.REMOVE_ITEM) {
                if (manager.remove(data.getKey(), player.getUUID())) {
                    FrequencyIdentity current = item.getFrequencyIdentity(stack);
                    if (current != null && current.equals(data)) {
                        // If the frequency we are removing matches the stored frequency set it to nothing
                        item.setFrequency(stack, null);
                    }
                }
            }
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IFrequencyHandler(mekanism.common.lib.frequency.IFrequencyHandler) FrequencyIdentity(mekanism.common.lib.frequency.Frequency.FrequencyIdentity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ItemStack(net.minecraft.item.ItemStack) IFrequencyItem(mekanism.common.lib.frequency.IFrequencyItem)

Aggregations

FrequencyIdentity (mekanism.common.lib.frequency.Frequency.FrequencyIdentity)2 IFrequencyItem (mekanism.common.lib.frequency.IFrequencyItem)2 IFrequencyHandler (mekanism.common.lib.frequency.IFrequencyHandler)1 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)1 ItemStack (net.minecraft.item.ItemStack)1 CompoundNBT (net.minecraft.nbt.CompoundNBT)1 TileEntity (net.minecraft.tileentity.TileEntity)1