Jump to content

Recommended Posts

Posted

So, I have 2 questions.

1. How would I draw a tool-tip when I hover over Certain coordinates on the GUI

2. Is there an interface I can implement for liquid storage in my tile entity

 

This is my current code so you can see how am I doing it,

if you see stuff that needs to be optimized please tell me, I would greatly appreciate it.

Tile Entity

package industrialage.core.tileentitiy;

import industrialage.core.recipes.SteamGeneratorRecipes;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemHoe;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.item.ItemTool;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class TileEntitySteamGenerator extends TileEntity implements
	ISidedInventory {
/**
 * The ItemStacks that hold the items currently being used in the furnace
 */
private ItemStack[] generatorItemStacks = new ItemStack[3];
/** The number of ticks that the furnace will keep burning */
public int generatorBurnTime;
/**
 * The number of ticks that a fresh copy of the currently-burning item would
 * keep the furnace burning for
 */
public int currentItemBurnTime;
/** The number of ticks that the current item has been cooking for */
public int generatorCookTime;
private String customInventoryName;
int cookRate = 1, fuelBurnRate = 1;
private int steam = 0;
private int maxSteam = 10000;
private int water = 0;
private int maxWater = 1000;

/**
 * Returns the number of slots in the inventory.
 */
public int getSizeInventory() {
	return this.generatorItemStacks.length;
}

/**
 * Returns the stack in slot i
 */
public ItemStack getStackInSlot(int par1) {
	return this.generatorItemStacks[par1];
}

/**
 * Removes from an inventory slot (first arg) up to a specified number
 * (second arg) of items and returns them in a new stack.
 */
public ItemStack decrStackSize(int slot, int number) {
	if (this.generatorItemStacks[slot] != null) {
		ItemStack itemstack;
		if (this.generatorItemStacks[slot].stackSize <= number) {
			itemstack = this.generatorItemStacks[slot];
			this.generatorItemStacks[slot] = null;
			return itemstack;
		} else {
			itemstack = this.generatorItemStacks[slot].splitStack(number);

			if (this.generatorItemStacks[slot].stackSize == 0) {
				this.generatorItemStacks[slot] = null;
			}

			return itemstack;
		}
	} else {
		return null;
	}
}

/**
 * When some containers are closed they call this on each slot, then drop
 * whatever it returns as an EntityItem - like when you close a workbench
 * GUI.
 */
public ItemStack getStackInSlotOnClosing(int par1) {
	if (this.generatorItemStacks[par1] != null) {
		ItemStack itemstack = this.generatorItemStacks[par1];
		this.generatorItemStacks[par1] = null;
		return itemstack;
	} else {
		return null;
	}
}

/**
 * Sets the given item stack to the specified slot in the inventory (can be
 * crafting or armor sections).
 */
public void setInventorySlotContents(int par1, ItemStack par2ItemStack) {
	this.generatorItemStacks[par1] = par2ItemStack;

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

/**
 * Returns the name of the inventory
 */
public String getInventoryName() {
	return this.hasCustomInventoryName() ? this.customInventoryName
			: "container.furnace";
}

/**
 * Returns if the inventory is named
 */
public boolean hasCustomInventoryName() {
	return this.customInventoryName != null
			&& this.customInventoryName.length() > 0;
}

public void setCustomInventoryName(String name) {
	this.customInventoryName = name;
}

@Override
public Packet getDescriptionPacket() {
	NBTTagCompound tag = new NBTTagCompound();
	writeToNBT(tag);
	return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 0, tag);
}

@Override
public void onDataPacket(NetworkManager net,
		S35PacketUpdateTileEntity packet) {
	NBTTagCompound tag = packet.func_148857_g();
	readFromNBT(tag);
}

@Override
public void readFromNBT(NBTTagCompound tagCompound) {
	NBTTagList nbttaglist = tagCompound.getTagList("Items", 10);
	this.generatorItemStacks = new ItemStack[this.getSizeInventory()];

	for (int i = 0; i < nbttaglist.tagCount(); ++i) {
		NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
		byte b0 = nbttagcompound1.getByte("Slot");

		if (b0 >= 0 && b0 < this.generatorItemStacks.length) {
			this.generatorItemStacks[b0] = ItemStack
					.loadItemStackFromNBT(nbttagcompound1);
		}
	}

	this.generatorBurnTime = tagCompound.getShort("BurnTime");
	this.generatorCookTime = tagCompound.getShort("CookTime");
	this.currentItemBurnTime = getItemBurnTime(this.generatorItemStacks[1]);
	this.steam = tagCompound.getInteger("Steam");
	this.water = tagCompound.getInteger("Water");

	if (tagCompound.hasKey("CustomName", ) {
		this.customInventoryName = tagCompound.getString("CustomName");
	}
	super.readFromNBT(tagCompound);
}

@Override
public void writeToNBT(NBTTagCompound tagCompound) {
	tagCompound.setShort("BurnTime", (short) this.generatorBurnTime);
	tagCompound.setShort("CookTime", (short) this.generatorCookTime);
	tagCompound.setInteger("Steam", this.steam);
	tagCompound.setInteger("Water", this.water);
	NBTTagList nbttaglist = new NBTTagList();

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

	tagCompound.setTag("Items", nbttaglist);

	if (this.hasCustomInventoryName()) {
		tagCompound.setString("CustomName", this.customInventoryName);
	}
	super.writeToNBT(tagCompound);
}

/**
 * Returns the maximum stack size for a inventory slot.
 */
@Override
public int getInventoryStackLimit() {
	return 64;
}

/**
 * Returns an integer between 0 and the passed value representing how close
 * the current item is to being completely cooked
 */
@SideOnly(Side.CLIENT)
public int getCookProgressScaled(int max) {
	return this.generatorCookTime * max / (200 / fuelBurnRate);
}

public int getSteam() {
	return this.steam;
}

public int getMaxSteam() {
	return this.maxSteam;
}

public int getWater() {
	return this.water;
}

public int getMaxWater() {
	return this.maxWater;
}

/**
 * Returns an integer between 0 and the passed value representing how much
 * burn time is left on the current fuel item, where 0 means that the item
 * is exhausted and the passed value means that the item is fresh
 */
@SideOnly(Side.CLIENT)
public int getBurnTimeRemainingScaled(int max) {
	if (this.currentItemBurnTime == 0) {
		this.currentItemBurnTime = (200 / cookRate);
	}

	return this.generatorBurnTime * max / this.currentItemBurnTime;
}

/**
 * Furnace isBurning
 */
public boolean isBurning() {
	return this.generatorBurnTime > 0;
}

int oldSteam, oldWater;
int i = 0;

@Override
public void updateEntity() {
	boolean flag = this.generatorBurnTime > 0;
	boolean flag1 = false;
	if (this.generatorBurnTime > 0) {
		--this.generatorBurnTime;
	}
	if (!this.worldObj.isRemote) {
		if (this.generatorBurnTime == 0 && this.canVapor()) {
			this.currentItemBurnTime = this.generatorBurnTime = getItemBurnTime(this.generatorItemStacks[1]);

			if (this.generatorBurnTime > 0) {
				flag1 = true;

				if (this.generatorItemStacks[1] != null) {
					--this.generatorItemStacks[1].stackSize;

					if (this.generatorItemStacks[1].stackSize == 0) {
						this.generatorItemStacks[1] = generatorItemStacks[1]
								.getItem().getContainerItem(
										generatorItemStacks[1]);
					}
				}
			}
		}
		if (this.isBurning() && this.canVapor()) {
			if (i < 4)
				i++;
			else {
				i = 0;
				this.vapor();
				flag1 = true;
			}
		}
		if (this.canSmelt()) {
			smeltItem();
		}

		if (flag != this.generatorBurnTime > 0) {
			flag1 = true;
			// BlockFurnace.updateFurnaceBlockState(this.furnaceBurnTime >
			// 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
		}
	}

	if (flag1) {
		this.markDirty();
	}
}

private void vapor() {
	if (canVapor()) {
		this.steam += 10;
		this.water -= 1;
	}
}

private boolean canVapor() {
	if (this.water > 0 && this.steam < this.maxSteam) {
		return true;
	}
	return false;
}

public void sync() {
	if (!worldObj.isRemote) {
		if (oldSteam != steam || oldWater != water) {
			worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
			oldSteam = steam;
			oldWater = water;
		}
	}
}

/**
 * Returns true if the furnace can smelt an item, i.e. has a source item,
 * destination stack isn't full, etc.
 */
private boolean canSmelt() {
	if (this.generatorItemStacks[0] == null) {
		return false;
	} else {
		Integer amt = SteamGeneratorRecipes.smelting().getSmeltingResult(
				this.generatorItemStacks[0]);
		if (amt == null)
			return false;
		if (getWater() + amt > getMaxWater())
			return false;
		if (this.generatorItemStacks[2] != null)
			if (this.generatorItemStacks[2].stackSize == 16)
				return false;
		return true;
	}
}

/**
 * Turn one item from the furnace source stack into the appropriate smelted
 * item in the furnace result stack
 */
public void smeltItem() {
	if (this.canSmelt()) {
		Integer amt = SteamGeneratorRecipes.smelting().getSmeltingResult(
				this.generatorItemStacks[0]);
		ItemStack itemstack = new ItemStack(Items.bucket, 1);
		water += amt;
		if (this.generatorItemStacks[2] == null) {
			this.generatorItemStacks[2] = itemstack.copy();
		} else if (this.generatorItemStacks[2].stackSize < 16) {
			this.generatorItemStacks[2].stackSize += itemstack.stackSize;
		}
		--this.generatorItemStacks[0].stackSize;

		if (this.generatorItemStacks[0].stackSize <= 0) {
			this.generatorItemStacks[0] = null;
		}
	}
}

/**
 * Returns the number of ticks that the supplied fuel item will keep the
 * furnace burning, or 0 if the item isn't fuel
 */
public static int getItemBurnTime(ItemStack fuel) {
	if (fuel == null) {
		return 0;
	} else {
		Item item = fuel.getItem();

		if (item instanceof ItemBlock
				&& Block.getBlockFromItem(item) != Blocks.air) {
			Block block = Block.getBlockFromItem(item);

			if (block == Blocks.wooden_slab) {
				return 150;
			}

			if (block.getMaterial() == Material.wood) {
				return 300;
			}

			if (block == Blocks.coal_block) {
				return 16000;
			}
		}

		if (item instanceof ItemTool
				&& ((ItemTool) item).getToolMaterialName().equals("WOOD"))
			return 200;
		if (item instanceof ItemSword
				&& ((ItemSword) item).getToolMaterialName().equals("WOOD"))
			return 200;
		if (item instanceof ItemHoe
				&& ((ItemHoe) item).getToolMaterialName().equals("WOOD"))
			return 200;
		if (item == Items.stick)
			return 100;
		if (item == Items.coal)
			return 1600;
		if (item == Items.lava_bucket)
			return 20000;
		if (item == Item.getItemFromBlock(Blocks.sapling))
			return 100;
		if (item == Items.blaze_rod)
			return 2400;
		return GameRegistry.getFuelValue(fuel);
	}
}

public static boolean isItemFuel(ItemStack item) {
	/**
	 * Returns the number of ticks that the supplied fuel item will keep the
	 * furnace burning, or 0 if the item isn't fuel
	 */
	return getItemBurnTime(item) > 0;
}

/**
 * Do not make give this method the name canInteractWith because it clashes
 * with Container
 */
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	return this.worldObj.getTileEntity(this.xCoord, this.yCoord,
			this.zCoord) != this ? false : player.getDistanceSq(
			(double) this.xCoord + 0.5D, (double) this.yCoord + 0.5D,
			(double) this.zCoord + 0.5D) <= 64.0D;
}

@Override
public void openInventory() {
}

@Override
public void closeInventory() {
}

/**
 * Returns true if automation is allowed to insert the given stack (ignoring
 * stack size) into the given slot.
 */
public boolean isItemValidForSlot(int slot, ItemStack item) {
	return slot == 2 ? false : (slot == 1 ? isItemFuel(item) : true);
}

@Override
public int[] getAccessibleSlotsFromSide(int var1) {
	// TODO Auto-generated method stub
	return null;
}

@Override
public boolean canInsertItem(int var1, ItemStack var2, int var3) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public boolean canExtractItem(int var1, ItemStack var2, int var3) {
	// TODO Auto-generated method stub
	return false;
}
}

GUI

package industrialage.core.gui;

import industrialage.core.container.ContainerSteamGenerator;
import industrialage.core.tileentitiy.TileEntitySteamGenerator;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;
import org.lwjgl.util.Color;
import org.lwjgl.util.ReadableColor;

public class GuiSteamGenerator extends GuiContainer {
/**
 * See TestGUI
 */
private static final ResourceLocation guiTextures = new ResourceLocation(
		"textures/gui/container/furnace.png");
private TileEntitySteamGenerator tileEntity;

public GuiSteamGenerator(InventoryPlayer inventoryPlayer,
		TileEntitySteamGenerator tileEntity) {
	super(new ContainerSteamGenerator(inventoryPlayer, tileEntity));
	this.tileEntity = tileEntity;
}

/**
 * Draw the foreground layer for the GuiContainer (everything in front of
 * the items)
 */
protected void drawGuiContainerForegroundLayer(int par1, int par2) {
	String s = this.tileEntity.hasCustomInventoryName() ? this.tileEntity
			.getInventoryName() : I18n.format(
			this.tileEntity.getInventoryName(), new Object[0]);
	this.fontRendererObj.drawString(s, this.xSize / 2
			- this.fontRendererObj.getStringWidth(s) / 2, 6, 4210752);
	this.fontRendererObj.drawString(
			I18n.format("container.inventory", new Object[0]), 8,
			this.ySize - 96 + 2, 4210752);
	this.fontRendererObj.drawString(
			Integer.toString(this.tileEntity.getSteam()),
			this.xSize
					/ 2
					- this.fontRendererObj.getStringWidth(Integer
							.toString(this.tileEntity.getSteam())) / 2, 14,
			4210752);
}

protected void drawGuiContainerBackgroundLayer(float par1, int par2,
		int par3) {
	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
	this.mc.getTextureManager().bindTexture(guiTextures);
	int k = (this.width - this.xSize) / 2;
	int l = (this.height - this.ySize) / 2;
	this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);
	int i1;

	if (this.tileEntity.isBurning()) {
		i1 = this.tileEntity.getBurnTimeRemainingScaled(12);
		this.drawTexturedModalRect(k + 56, l + 36 + 12 - i1, 176, 12 - i1,
				14, i1 + 2);
	}

	i1 = this.tileEntity.getCookProgressScaled(24);
	this.drawTexturedModalRect(k + 79, l + 34, 176, 14, i1 + 1, 16);
	System.out.println(this.tileEntity.getWater() * 10 + ":"
			+ this.tileEntity.getSteam());
	this.tileEntity.sync();
	drawRectangle(k + 7, l + 70, 15, (int) (54f * ((float) this.tileEntity
			.getSteam() / (float) this.tileEntity.getMaxSteam())),
			Color.GREY);
	drawRectangle(k + 27, l + 70, 15, (int) (54f * ((float) this.tileEntity
			.getWater() / (float) this.tileEntity.getMaxWater())),
			Color.BLUE);
}

public static void drawRectangle(int x0, int y0, int width, int height,
		ReadableColor c) {
	int x1 = x0 + width;
	int y1 = y0 - height;
	float f3 = (float) c.getAlpha() / 255.0F;
	float f = (float) c.getRed() / 255.0F;
	float f1 = (float) c.getGreen() / 255.0F;
	float f2 = (float) c.getBlue() / 255.0F;
	Tessellator tessellator = Tessellator.instance;
	GL11.glEnable(GL11.GL_BLEND);
	GL11.glDisable(GL11.GL_TEXTURE_2D);
	OpenGlHelper.glBlendFunc(770, 771, 1, 0);
	GL11.glColor4f(f, f1, f2, f3);
	tessellator.startDrawingQuads();
	tessellator.addVertex((double) x0, (double) y0, 0.0D);
	tessellator.addVertex((double) x1, (double) y0, 0.0D);
	tessellator.addVertex((double) x1, (double) y1, 0.0D);
	tessellator.addVertex((double) x0, (double) y1, 0.0D);
	tessellator.draw();
	GL11.glEnable(GL11.GL_TEXTURE_2D);
	GL11.glDisable(GL11.GL_BLEND);
}

public static void drawRectangle(int x0, int y0, int width, int height,
		Color c) {
	int x1 = x0 + width;
	int y1 = y0 - height;
	float f3 = (float) c.getAlpha() / 255.0F;
	float f = (float) c.getRed() / 255.0F;
	float f1 = (float) c.getGreen() / 255.0F;
	float f2 = (float) c.getBlue() / 255.0F;
	Tessellator tessellator = Tessellator.instance;
	GL11.glEnable(GL11.GL_BLEND);
	GL11.glDisable(GL11.GL_TEXTURE_2D);
	OpenGlHelper.glBlendFunc(770, 771, 1, 0);
	GL11.glColor4f(f, f1, f2, f3);
	tessellator.startDrawingQuads();
	tessellator.addVertex((double) x0, (double) y0, 0.0D);
	tessellator.addVertex((double) x1, (double) y0, 0.0D);
	tessellator.addVertex((double) x1, (double) y1, 0.0D);
	tessellator.addVertex((double) x0, (double) y1, 0.0D);
	tessellator.draw();
	GL11.glEnable(GL11.GL_TEXTURE_2D);
	GL11.glDisable(GL11.GL_BLEND);
}
}

Posted

For second question, you can implement IFluidHandler. You can use the FluidTank to support it.

Let the tileentity implement it, and when a bucket is interacting with the tileentity, let the block handle the state.

 

For first question, I think it would be related with slot, so please post your container...

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

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.