# Solved Replacing certain sections of a String

Discussion in 'Plugin Development' started by TfT_02, Dec 3, 2012.

Not open for further replies.
1. Offline

### Forge_User_29648960

So I'm looking for a way to randomly chance letters in a chat message with ChatColor.MAGIC, so I thought of this to do this.

Edit: I've figured it out. Wasn't that hard with the StringBuilder. Here's how I did it:
Code:java
`public String Scrambled(String msg) {		StringBuilder sb = new StringBuilder(msg);		int halfLength;		int start1;		int end1;		int start2;		int end2;		int scrambleLength1;		int scrambleLength2; 		int length = msg.length();		if (length == 0){			//This is not possible, but hey! You never know, right? :D		}else if (length == 1) {			int scramble = random.nextInt(100);			if (scramble > 50) {				sb.insert(0, ChatColor.MAGIC);			}		}else if (length <= 3) {			start1 = random.nextInt(length);			end1 = random.nextInt(length + 1);			sb.insert(start1, ChatColor.MAGIC);			sb.insert(end1 + 2, ChatColor.RESET);		}else if (length >= 4) {			halfLength = (int) length / 2;			scrambleLength1 = random.nextInt(5) + 3;			scrambleLength2 = random.nextInt(5) + 3;			start1 = random.nextInt(halfLength);			end1 = scrambleLength1 + start1;			start2 = random.nextInt(halfLength) + end1;			end2 = scrambleLength2 + start2;			if (end2 > length) {				end2 = length;			}			if (start2 > length) {				start2 = length;			}			sb.insert(start1, ChatColor.MAGIC);			sb.insert(end1 + 2, ChatColor.RESET);			sb.insert(start2 + 4, ChatColor.MAGIC);			sb.insert(end2 + 6, ChatColor.RESET);			if (plugin.debug_mode) {				System.out.println("DEBUG INFORMATION:");				System.out.println(start1 + "-" + end1 + " | " + scrambleLength1);				System.out.println(start2 + "-" + end2 + " | " + scrambleLength2);			}		}		String scrambled = sb.toString();		return scrambled;	} `

2. Offline

### leiger

It really depends on what you want to do. If you just wanted random letters in the chat message to be changed, not specific letters, you could get a few random numbers between 0 and the chat message's length, then insert your colour code there.

What you're doing at the moment will lead to approximately 50% of the message having the magic effect. Probably more, if vowels are returned as true for the effect. That would make messages a bit unreadable.

What is the end goal for this? Will it apply to every message that is posted in chat, or only some of them?

#2
3. Offline

### Forge_User_29648960

I'm trying to make the chat unreadable / barely readable, depending on random odds of how many characters get replaced with the magic effect. So I thought that the easiest way would be to have 50% of each letter to get replaced, but the way that I've got it now is not right. Just replacing random characters might work even better.

How would I replace random characters of a string? Is there a method that will allow me to replace specific characters? E.g. characters 1-3, 5-6, 7 and 9 of a string?

4. Offline

### leiger

You could very easily write your own method to do that (e.g. concatenate two substrings and your colour code together).

The algorithm used to determine which random characters will be replaced depends entirely on what your desired effect is. I'd suggest not tying it to specific letters of the alphabet though - just go through each character in the sentence and randomly decide whether it's to be modified or not.

#4
5. Offline

### Forge_User_29648960

Yeah that seems like a good idea.

6. Offline

### Forge_User_02764350

can you use this, to decrypt the nameplate by keeping the skin?

thx

julia

7. Offline

### Forge_User_29648960

Heh? That doesn't make any sense.

I'm using this code to "scramble" chat message; e.g. Insert ChatColor.MAGIC in parts of messages, making them unreadable.

8. Offline

### Forge_User_02764350

o.k. thx TFT_02, i don´t understand your code. But i thought from readeing the thread, it would be possible to make the tagname unreadable by using your code or part of it.

9. Offline

### Forge_User_29648960

Well, just change the color of the nametag to MAGIC

10. Offline

### Forge_User_02764350

yes i don´t programm anything - i can´t. i am just searching for bukkit-plugin, that can change my tagname to magic-format(&kNAME) but leave chatname, welcome-message-name/deathname & playerlistname as the normal Playername. And i found your Thread.

thx

julia