Jump to content

[1.7.2]EnumChatFormatting doesnt work on all the string


BlackXnt

Recommended Posts

an example from some of my code (the string is already split into params[]):

 

          String msg = "";

	for(int i = 0; i < params.length; i++){
		msg += params[i];
		msg += " ";
		msg += EnumChatFormatting.GRAY.toString() + EnumChatFormatting.ITALIC.toString();
	}

I'll need help, and I'll give help. Just ask, you know I will!

Link to comment
Share on other sites

String[] stringList = message.split(" +"); // string broken into words
String formatting = EnumChatFormatting.GRAY.toString() +  EnumChatFormatting.ITALIC.toString(); // formatting codes

resultString = formatting + String.join(" " + formatting, stringList); // result with formatting between words

 

Link to comment
Share on other sites

String[] stringList = String.split(" +",message); // string broken into words
String formatting = EnumChatFormatting.GRAY.toString() +  EnumChatFormatting.ITALIC.toString(); // formatting codes

resultString = formatting + String.join(" " + formatting, stringList); // result with formatting between words

"The method join(String, String[]) is undefined for the type String"

1410831192]

an example from some of my code (the string is already split into params[]):

 

          String msg = "";

	for(int i = 0; i < params.length; i++){
		msg += params[i];
		msg += " ";
		msg += EnumChatFormatting.GRAY.toString() + EnumChatFormatting.ITALIC.toString();
	}

It doesnt work

It does split the word but the color doesn't work

I did this:

String[] stringList = msg.split(" ");
    	    		for(int i = 0; i < stringList.length; i++){
    	    			finalstring += stringList[i];
    	    			finalstring += " ";
    	    			finalstring += EnumChatFormatting.AQUA.toString() + EnumChatFormatting.ITALIC.toString();

Link to comment
Share on other sites

For complete clarity, can you please post the entire method containing the lines in question? Then we can make specific edits instead of theoretical ones and get you squared away.

    			else{
    	    		String[] stringList = msg.split(" ");
    	    		for(int i = 0; i < stringList.length; i++){
    	    			finalstring += stringList[i];
    	    			finalstring += " ";
    	    			finalstring += EnumChatFormatting.AQUA;
    	    		}
    				e.setCanceled(true);
    				mc.thePlayer.addChatMessage(new ChatComponentText(finalstring));
}

All the class:

package com.TealNerd.SnitchCensor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.minecraft.client.Minecraft;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraftforge.client.event.ClientChatReceivedEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;

public class ChatFilter {
    
    Minecraft mc = Minecraft.getMinecraft();

    public String msg;
    public String finalstring ="";
    private Pattern snitch = Pattern.compile("\\[[-0-9]* [0-9]* [-0-9]*\\]");
    private Pattern user = Pattern.compile("([a-zA-Z0-9_]+?) entered snitch at");
    public String username;
    protected boolean isEnemy = false;
    public Matcher snitchMatcher;
    public Matcher usernameMatcher;
    private String modDir = mc.mcDataDir + "/mods/SnitchCensor/";
    public File bounties = new File(modDir, "perps.txt");
    private Pattern name = Pattern.compile("\\\"name\\\":\\\"([A-Za-z0-9_]+?)\\\"");
    boolean hasBounty = false;

    {

    }
      
    @SubscribeEvent
    public void onChat(ClientChatReceivedEvent e) throws IOException{
    	msg = e.message.getUnformattedText();
    	Matcher snitchMatcher = snitch.matcher(msg);
    	Matcher usernameMatcher = user.matcher(msg);
    	
    	if(SnitchCensor.custom){
    		if(snitchMatcher.find()){
        	if(usernameMatcher.find()){
        		username = usernameMatcher.group(1);
        	}
        	String dir = mc.mcDataDir + "/mods/RadarBro/";
        	File enemies = new File(dir, "EnemyList.txt");
        	Scanner input = new Scanner(enemies);
        	
        	while(input.hasNext()){
        		String nextLine = input.nextLine();
        		if(nextLine.equals(username)){
        			isEnemy = true;
        		}else{
        			isEnemy = false;
        		}
        	}
        	
        	input.close();
        	
        	String contents;   		
    		Scanner in = new Scanner(bounties);
    		contents = readFile(bounties);
    		Matcher nameMatcher = name.matcher(contents);
    		while(nameMatcher.find())
    		{	
    		        if(nameMatcher.group(1).equals(username))
    		        {
    		                hasBounty = true;
    		                break;
    		        }
    		        else
    		        {
    		                hasBounty = false;
    		        }
    		}
    		in.close();
        	
    		if(isEnemy){
    			if(SnitchCensor.isEnabled){
    				e.setCanceled(true);
    				finalstring = snitchMatcher.replaceAll("[**** ** ****]");
    				mc.thePlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + finalstring));
    			}else{
    				e.setCanceled(true);
    				finalstring = msg;
    				mc.thePlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.GOLD + finalstring));
    			}
    		}else if(hasBounty){
    			if(SnitchCensor.isEnabled){
    				e.setCanceled(true);
    				finalstring = snitchMatcher.replaceAll("[**** ** ****]");
    				mc.thePlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + finalstring));
    			}else{
    				e.setCanceled(true);
    				finalstring = msg;
    				mc.thePlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + finalstring));
    			}
    		}else{
    			if(SnitchCensor.isEnabled){
    	    		String[] stringList = msg.split(" ");
    	    		for(int i = 0; i < stringList.length; i++){
    	    			finalstring += stringList[i];
    	    			finalstring += " ";
    	    			finalstring += EnumChatFormatting.AQUA;
    	    		}
    				e.setCanceled(true);
    				finalstring = snitchMatcher.replaceAll("[**** ** ****]");
    				mc.thePlayer.addChatMessage(new ChatComponentText(finalstring));
    				mc.thePlayer.addChatMessage(new ChatComponentText(stringList[stringList.length-1]));
    			}else{
    	    		String[] stringList = msg.split(" ");

    				e.setCanceled(true);
    				mc.thePlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.AQUA + finalstring));
    			}
    		}
        	
    		}
    	}else if(SnitchCensor.isEnabled){
    		e.setCanceled(true);
    		finalstring = snitchMatcher.replaceAll("[**** ** ****]");
		mc.thePlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.AQUA + finalstring));
    	}else{
    		e.setCanceled(true);
		finalstring = msg;
		mc.thePlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.AQUA + finalstring));
    	}
        }
    
    	
    	
         

    
    String readFile(File file) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file));
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();
     
            while (line != null) {
                sb.append(line);
                sb.append("\n");
                line = br.readLine();
            }
            return sb.toString();
        } finally {
            br.close();
        }
    }
    }

Link to comment
Share on other sites

First off put the chat formatting line at the beginning of your loop, before you add the split string. Also I see you made an edit to your post... maybe comment out the snitch replace method thing to see if it is what causes you problems. I personally am having browser issues trying to view your entire class on my mobile device or I could get more in depth :/

I'll need help, and I'll give help. Just ask, you know I will!

Link to comment
Share on other sites

One more thing... add .toString () at the end of the chat formatting.

Thx its working now!

btw I have another question:

I want to make a mod for multiplayer that msg the player every time a player join or leave the server.

How do I identify when a new player join or leave the server?

Link to comment
Share on other sites

Well it would be wise to post the question in a new thread, but you will have to hook an EntityPlayer event... is this something you are familiar with? You can most likely find a 5 minute tutorial video on the subject and theforge github references every event you can subscribe to.

I'll need help, and I'll give help. Just ask, you know I will!

Link to comment
Share on other sites

Well it would be wise to post the question in a new thread, but you will have to hook an EntityPlayer event... is this something you are familiar with? You can most likely find a 5 minute tutorial video on the subject and theforge github references every event you can subscribe to.

I am new with Forge.

I am just trying to make a simple mod for a server I am playing on.

I know only basic java so I am having hard time doing it.

If u know any other mod that does what I want it can really help me

Link to comment
Share on other sites

If you are new to forge but interested in learning more, then subscribing to events and maybe writing a custom save file should be within your scope/a good learning experience and easy enough to find basic outlines and tutorials about them. My first thoughts on your approach would be as follows:

 

I'll need help, and I'll give help. Just ask, you know I will!

Link to comment
Share on other sites

Don't store the names, but the UUIDs, because there's gonna be name-change support and that will mess up your names.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Don't store the names, but the UUIDs, because there's gonna be name-change support and that will mess up your names.

 

This is pretty wise, especially if you plan on your server almost never restarting. Unless you are saving this list to file, it will clear out every time you restart the server.

 

Edit: Although if you remove a player from the list every time you find a null, it will be self-cleaning anyway, and the "new name" (when that feature comes available) will be used the next time that individual player logs in. So if all you are using this list for is notifying players of login, you will only run into issues for one specific player, during the specific session in which they changed their name. If you want a list for more sophisticated and long-term uses, then you should most definitely take larsgerrits' advice. If it were me I would start simple and work up, since it's pretty much all stilla challenge for you.

I'll need help, and I'll give help. Just ask, you know I will!

Link to comment
Share on other sites

Don't store the names, but the UUIDs, because there's gonna be name-change support and that will mess up your names.

 

This is pretty wise, especially if you plan on your server almost never restarting. Unless you are saving this list to file, it will clear out every time you restart the server.

 

Edit: Although if you remove a player from the list every time you find a null, it will be self-cleaning anyway, and the "new name" (when that feature comes available) will be used the next time that individual player logs in. So if all you are using this list for is notifying players of login, you will only run into issues for one specific player, during the specific session in which they changed their name. If you want a list for more sophisticated and long-term uses, then you should most definitely take larsgerrits' advice. If it were me I would start simple and work up, since it's pretty much all stilla challenge for you.

I am not making this mod for the server- this is a bukkit server and I am not the owner

Is it going to work if the mod isnt install on the server?

Link to comment
Share on other sites

Don't store the names, but the UUIDs, because there's gonna be name-change support and that will mess up your names.

 

This is pretty wise, especially if you plan on your server almost never restarting. Unless you are saving this list to file, it will clear out every time you restart the server.

 

Edit: Although if you remove a player from the list every time you find a null, it will be self-cleaning anyway, and the "new name" (when that feature comes available) will be used the next time that individual player logs in. So if all you are using this list for is notifying players of login, you will only run into issues for one specific player, during the specific session in which they changed their name. If you want a list for more sophisticated and long-term uses, then you should most definitely take larsgerrits' advice. If it were me I would start simple and work up, since it's pretty much all stilla challenge for you.

I am not making this mod for the server- this is a bukkit server and I am not the owner

Is it going to work if the mod isnt install on the server?

 

That's a big no. It will work in single player. Or if you are the server, any client who also has the mod will be able to log in and it will work. If it is installed on the server a client can not log in unless they have it installed also. Simple as that.

 

EnumChatFormatting.RED + String1 + EnumChatFormatting.RESET + String2

 

? Issue already solved, and that's not it...

I'll need help, and I'll give help. Just ask, you know I will!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.