Jump to content

A couple of Tile Entity + Crafting things...


Flenix

Recommended Posts

Hey guys, got 3 questions all related to the same block so I thought I'd put them in the same thread for convenience!

 

Alright,

#1

How would I go about making my own crafting table? I know that's a big topic really so just any links to a tutorial or anything? (I want it to keep the items inside when you exit the inventory though, they shouldn't fall out)

 

#2

I want a certain slot to accept only one block as an input, but also work as an output at the same time. How can I go about doing this?

 

#3

When that certain block is in that slot, I want to change the model. I understand I need to use Switch statements(?), but other than that I have no idea what to do?

 

Here's the code I have so far:

 

[spoiler=Block]

package co.uk.silvania.roads.tileentities.blocks;

import java.util.Random;

import co.uk.silvania.roads.Roads;
import co.uk.silvania.roads.tileentities.entities.TileEntityRoadPainterEntity;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;

public class TileEntityRoadPainterBlock extends BlockContainer {

public TileEntityRoadPainterBlock(int id) {
	super(id, Material.iron);
	this.setCreativeTab(Roads.tabRoads);
	this.setHardness(1.0F);
}

@Override
public TileEntity createNewTileEntity(World world) {
	return new TileEntityRoadPainterEntity();
}

@Override
    public boolean onBlockActivated(World world, int x, int y, int z,
                    EntityPlayer player, int i, float j, float k, float l) {
            TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
            if (tileEntity == null || player.isSneaking()) {
                    return false;
            }
    player.openGui(Roads.instance, 0, world, x, y, z);
            return true;
    }

    @Override
    public void breakBlock(World world, int x, int y, int z, int par5, int par6) {
            dropItems(world, x, y, z);
            super.breakBlock(world, x, y, z, par5, par6);
    }

@Override
public int getRenderType() {
	return -1;
}

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

public boolean renderAsNormalBlock() {
	return false;
}

@Override
public void onBlockPlacedBy(World par1World, int par2, int par3, int par4,
		EntityLiving par5EntityLiving, ItemStack par6ItemStack) {
	int var6 = MathHelper
			.floor_double(par5EntityLiving.rotationYaw * 4.0F / 360.0F + 0.5D) & 3;

	if (var6 == 0) {
		par1World.setBlockMetadataWithNotify(par2, par3, par4, 2, 0);
	}

	if (var6 == 1) {
		par1World.setBlockMetadataWithNotify(par2, par3, par4, 5, 0);
	}

	if (var6 == 2) {
		par1World.setBlockMetadataWithNotify(par2, par3, par4, 3, 0);
	}

	if (var6 == 3) {
		par1World.setBlockMetadataWithNotify(par2, par3, par4, 4, 0);
	}
}

private void dropItems(World world, int x, int y, int z){
	Random rand = new Random();

	TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
	if (!(tileEntity instanceof IInventory)) {
		return;
	}
	IInventory inventory = (IInventory) tileEntity;

	for (int i = 0; i < inventory.getSizeInventory(); i++) {
		ItemStack item = inventory.getStackInSlot(i);

		if (item != null && item.stackSize > 0) {
			float rx = rand.nextFloat() * 0.8F + 0.1F;
			float ry = rand.nextFloat() * 0.8F + 0.1F;
			float rz = rand.nextFloat() * 0.8F + 0.1F;

			EntityItem entityItem = new EntityItem(world, x + rx, y + ry, z + rz, new ItemStack(item.itemID, item.stackSize, item.getItemDamage()));

			if (item.hasTagCompound()) {
				entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.getTagCompound().copy());
			}

			float factor = 0.05F;
			entityItem.motionX = rand.nextGaussian() * factor;
			entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
			entityItem.motionZ = rand.nextGaussian() * factor;
			world.spawnEntityInWorld(entityItem);
			item.stackSize = 0;
		}
	}
}
      

public void registerIcons(IconRegister icon) {
	this.blockIcon = icon.registerIcon("Roads:CementBlock");
}

}

 

 

[spoiler=GUI]

package co.uk.silvania.roads.tileentities;

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

import org.lwjgl.opengl.GL11;

import co.uk.silvania.roads.tileentities.entities.TileEntityRoadPainterEntity;

public class GuiRoadPainter extends GuiContainer {

        public GuiRoadPainter (InventoryPlayer inventoryPlayer, TileEntityRoadPainterEntity tileEntity) {
                                super(new ContainerRoadPainter(inventoryPlayer, tileEntity));
        }
        
        /** The X size of the inventory window in pixels. */
        protected int xSize = 176;

        /** The Y size of the inventory window in pixels. */
        protected int ySize = 204;

        @Override
        protected void drawGuiContainerForegroundLayer(int param1, int param2) {
                fontRenderer.drawString("Road Painter", 8, -12, 4210752);
                fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 8, ySize - 113, 4210752);
        }

        @Override
        protected void drawGuiContainerBackgroundLayer(float par1, int par2,
                        int par3) {
                GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
                this.mc.renderEngine.bindTexture("/mods/Roads/textures/gui/RoadPainterGui.png");
                int x = (width - xSize) / 2;
                int y = (height - ySize) / 2;
                this.drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
        }
}

 

 

[spoiler=Tile Entity]

package co.uk.silvania.roads.tileentities.entities;

import java.util.Iterator;
import java.util.List;

import co.uk.silvania.roads.Roads;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ContainerChest;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryLargeChest;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.util.AxisAlignedBB;

public class TileEntityRoadPainterEntity extends TileEntityChest implements IInventory
{
    private ItemStack[] roadPainterContents = new ItemStack[25];

    public int numUsingPlayers;

    private int ticksSinceSync;
    private String field_94045_s;

    public int getSizeInventory()
    {
        return 25;
    }

    public ItemStack getStackInSlot(int par1)
    {
        return this.roadPainterContents[par1];
    }

    public ItemStack decrStackSize(int par1, int par2)
    {
        if (this.roadPainterContents[par1] != null)
        {
            ItemStack var3;

            if (this.roadPainterContents[par1].stackSize <= par2)
            {
                var3 = this.roadPainterContents[par1];
                this.roadPainterContents[par1] = null;
                this.onInventoryChanged();
                return var3;
            }
            else
            {
                var3 = this.roadPainterContents[par1].splitStack(par2);

                if (this.roadPainterContents[par1].stackSize == 0)
                {
                    this.roadPainterContents[par1] = null;
                }

                this.onInventoryChanged();
                return var3;
            }
        }
        else
        {
            return null;
        }
    }

    public ItemStack getStackInSlotOnClosing(int par1)
    {
        if (this.roadPainterContents[par1] != null)
        {
            ItemStack var2 = this.roadPainterContents[par1];
            this.roadPainterContents[par1] = null;
            return var2;
        }
        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.roadPainterContents[par1] = par2ItemStack;

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

        this.onInventoryChanged();
    }

    public String getInvName()
    {
        return "Road Painter";
    }

    public void readFromNBT(NBTTagCompound par1NBTTagCompound)
    {
        super.readFromNBT(par1NBTTagCompound);
        NBTTagList nbttaglist = par1NBTTagCompound.getTagList("Items");
        this.roadPainterContents = new ItemStack[this.getSizeInventory()];

        if (par1NBTTagCompound.hasKey("CustomName"))
        {
            this.field_94045_s = par1NBTTagCompound.getString("CustomName");
        }

        for (int i = 0; i < nbttaglist.tagCount(); ++i)
        {
            NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttaglist.tagAt(i);
            int j = nbttagcompound1.getByte("Slot") & 255;

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


    public void writeToNBT(NBTTagCompound par1NBTTagCompound)
    {
        super.writeToNBT(par1NBTTagCompound);
        NBTTagList nbttaglist = new NBTTagList();

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

        par1NBTTagCompound.setTag("Items", nbttaglist);

        if (this.isInvNameLocalized())
        {
            par1NBTTagCompound.setString("CustomName", this.field_94045_s);
        }
    }

    public int getInventoryStackLimit()
    {
        return 64;
    }

    public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
    {
        return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : par1EntityPlayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
    }

    public boolean receiveClientEvent(int par1, int par2)
    {
        if (par1 == 1)
        {
            this.numUsingPlayers = par2;
            return true;
        }
        else
        {
            return super.receiveClientEvent(par1, par2);
        }
    }
    
    public void openChest()
    {
        ++this.numUsingPlayers;
        this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, Roads.roadPainter.blockID, 1, this.numUsingPlayers);
    }

    public void closeChest()
    {
        --this.numUsingPlayers;
        this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, Roads.roadPainter.blockID, 1, this.numUsingPlayers);
    }

    public void invalidate()
    {
        super.invalidate();
        this.updateContainingBlockInfo();
        this.checkForAdjacentChests();
    }
}

 

 

 

[spoiler=Renderer]

package co.uk.silvania.roads.tileentities.renderers;

import org.lwjgl.opengl.GL11;


import co.uk.silvania.roads.Roads;
import co.uk.silvania.roads.client.models.LightBollardModel;
import co.uk.silvania.roads.client.models.RoadPainterEmptyModel;
import co.uk.silvania.roads.client.models.RoadPainterFullModel;
import co.uk.silvania.roads.client.models.TrafficLightModel;

import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;


public class TileEntityRoadPainterRenderer extends TileEntitySpecialRenderer {

private final RoadPainterEmptyModel modelempty;
private final RoadPainterFullModel modelfull;

public TileEntityRoadPainterRenderer() {
	this.modelempty = new RoadPainterEmptyModel();
	this.modelfull = new RoadPainterFullModel();
}

@Override
public void renderTileEntityAt(TileEntity te, double x, double y, double z, float scale) {
	int i = te.getBlockMetadata();
	int meta = 180;

	if (i == 3) {
		meta = 0;
	}

	if (i == 5) {
		meta = 90;
	}

	if (i == 2) {
		meta = 180;
	}

	if (i == 4) {
		meta = 270;
	}

	bindTextureByName("/mods/Roads/textures/blocks/RoadPainter.png");
	GL11.glPushMatrix();
	GL11.glTranslatef((float) x + 0.5F, (float) y + 1.5F, (float) z + 0.5F);
	GL11.glRotatef(meta, 0.0F, 1.0F, 0.0F);
	//GL11.glRotatef(((TileEntityBarrierEntity)tile).getRotationPivot()), 0.0F, 1.0F, 0.0F);
	GL11.glScalef(1.0F, -1F, -1F);
	this.modelempty.render((Entity)null, 0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F);
	GL11.glPopMatrix();
}

private void adjustLightFixture(World world, int i, int j, int k, Block block) {
	Tessellator tess = Tessellator.instance;
	float brightness = block.getBlockBrightness(world, i, j, k);
	int skyLight = world.getLightBrightnessForSkyBlocks(i, j, k, 0);
	int modulousModifier = skyLight % 65536;
	int divModifier = skyLight / 65536;
	tess.setColorOpaque_F(brightness, brightness, brightness);
	OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit,  (float) modulousModifier,  divModifier);
}
}

 

 

 

width=463 height=200

http://s13.postimg.org/z9mlly2av/siglogo.png[/img]

My mods (Links coming soon)

Cities | Roads | Remula | SilvaniaMod | MoreStats

Link to comment
Share on other sites

(1) This and this look helpful. I also have a TE/container/recipe system in my mod, which is open-source, and the Github link is in my sig.

 

(2) Create a custom slot and override isItemValid. Then just set the stack, and transcend that function.

 

(3) I believe you get a TE arg passed to your renderer or your model, which you can then use to alter the rendering code.

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

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

    • WHO CAN HELP ME GET MY BITC0IN BACK WITH MIGHTY HACKER REC0VERY AND HIRE THE BEST HACKER FOR ALL HACKER SERVICES AT MIGHTY HACKER RECOVERY I can't believe the turn of events that led me to reclaiming my lost Bitc0in! A few months ago, I fell victim to a phishing scam and lost a significant amount of money. I was devastated and hopeless, believing I'd never see my money again. After some research, I came across Mighty Hacker Rec0very. Skeptical but desperate, I decided to contact them. I began chatting with one of their hackers and was immediately impressed by their expertise and professionalism. They patiently walked me through the recovery process, reassuring me along the way. They worked diligently on my case for several weeks, using their expertise to locate my lost funds. It was intense, but I felt optimistic for the first time since the scam. The communication was clear, and I received regular updates, which reduced my anxiety. Finally, I received the news that my Bitc0in had been successfully recovered! I couldn't believe it—I was ecstatic! I'm extremely grateful to the hacker who assisted me. Mighty Hacker Rec0very transformed a nightmare into a triumph, and I'm grateful beyond words. If anyone is in a similar situation, I highly recommend contacting them! WH@TS@PP: +1 8  45 6 99 50  44 EM@IL: support @ mightyhackerrecovery . com FB: mighty hacker recovery
    • A friend found this code, but I don't know where. It seems to be very outdated, maybe from 1.12? and so uses TextureManager$loadTexture and TextureManager$deleteTexture which both don't seem to exist anymore. It also uses Minecraft.getMinecraft().mcDataDir.getCanonicalPath() which I replaced with the resource location of my texture .getPath()? Not sure if thats entirely correct. String textureName = "entitytest.png"; File textureFile = null; try { textureFile = new File(Minecraft.getMinecraft().mcDataDir.getCanonicalPath(), textureName); } catch (Exception ex) { } if (textureFile != null && textureFile.exists()) { ResourceLocation MODEL_TEXTURE = Resources.OTHER_TESTMODEL_CUSTOM; TextureManager texturemanager = Minecraft.getMinecraft().getTextureManager(); texturemanager.deleteTexture(MODEL_TEXTURE); Object object = new ThreadDownloadImageData(textureFile, null, MODEL_TEXTURE, new ImageBufferDownload()); texturemanager.loadTexture(MODEL_TEXTURE, (ITextureObject)object); return true; } else { return false; }   Then I've been trying to go through the source code of the reload resource packs from minecraft, to see if I can "cache" some data and simply reload some textures and swap them out, but I can't seem to figure out where exactly its "loading" the texture files and such. Minecraft$reloadResourcePacks(bool) seems to be mainly controlling the loading screen, and using this.resourcePackRepository.reload(); which is PackRepository$reload(), but that function seems to be using this absolute confusion of a line List<String> list = this.selected.stream().map(Pack::getId).collect(ImmutableList.toImmutableList()); and then this.discoverAvailable() and this.rebuildSelected. The rebuild selected seemed promising, but it seems to just be going through each pack and doing this to them? pack.getDefaultPosition().insert(list, pack, Functions.identity(), false); e.g. putting them into a list of packs and returning that into this.selected? Where do the textures actually get baked/loaded/whatever? Any info on how Minecraft reloads resource packs or how the texture manager works would be appreciated!
    • This might be a long shot , but do you remember how you fixed that?
    • Yeah, I'll start with the ones I added last night.  Wasn't crashing until today and wasn't crashing at all yesterday (+past few days since removing Cupboard), so deductive reasoning says it's likeliest to be one of the new ones.  A few horse armor mods and a corn-based add-on to Farmer's Delight, the latter which I hope to keep - I could do without the horse armor mods if necessary.  Let me try a few things and we'll see. 
  • Topics

×
×
  • Create New...

Important Information

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