public class CustomChat extends GuiScreen implements ITabCompleter{
private static final Logger LOGGER = LogManager.getLogger();
private String historyBuffer = "";
private int sentHistoryCursor = -1;
private TabCompleter tabCompleter;
protected GuiTextField inputField;
private String defaultInputFieldText = "";
private final List<ChatLine> drawnChatLines = Lists.<ChatLine>newArrayList();
private int scrollPos;
public CustomChat()
{
}
public CustomChat(String defaultText)
{
this.defaultInputFieldText = defaultText;
}
public void initGui()
{
Keyboard.enableRepeatEvents(true);
this.sentHistoryCursor = this.mc.ingameGUI.getChatGUI().getSentMessages().size();
this.inputField = new GuiTextField(0, this.fontRenderer, 57, this.height - 26, this.width - 4, 12);
this.inputField.setMaxStringLength(30);
this.inputField.setEnableBackgroundDrawing(false);
this.inputField.setFocused(true);
this.inputField.setText(this.defaultInputFieldText);
this.inputField.setCanLoseFocus(false);
this.tabCompleter = new CustomChat.ChatTabCompleter(this.inputField);
}
public void onGuiClosed()
{
Keyboard.enableRepeatEvents(false);
this.mc.ingameGUI.getChatGUI().resetScroll();
}
public void updateScreen()
{
this.inputField.updateCursorCounter();
}
protected void keyTyped(char typedChar, int keyCode) throws IOException
{
this.tabCompleter.resetRequested();
if (keyCode == 15)
{
this.tabCompleter.complete();
}
else
{
this.tabCompleter.resetDidComplete();
}
if (keyCode == 1)
{
this.mc.displayGuiScreen((GuiScreen)null);
}
else if (keyCode != 28 && keyCode != 156)
{
if (keyCode == 200)
{
this.getSentHistory(-1);
}
else if (keyCode == 208)
{
this.getSentHistory(1);
}
else if (keyCode == 201)
{
this.mc.ingameGUI.getChatGUI().scroll(this.mc.ingameGUI.getChatGUI().getLineCount() - 1);
}
else if (keyCode == 209)
{
this.mc.ingameGUI.getChatGUI().scroll(-this.mc.ingameGUI.getChatGUI().getLineCount() + 1);
}
else
{
this.inputField.textboxKeyTyped(typedChar, keyCode);
}
}
else
{
String s = this.inputField.getText().trim();
if (!s.isEmpty())
{
this.sendChatMessage(s);
}
this.mc.displayGuiScreen((GuiScreen)null);
}
}
public void handleMouseInput() throws IOException
{
super.handleMouseInput();
int i = Mouse.getEventDWheel();
if (i != 0)
{
if (i > 1)
{
i = 1;
}
if (i < -1)
{
i = -1;
}
if (!isShiftKeyDown())
{
i *= 7;
}
this.mc.ingameGUI.getChatGUI().scroll(i);
}
}
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException
{
if (mouseButton == 0)
{
ITextComponent itextcomponent = this.mc.ingameGUI.getChatGUI().getChatComponent(Mouse.getX(), Mouse.getY());
if (itextcomponent != null && this.handleComponentClick(itextcomponent))
{
return;
}
}
this.inputField.mouseClicked(mouseX, mouseY, mouseButton);
super.mouseClicked(mouseX, mouseY, mouseButton);
}
protected void setText(String newChatText, boolean shouldOverwrite)
{
if (shouldOverwrite)
{
this.inputField.setText(newChatText);
}
else
{
this.inputField.writeText(newChatText);
}
}
public void getSentHistory(int msgPos)
{
int i = this.sentHistoryCursor + msgPos;
int j = this.mc.ingameGUI.getChatGUI().getSentMessages().size();
i = MathHelper.clamp(i, 0, j);
if (i != this.sentHistoryCursor)
{
if (i == j)
{
this.sentHistoryCursor = j;
this.inputField.setText(this.historyBuffer);
}
else
{
if (this.sentHistoryCursor == j)
{
this.historyBuffer = this.inputField.getText();
}
this.inputField.setText((String)this.mc.ingameGUI.getChatGUI().getSentMessages().get(i));
this.sentHistoryCursor = i;
}
}
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
drawRect(2, this.height - 14, this.width - 2, this.height - 2, Integer.MIN_VALUE);
this.inputField.drawTextBox();
ITextComponent itextcomponent = this.mc.ingameGUI.getChatGUI().getChatComponent(Mouse.getX(), Mouse.getY());
if (itextcomponent != null && itextcomponent.getStyle().getHoverEvent() != null)
{
this.handleComponentHover(itextcomponent, mouseX, mouseY);
}
super.drawScreen(mouseX, mouseY, partialTicks);
}
public boolean doesGuiPauseGame()
{
return false;
}
public void setCompletions(String... newCompletions)
{
this.tabCompleter.setCompletions(newCompletions);
}
@SideOnly(Side.CLIENT)
public static class ChatTabCompleter extends TabCompleter
{
private final Minecraft client = Minecraft.getMinecraft();
public ChatTabCompleter(GuiTextField p_i46749_1_)
{
super(p_i46749_1_, false);
}
public void complete()
{
super.complete();
if (this.completions.size() > 1)
{
StringBuilder stringbuilder = new StringBuilder();
for (String s : this.completions)
{
if (stringbuilder.length() > 0)
{
stringbuilder.append(", ");
}
stringbuilder.append(s);
}
this.client.ingameGUI.getChatGUI().printChatMessageWithOptionalDeletion(new TextComponentString(stringbuilder.toString()), 1);
}
}
@Nullable
public BlockPos getTargetBlockPos()
{
BlockPos blockpos = null;
if (this.client.objectMouseOver != null && this.client.objectMouseOver.typeOfHit == RayTraceResult.Type.BLOCK)
{
blockpos = this.client.objectMouseOver.getBlockPos();
}
return blockpos;
}
}
}