Jump to content

[1.10.2] Add Player tab completer into GUI


JimiIT92

Recommended Posts

I have a GUI with a textbox. What i want is that pressing tab will loop through all the usernames of players in the world. I've looked into command block GUI and see how it's tab completer works but editing that didn't worked. So how can i add this tab completer inside my GUI?

Don't blame me if i always ask for your help. I just want to learn to be better :)

Link to comment
Share on other sites

This is the GUI code i'm using where i want to put the tab completer

 
	package com.cf.gui;
	import java.io.IOException;
import java.util.List;
import java.util.UUID;
	import javax.annotation.Nullable;
	import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;
	import com.cf.CoreFaction;
import com.cf.capabilities.world.FactionCapabilitiesProvider;
import com.cf.capabilities.world.IFactionCapabilities;
import com.cf.messages.FactionCapabilitiesMessage;
import com.cf.utils.Utils;
	import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.TabCompleter;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;
	public class GuiFactionHandling extends GuiScreen {
	    private static final ResourceLocation BG = new ResourceLocation(CoreFaction.MODID, "textures/gui/scroll.png");
	    private GuiButton inviteBtn;
    private GuiButton kickBtn;
    private GuiButton cancelBtn;
    
    private GuiTextField nameTextField;
    private GuiTextField kickTextField;
    private TabCompleter tabCompleter;
	    public void updateScreen()
    {
        this.nameTextField.updateCursorCounter();
        this.kickTextField.updateCursorCounter();
        if(nameTextField.getText().trim().length() <= 0)
            this.inviteBtn.enabled = false;
        else
            this.inviteBtn.enabled = true;
        
        if(kickTextField.getText().trim().length() <= 0)
            this.kickBtn.enabled = false;
        else
            this.kickBtn.enabled = true;
    }
    
    public void initGui() {
        int xPos = (this.width - 256) / 2;
        int yPos = (this.height - 256) / 2;
        this.buttonList.clear();
	        this.inviteBtn = this.addButton(
                new GuiButton(0, xPos + 53, yPos + 50, 150, 20, Utils.getTranslation("gui.handling.invite")));
        
	        this.kickBtn = this.addButton(
                new GuiButton(1, xPos + 53, yPos + 100, 150, 20, Utils.getTranslation("gui.handling.kick")));
	        this.cancelBtn = this.addButton(
                new GuiButton(2, xPos + 53, yPos + 150, 150, 20, Utils.getTranslation("gui.handling.cancel")));
        
        Keyboard.enableRepeatEvents(true);
        this.nameTextField = new GuiTextField(3, this.fontRendererObj, xPos + 53, yPos + 70, 150, 20);
        this.nameTextField.setMaxStringLength(32500);
        this.nameTextField.setFocused(true);
        
        this.kickTextField = new GuiTextField(4, this.fontRendererObj, xPos + 53, yPos + 120, 150, 20);
        this.kickTextField.setMaxStringLength(32500);
        this.kickTextField.setFocused(false);
        
        this.tabCompleter = new TabCompleter(this.nameTextField, false)
        {
            @Nullable
            public BlockPos getTargetBlockPos()
            {
                return null;
            }
        };
    }
	    public void onGuiClosed()
    {
        Keyboard.enableRepeatEvents(false);
    }
    
    protected void keyTyped(char typedChar, int keyCode) throws IOException
    {
        this.tabCompleter.resetRequested();
	        if (keyCode == 15)
        {
            this.tabCompleter.complete();
        }
        else
        {
            this.tabCompleter.resetDidComplete();
        }
        
        this.nameTextField.textboxKeyTyped(typedChar, keyCode);
        this.kickTextField.textboxKeyTyped(typedChar, keyCode);
	        if (keyCode != 28 && keyCode != 156)
        {
            if (keyCode == 1)
            {
                this.actionPerformed(cancelBtn);
            }
        }
        else {
            if(this.nameTextField.isFocused())
                this.actionPerformed(inviteBtn);
            else if(this.kickTextField.isFocused())
                this.actionPerformed(kickBtn);
        }
            
    }
	    protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException
    {
        super.mouseClicked(mouseX, mouseY, mouseButton);
        this.nameTextField.mouseClicked(mouseX, mouseY, mouseButton);
        this.kickTextField.mouseClicked(mouseX, mouseY, mouseButton);
    }
    
    @Override
    public boolean doesGuiPauseGame() {
        return false;
    }
    
    @Override
    protected void actionPerformed(GuiButton button) throws IOException {
        if (button.enabled) {
            World world = this.mc.theWorld;
            EntityPlayer player = this.mc.thePlayer;
            if (button.id == 0) {
                IFactionCapabilities cap = world.getCapability(FactionCapabilitiesProvider.FACTION_CAP, null);
	                EntityPlayerMP inv = FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayerByUsername(this.nameTextField.getText());
                if(inv != null) {
                    if(cap.getFaction(inv.getUniqueID()) == null) {
                        cap.getFaction(player.getUniqueID()).addPlayer(inv.getUniqueID());
                        CoreFaction.NETWORK.sendToServer(new FactionCapabilitiesMessage(cap.getFaction(player.getUniqueID())));
                        CoreFaction.NETWORK.sendTo(new FactionCapabilitiesMessage(cap.getFaction(player.getUniqueID())), inv);
                        this.actionPerformed(cancelBtn);
                    } else {
                        this.actionPerformed(cancelBtn);
                        Utils.sendMessage(player, TextFormatting.RED + Utils.getTranslation("faction.invite.already"));
                    }
                } else {
                    this.actionPerformed(cancelBtn);
                    Utils.sendMessage(player, TextFormatting.RED + Utils.getTranslation("faction.invite.notfound"));
                }
	            } else if (button.id == 1) {
                IFactionCapabilities cap = world.getCapability(FactionCapabilitiesProvider.FACTION_CAP, null);
	                EntityPlayerMP inv = FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayerByUsername(this.nameTextField.getText());
                if(inv != null) {
                    if(cap.getFaction(inv.getUniqueID()) == cap.getFaction(player.getUniqueID())) {
                        cap.getFaction(player.getUniqueID()).removePlayer(inv.getUniqueID());
                        CoreFaction.NETWORK.sendToServer(new FactionCapabilitiesMessage(cap.getFaction(player.getUniqueID())));
                        CoreFaction.NETWORK.sendTo(new FactionCapabilitiesMessage(cap.getFaction(player.getUniqueID())), inv);
                        this.actionPerformed(cancelBtn);
                    } else {
                        this.actionPerformed(cancelBtn);
                        Utils.sendMessage(player, TextFormatting.RED + Utils.getTranslation("faction.kick.nofaction"));
                    }
                } else {
                    this.actionPerformed(cancelBtn);
                    Utils.sendMessage(player, TextFormatting.RED + Utils.getTranslation("faction.invite.notfound"));
                }
            } else if (button.id == 2) {
                this.mc.displayGuiScreen((GuiScreen) null);
            }
        }
    }
	    @Override
    public void drawScreen(int mouseX, int mouseY, float partialTicks) {
        World world = this.mc.theWorld;
        EntityPlayer player = this.mc.thePlayer;
        int xPos = (this.width - 256) / 2;
        int yPos = (this.height - 256) / 2;
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        GL11.glDisable(GL11.GL_LIGHTING);
        this.mc.getTextureManager().bindTexture(this.BG);
        this.drawTexturedModalRect(xPos, yPos, 0, 0, 256, 256);
	        IFactionCapabilities capabilities = world
                .getCapability(FactionCapabilitiesProvider.FACTION_CAP, null);
	        this.drawString(fontRendererObj,
                Utils.getTranslation("gui.handling.faction")+ " " + capabilities.getFaction(player.getUniqueID()).getName(),
                xPos + 53, yPos + 27, 0xFFFFE6);
        this.nameTextField.drawTextBox();
        this.kickTextField.drawTextBox();
        
        super.drawScreen(mouseX, mouseY, partialTicks);
    }
    
    public void setCompletions(String... newCompletions)
    {
        this.tabCompleter.setCompletions(FMLCommonHandler.instance().getMinecraftServerInstance().getAllUsernames());
    }
}
Edited by diesieben07
syntax highlighting

Don't blame me if i always ask for your help. I just want to learn to be better :)

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.