Jump to content

Recommended Posts

Posted

I have made a custom block (flux grinder) which is loaded into the world perfectly fine.

I tried making a custom gui (so that it has a working second output slot), but not only am I having troubles aligning it properly, it also crashes once I hover over the place where the inputslot is being put right now (which is not aligned properly)

 

GuiFluxGrinder:

package com.messy.core.gui;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import com.messy.core.Reference;
import com.messy.core.containers.ContainerFluxGrinder;
import com.messy.core.tileentities.TileEntityFluxGrinder;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class GuiFluxGrinder  extends GuiContainer
{
    private static final ResourceLocation texture = new ResourceLocation(Reference.MOD_ID + ":textures/gui/flux_grinder_gui.png");
    private final TileEntityFluxGrinder entity;

    public GuiFluxGrinder(InventoryPlayer player, TileEntityFluxGrinder entity)
    {
        super(new ContainerFluxGrinder(player, entity));
        
        xSize = 176;
        ySize = 166;
        
        this.entity = entity;
    }

    final int process_bar_xpos = 49;
    final int process_bar_ypos = 60;
    final int process_bar_icon_u = 176;
    final int process_bar_icon_v = 14;
    final int process_bar_width = 24;
    final int process_bar_height = 17;

    final int flame_xpos = 57;
    final int flame_ypos = 37;
    final int flame_icon_u = 176;
    final int flame_icon_v = 0;
    final int flame_width = 14;
    final int flame_height = 14;
    final int flame_x_spacing = 18;
    
    /**
     * Args : renderPartialTicks, mouseX, mouseY
     */
    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY)
    {
    	Minecraft.getMinecraft().getTextureManager().bindTexture(texture);
    	GlStateManager.color(1.0f, 1.0f, 1.0f, 1.0f);
    	this.drawTexturedModalRect(this.guiLeft, this.guiTop, 0, 0, xSize, ySize);
    	
    	double processProgress = entity.fractionOfProcessingTimeComplete();
    	this.drawTexturedModalRect(this.guiLeft + process_bar_xpos, 
    			this.guiTop + process_bar_ypos, 
    			process_bar_icon_u, 
    			process_bar_icon_v, 
    			(int)(processProgress * process_bar_width), 
    			 process_bar_height);
    	
    	for ( int i = 0; i < entity.fuel_slots; i++)
    	{
    		double burnRemaining = entity.fractionOfFuelRemaining(i);
    		int yOffset = (int)((1.0 - burnRemaining) * flame_height);
    		
    		this.drawTexturedModalRect(guiLeft + flame_xpos + flame_x_spacing * i, 
    				guiTop + flame_ypos + yOffset, 
    				flame_icon_u, 
    				flame_icon_v + yOffset, 
    				flame_width, 
    				flame_height - yOffset);
    	}
    }
    
    @Override
    protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
    {
    	super.drawGuiContainerForegroundLayer(mouseX, mouseY);

    	final int label_xpos = 5;
    	final int label_ypos = 5;
    	
    	fontRendererObj.drawString(entity.getDisplayName().getUnformattedText(), label_xpos, label_ypos, Color.DARK_GRAY.getRGB());
    	
    	List<String> text = new ArrayList<String>();
    	
    	if (isInRect(guiLeft + process_bar_xpos, guiTop + process_bar_ypos, process_bar_width, process_bar_height, mouseX, mouseY))
    	{
    		text.add("Progress:");
    		
    		int processPercentage = (int)(entity.fractionOfProcessingTimeComplete() * 100);
    		
    		text.add(processPercentage + "%");
    	}
    	
    	for(int i = 0; i < entity.fuel_slots; i++)
    	{
        	if (isInRect(guiLeft + flame_xpos + flame_x_spacing * i, guiTop + flame_ypos, flame_width, flame_height, mouseX, mouseY))
        	{
        		text.add("Fuel Time: ");
        		text.add(entity.secondsOfFuelRemaining(i) + "s");
        	}
    	}
    	
    	if (!text.isEmpty())
    	{
    		drawHoveringText(text, mouseX - guiLeft, mouseY - guiTop, fontRendererObj);
    	}
    }
    
    public static boolean isInRect(int x, int y, int xSize, int ySize, int mouseX, int mouseY)
    {
    	return ((mouseX >= x && mouseX <= x + xSize) && (mouseY >= y && mouseY <= y + ySize));
    }
}

 

GuiHandlerFluxGrinder:

package com.messy.core.gui;

import com.messy.core.containers.ContainerFluxGrinder;
import com.messy.core.tileentities.TileEntityFluxGrinder;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.IGuiHandler;

public class GuiHandlerFluxGrinder implements IGuiHandler
{
public static final int FLUX_GRINDER_GUI = 34;
public static int getGUIID() { return FLUX_GRINDER_GUI; }

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	if (ID == FLUX_GRINDER_GUI)
	{
		return new ContainerFluxGrinder(player.inventory, (TileEntityFluxGrinder)world.getTileEntity(new BlockPos(x, y, z)));
	}
	return null;
}

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
	if (ID == FLUX_GRINDER_GUI)
	{
		return new GuiFluxGrinder(player.inventory, (TileEntityFluxGrinder)world.getTileEntity(new BlockPos(x, y, z)));
	}
	return null;
}

}

 

Crashlog:

  Reveal hidden contents

 

 

What am I missing here?

  • Replies 79
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted

package com.messy.core.tileentities;

import com.messy.core.crafting.FluxGrinderRecipes;

import net.minecraft.item.ItemStack;

public class TileEntityFluxGrinder extends ModTileEntity
{
public static final int fuel_slots = 1;
public static final int input_slots = 2;
public static final int output_slots = 1;

public int[] burnTimeRemaining = new int[fuel_slots];
public int[] burnTimeInitial = new int[fuel_slots];

public static ItemStack getProcessingResultForItem(ItemStack stack){
	return ModTileEntity.getProcessingResultForItem(FluxGrinderRecipes.instance().getGrindingResult(stack));
}

@Override
public String getName() {
	return "container.tile_entity_flux_grinder.name";
}


public double fractionOfFuelRemaining(int fuelSlot) {
	return super.fractionOfFuelRemaining(fuelSlot, burnTimeInitial);
}

public void setCustomInventoryName(String displayName) {
}
}

 

package com.messy.core.tileentities;

import java.util.Arrays;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagIntArray;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.EnumSkyBlock;

public class ModTileEntity extends TileEntity implements IInventory, ITickable {
public static final int fuel_slots = 0;
public static final int input_slots = 0;
public static final int output_slots = 0;
public static final int total_slots = fuel_slots + input_slots + output_slots;

public static final int first_fuel_slot = 0;
public static final int first_input_slot = first_fuel_slot + input_slots;
public static final int first_output_slot = first_input_slot + output_slots;

protected ItemStack[] itemStacks = new ItemStack[total_slots];

protected int[] burnTimeRemaining;
protected int[] burnTimeInitial;

protected short processTime;
protected static final short working_time_for_completion = 200;
protected int cachedNumberOfBurningSlots = -1;

public double fractionOfFuelRemaining(int fuelSlot, int[] burnTimeInitial) {
	double fraction;

	this.burnTimeInitial = burnTimeInitial;

	if (this.burnTimeInitial[fuelSlot] <= 0) {
		fraction = 0;
	} else {
		fraction = burnTimeRemaining[fuelSlot] / (double) burnTimeInitial[fuelSlot];
	}

	return MathHelper.clamp_double(fraction, 0.0, 1.0);
}

public int secondsOfFuelRemaining(int fuelSlot) {
	if (burnTimeRemaining[fuelSlot] <= 0) {
		return 0;
	}

	return burnTimeRemaining[fuelSlot] / 20;
}

public int numberOfBurningFuelSlots() {
	int burningCount = 0;

	for (int burnTime : burnTimeRemaining) {
		if (burnTime > 0) {
			++burningCount;
		}
	}

	return burningCount;
}

public double fractionOfProcessingTimeComplete() {
	double fraction = processTime / (double) working_time_for_completion;

	return MathHelper.clamp_double(fraction, 0.0, 1.0);
}

protected int burnFuel() {
	int burningCount = 0;
	boolean inventorychanged = false;

	for (int i = 0; i < fuel_slots; i++) {
		int fuelSlotNumber = i + first_fuel_slot;

		if (burnTimeRemaining[i] > 0) {
			--burnTimeRemaining[i];
			++burningCount;
		}

		if (burnTimeRemaining[i] == 0) {
			if (itemStacks[fuelSlotNumber] != null && getItemBurnTime(itemStacks[fuelSlotNumber]) > 0) {
				burnTimeRemaining[i] = burnTimeInitial[i] = getItemBurnTime(itemStacks[fuelSlotNumber]);
				--itemStacks[fuelSlotNumber].stackSize;
				++burningCount;
				inventorychanged = true;

				if (itemStacks[fuelSlotNumber].stackSize == 0) {
					itemStacks[fuelSlotNumber] = itemStacks[fuelSlotNumber].getItem()
							.getContainerItem(itemStacks[fuelSlotNumber]);
				}
			}
		}
	}

	if (inventorychanged) {
		markDirty();
	}

	return burningCount;
}

protected boolean canProcess() {
	return processItem(false);
}

protected void processItem() {
	processItem(true);
}

protected boolean processItem(boolean performProcess) {
	Integer firstSuitableInputSlot = null;
	Integer firstSuitableOutputSlot = null;
	ItemStack result = null;

	for (int inputSlot = first_input_slot; inputSlot < first_input_slot + inputSlot; inputSlot++) {
		if (itemStacks[inputSlot] != null) {
			result = getProcessingResultForItem(itemStacks[inputSlot]);

			if (result != null) {
				for (int outputSlot = first_output_slot; outputSlot < first_output_slot
						+ output_slots; outputSlot++) {
					ItemStack outputStack = itemStacks[outputSlot];

					if (outputStack == null) {
						firstSuitableInputSlot = inputSlot;
						firstSuitableOutputSlot = outputSlot;
						break;
					}

					if (outputStack.getItem() == result.getItem() && (!outputStack.getHasSubtypes()
							|| outputStack.getMetadata() == outputStack.getMetadata()
									&& ItemStack.areItemStacksEqual(outputStack, result))) {
						int combinedSize = itemStacks[outputSlot].stackSize + result.stackSize;

						if (combinedSize <= getInventoryStackLimit()
								&& combinedSize <= itemStacks[outputSlot].getMaxStackSize()) {
							firstSuitableInputSlot = inputSlot;
							firstSuitableOutputSlot = outputSlot;
							break;
						}
					}
				}

				if (firstSuitableInputSlot != null) {
					break;
				}
			}
		}
	}

	if (firstSuitableInputSlot == null) {
		return false;
	}

	if (!performProcess) {
		return true;
	}

	itemStacks[firstSuitableInputSlot].stackSize--;

	if (itemStacks[firstSuitableInputSlot].stackSize <= 0) {
		itemStacks[firstSuitableInputSlot] = null;
	}

	if (itemStacks[firstSuitableOutputSlot] == null) {
		itemStacks[firstSuitableOutputSlot] = result.copy();
	} else {
		itemStacks[firstSuitableOutputSlot].stackSize += result.stackSize;
	}

	markDirty();
	return true;
}

public static short getItemBurnTime(ItemStack stack)
{
	int burnTime = TileEntityFurnace.getItemBurnTime(stack);
	return (short) MathHelper.clamp_int(burnTime, 0, Short.MAX_VALUE);
}

public static ItemStack getProcessingResultForItem(ItemStack stack){
	return stack;
}

@Override
public void update() {
	if (canProcess()) {
		int numberOfFuelBurning = burnFuel();

		if (numberOfFuelBurning > 0) {
			processTime += numberOfFuelBurning;
		} else {
			processTime -= 2;
		}

		if (processTime < 0) {
			processTime = 0;
		}

		if (processTime >= working_time_for_completion) {
			processItem();
			processTime = 0;
		}
	} else {
		processTime = 0;
	}

	int numberBurning = numberOfBurningFuelSlots();

	if (cachedNumberOfBurningSlots != numberBurning) {
		cachedNumberOfBurningSlots = numberBurning;

		if (worldObj.isRemote) {
			worldObj.markBlockRangeForRenderUpdate(pos, pos);
		}

		worldObj.checkLightFor(EnumSkyBlock.BLOCK, pos);
	}
}

@Override
public int getSizeInventory() {
	return itemStacks.length;
}

@Override
public ItemStack getStackInSlot(int index) {
	return itemStacks[index];
}

@Override
public ItemStack decrStackSize(int slotIndex, int count) {
	ItemStack itemStackInSlot = getStackInSlot(slotIndex);

	if (itemStackInSlot == null){
		return null;
	}

	ItemStack itemStackRemoved;

	if (itemStackInSlot.stackSize <= count)
	{
		itemStackRemoved = itemStackInSlot;
		setInventorySlotContents(slotIndex, null);
	}
	else
	{
		itemStackRemoved = itemStackInSlot.splitStack(count);

		if (itemStackInSlot.stackSize == 0)
		{
			setInventorySlotContents(slotIndex, null);
		}
	}

	markDirty();
	return itemStackRemoved;
}

@Override
public void setInventorySlotContents(int slotIndex, ItemStack stack) {
	itemStacks[slotIndex] = stack;

	if (stack != null && stack.stackSize > getInventoryStackLimit())
	{
		stack.stackSize = getInventoryStackLimit();
	}

	markDirty();
}

@Override
public int getInventoryStackLimit() {
	return 64;
}

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	if (this.worldObj.getTileEntity(this.pos) != this)
	{
		return false;
	}

	final double x_center_offset = 0.5;
	final double y_center_offset = 0.5;
	final double z_center_offset = 0.5;
	final double maximum_distance_sq = 8.0 * 8.0;

	return player.getDistanceSq(pos.getX() + x_center_offset, pos.getY() + y_center_offset, pos.getZ() + z_center_offset) < maximum_distance_sq;
}

public boolean isItemValidForFuelSlot(ItemStack stack) {
	return false;
}

public boolean isItemValidForInputSlot(ItemStack stack) {
	return false;
}

public boolean isItemValidForOutputSlot(ItemStack stack) {
	return false;
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound parentNBTTagCompound)
{
	super.writeToNBT(parentNBTTagCompound);

	NBTTagList dataForAllSlots = new NBTTagList();

	for (int i = 0; i < this.itemStacks.length; i++)
	{
		if (this.itemStacks[i] != null)
		{
			NBTTagCompound dataForThisSlot = new NBTTagCompound();
			dataForThisSlot.setByte("Slot", (byte)i);
			this.itemStacks[i].writeToNBT(dataForThisSlot);
			dataForAllSlots.appendTag(dataForThisSlot);
		}
	}

	parentNBTTagCompound.setTag("Items", dataForAllSlots);
	parentNBTTagCompound.setShort("ProcessTime", processTime);
	parentNBTTagCompound.setTag("BurnTimeRemaining", new NBTTagIntArray(burnTimeRemaining));
	parentNBTTagCompound.setTag("BurnTimeInitial", new NBTTagIntArray(burnTimeInitial));

	return parentNBTTagCompound;
}

@Override
public void readFromNBT(NBTTagCompound nbtTagCompound)
{
	super.readFromNBT(nbtTagCompound);

	final byte nbt_type_compound = 10;
	NBTTagList dataForAllSlots = nbtTagCompound.getTagList("Items", nbt_type_compound);
	Arrays.fill(itemStacks, null);

	for (int i = 0; i < dataForAllSlots.tagCount(); i++)
	{
		NBTTagCompound dataForOneSlot = dataForAllSlots.getCompoundTagAt(i);
		byte slotNumber = dataForOneSlot.getByte("Slot");

		if (slotNumber >= 0 && slotNumber < this.itemStacks.length)
		{
			this.itemStacks[slotNumber] = ItemStack.loadItemStackFromNBT(dataForOneSlot);
		}
	}

	processTime = nbtTagCompound.getShort("ProcessTime");
	burnTimeRemaining = Arrays.copyOf(nbtTagCompound.getIntArray("BurnTimeRemaining"), fuel_slots);
	burnTimeInitial = Arrays.copyOf(nbtTagCompound.getIntArray("BurnTimeInitial"), fuel_slots);
	cachedNumberOfBurningSlots = -1;
}

@SuppressWarnings("rawtypes")
public Packet getDescriptionPacket()
{
	NBTTagCompound nbtTagCompound = new NBTTagCompound();
	writeToNBT(nbtTagCompound);
	final int metadata = 0;
	return new SPacketUpdateTileEntity(this.pos, metadata, nbtTagCompound);
}

public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt)
{
	readFromNBT(pkt.getNbtCompound());
}

@Override
public void clear() {
	Arrays.fill(itemStacks, null);
}

@Override
public String getName() {
	return "Set this first";
}

@Override
public boolean hasCustomName() {
	return false;
}

public ITextComponent getDisplayName()
{
	return this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName());
}

private static final byte process_field_id = 0;
private static final byte first_burn_time_remaining_field_id = 1;
private static final byte first_burn_time_initial_field_id = first_burn_time_remaining_field_id + (byte)fuel_slots;
private static final byte number_of_fields = first_burn_time_initial_field_id + (byte)fuel_slots;

@Override
public int getField(int id) {
	if (id == process_field_id)
	{	
		return 0;
	}

	if (id >= first_burn_time_remaining_field_id && id < first_burn_time_remaining_field_id + (byte)fuel_slots)
	{
		return burnTimeRemaining[id - first_burn_time_remaining_field_id];
	}

	if (id >= first_burn_time_initial_field_id && id < first_burn_time_initial_field_id + (byte)fuel_slots)
	{
		return burnTimeInitial[id - first_burn_time_initial_field_id];
	}

	System.err.println("Invalid field ID in TileInventoryProcessing.getField: " + id);
	return 0;
}

@Override
public void setField(int id, int value) {
	if (id == process_field_id)
	{
		processTime = (short)value;
	}
	else if (id >= first_burn_time_remaining_field_id && id < first_burn_time_remaining_field_id + fuel_slots)
	{
		burnTimeRemaining[id - first_burn_time_remaining_field_id] = value;
	}
	else if (id >= first_burn_time_initial_field_id && id < first_burn_time_initial_field_id + fuel_slots)
	{
		burnTimeInitial[id - first_burn_time_initial_field_id] = value;
	}
	else
	{
		System.err.println("Invalid field ID in TileInventoryProcessing.getField: " + id);
	}
}

@Override
public int getFieldCount() {
	return number_of_fields;
}

@Override
public boolean isItemValidForSlot(int index, ItemStack stack) {
	return false;
}

@Override
public ItemStack removeStackFromSlot(int slotIndex) {
	ItemStack stack = getStackInSlot(slotIndex);

	if (stack != null)
	{
		setInventorySlotContents(slotIndex, null);
	}

	return stack;
}

@Override
public void openInventory(EntityPlayer player) {
	// TODO Auto-generated method stub

}

@Override
public void closeInventory(EntityPlayer player) {
	// TODO Auto-generated method stub

}
}

Posted

You never actually change how many slots you have :P

        public static final int fuel_slots = 0;
public static final int input_slots = 0;
public static final int output_slots = 0;
public static final int total_slots = fuel_slots + input_slots + output_slots;

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

I have changed the way I set the fields to their values in my TileEntityFluxGrinder class (see below)

 

package com.messy.core.tileentities;

import com.messy.core.crafting.FluxGrinderRecipes;

import net.minecraft.item.ItemStack;

public class TileEntityFluxGrinder extends ModTileEntity
{
private static boolean setupDone = false; 
public static final int fuel_slots = 1;
public static final int input_slots = 2;
public static final int output_slots = 1;

protected ItemStack[] itemStacks = new ItemStack[total_slots];

public static ItemStack getProcessingResultForItem(ItemStack stack){
	return ModTileEntity.getProcessingResultForItem(FluxGrinderRecipes.instance().getGrindingResult(stack));
}

@Override
public String getName() {
	return "container.tile_entity_flux_grinder.name";
}


public int numberOfBurningFuelSlots() {
	if (!setupDone)
	{
		total_slots = fuel_slots + input_slots + output_slots;
		first_input_slot = first_fuel_slot + input_slots;
		first_output_slot = first_input_slot + output_slots;
		burnTimeInitial = new int[fuel_slots];
		burnTimeRemaining = new int[fuel_slots];

		setupDone = true;
	}

	return super.numberOfBurningFuelSlots();
}

public void setCustomInventoryName(String displayName) {
}
}

 

Now whenever I open the Flux grinder, the whole thing just crashes:

 

  Reveal hidden contents

 

 

I guess you could call this progress :P

Posted

for (int inputSlot = first_input_slot; inputSlot < first_input_slot + inputSlot; inputSlot++) {

Just be glad a null pointer exception was caused before a different one was.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

After fixing that, it still crashed but with a different log:

 

  Reveal hidden contents

 

 

As you can see, it seems very familiar...

burnTimeRemaining has to be the cause, but I'm not sure in what way

Posted

Well... Didn't realize that you meant that :P

My bad :P

 

The functionality is based around it making the block in question work like a machine with a way of processing items.

I'm making the flux grinder first, but flux furnace will be next for example.

 

A bit similar to Thermal expansions pulverizer and redstone furnace.

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.