<commit_before>package codechicken.microblock

import net.minecraft.item.Item
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
import ItemMicroPart._
import net.minecraftforge.client.IItemRenderer
import net.minecraftforge.client.IItemRenderer.ItemRenderType
import net.minecraftforge.client.IItemRenderer.ItemRendererHelper
import org.lwjgl.opengl.GL11
import codechicken.microblock.MicroMaterialRegistry.IMicroMaterial
import net.minecraft.util.StatCollector
import java.util.List
import net.minecraft.creativetab.CreativeTabs
import codechicken.microblock.MicroblockClassRegistry._
import codechicken.lib.render.CCRenderState
import net.minecraft.client.renderer.texture.IconRegister
import codechicken.microblock.handler.MicroblockProxy
import net.minecraft.util.MovingObjectPosition
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.world.World
import codechicken.lib.raytracer.RayTracer
import net.minecraft.util.EnumMovingObjectType
import codechicken.lib.vec.Vector3
import codechicken.lib.render.TextureUtils

class ItemMicroPart(id:Int) extends Item(id)
{
    setHasSubtypes(true)
    
    override def getItemDisplayName(stack:ItemStack):String =
    {
        val material = getMaterial(stack)
        val mcrClass = MicroblockClassRegistry.getMicroClass(stack.getItemDamage)
        val size = stack.getItemDamage&0xFF
        if(material == null || mcrClass == null)
            return "Unnamed"
        
        return material.getLocalizedName+" "+mcrClass.getDisplayName(size)
    }
    
    override def getSubItems(id:Int, tab:CreativeTabs, list$:List[_])
    {
        val list = list$.asInstanceOf[List[ItemStack]]
        for(classId <- 0 until classes.length)
        {
            val mcrClass = classes(classId)
            if(mcrClass != null)
                for(size <- Seq(1, 2, 4))
                    MicroMaterialRegistry.getIdMap.foreach(e => list.add(create(classId<<8|size, e._1)))
        }
    }
    
    override def registerIcons(register:IconRegister){}
    
    override def onItemUse(item:ItemStack, player:EntityPlayer, world:World, x:Int, y:Int, z:Int, s:Int, hitX:Float, hitY:Float, hitZ:Float):Boolean =
    {
        val material = getMaterialID(item)
        val mcrClass = MicroblockClassRegistry.getMicroClass(item.getItemDamage)
        val size = item.getItemDamage&0xFF
        if(material < 0 || mcrClass == null)
            return false
            
        val hit = RayTracer.retraceBlock(world, player, x, y, z)
        if(hit != null && hit.typeOfHit == EnumMovingObjectType.TILE)
        {
            val placement = MicroblockPlacement(world, player, hit, size, material, !player.capabilities.isCreativeMode, mcrClass.placementProperties)
            if(placement == null)
                return false
            
            if(!world.isRemote)
            {
                placement.place(world, player, item)
                if(!player.capabilities.isCreativeMode)
                    placement.consume(world, player, item)
                
                val sound = MicroMaterialRegistry.getMaterial(material).getSound
                if(sound != null)
                    world.playSoundEffect(placement.pos.x + 0.5D, placement.pos.y + 0.5D, placement.pos.z + 0.5D, sound.getPlaceSound, (sound.getVolume + 1.0F) / 2.0F, sound.getPitch * 0.8F)
            }
            
            return true
        }
        
        return false
    }
}

object ItemMicroPart
{
    def checkTagCompound(stack:ItemStack)
    {
        if(!stack.hasTagCompound())
            stack.setTagCompound(new NBTTagCompound("tag"))
    }
    
    def create(damage:Int, material:Int):ItemStack = create(damage, MicroMaterialRegistry.materialName(material))
    
    def create(damage:Int, material:String):ItemStack = create(1, damage, material)
    
    def create(amount:Int, damage:Int, material:String):ItemStack = 
    {
        val stack = new ItemStack(MicroblockProxy.itemMicro, amount, damage)
        checkTagCompound(stack)
        stack.getTagCompound().setString("mat", material)
        return stack
    }
    
    def getMaterial(stack:ItemStack):IMicroMaterial =
    {
        checkTagCompound(stack)
        if(!stack.getTagCompound().hasKey("mat"))
            return null;
        
        return MicroMaterialRegistry.getMaterial(stack.getTagCompound().getString("mat"))
    }
    
    def getMaterialID(stack:ItemStack):Int =
    {
        checkTagCompound(stack)
        if(!stack.getTagCompound().hasKey("mat"))
            return -1;
        
        return MicroMaterialRegistry.materialID(stack.getTagCompound().getString("mat"))
    }
}

object ItemMicroPartRenderer extends IItemRenderer
{
    def handleRenderType(item:ItemStack, t:ItemRenderType) = true
    
    def shouldUseRenderHelper(t:ItemRenderType, item:ItemStack, helper:ItemRendererHelper) = true
    
    def renderItem(t:ItemRenderType, item:ItemStack, data:Object*)
    {
        GL11.glPushMatrix()
        if(t == ItemRenderType.ENTITY)
            GL11.glScaled(0.5, 0.5, 0.5)
        if(t == ItemRenderType.INVENTORY || t == ItemRenderType.ENTITY)
            GL11.glTranslatef(-0.5F, -0.5F, -0.5F)
        
        val material = getMaterial(item)
        val mcrClass = MicroblockClassRegistry.getMicroClass(item.getItemDamage)
        val size = item.getItemDamage&0xFF
        if(material == null || mcrClass == null)
            return
        
        CCRenderState.reset()
        TextureUtils.bindAtlas(0);
        CCRenderState.useNormals(true)
        CCRenderState.useModelColours(true)<commit_msg>Fixed GL stack overflow when rendering invalid microblock items.<commit_after>package codechicken.microblock

import net.minecraft.item.Item
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
import ItemMicroPart._
import net.minecraftforge.client.IItemRenderer
import net.minecraftforge.client.IItemRenderer.ItemRenderType
import net.minecraftforge.client.IItemRenderer.ItemRendererHelper
import org.lwjgl.opengl.GL11
import codechicken.microblock.MicroMaterialRegistry.IMicroMaterial
import net.minecraft.util.StatCollector
import java.util.List
import net.minecraft.creativetab.CreativeTabs
import codechicken.microblock.MicroblockClassRegistry._
import codechicken.lib.render.CCRenderState
import net.minecraft.client.renderer.texture.IconRegister
import codechicken.microblock.handler.MicroblockProxy
import net.minecraft.util.MovingObjectPosition
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.world.World
import codechicken.lib.raytracer.RayTracer
import net.minecraft.util.EnumMovingObjectType
import codechicken.lib.vec.Vector3
import codechicken.lib.render.TextureUtils

class ItemMicroPart(id:Int) extends Item(id)
{
    setHasSubtypes(true)
    
    override def getItemDisplayName(stack:ItemStack):String =
    {
        val material = getMaterial(stack)
        val mcrClass = MicroblockClassRegistry.getMicroClass(stack.getItemDamage)
        val size = stack.getItemDamage&0xFF
        if(material == null || mcrClass == null)
            return "Unnamed"
        
        return material.getLocalizedName+" "+mcrClass.getDisplayName(size)
    }
    
    override def getSubItems(id:Int, tab:CreativeTabs, list$:List[_])
    {
        val list = list$.asInstanceOf[List[ItemStack]]
        for(classId <- 0 until classes.length)
        {
            val mcrClass = classes(classId)
            if(mcrClass != null)
                for(size <- Seq(1, 2, 4))
                    MicroMaterialRegistry.getIdMap.foreach(e => list.add(create(classId<<8|size, e._1)))
        }
    }
    
    override def registerIcons(register:IconRegister){}
    
    override def onItemUse(item:ItemStack, player:EntityPlayer, world:World, x:Int, y:Int, z:Int, s:Int, hitX:Float, hitY:Float, hitZ:Float):Boolean =
    {
        val material = getMaterialID(item)
        val mcrClass = MicroblockClassRegistry.getMicroClass(item.getItemDamage)
        val size = item.getItemDamage&0xFF
        if(material < 0 || mcrClass == null)
            return false
            
        val hit = RayTracer.retraceBlock(world, player, x, y, z)
        if(hit != null && hit.typeOfHit == EnumMovingObjectType.TILE)
        {
            val placement = MicroblockPlacement(world, player, hit, size, material, !player.capabilities.isCreativeMode, mcrClass.placementProperties)
            if(placement == null)
                return false
            
            if(!world.isRemote)
            {
                placement.place(world, player, item)
                if(!player.capabilities.isCreativeMode)
                    placement.consume(world, player, item)
                
                val sound = MicroMaterialRegistry.getMaterial(material).getSound
                if(sound != null)
                    world.playSoundEffect(placement.pos.x + 0.5D, placement.pos.y + 0.5D, placement.pos.z + 0.5D, sound.getPlaceSound, (sound.getVolume + 1.0F) / 2.0F, sound.getPitch * 0.8F)
            }
            
            return true
        }
        
        return false
    }
}

object ItemMicroPart
{
    def checkTagCompound(stack:ItemStack)
    {
        if(!stack.hasTagCompound())
            stack.setTagCompound(new NBTTagCompound("tag"))
    }
    
    def create(damage:Int, material:Int):ItemStack = create(damage, MicroMaterialRegistry.materialName(material))
    
    def create(damage:Int, material:String):ItemStack = create(1, damage, material)
    
    def create(amount:Int, damage:Int, material:String):ItemStack = 
    {
        val stack = new ItemStack(MicroblockProxy.itemMicro, amount, damage)
        checkTagCompound(stack)
        stack.getTagCompound().setString("mat", material)
        return stack
    }
    
    def getMaterial(stack:ItemStack):IMicroMaterial =
    {
        checkTagCompound(stack)
        if(!stack.getTagCompound().hasKey("mat"))
            return null;
        
        return MicroMaterialRegistry.getMaterial(stack.getTagCompound().getString("mat"))
    }
    
    def getMaterialID(stack:ItemStack):Int =
    {
        checkTagCompound(stack)
        if(!stack.getTagCompound().hasKey("mat"))
            return -1;
        
        return MicroMaterialRegistry.materialID(stack.getTagCompound().getString("mat"))
    }
}

object ItemMicroPartRenderer extends IItemRenderer
{
    def handleRenderType(item:ItemStack, t:ItemRenderType) = true
    
    def shouldUseRenderHelper(t:ItemRenderType, item:ItemStack, helper:ItemRendererHelper) = true
    
    def renderItem(t:ItemRenderType, item:ItemStack, data:Object*)
    {
        val material = getMaterial(item)
        val mcrClass = MicroblockClassRegistry.getMicroClass(item.getItemDamage)
        val size = item.getItemDamage&0xFF
        if(material == null || mcrClass == null)
            return
        
        GL11.glPushMatrix()
        if(t == ItemRenderType.ENTITY)
            GL11.glScaled(0.5, 0.5, 0.5)
        if(t == ItemRenderType.INVENTORY || t == ItemRenderType.ENTITY)
            GL11.glTranslatef(-0.5F, -0.5F, -0.5F)
        
        CCRenderState.reset()
        TextureUtils.bindAtlas(0);
        CCRenderState.useNormals(true)
        CCRenderState.useModelColours(true)
{'fraction_non_alphanumeric': 0.09835628609506886, 'fraction_numerical': 0.008174144824522434, 'mean_word_length': 2.8259687287559485, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '401240', 'n_tokens_mistral': 3380, 'n_tokens_neox': 3318, 'n_words': 563}