Search in sources :

Example 1 with IWorldNameable

use of net.minecraft.world.IWorldNameable in project Railcraft by Railcraft.

the class ItemNotepad method onItemUse.

@Override
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    if (Game.isClient(world))
        return EnumActionResult.SUCCESS;
    TileEntity tileEntity = world.getTileEntity(pos);
    if (tileEntity != null) {
        if (// COPY
        player.isSneaking()) {
            EnumMap<Contents, NBTTagCompound> contents = new EnumMap<Contents, NBTTagCompound>(Contents.class);
            for (Contents contentType : Contents.VALUES) {
                NBTTagCompound data = contentType.copy(tileEntity);
                if (data != null)
                    contents.put(contentType, data);
            }
            if (contents.isEmpty()) {
                //TODO: Fix in 1.8 to use getDisplayName
                ChatPlugin.sendLocalizedChatFromServer(player, "item.railcraft.tool.notepad.action.copy.fail");
            } else {
                setContents(stack, contents);
                if (tileEntity instanceof IWorldNameable)
                    ChatPlugin.sendLocalizedChatFromServer(player, "item.railcraft.tool.notepad.action.copy", tileEntity.getDisplayName());
                stack.damageItem(1, player);
            }
        } else // PASTE
        {
            EnumMap<Contents, NBTTagCompound> contents = getContents(stack);
            if (contents.isEmpty()) {
                ChatPlugin.sendLocalizedChatFromServer(player, "item.railcraft.tool.notepad.action.empty");
            } else {
                PasteMode pasteMode = getPasteMode(stack);
                boolean pasted = false;
                for (Map.Entry<Contents, NBTTagCompound> entry : getContents(stack).entrySet()) {
                    if (pasteMode.allows(entry.getKey()))
                        pasted |= entry.getKey().paste(tileEntity, entry.getValue());
                }
                if (pasted) {
                    if (tileEntity instanceof IWorldNameable)
                        ChatPlugin.sendLocalizedChatFromServer(player, "item.railcraft.tool.notepad.action.paste", tileEntity.getDisplayName());
                } else
                    //TODO: Fix in 1.8 to use getDisplayName
                    ChatPlugin.sendLocalizedChatFromServer(player, "item.railcraft.tool.notepad.action.paste.fail");
            }
        }
    }
    return EnumActionResult.SUCCESS;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IWorldNameable(net.minecraft.world.IWorldNameable)

Example 2 with IWorldNameable

use of net.minecraft.world.IWorldNameable in project SpongeCommon by SpongePowered.

the class DisplayNameDataProcessor method from.

@Override
public Optional<DisplayNameData> from(DataHolder holder) {
    if (holder instanceof Entity) {
        @Nullable Text displayName = ((IMixinEntity) holder).getDisplayNameText();
        if (displayName != null) {
            return Optional.of(new SpongeDisplayNameData(displayName));
        }
        return Optional.empty();
    } else if (holder instanceof ItemStack) {
        ItemStack stack = (ItemStack) holder;
        if (!stack.hasDisplayName()) {
            return Optional.empty();
        }
        if (stack.getItem() == Items.WRITTEN_BOOK) {
            final NBTTagCompound compound = stack.getTagCompound();
            if (compound == null) {
                // The book wasn't initialized.
                return Optional.empty();
            }
            return Optional.of(new SpongeDisplayNameData(SpongeTexts.fromLegacy(compound.getString(NbtDataUtil.ITEM_BOOK_TITLE))));
        }
        final NBTTagCompound compound = ((ItemStack) holder).getSubCompound(NbtDataUtil.ITEM_DISPLAY);
        if (compound != null && compound.hasKey(NbtDataUtil.ITEM_DISPLAY_NAME, NbtDataUtil.TAG_STRING)) {
            return Optional.of(new SpongeDisplayNameData(SpongeTexts.fromLegacy(compound.getString(NbtDataUtil.ITEM_DISPLAY_NAME))));
        }
        return Optional.empty();
    } else if (holder instanceof IWorldNameable) {
        if (((IWorldNameable) holder).hasCustomName()) {
            final String customName = ((IWorldNameable) holder).getName();
            final DisplayNameData data = new SpongeDisplayNameData(SpongeTexts.fromLegacy(customName));
            return Optional.of(data);
        }
        return Optional.empty();
    }
    return Optional.empty();
}
Also used : ImmutableSpongeDisplayNameData(org.spongepowered.common.data.manipulator.immutable.ImmutableSpongeDisplayNameData) SpongeDisplayNameData(org.spongepowered.common.data.manipulator.mutable.SpongeDisplayNameData) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) Entity(net.minecraft.entity.Entity) ImmutableSpongeDisplayNameData(org.spongepowered.common.data.manipulator.immutable.ImmutableSpongeDisplayNameData) SpongeDisplayNameData(org.spongepowered.common.data.manipulator.mutable.SpongeDisplayNameData) DisplayNameData(org.spongepowered.api.data.manipulator.mutable.DisplayNameData) ImmutableDisplayNameData(org.spongepowered.api.data.manipulator.immutable.ImmutableDisplayNameData) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Text(org.spongepowered.api.text.Text) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) ItemStack(net.minecraft.item.ItemStack) Nullable(javax.annotation.Nullable) IWorldNameable(net.minecraft.world.IWorldNameable)

Example 3 with IWorldNameable

use of net.minecraft.world.IWorldNameable in project takumicraft by TNTModders.

the class BlockTakumiMonsterBomb method harvestBlock.

@Override
public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te, ItemStack stack) {
    if (te instanceof IWorldNameable && ((IWorldNameable) te).hasCustomName()) {
        player.addStat(StatList.getBlockStats(this));
        player.addExhaustion(0.005F);
        if (worldIn.isRemote) {
            return;
        }
        int i = EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, stack);
        Item item = this.getItemDropped(state, worldIn.rand, i);
        if (item == Items.AIR) {
            return;
        }
        ItemStack itemstack = new ItemStack(item, this.quantityDropped(worldIn.rand));
        itemstack.setStackDisplayName(((IWorldNameable) te).getName());
        spawnAsEntity(worldIn, pos, itemstack);
    } else {
        super.harvestBlock(worldIn, player, pos, state, null, stack);
    }
}
Also used : Item(net.minecraft.item.Item) ItemStack(net.minecraft.item.ItemStack) IWorldNameable(net.minecraft.world.IWorldNameable)

Example 4 with IWorldNameable

use of net.minecraft.world.IWorldNameable in project Railcraft by Railcraft.

the class ItemNotepad method onItemUse.

@Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    if (Game.isClient(world))
        return EnumActionResult.SUCCESS;
    ItemStack stack = player.getHeldItem(hand);
    TileEntity tileEntity = world.getTileEntity(pos);
    if (tileEntity != null) {
        if (// COPY
        player.isSneaking()) {
            EnumMap<Contents, NBTTagCompound> contents = new EnumMap<>(Contents.class);
            for (Contents contentType : Contents.VALUES) {
                NBTTagCompound data = contentType.copy(tileEntity);
                if (data != null)
                    contents.put(contentType, data);
            }
            if (contents.isEmpty()) {
                // TODO: Fix in 1.8 to use getDisplayName
                ChatPlugin.sendLocalizedChatFromServer(player, "item.railcraft.tool.notepad.action.copy.fail");
            } else {
                setContents(stack, contents);
                if (tileEntity instanceof IWorldNameable)
                    ChatPlugin.sendLocalizedChatFromServer(player, "item.railcraft.tool.notepad.action.copy", tileEntity.getDisplayName());
                stack.damageItem(1, player);
            }
        } else // PASTE
        {
            EnumMap<Contents, NBTTagCompound> contents = getContents(stack);
            if (contents.isEmpty()) {
                ChatPlugin.sendLocalizedChatFromServer(player, "item.railcraft.tool.notepad.action.empty");
            } else {
                PasteMode pasteMode = getPasteMode(stack);
                boolean pasted = getContents(stack).entrySet().stream().filter(entry -> pasteMode.allows(entry.getKey())).map(entry -> entry.getKey().paste(tileEntity, entry.getValue())).reduce(false, (a, b) -> a || b);
                if (pasted) {
                    if (tileEntity instanceof IWorldNameable)
                        ChatPlugin.sendLocalizedChatFromServer(player, "item.railcraft.tool.notepad.action.paste", tileEntity.getDisplayName());
                } else
                    ChatPlugin.sendLocalizedChatFromServer(player, "item.railcraft.tool.notepad.action.paste.fail", tileEntity.getDisplayName());
            }
        }
    }
    return EnumActionResult.SUCCESS;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) net.minecraft.util(net.minecraft.util) TileManipulatorCart(mods.railcraft.common.blocks.machine.manipulator.TileManipulatorCart) java.util(java.util) InventoryAdvanced(mods.railcraft.common.util.inventory.InventoryAdvanced) ChatPlugin(mods.railcraft.common.plugins.forge.ChatPlugin) ITooltipFlag(net.minecraft.client.util.ITooltipFlag) StringUtils(org.apache.commons.lang3.StringUtils) ModelResourceLocation(net.minecraft.client.renderer.block.model.ModelResourceLocation) ItemStack(net.minecraft.item.ItemStack) TileFluidManipulator(mods.railcraft.common.blocks.machine.manipulator.TileFluidManipulator) Side(net.minecraftforge.fml.relauncher.Side) EnumTools(mods.railcraft.common.util.misc.EnumTools) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) IBlockAccess(net.minecraft.world.IBlockAccess) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) CraftingPlugin(mods.railcraft.common.plugins.forge.CraftingPlugin) Items(net.minecraft.init.Items) IWorldNameable(net.minecraft.world.IWorldNameable) World(net.minecraft.world.World) TextFormatting(net.minecraft.util.text.TextFormatting) InvToolsAPI(mods.railcraft.api.items.InvToolsAPI) Game(mods.railcraft.common.util.misc.Game) BlockPos(net.minecraft.util.math.BlockPos) Code(mods.railcraft.common.util.misc.Code) Collectors(java.util.stream.Collectors) LocalizationPlugin(mods.railcraft.common.plugins.forge.LocalizationPlugin) Nullable(org.jetbrains.annotations.Nullable) TileItemManipulator(mods.railcraft.common.blocks.machine.manipulator.TileItemManipulator) ModelManager(mods.railcraft.client.render.models.resource.ModelManager) NBTPlugin(mods.railcraft.common.plugins.forge.NBTPlugin) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ActivationBlockingItem(mods.railcraft.api.items.ActivationBlockingItem) TileEntity(net.minecraft.tileentity.TileEntity) RailcraftConstants(mods.railcraft.common.core.RailcraftConstants) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ItemStack(net.minecraft.item.ItemStack) IWorldNameable(net.minecraft.world.IWorldNameable)

Aggregations

IWorldNameable (net.minecraft.world.IWorldNameable)4 ItemStack (net.minecraft.item.ItemStack)3 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)3 TileEntity (net.minecraft.tileentity.TileEntity)2 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 ActivationBlockingItem (mods.railcraft.api.items.ActivationBlockingItem)1 InvToolsAPI (mods.railcraft.api.items.InvToolsAPI)1 ModelManager (mods.railcraft.client.render.models.resource.ModelManager)1 TileFluidManipulator (mods.railcraft.common.blocks.machine.manipulator.TileFluidManipulator)1 TileItemManipulator (mods.railcraft.common.blocks.machine.manipulator.TileItemManipulator)1 TileManipulatorCart (mods.railcraft.common.blocks.machine.manipulator.TileManipulatorCart)1 RailcraftConstants (mods.railcraft.common.core.RailcraftConstants)1 ChatPlugin (mods.railcraft.common.plugins.forge.ChatPlugin)1 CraftingPlugin (mods.railcraft.common.plugins.forge.CraftingPlugin)1 LocalizationPlugin (mods.railcraft.common.plugins.forge.LocalizationPlugin)1 NBTPlugin (mods.railcraft.common.plugins.forge.NBTPlugin)1 InventoryAdvanced (mods.railcraft.common.util.inventory.InventoryAdvanced)1 Code (mods.railcraft.common.util.misc.Code)1