Search in sources :

Example 26 with ITextComponent

use of net.minecraft.util.text.ITextComponent in project Cavern2 by kegare.

the class RegenerationMessage method process.

@Override
public IMessage process() {
    MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
    File rootDir = DimensionManager.getCurrentSaveRootDirectory();
    if (rootDir == null || !rootDir.exists()) {
        return new RegenerationGuiMessage(EnumType.FAILED);
    }
    server.getPlayerList().saveAllPlayerData();
    for (DimensionType type : dimensions) {
        WorldServer world = DimensionManager.getWorld(type.getId());
        if (world != null) {
            for (EntityPlayer player : world.playerEntities) {
                if (player != null && player instanceof EntityPlayerMP) {
                    EntityPlayerMP playerMP = (EntityPlayerMP) player;
                    if (playerMP.connection != null) {
                        playerMP.connection.disconnect(new TextComponentTranslation("cavern.message.disconnect.unload"));
                    }
                }
            }
            try {
                world.saveAllChunks(true, null);
            } catch (MinecraftException e) {
                return new RegenerationGuiMessage(EnumType.FAILED);
            }
            world.flush();
            MinecraftForge.EVENT_BUS.post(new WorldEvent.Unload(world));
            DimensionManager.setWorld(world.provider.getDimension(), null, server);
            world.getWorldInfo().setDimensionData(world.provider.getDimension(), null);
        }
        for (EntityPlayerMP player : server.getPlayerList().getPlayers()) {
            if (player != null) {
                PortalCache.get(player).clearLastPos(null, type);
            }
        }
        File dimDir = new File(rootDir, "DIM" + type.getId());
        if (!dimDir.exists()) {
            return new RegenerationGuiMessage(EnumType.FAILED);
        }
        ITextComponent name = new TextComponentString(CaveDimensions.getLocalizedName(type));
        name.getStyle().setBold(true);
        sendProgress(EnumType.START);
        ITextComponent message = new TextComponentTranslation("cavern.regeneration.regenerating", name);
        message.getStyle().setColor(TextFormatting.GRAY);
        server.getPlayerList().sendMessage(message);
        if (backup) {
            message = new TextComponentTranslation("cavern.regeneration.backup", name);
            message.getStyle().setColor(TextFormatting.GRAY);
            server.getPlayerList().sendMessage(message);
            sendProgress(EnumType.BACKUP);
            Calendar calendar = Calendar.getInstance();
            String year = Integer.toString(calendar.get(Calendar.YEAR));
            String month = String.format("%02d", calendar.get(Calendar.MONTH) + 1);
            String day = String.format("%02d", calendar.get(Calendar.DATE));
            String hour = String.format("%02d", calendar.get(Calendar.HOUR_OF_DAY));
            String minute = String.format("%02d", calendar.get(Calendar.MINUTE));
            String second = String.format("%02d", calendar.get(Calendar.SECOND));
            File bak = new File(rootDir, type.getName() + "_bak-" + String.join("", year, month, day) + "-" + String.join("", hour, minute, second) + ".zip");
            if (CaveUtils.archiveDirectory(dimDir, bak)) {
                message = new TextComponentTranslation("cavern.regeneration.backup.success", name);
                message.getStyle().setColor(TextFormatting.GRAY);
                server.getPlayerList().sendMessage(message);
            } else {
                message = new TextComponentTranslation("cavern.regeneration.backup.failed", name);
                message.getStyle().setColor(TextFormatting.RED);
                server.getPlayerList().sendMessage(message);
                return new RegenerationGuiMessage(EnumType.FAILED);
            }
        }
        try {
            FileUtils.deleteDirectory(dimDir);
        } catch (IOException e) {
            return new RegenerationGuiMessage(EnumType.FAILED);
        }
        message = new TextComponentTranslation("cavern.regeneration.regenerated", name);
        message.getStyle().setColor(TextFormatting.GRAY);
        server.getPlayerList().sendMessage(message);
        if (type.shouldLoadSpawn()) {
            world = server.getWorld(type.getId());
            try {
                world.saveAllChunks(true, null);
            } catch (MinecraftException e) {
            }
            world.flush();
        }
    }
    return new RegenerationGuiMessage(EnumType.REGENERATED);
}
Also used : DimensionType(net.minecraft.world.DimensionType) TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) MinecraftException(net.minecraft.world.MinecraftException) Calendar(java.util.Calendar) ITextComponent(net.minecraft.util.text.ITextComponent) WorldServer(net.minecraft.world.WorldServer) RegenerationGuiMessage(cavern.network.client.RegenerationGuiMessage) TextComponentString(net.minecraft.util.text.TextComponentString) IOException(java.io.IOException) MinecraftServer(net.minecraft.server.MinecraftServer) TextComponentString(net.minecraft.util.text.TextComponentString) WorldEvent(net.minecraftforge.event.world.WorldEvent) EntityPlayer(net.minecraft.entity.player.EntityPlayer) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) File(java.io.File)

Example 27 with ITextComponent

use of net.minecraft.util.text.ITextComponent in project Cavern2 by kegare.

the class ItemBowCavenic method getBowModeMessage.

public ITextComponent getBowModeMessage(ItemStack stack) {
    BowMode mode = BowMode.byItemStack(stack);
    ITextComponent name = new TextComponentTranslation(mode.getUnlocalizedName(stack));
    ITextComponent title = new TextComponentTranslation(stack.getUnlocalizedName() + ".mode");
    return title.appendText(": ").appendSibling(name);
}
Also used : TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) ITextComponent(net.minecraft.util.text.ITextComponent)

Example 28 with ITextComponent

use of net.minecraft.util.text.ITextComponent in project Binnie by ForestryMC.

the class ItemSoilMeter method onItemUse.

@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    Block block = worldIn.getBlockState(pos).getBlock();
    IGardeningManager gardening = BotanyCore.getGardening();
    if (!gardening.isSoil(block)) {
        pos = pos.down();
        block = worldIn.getBlockState(pos).getBlock();
    }
    if (!gardening.isSoil(block)) {
        pos = pos.down();
        block = worldIn.getBlockState(pos).getBlock();
    }
    if (gardening.isSoil(block) && worldIn.isRemote) {
        IBlockSoil soil = (IBlockSoil) block;
        String info = I18N.localise("botany.soil.type") + ": " + EnumHelper.getLocalisedName(soil.getType(worldIn, pos), true) + ", " + TextFormatting.WHITE + I18N.localise("botany.moisture") + ": " + EnumHelper.getLocalisedName(soil.getMoisture(worldIn, pos), true) + ", " + TextFormatting.WHITE + I18N.localise("botany.ph") + ": " + EnumHelper.getLocalisedName(soil.getPH(worldIn, pos), true);
        ITextComponent chat = new TextComponentString(info);
        player.sendStatusMessage(chat, false);
    }
    return EnumActionResult.SUCCESS;
}
Also used : IBlockSoil(binnie.botany.api.gardening.IBlockSoil) ITextComponent(net.minecraft.util.text.ITextComponent) Block(net.minecraft.block.Block) TextComponentString(net.minecraft.util.text.TextComponentString) IGardeningManager(binnie.botany.api.gardening.IGardeningManager) TextComponentString(net.minecraft.util.text.TextComponentString)

Example 29 with ITextComponent

use of net.minecraft.util.text.ITextComponent in project ChatTweaks by blay09.

the class ChatView method addChatLine.

public ChatMessage addChatLine(ChatMessage chatLine) {
    chatLine = chatLine.copy();
    chatLines.add(chatLine);
    if (chatLines.size() > MAX_MESSAGES) {
        chatLines.remove(0);
    }
    try {
        if (chatLine.getSender() == null) {
            chatLine.setSender(subTextComponent(chatLine.getTextComponent(), lastMatcher.start("s"), lastMatcher.end("s")));
        }
        if (chatLine.getMessage() == null) {
            chatLine.setMessage(subTextComponent(chatLine.getTextComponent(), lastMatcher.start("m"), lastMatcher.end("m")));
        }
    } catch (IllegalArgumentException ignored) {
        if (chatLine.getMessage() == null) {
            chatLine.setMessage(chatLine.getTextComponent());
        }
    }
    ITextComponent source = chatLine.getTextComponent();
    ITextComponent textComponent = chatLine.getTextComponent();
    if (!builtOutputFormat.equals("$0")) {
        textComponent = new TextComponentString("");
        int last = 0;
        Matcher matcher = groupPattern.matcher(builtOutputFormat);
        while (matcher.find()) {
            if (matcher.start() > last) {
                textComponent.appendText(builtOutputFormat.substring(last, matcher.start()));
            }
            ITextComponent groupValue = null;
            String namedGroup = matcher.group(2);
            if (namedGroup != null) {
                if (namedGroup.equals("s") && chatLine.getSender() != null) {
                    groupValue = chatLine.getSender();
                } else if (namedGroup.equals("m") && chatLine.getMessage() != null) {
                    groupValue = chatLine.getMessage();
                } else if (namedGroup.equals("t")) {
                    groupValue = new TextComponentString(ChatTweaksConfig.timestampFormat.format(new Date(chatLine.getTimestamp())));
                    groupValue.getStyle().setColor(TextFormatting.GRAY);
                } else {
                    int groupStart = -1;
                    int groupEnd = -1;
                    try {
                        groupStart = lastMatcher.start(namedGroup);
                        groupEnd = lastMatcher.end(namedGroup);
                    } catch (IllegalArgumentException ignored) {
                    }
                    if (groupStart != -1 && groupEnd != -1) {
                        groupValue = subTextComponent(source, groupStart, groupEnd);
                    } else {
                        groupValue = chatLine.getOutputVar(namedGroup);
                    }
                }
            } else {
                int group = Integer.parseInt(matcher.group(1));
                if (group >= 0 && group <= lastMatcher.groupCount()) {
                    groupValue = subTextComponent(source, lastMatcher.start(group), lastMatcher.end(group));
                }
            }
            if (groupValue == null) {
                groupValue = new TextComponentString("missingno");
            }
            last = matcher.end();
            textComponent.appendSibling(groupValue);
        }
        if (last < builtOutputFormat.length()) {
            textComponent.appendText(builtOutputFormat.substring(last, builtOutputFormat.length()));
        }
    }
    ITextComponent newComponent = null;
    for (ITextComponent component : textComponent) {
        if (component instanceof TextComponentString) {
            String text = ((TextComponentString) component).getText();
            if (text.length() > 1) {
                int index = 0;
                StringBuilder sb = new StringBuilder();
                List<PositionedEmote> emotes = emoteScanner.scanForEmotes(text, null);
                for (PositionedEmote emoteData : emotes) {
                    if (index < emoteData.getStart()) {
                        sb.append(text.substring(index, emoteData.getStart()));
                    }
                    int imageIndex = sb.length() + 1;
                    sb.append("\u00a7*");
                    for (int i = 0; i < emoteData.getEmote().getWidthInSpaces(); i++) {
                        sb.append(' ');
                    }
                    chatLine.addImage(new ChatImageEmote(imageIndex, emoteData.getEmote()));
                    index = emoteData.getEnd() + 1;
                }
                if (index < text.length()) {
                    sb.append(text.substring(index));
                }
                ((TextComponentString) component).text = sb.toString();
            }
            if (text.length() > 0) {
                if (newComponent == null) {
                    newComponent = new TextComponentString("");
                    newComponent.setStyle(textComponent.getStyle().createDeepCopy());
                }
                TextComponentString copyComponent = new TextComponentString(((TextComponentString) component).text);
                copyComponent.setStyle(component.getStyle());
                newComponent.appendSibling(copyComponent);
            }
        }
    }
    if (newComponent == null) {
        newComponent = textComponent;
    }
    chatLine.setTextComponent(newComponent);
    return chatLine;
}
Also used : Matcher(java.util.regex.Matcher) ITextComponent(net.minecraft.util.text.ITextComponent) TextComponentString(net.minecraft.util.text.TextComponentString) PositionedEmote(net.blay09.mods.chattweaks.chat.emotes.PositionedEmote) ChatImageEmote(net.blay09.mods.chattweaks.image.ChatImageEmote) Date(java.util.Date) TextComponentString(net.minecraft.util.text.TextComponentString)

Example 30 with ITextComponent

use of net.minecraft.util.text.ITextComponent in project RFToolsControl by McJty.

the class ProgramCommand method saveProgram.

private void saveProgram(ICommandSender sender, String arg, ItemStack item) {
    ProgramCardInstance program = ProgramCardInstance.parseInstance(item);
    String json = program.writeToJson();
    // File file = new File("." + File.separator + "rftoolscontrol" + File.separator + arg);
    File file = new File(arg);
    if (file.exists()) {
        file.delete();
    }
    try (PrintWriter writer = new PrintWriter(file)) {
        writer.print(json);
    } catch (FileNotFoundException e) {
        ITextComponent component = new TextComponentString(TextFormatting.RED + "Error opening file for writing!");
        if (sender instanceof EntityPlayer) {
            ((EntityPlayer) sender).sendStatusMessage(component, false);
        } else {
            sender.sendMessage(component);
        }
        return;
    }
    ITextComponent component = new TextComponentString("Saved program!");
    if (sender instanceof EntityPlayer) {
        ((EntityPlayer) sender).sendStatusMessage(component, false);
    } else {
        sender.sendMessage(component);
    }
}
Also used : ProgramCardInstance(mcjty.rftoolscontrol.logic.grid.ProgramCardInstance) ITextComponent(net.minecraft.util.text.ITextComponent) EntityPlayer(net.minecraft.entity.player.EntityPlayer) TextComponentString(net.minecraft.util.text.TextComponentString) TextComponentString(net.minecraft.util.text.TextComponentString)

Aggregations

ITextComponent (net.minecraft.util.text.ITextComponent)96 TextComponentString (net.minecraft.util.text.TextComponentString)54 EntityPlayer (net.minecraft.entity.player.EntityPlayer)26 TextComponentTranslation (net.minecraft.util.text.TextComponentTranslation)25 ClickEvent (net.minecraft.util.text.event.ClickEvent)16 ItemStack (net.minecraft.item.ItemStack)11 Style (net.minecraft.util.text.Style)8 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)8 HoverEvent (net.minecraft.util.text.event.HoverEvent)7 World (net.minecraft.world.World)7 ArrayList (java.util.ArrayList)6 BlockPos (net.minecraft.util.math.BlockPos)6 DimensionInformation (mcjty.rftoolsdim.dimensions.DimensionInformation)5 RfToolsDimensionManager (mcjty.rftoolsdim.dimensions.RfToolsDimensionManager)5 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)5 Minecraft (net.minecraft.client.Minecraft)4 Text (org.spongepowered.api.text.Text)4 MagicBook (cavern.magic.MagicBook)3 SpecialMagic (cavern.magic.SpecialMagic)3 Matcher (java.util.regex.Matcher)3