Jump to content

[1.7.10] NullPointerException when editing guiButton.displayText


artman41

Recommended Posts

As said in the title, I'm getting a NullPointerException when I edit the guiButton.displayText. I'm doing this while the gui is open and I'm not sure of any other way of doing it. Any help?

 

 protected void actionPerformed(GuiButton guiButton) {
	super.actionPerformed(guiButton);
	switch(guiButton.id) {
	case 1:
		System.out.println("Button clicked");
		if(this.isLinkEnabled == 0){
			System.out.println("Disabled");
			this.isLinkEnabled = 1;
			drawButton(false, isLinkEnabled);
		} else{
			System.out.println("Enabled");
			this.isLinkEnabled = 0;
			drawButton(false, isLinkEnabled);
		}
		break;
	}
	//Packet code here
	//PacketDispatcher.sendPacketToServer(packet); //send packet
}

private void drawButton(boolean firstRun, int enabled){
	if(firstRun){
		this.buttonList.clear();
		//id, x, y, width, height, text
		this.buttonList.add(new GuiButton(neiLinkID, this.textBoxFilter.xPosition, (this.textBoxFilter.yPosition + 10) + 2, neiLinkWidth, neiLinkHeight, "A"));
		this.isLinkEnabled = 0;
	}else{
		try{
			switch(enabled){
			case 0:
				this.neiLink.displayString = "A";
				break;
			case 1:
				this.neiLink.displayString = "B";
				break;
			}
		} catch(NullPointerException e){
			e.printStackTrace();
		}
	}
} 

Link to comment
Share on other sites

I'm adding a button to allow the search bar in ProjectE to interface with the NEI search bar. Here's the code:

 

package moze_intel.projecte.gameObjs.gui;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import moze_intel.projecte.PECore;
import moze_intel.projecte.gameObjs.container.TransmuteContainer;
import moze_intel.projecte.gameObjs.tiles.TransmuteTile;
import moze_intel.projecte.network.PacketHandler;
import moze_intel.projecte.network.packets.ClientSyncTabletLinkPKT;
import moze_intel.projecte.network.packets.SearchUpdatePKT;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiTextField;

import org.lwjgl.opengl.GL11;

public class GUITransmute extends GuiContainer
{
private static final ResourceLocation texture = new ResourceLocation(PECore.MODID.toLowerCase(), "textures/gui/transmute.png");
private TransmuteTile tile;
private GuiTextField textBoxFilter;
private GuiButton neiLink;

int xLocation;
int yLocation;

int neiLinkID = 1;
int neiLinkWidth = 15;
int neiLinkHeight = 10;

private static int isLinkEnabled = 0;

public GUITransmute(InventoryPlayer invPlayer, TransmuteTile tile) 
{
	super(new TransmuteContainer(invPlayer, tile));
	this.tile = tile;
	this.xSize = 228;
	this.ySize = 196;
}

@Override
public void initGui() 
{
	tile.setPlayer(Minecraft.getMinecraft().thePlayer);
	super.initGui();

	this.xLocation = (this.width - this.xSize) / 2;
	this.yLocation = (this.height - this.ySize) / 2;

	this.textBoxFilter = new GuiTextField(this.fontRendererObj, this.xLocation + 88, this.yLocation + 8, 45, 10);
	this.textBoxFilter.setText(tile.filter);
	drawButton(true, isLinkEnabled);
}

protected void actionPerformed(GuiButton guiButton) {
	super.actionPerformed(guiButton);
	switch(guiButton.id) {
	case 1:
		System.out.println("Button clicked");
		if(this.isLinkEnabled == 0){
			System.out.println("Disabled");
			PacketHandler.sendToServer(new ClientSyncTabletLinkPKT(1));
			drawButton(false, isLinkEnabled);
		} else{
			System.out.println("Enabled");
			PacketHandler.sendToServer(new ClientSyncTabletLinkPKT(0));
			drawButton(false, isLinkEnabled);
		}
		break;
	}
}

public static int getLinkEnabled(){
	return isLinkEnabled;
}

public static void setLinkEnabled(int i){
	isLinkEnabled = i;
}

private void drawButton(boolean firstRun, int enabled){
	if(firstRun){
		this.buttonList.clear();
		//id, x, y, width, height, text
		this.buttonList.add(new GuiButton(neiLinkID, this.textBoxFilter.xPosition, (this.textBoxFilter.yPosition + 10) + 2, neiLinkWidth, neiLinkHeight, "A"));
		this.isLinkEnabled = 0;
	}else{
		try{
			switch(enabled){
			case 0:
				this.neiLink.displayString = "A";
				break;
			case 1:
				this.neiLink.displayString = "B";
				break;
			}
		} catch(NullPointerException e){
			e.printStackTrace();
		}
	}
}

@Override
public void drawScreen(int par1, int par2, float par3)
{
	super.drawScreen(par1, par2, par3);
	this.textBoxFilter.drawTextBox();
}

@Override
protected void drawGuiContainerBackgroundLayer(float var1, int var2, int var3) 
{
	GL11.glColor4f(1F, 1F, 1F, 1F);
	Minecraft.getMinecraft().renderEngine.bindTexture(texture);
	this.drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
}

@Override
protected void drawGuiContainerForegroundLayer(int var1, int var2) 
{
	this.fontRendererObj.drawString("Transmutation", 6, 8, 4210752);
	String emc = String.format("EMC: %,d", (int) tile.getStoredEmc());
	this.fontRendererObj.drawString(emc, 6, this.ySize - 94, 4210752);

	if (tile.learnFlag > 0)
	{
		this.fontRendererObj.drawString("L", 98, 30, 4210752);
		this.fontRendererObj.drawString("e", 99, 38, 4210752);
		this.fontRendererObj.drawString("a", 100, 46, 4210752);
		this.fontRendererObj.drawString("r", 101, 54, 4210752);
		this.fontRendererObj.drawString("n", 102, 62, 4210752);
		this.fontRendererObj.drawString("e", 103, 70, 4210752);
		this.fontRendererObj.drawString("d", 104, 78, 4210752);
		this.fontRendererObj.drawString("!", 107, 86, 4210752);

		tile.learnFlag--;
	}
}

@Override
public void updateScreen() 
{
	super.updateScreen();
	this.textBoxFilter.updateCursorCounter();
}

@Override
protected void keyTyped(char par1, int par2)
{
	if (this.textBoxFilter.isFocused()) 
	{
		this.textBoxFilter.textboxKeyTyped(par1, par2);

		String srch = this.textBoxFilter.getText().toLowerCase();

		if (!tile.filter.equals(srch)) 
		{
			PacketHandler.sendToServer(new SearchUpdatePKT(srch));
			tile.filter = srch;
			tile.updateOutputs();
		}
	}

	if (par2 == 1 || par2 == this.mc.gameSettings.keyBindInventory.getKeyCode() && !this.textBoxFilter.isFocused())
	{
		this.mc.thePlayer.closeScreen();
	}
}

@Override
protected void mouseClicked(int x, int y, int mouseButton)
{
	super.mouseClicked(x, y, mouseButton);

	int minX = textBoxFilter.xPosition;
	int minY = textBoxFilter.yPosition;
	int maxX = minX + textBoxFilter.width;
	int maxY = minY + textBoxFilter.height;

	if (mouseButton == 1 && x >= minX && x <= maxX && y <= maxY)
	{
		PacketHandler.sendToServer(new SearchUpdatePKT(""));
		tile.filter = "";
		tile.updateOutputs();
		this.textBoxFilter.setText("");
	}

	this.textBoxFilter.mouseClicked(x, y, mouseButton);

}

@Override
public void onGuiClosed()
{
	super.onGuiClosed();
	tile.learnFlag = 0;
}
}

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • For hours I have been trying to just instal Mr. Crayfish's Refurbished Furniture Mod, but each step to fix the error codes, the more problems arise. The farthest I got through the steps was getting to the Forge Installer, but once I had selected the file, an error saying "The directory is missing a launcher profile. Please run the minecraft launcher first". At this point I don;'t know what more I can do. Please help.
    • I create my mod pack,yesterday my mod pack is fine but i add one mod and error. I'm delete this mmod but minecraft is still stop on CONFIG_LOAD then I tried to delete config and restart it but again. If you can pleace help me. https://imgur.com/ngZBzuv
    • game crashes before even opening (log:https://mclo.gs/M8xvX7c)
    • I have created a custom entity that extends "TamableAnimal", but I am wanting to have it spawn in the ocean. I have it spawning right now, but it spawns way too frequently even with weight set to 1. I am guessing it is because it is rolling in the spawn pool of land animals since TameableAnimal extends Animal and is different than WaterAnimal, and since no land animals spawn in the ocean it just fills every inch up with my custom entity. I have followed basic tutorials for spawning entities with Forge, but I feel like I am missing something about how to change what spawn pool this custom entity ends up in. Is it possible to change that or do I need to refactor it to be based off of WaterAnimal to get those spawn? My biome modifier JSON file: { "type": "forge:add_spawns", "biomes": "#minecraft:is_ocean", "spawners": { "type": "darwinsmysticalmounts:water_animal", "weight": 20, "minCount": 1, "maxCount": 1 } } My client event: event.register(ModEntityTypes.WATER_ANIMAL.get(), SpawnPlacements.Type.NO_RESTRICTIONS, Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, WaterWyvernEntity::checkCustomWaterAnimalSpawnRules, SpawnPlacementRegisterEvent.Operation.REPLACE); And the actual custom spawn rule that makes it spawn in the water: public static boolean checkCustomWaterAnimalSpawnRules(EntityType<WaterAnimalEntity> pAnimal, LevelAccessor pLevel, MobSpawnType pSpawnType, BlockPos pPos, RandomSource pRandom) { return pPos.getY() > pLevel.getSeaLevel() - 16 && pLevel.getFluidState(pPos.below()).is(FluidTags.WATER); }  
    • Starting today, I am unable to load my modded minecraft world. Forge crash log initially said it was a specific mod called Doggy Talents, which I disabled. Then it blamed JEI, and when that was disabled it blamed another mod so I assume it's something more than a specific mod. Minecraft launcher log claims "Exit Code 1". Nothing had changed since last night when it was working fine Forge Log: https://pastebin.com/S1GiBGVJ Client Log: https://pastebin.com/aLwuGUNL  
  • Topics

×
×
  • Create New...

Important Information

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