Jump to content

Universal Electricity getJoules() function returns the default value for joules


felesmortis

Recommended Posts

I'm creating a Universal Electricity compatible automatic miner.

I am using minecraft 1.5.2 with forge 7.8.1.737 and the UE API 1.4.0.89.

I know that Complex Machines already does this, this is just for fun. Also, I am neither a Java nor a Forge expert, but I know how to use google. Everything I'm asking I've researched.

I'm trying to get my autominer to require electricity to run. Normally, I would work on it on my own, but it has been four hours, so I think it is time to look for help.

 

I have a Tile Entity extending TileEntityElectricityStorage and implementing IInventory

package felesmortis.autominer;

import java.util.ArrayList;
import java.util.Random;

import universalelectricity.core.electricity.ElectricityPack;
import universalelectricity.prefab.tile.TileEntityElectrical;
import universalelectricity.prefab.tile.TileEntityElectricityStorage;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.oredict.OreDictionary;

public class TileEntityMiner extends TileEntityElectricityStorage implements IInventory {
private int[] timeToMake = {-1,-1,-1,-1,-1,-1};
private int[] fullTime = {-1,-1,-1,-1,-1,-1};
///public BlockMiner block;
private ItemStack[] inv;
private int ticks = 0;
private static Random r = new Random();
private String[] oresFull = OreDictionary.getOreNames();
private String[] ores = getOres();
private double joules = 0;
public int scaled = 1;
public TileEntityMiner(){
	inv = new ItemStack[18];
}
private String[] getOres() {
	ArrayList<String> ores = new ArrayList<String>();
	for(int i = 0; i < oresFull.length; i++) {
		if(oresFull[i].contains("ore")) {
			ores.add(oresFull[i]);
		}
	}
	return ores.toArray(new String[ores.size()]);
}
public int getProgressScaled(int scale, int pos){
	return scale - (timeToMake[pos] * scale / fullTime[pos]);
}
public int getJoulesScaled(int scale) {
	return (int)getJoules() * scale / (int)getMaxJoules();
}
public ItemStack isValid(int pos) {
	ItemStack is = null;
	boolean flag = true;
	int pos2 = pos + 6;
	if(inv[pos] == null || !inv[pos].getItem().equals(Autominer.itemOreUpgrade)){
		flag = false;
	}
	else {
		is = ItemOreUpgrade.getStackFromDamage(inv[pos].getItemDamage());
		is.stackSize = 1;
	}
	if(flag && inv[pos2] != null) 
		if(!inv[pos2].isItemEqual(is))
			flag = false;
	if(flag && inv[pos2] != null && inv[pos2].stackSize == 64)
		flag = false;
	if(!flag) {
		is = null;
		fullTime[pos] = -1;
		timeToMake[pos] = -1;
	}
	return is;
}
public boolean isSlotToMine(int pos) {
	return timeToMake[pos] == 0;
}
public void addToMiner(ItemStack is, int pos) {
	int j = pos + 6;
	is.stackSize = 1;
	if(inv[j] == null){
		inv[j] = is.copy();
	} else if(inv[j].isItemEqual(is) && inv[j].stackSize < 64) {
		inv[j].stackSize += 1;
	}
	timeToMake[pos] = getItemMineTime(is);
}
public int getItemMineTime(ItemStack is) {
	int i = -1;
	String name = is.getItemName().toLowerCase();
	if(name.contains("stone")) {
		i = TimesMiner.STONE.time;
	} else if(name.contains("iron"))
		i = TimesMiner.IRON.time;
	else if(name.contains("redstone"))
		i = TimesMiner.REDSTONE.time;
	else if(name.contains("gold"))
		i = TimesMiner.GOLD.time;
	else if(name.contains("emerald"))
		i = TimesMiner.EMERALD.time;
	else if(name.contains("lapis"))
		i = TimesMiner.LAPIS.time;
	else if(name.contains("diamond"))
		i = TimesMiner.DIAMOND.time;
	else if(name.contains("quartz"))
		i = TimesMiner.QUARTZ.time;
	else if(name.contains("certus"))
		i = TimesMiner.CERTUS.time;
	else if(name.contains("dirt"))
		i = TimesMiner.DIRT.time;
	else if(name.contains("obsidian"))
		i = TimesMiner.OBSIDIAN.time;
	else if(name.contains("tin"))
		i = TimesMiner.TIN.time;
	else if(name.contains("copper"))
		i = TimesMiner.COPPER.time;
	else if(name.contains("osmium"))
		i = TimesMiner.OSMIUM.time;
	else if(name.contains("coal"))
		i = TimesMiner.COAL.time;
	else
		i = TimesMiner.UNDEF.time;
	return i;
}
public void updateEntity() {
	super.updateEntity();
	scaled = getJoulesScaled(52);
	ItemStack stack;
	if(ores != null)
		for (int i = 0; i < 6; i++) {
			stack = isValid(i);
			if(stack != null) {
				if(fullTime[i] == -1)
					fullTime[i] = getItemMineTime(stack);
				if(timeToMake[i] == -1)
					timeToMake[i] = fullTime[i];
				else
					timeToMake[i] -= 1;
				if(isSlotToMine(i)) {
					addToMiner(stack, i);
				}
			}
		}
}
@Override
public int getSizeInventory() {
	return inv.length;
}

@Override
public ItemStack getStackInSlot(int slot) {
	return inv[slot];
}

@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
	inv[slot] = stack;
	if (stack != null && stack.stackSize > getInventoryStackLimit()) {
		stack.stackSize = getInventoryStackLimit();
	}               
}

@Override
public ItemStack decrStackSize(int slot, int amt) {
	ItemStack stack = getStackInSlot(slot);
	if (stack != null) {
		if (stack.stackSize <= amt) {
			setInventorySlotContents(slot, null);
		} else {
			stack = stack.splitStack(amt);
			if (stack.stackSize == 0) {
				setInventorySlotContents(slot, null);
			}
		}
	}
	return stack;
}

@Override
public ItemStack getStackInSlotOnClosing(int slot) {
	ItemStack stack = getStackInSlot(slot);
	if (stack != null) {
		setInventorySlotContents(slot, null);
	}
	return stack;
}

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

@Override
public boolean isUseableByPlayer(EntityPlayer player) {
	return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this &&
			player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
}

@Override
public void openChest() {}

@Override
public void closeChest() {}

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

	NBTTagList tagList = tagCompound.getTagList("Inventory");
	for (int i = 0; i < tagList.tagCount(); i++) {
		NBTTagCompound tag = (NBTTagCompound) tagList.tagAt(i);
		byte slot = tag.getByte("Slot");
		if (slot >= 0 && slot < inv.length) {
			inv[slot] = ItemStack.loadItemStackFromNBT(tag);
		}
	}
}

@Override
public void writeToNBT(NBTTagCompound tagCompound) {
	super.writeToNBT(tagCompound);

	NBTTagList itemList = new NBTTagList();
	for (int i = 0; i < inv.length; i++) {
		ItemStack stack = inv[i];
		if (stack != null) {
			NBTTagCompound tag = new NBTTagCompound();
			tag.setByte("Slot", (byte) i);
			stack.writeToNBT(tag);
			itemList.appendTag(tag);
		}
	}
	tagCompound.setTag("Inventory", itemList);
}

@Override
public String getInvName() {
	return "tco.tileentitytiny";
}

@Override
public boolean isInvNameLocalized() {
	// TODO Auto-generated method stub
	return false;
}

@Override
public boolean isStackValidForSlot(int i, ItemStack itemstack) {
	// TODO Auto-generated method stub
	return false;
}
@Override
public boolean canConnect(ForgeDirection direction) {
	// TODO fix this
	return direction.equals(ForgeDirection.NORTH);
}
@Override
public double getMaxJoules() {
	return 50000;
}
@Override
public ElectricityPack getRequest()
{
	return new ElectricityPack((float) Math.min((this.getMaxJoules() - this.getJoules()), 5000),480);
}
@Override
public double getVoltage()
{
	return 480;
}
}

I just want this box to store energy and use it when it creates ores. However, when I try to call getJoules(), occasionally it returns the value it was initialized. When I called it from the updateEntity() function, it alternates between 0 and the normal value. When I call it from my gui class, or call a function from my tile entity class from my gui class, it always returns 0.

package felesmortis.autominer;

import java.io.File;
import java.io.IOException;

import org.lwjgl.opengl.GL11;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.StatCollector;

public class GuiMiner extends GuiContainer {
private TileEntityMiner minerInventory;
public GuiMiner (InventoryPlayer inventoryPlayer,
		TileEntityMiner tileEntity) {
	//the container is instanciated and passed to the superclass for handling
	super(new ContainerMiner(inventoryPlayer, tileEntity));
	this.ySize = 170;
	minerInventory = tileEntity;
}

@Override
protected void drawGuiContainerForegroundLayer(int param1, int param2) {
}

@Override
protected void drawGuiContainerBackgroundLayer(float par1, int par2,
		int par3) {

	GL11.glBindTexture(GL11.GL_TEXTURE_2D, mc.renderEngine.getTexture("/mods/autominer/textures/gui/autominer.png"));
	int x = (width - xSize) / 2;
        int y = (height - ySize) / 2;
        this.drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
        int i1;
        for (int i = 0; i < 6; i++) {
        i1 = minerInventory.getProgressScaled(14,i);
        this.drawTexturedModalRect(x + 12 + i * 18, y + 27, 181, 30, 7, i1 + 1);
        }
        i1 = minerInventory.getJoulesScaled(52);
        this.drawTexturedModalRect(x + 165, y + 16 + 52 - i1 + 1, 176, 16 + 52 - i1 + 1, 4, i1 + 1);
}
}

The miner itself works, I'm just looking for help with the getJoules() function occasionally returning the default value.

Thanks in advance.

 

--felesmortis

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

    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
    • Remove Instant Massive Structures Mod from your server     Add new crash-reports with sites like https://paste.ee/  
    • Update your drivers: https://www.amd.com/en/support/graphics/amd-radeon-r9-series/amd-radeon-r9-200-series/amd-radeon-r9-280x
  • Topics

×
×
  • Create New...

Important Information

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