下面列出了怎么用net.minecraft.util.ChatAllowedCharacters的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* @author CCBlueX
*/
@Overwrite
protected void keyTyped(char typedChar, int keyCode) throws IOException {
this.signCommand1.textboxKeyTyped(typedChar, keyCode);
this.signCommand2.textboxKeyTyped(typedChar, keyCode);
this.signCommand3.textboxKeyTyped(typedChar, keyCode);
this.signCommand4.textboxKeyTyped(typedChar, keyCode);
if(signCommand1.isFocused() || signCommand2.isFocused() || signCommand3.isFocused() || signCommand4.isFocused())
return;
if(keyCode == 200) {
this.editLine = this.editLine - 1 & 3;
}
if(keyCode == 208 || keyCode == 28 || keyCode == 156) {
this.editLine = this.editLine + 1 & 3;
}
String s = this.tileSign.signText[this.editLine].getUnformattedText();
if(keyCode == 14 && s.length() > 0) {
s = s.substring(0, s.length() - 1);
}
if((ChatAllowedCharacters.isAllowedCharacter(typedChar) || (enabled && typedChar == '§')) && this.fontRendererObj.getStringWidth(s + typedChar) <= 90) {
s = s + typedChar;
}
this.tileSign.signText[this.editLine] = new ChatComponentText(s);
if(keyCode == 1) {
this.actionPerformed(this.doneBtn);
}
}
/**
* @author CCBlueX
*/
@Overwrite
protected void keyTyped(char typedChar, int keyCode) throws IOException {
this.signCommand1.textboxKeyTyped(typedChar, keyCode);
this.signCommand2.textboxKeyTyped(typedChar, keyCode);
this.signCommand3.textboxKeyTyped(typedChar, keyCode);
this.signCommand4.textboxKeyTyped(typedChar, keyCode);
if(signCommand1.isFocused() || signCommand2.isFocused() || signCommand3.isFocused() || signCommand4.isFocused())
return;
if(keyCode == 200) {
this.editLine = this.editLine - 1 & 3;
}
if(keyCode == 208 || keyCode == 28 || keyCode == 156) {
this.editLine = this.editLine + 1 & 3;
}
String s = this.tileSign.signText[this.editLine].getUnformattedText();
if(keyCode == 14 && s.length() > 0) {
s = s.substring(0, s.length() - 1);
}
if((ChatAllowedCharacters.isAllowedCharacter(typedChar) || (enabled && typedChar == '§')) && this.fontRendererObj.getStringWidth(s + typedChar) <= 90) {
s = s + typedChar;
}
this.tileSign.signText[this.editLine] = new ChatComponentText(s);
if(keyCode == 1) {
this.actionPerformed(this.doneBtn);
}
}
public void keyTyped(char typedChar, int keyCode) {
if (focused) {
if (keyCode == 28) {
focused = false;
} else if (keyCode == 14) {
if (!text.isEmpty()) {
text = text.substring(0, text.length() - 1);
}
} else if (ChatAllowedCharacters.isAllowedCharacter(typedChar)) {
text += typedChar;
}
}
}
public static String filerAllowedCharacters(String str, boolean section) {
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray()) {
if (!ChatAllowedCharacters.isAllowedCharacter((char) c) && (!section || c != '\u00a7' && c != '\n')) {
continue;
}
sb.append(c);
}
return sb.toString();
}
@Override
public boolean handleKeyPress(int keyID, char keyChar) {
if (!focused) {
return false;
}
if (keyID == Keyboard.KEY_BACK) {
if (label.length() > 0) {
label = label.substring(0, label.length() - 1);
onTextChange();
backdowntime = System.currentTimeMillis();
}
} else if (keyID == Keyboard.KEY_RETURN) {
focused = false;
} else if (keyChar == 22)//paste
{
String pastestring = GuiScreen.getClipboardString();
if (pastestring == null) {
pastestring = "";
}
label = label + pastestring;
onTextChange();
} else if (ChatAllowedCharacters.isAllowedCharacter(keyChar)) {
label = label + keyChar;
onTextChange();
}
return true;
}
@Override
public boolean handleKeyPress(int keyID, char keyChar)
{
if(!focused)
return false;
if(keyID == Keyboard.KEY_BACK)
{
if(label.length() > 0)
{
label = label.substring(0, label.length() - 1);
onTextChange();
backdowntime = System.currentTimeMillis();
}
}
else if(keyID == Keyboard.KEY_RETURN)
{
focused = false;
}
else if(keyChar == 22)//paste
{
String pastestring = GuiScreen.getClipboardString();
if(pastestring == null)
pastestring = "";
label = label + pastestring;
onTextChange();
}
else if(ChatAllowedCharacters.isAllowedCharacter(keyChar))
{
label = label + keyChar;
onTextChange();
}
return true;
}
public static String filerAllowedCharacters(String str, boolean section) {
StringBuilder sb = new StringBuilder();
char[] arr = str.toCharArray();
int length = arr.length;
for (int i = 0; i < length; ++i) {
char c = arr[i];
if (ChatAllowedCharacters.isAllowedCharacter(c) || (section && (c == NBTStringHelper.SECTION_SIGN || c == '\n')))
sb.append(c);
}
return sb.toString();
}
@Override
protected void keyTyped(char par1, int par2){
if(par2 == 1) {
NetworkHandler.sendToServer(new PacketAphorismTileUpdate(tile));
} else if(par2 == 200) {
cursorY--;
if(cursorY < 0) cursorY = textLines.length - 1;
} else if(par2 == 208 || par2 == 156) {
cursorY++;
if(cursorY >= textLines.length) cursorY = 0;
} else if(par2 == 28) {
cursorY++;
textLines = ArrayUtils.add(textLines, cursorY, "");
} else if(par2 == 14) {
if(textLines[cursorY].length() > 0) {
textLines[cursorY] = textLines[cursorY].substring(0, textLines[cursorY].length() - 1);
} else if(textLines.length > 1) {
textLines = ArrayUtils.remove(textLines, cursorY);
cursorY--;
if(cursorY < 0) cursorY = 0;
}
} else if(ChatAllowedCharacters.isAllowedCharacter(par1)) {
textLines[cursorY] = textLines[cursorY] + par1;
}
tile.setTextLines(textLines);
super.keyTyped(par1, par2);
}
public boolean textboxKeyTyped(char par1, int par2) {
if (this.isEnabled && this.isFocused) {
switch (par1) {
case '\u0001': {
this.setCursorPositionEnd();
this.setSelectionPos(0);
return true;
}
case '\u0003': {
GuiScreen.setClipboardString(this.getSelectedtext());
return true;
}
case '\u0016': {
this.writeText(GuiScreen.getClipboardString());
return true;
}
case '\u0018': {
GuiScreen.setClipboardString(this.getSelectedtext());
this.writeText("");
return true;
}
}
switch (par2) {
case 14: {
if (GuiScreen.isCtrlKeyDown()) {
this.deleteWords(-1);
} else {
this.deleteFromCursor(-1);
}
return true;
}
case 199: {
if (GuiScreen.isShiftKeyDown()) {
this.setSelectionPos(0);
} else {
this.setCursorPositionZero();
}
return true;
}
case 203: {
if (GuiScreen.isShiftKeyDown()) {
if (GuiScreen.isCtrlKeyDown()) {
this.setSelectionPos(this.getNthWordFromPos(-1, this.getSelectionEnd()));
} else {
this.setSelectionPos(this.getSelectionEnd() - 1);
}
} else if (GuiScreen.isCtrlKeyDown()) {
this.setCursorPosition(this.getNthWordFromCursor(-1));
} else {
this.moveCursorBy(-1);
}
return true;
}
case 205: {
if (GuiScreen.isShiftKeyDown()) {
if (GuiScreen.isCtrlKeyDown()) {
this.setSelectionPos(this.getNthWordFromPos(1, this.getSelectionEnd()));
} else {
this.setSelectionPos(this.getSelectionEnd() + 1);
}
} else if (GuiScreen.isCtrlKeyDown()) {
this.setCursorPosition(this.getNthWordFromCursor(1));
} else {
this.moveCursorBy(1);
}
return true;
}
case 207: {
if (GuiScreen.isShiftKeyDown()) {
this.setSelectionPos(this.text.length());
} else {
this.setCursorPositionEnd();
}
return true;
}
case 211: {
if (GuiScreen.isCtrlKeyDown()) {
this.deleteWords(1);
} else {
this.deleteFromCursor(1);
}
return true;
}
}
if (ChatAllowedCharacters.isAllowedCharacter((char) par1)) {
this.writeText(Character.toString(par1));
return true;
}
return false;
}
return false;
}
/**
* Adds the given text after the cursor, or replaces the currently selected text if there is a selection.
*/
public void writeText(String textToWrite)
{
String s = "";
String s1 = ChatAllowedCharacters.filterAllowedCharacters(textToWrite);
int i = this.cursorPosition < this.selectionEnd ? this.cursorPosition : this.selectionEnd;
int j = this.cursorPosition < this.selectionEnd ? this.selectionEnd : this.cursorPosition;
int k = this.maxStringLength - this.text.length() - (i - j);
if (!this.text.isEmpty())
{
s = s + this.text.substring(0, i);
}
int l;
if (k < s1.length())
{
s = s + s1.substring(0, k);
l = k;
}
else
{
s = s + s1;
l = s1.length();
}
if (!this.text.isEmpty() && j < this.text.length())
{
s = s + this.text.substring(j);
}
if (this.validator.apply(s))
{
this.text = s;
this.moveCursorBy(i - this.selectionEnd + l);
if (this.guiResponder != null)
{
this.guiResponder.setEntryValue(this.id, this.text);
}
}
}
public boolean isValid(String string) {
// Solve the problem that Minecraft can't post Chinese characters
return ChatAllowedCharacters.isAllowedCharacter(string.charAt(string.length() - 1));
}
public boolean isValid(String string) {
// Solve the problem that Minecraft can't post Chinese characters
return ChatAllowedCharacters.isAllowedCharacter(string.charAt(string.length() - 1));
}
public boolean canAddChar(char c)
{
return allowedCharacters == null ? ChatAllowedCharacters.isAllowedCharacter(c) : allowedCharacters.indexOf(c) >= 0;
}