Jump to content

[1.7.10] [Solved] Trouble with directions.


Awesome_Spider
 Share

Recommended Posts

Hi everyone. For the past few weeks I have been creating a steam engine that creates rf using fuel and water. I'm using some of the vanilla furnace code but I've run into a problem I can't seem to solve.

 

In the furnace code, this line int l = MathHelper.floor_double((double) (entity.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3; determines which way the player is looking to change the direction the block is facing. That is fine, but lately I can't get it to work. This code can also come to the conclusion that the player is looking up or down when they are really not and place my block facing up or down. I don't want this to happen, I want it to only face north, east, south, or west. How would I fix this?

 

My code:

 

 

BlockSteamEngine:

public class BlockSteamEngine extends BlockContainer {
    IIcon[] icons;

    public BlockSteamEngine(Material material) {
        super(material);

        setBlockName("steamEngine");
        setBlockTextureName(Usus.MODID + ":steamEngine");
        setHardness(5F);
        setHarvestLevel("pickaxe", 0);
        setStepSound(Block.soundTypeMetal);
        setResistance(30F);
    }

    @Override
    public TileEntity createNewTileEntity(World world, int par2) {
        return new TileEntitySteamEngine();
    }

    @Override
    public void registerBlockIcons(IIconRegister reg){
        icons = new IIcon[11];

        icons[0] = reg.registerIcon(getTextureName() + "Front");
        icons[1] = reg.registerIcon(getTextureName() + "FrontWater1");
        icons[2] = reg.registerIcon(getTextureName() + "FrontWater2");
        icons[3] = reg.registerIcon(getTextureName() + "FrontWater3");
        icons[4] = reg.registerIcon(getTextureName() + "FrontWater4");
        icons[5] = reg.registerIcon(getTextureName() + "FrontActiveWater1");
        icons[6] = reg.registerIcon(getTextureName() + "FrontActiveWater2");
        icons[7] = reg.registerIcon(getTextureName() + "FrontActiveWater3");
        icons[8] = reg.registerIcon(getTextureName() + "FrontActiveWater4");
        icons[9] = reg.registerIcon(getTextureName() + "Side");
        icons[10] = reg.registerIcon(getTextureName() + "WaterPort");
    }

    @Override
    public IIcon getIcon(IBlockAccess world, int posX, int posY, int posZ, int side){
        ForgeDirection side2 = ForgeDirection.getOrientation(side);
        TileEntitySteamEngine te = (TileEntitySteamEngine) world.getTileEntity(posX, posY, posZ);
        int waterLevel = te.waterMb;
        int waterLevelScaled = te.scaleWaterForDisplay(4);
        boolean active = te.burningFuel;

        if (te.facing.equals(side2)) {
            if (waterLevelScaled == 0 && !active) {
                return icons[0];
            } else if (waterLevelScaled == 1 && !active) {
                return icons[1];
            } else if (waterLevelScaled == 2 && !active) {
                return icons[2];
            } else if (waterLevelScaled == 3 && !active) {
                return icons[3];
            } else if (waterLevelScaled == 4 && !active) {
                return icons[4];
            } else if (waterLevelScaled == 1 && active) {
                return icons[5];
            } else if (waterLevelScaled == 2 && active) {
                return icons[6];
            } else if (waterLevelScaled == 3 && active) {
                return icons[7];
            } else if (waterLevelScaled == 4 && active) {
                return icons[8];
            }
        } else if (side2.equals(ForgeDirection.NORTH)){
            return icons[9];
        } else if (side2.equals(ForgeDirection.SOUTH)){
            return icons[9];
        } else if (side2.equals(ForgeDirection.EAST)){
            return icons[9];
        } else if (side2.equals(ForgeDirection.WEST)){
            return icons[9];
        } else if (side2.equals(ForgeDirection.UP)){
            return icons[10];
        } else if (side2.equals(ForgeDirection.DOWN)){
            return icons[10];
        }

        return null;
    }

    @Override
    public IIcon getIcon(int side, int meta){
        if (side == 0){
            return icons[10];
        } else if (side == 1){
            return icons[10];
        } else if (side == 2){
            return icons[9];
        } else if (side == 3){
            return icons[0];
        } else if (side == 4){
            return icons[9];
        } else if (side == 5){
            return icons[9];
        }

        return null;
    }

    @Override
    public void onBlockPlacedBy(World world, int posX, int posY, int posZ, EntityLivingBase entity, ItemStack stack) {
        int lookDirection = MathHelper.floor_double((double) (entity.rotationYawHead * 4.0F / 360.0F) + 0.5D) & 3;

        TileEntitySteamEngine te = (TileEntitySteamEngine) world.getTileEntity(posX, posY, posZ);

        te.facing = ForgeDirection.getOrientation(lookDirection).getOpposite();
    }

    public boolean onBlockActivated(World world, int posX, int posY, int posZ, EntityPlayer player, int side, float hitX, float hitY, float hitZ){
        player.openGui(Usus.instance, 1, world, posX, posY, posZ);
        return true;
    }
}

 

TileEntitySteamEngine:

public class TileEntitySteamEngine extends TileEntity implements IEnergyProvider, IInventory {
    public ForgeDirection facing;

    int fuelSlot = 0;
    int ashSlot = 1;
    ItemStack[] slots = new ItemStack[2];

    public int waterMb = 0;
    int maxWaterMb = 2500;

    public boolean burningFuel = false;
    public ItemStack currentBurningFuel = null;
    int progress = 0;
    int burningCountdown = 0;
    int rfToGenerate = 0;

    IEnergyStorage energyStorage = new IEnergyStorage() {
        int stored = 0;
        int max = 250;

        @Override
        public int receiveEnergy(int maxReceive, boolean simulate) {
            int remainder = maxReceive;
            int accepted = 0;
            boolean energyTransferComplete = false;

            while (!energyTransferComplete){
                if (stored < max && remainder > 0){
                    remainder --;
                    if (!simulate) stored ++;
                    accepted ++;
                } else {
                    energyTransferComplete = true;
                }
            }

            return accepted;
        }

        @Override
        public int extractEnergy(int maxExtract, boolean simulate) {//TODO Clean this up for efficiency. Make a class named "TileEntityEnergyCreator" and have this code in there. Then extend that class in other tile entities and call supers.
            int remainder = maxExtract;
            int extracted = 0;
            boolean energyTransferComplete = false;

            while (!energyTransferComplete){
                if (stored > remainder && remainder > 0){
                    remainder --;
                    if (!simulate) stored --;
                    extracted ++;
                } else {
                    energyTransferComplete = true;
                }
            }

            return extracted;
        }

        @Override
        public int getEnergyStored() {
            return stored;
        }

        @Override
        public int getMaxEnergyStored() {
            return max;
        }
    };

    @Override
    public void updateEntity(){
        burningFuel = burningCountdown > 0;

        if (getStackInSlot(fuelSlot) != null)
            if (isFuel(getStackInSlot(fuelSlot)) && waterMb > 0) {
                burningCountdown = getFuelBurnTime(getStackInSlot(fuelSlot));
                rfToGenerate = getFuelRf(getStackInSlot(fuelSlot));
                currentBurningFuel = getStackInSlot(fuelSlot);
                decrStackSize(fuelSlot, 1);

                markDirty();
            }

        if (burningFuel){
            burningCountdown --;
            rfToGenerate -= energyStorage.receiveEnergy(1, false);
            waterMb --;

            markDirty();

            if (burningCountdown == 0){
                if (currentBurningFuel != null) {
                    if (doesFuelMakeAsh(currentBurningFuel)) {
                        if (getStackInSlot(ashSlot).getItem() == Usus.ash) {
                            int quantity = getStackInSlot(ashSlot).stackSize;

                            setInventorySlotContents(ashSlot, new ItemStack(Usus.ash, quantity + 1));
                        }
                    }

                    currentBurningFuel = null;
                }
            }
        }
    }

    public boolean isFuel(ItemStack stack){
        return getFuelBurnTime(stack) > 0;
    }

    public boolean doesFuelMakeAsh(ItemStack stack){
        Item item = stack.getItem();
        return item instanceof ItemTool && ((ItemTool)item).getToolMaterialName().equals("WOOD") |
                item instanceof ItemSword && ((ItemSword)item).getToolMaterialName().equals("WOOD") |
                item instanceof ItemHoe && ((ItemHoe)item).getToolMaterialName().equals("WOOD") |
                item == Items.stick |
                item == Items.coal |
                item == Item.getItemFromBlock(Blocks.sapling);
    }

    //Taken almost all from tile entity furnace class
    public int getFuelBurnTime(ItemStack stack){
        Item item = stack.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(stack);
    }

    public int getFuelRf(ItemStack stack){
        return getFuelBurnTime(stack) / 4;
    }

    public void addWaterBucketToTank(){
        int remainder = 1000;
        int added = 0;
        boolean waterTransferComplete = false;

        while (!waterTransferComplete){
            if (waterMb < maxWaterMb && remainder > 0){
                remainder --;
                waterMb --;
                added ++;
            } else {
                waterTransferComplete = true;
            }
        }
    }

    public int scaleRfForDisplay(int maxNumber){
        return energyStorage.getEnergyStored() * maxNumber / energyStorage.getMaxEnergyStored();
    }

    public int scaleWaterForDisplay(int maxNumber){
        return waterMb * maxNumber / maxWaterMb;
    }

    @SideOnly(Side.CLIENT)
    public int getCookProgressScaled(int maxNumber) {
        return progress * maxNumber / 200;
    }

    @SideOnly(Side.CLIENT)
    public int getBurnTimeRemainingScaled(int maxNumber) {
        if (this.burningCountdown == 0) {
            this.burningCountdown = 200;
        }

        return this.progress * maxNumber / this.burningCountdown;
    }

    @Override
    public void readFromNBT(NBTTagCompound nbt) {
        super.readFromNBT(nbt);
        NBTTagList listOfItems = nbt.getTagList("Items", 10);
        slots = new ItemStack[getSizeInventory()];

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

            if (b0 >= 0 && b0 < slots.length)
            {
                slots[b0] = ItemStack.loadItemStackFromNBT(slotNbt);
            }
        }

        waterMb = nbt.getInteger("WaterMb");
        progress = nbt.getInteger("BurnProgress");
        burningCountdown = nbt.getInteger("BurningTimer");
        currentBurningFuel = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("CurrentBurningFuel")); //TODO Confirm that this works.
        rfToGenerate = nbt.getInteger("RfToGenerate");

        energyStorage.receiveEnergy(nbt.getInteger("RF"), false);

        facing = ForgeDirection.getOrientation(nbt.getInteger("Facing"));
    }

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

        NBTTagList listOfItems = new NBTTagList();

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

        nbt.setTag("Items", listOfItems);

        nbt.setInteger("WaterMb", waterMb);
        nbt.setInteger("BurnProgress", progress);
        nbt.setInteger("BurningTimer", burningCountdown);
        NBTTagCompound currentBurningFuelTag = new NBTTagCompound();
        currentBurningFuel.writeToNBT(currentBurningFuelTag);
        nbt.setTag("CurrentBurningFuel", currentBurningFuelTag);
        nbt.setInteger("RfToGenerate", rfToGenerate);

        nbt.setInteger("RF", energyStorage.getEnergyStored());

        if (facing.equals(ForgeDirection.NORTH)){
            nbt.setInteger("Facing", 3);
        } else if (facing.equals(ForgeDirection.SOUTH)){
            nbt.setInteger("Facing", 4);
        } else if (facing.equals(ForgeDirection.WEST)){
            nbt.setInteger("Facing", 5);
        } else if (facing.equals(ForgeDirection.EAST)){
            nbt.setInteger("Facing", 6);
        }
    }

    @Override
    public int extractEnergy(ForgeDirection from, int maxExtract, boolean simulate) {
        if (from != ForgeDirection.UP){
            return energyStorage.extractEnergy(maxExtract, simulate);
        } else {
            return 0;
        }
    }

    @Override
    public int getEnergyStored(ForgeDirection from) {
        return energyStorage.getEnergyStored();
    }

    @Override
    public int getMaxEnergyStored(ForgeDirection from) {
        return energyStorage.getMaxEnergyStored();
    }

    @Override
    public boolean canConnectEnergy(ForgeDirection from) {
        return from != ForgeDirection.UP;
    }

    @Override
    public int getSizeInventory() {
        return 2;
    }

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

    @Override
    public ItemStack decrStackSize(int slot, int amount) {
        if (slots[slot].stackSize - amount > 0) {
            slots[slot].stackSize -= amount;
        } else {
            slots[slot] = null;
        }

        return slots[slot];
    }

    @Override
    public ItemStack getStackInSlotOnClosing(int slot) {
        return slots[slot];
    }

    @Override
    public void setInventorySlotContents(int slot, ItemStack stack) {
        slots[slot] = stack;
    }

    @Override
    public String getInventoryName() {
        return "inv.steamEngine.name"; //TODO Confirm that I'm doing this right. Also, if I am, add this to the lang file.
    }

    @Override
    public boolean hasCustomInventoryName() {
        return true;
    }

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

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

    @Override
    public void openInventory() {}

    @Override
    public void closeInventory() {}

    @Override
    public boolean isItemValidForSlot(int slot, ItemStack stack) {
        if (slot == fuelSlot){
            return isFuel(stack);
        } else if (slot == ashSlot){
           return stack.getItem() == Usus.ash;
        } else {
            return true;
        }
    }
}

 

 

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
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.

 Share



×
×
  • Create New...

Important Information

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