diff --git a/src/main/java/com/idtech/block/QuickBlock.java b/src/main/java/com/idtech/block/QuickBlock.java old mode 100644 new mode 100755 index 44910ca..98fef61 --- a/src/main/java/com/idtech/block/QuickBlock.java +++ b/src/main/java/com/idtech/block/QuickBlock.java @@ -1,395 +1,397 @@ -package com.idtech.block; - -import java.io.File; -import java.io.IOException; -import java.lang.reflect.Field; -import java.nio.file.Paths; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Random; -import java.util.Set; - -import org.apache.commons.io.FileUtils; - -import com.idtech.BaseMod; -import com.idtech.JSONManager; -import com.idtech.PackageManager; - -import net.minecraft.block.Block; -import net.minecraft.block.material.Material; -import net.minecraft.block.state.IBlockState; -import net.minecraft.creativetab.CreativeTabs; -import net.minecraft.entity.Entity; -import net.minecraft.entity.item.EntityItem; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.Item; -import net.minecraft.item.ItemBlock; -import net.minecraft.item.ItemStack; -import net.minecraft.util.EnumParticleTypes; -import net.minecraft.util.SoundCategory; -import net.minecraft.util.SoundEvent; -import net.minecraft.util.math.AxisAlignedBB; -import net.minecraft.util.math.BlockPos; -import net.minecraft.world.IBlockAccess; -import net.minecraft.world.World; -import net.minecraftforge.fml.common.registry.GameRegistry; - -public class QuickBlock extends Block { - - - public static enum BlockType { - CUBE_ALL, HALF_SLAB; - } - - protected static final String PICKAXE = "pickaxe"; - protected static final String SHOVEL = "shovel"; - - protected static final int WOOD = 0; - protected static final int STONE = 0; - protected static final int IRON = 0; - protected static final int DIAMOND = 0; - - - - private static final Material defaultMaterial = Material.GROUND; - private static Set registry = new HashSet(); - private static Map lookup = new HashMap(); - - protected String name = "Undefined"; - protected BlockType type = BlockType.CUBE_ALL; - protected CreativeTabs tab = CreativeTabs.MISC; - protected String texture = "undefined"; - protected Material material = defaultMaterial; - protected Item itemDropped = null; - - protected World world; - protected BlockPos pos; - protected IBlockState state; - protected Entity entity; - protected Random random; - protected double tickSpeed; - - // When QuickBlock is loaded, it will scan the block directory for all sub - // classes of QuickBlock - // and create a newInstance of them. This allows us to simply use the - // initialization block to specify each parameter - // This invokes the default super constructor of QuickBlock and registers - // it. Therefore, you should almost never - // need to call the constructor for QuickBlock manually. - static { - // Loads all of the QuickBlocks that are in this package - Set classes = PackageManager.loadClassesInPackage("com.idtech.block"); - for (Class klass : classes) { - if (QuickBlock.class.isAssignableFrom(klass) && QuickBlock.class != klass) { - try { - QuickBlock block = (QuickBlock)klass.newInstance(); - lookup.put(block.name, block); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - } - - public static QuickBlock getBlock(String name){ - return lookup.get(name); - } - - /** - * Returns an immutable view of the Registry - * - * @return - */ - public static Set getRegistry() { - return Collections.unmodifiableSet(registry); - } - - /** - * Creates a new QuickBlock and adds it to the registry. (Note: You probably - * shouldn't be calling this directly) - */ - public QuickBlock() { - super(defaultMaterial); - this.registry.add(this); - - } - - - /** - * This method should be called once prior to the init method. - */ - public static void preInit() { - System.out.println("Quick Block Registry Size: " + registry.size()); - // Loops through all registered QuickBlocks and initializes them - for (QuickBlock block : registry) { - block.setUnlocalizedName(BaseMod.MODID + "_" + JSONManager.safeString(block.name)); - block.setCreativeTab(block.tab); - System.out.println("preInit: " + block.name); - - // Assigns the block material using reflection since we are avoiding - // using constructors - setBlockField(block, block.material, "blockMaterial"); - setBlockField(block, block.material.getMaterialMapColor(), "blockMapColor"); - setBlockField(block, !block.material.blocksLight(), "translucent"); - - // Register the block and an item for the block. - GameRegistry.register(block.setRegistryName(block.name)); - GameRegistry.register(new ItemBlock(block).setRegistryName(block.getRegistryName())); - - } - } - - /** - * Private helper method which allows the change of private member variables - * in the parent class - * - * @param block - * the QuickBlock we want to change - * @param value - * the value we want to be assigned - * @param fieldName - * the field we want to assign - */ - private static void setBlockField(QuickBlock block, Object value, String fieldName) { - try { - Field f = Block.class.getDeclaredField(fieldName); - f.setAccessible(true); - f.set(block, value); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * This method should be run once before the mod launches - */ - public static void init() { - for (QuickBlock block : registry) { - System.out.println("init: " + block.name); - BaseMod.proxy.registerBlockInventoryRender(block, block.name); - } - } - - @Override - public Item getItemDropped(IBlockState state, Random rand, int fortune) { - return itemDropped; - } - - protected void onRandomTick(){} - - - - /** - * Returns a random neighboring block position. - * @return a random neighboring block position. - */ - protected BlockPos findNeighborBlock(){ - int spreadX = random.nextInt(3) - 1; - int spreadY = spreadX == 0 ? random.nextInt(3) - 1 : 0 ; - int spreadZ = spreadX == 0 && spreadY == 0 ? random.nextInt(3) - 1 : 0; - BlockPos spreadPos = pos.add(spreadX, spreadY, spreadZ); - return spreadPos; - } - - protected void cloneAt(BlockPos pos){ - world.setBlockState(pos, this.getDefaultState()); - } - - - @Override - public void randomTick(World worldIn, BlockPos posIn, IBlockState stateIn, Random randomIn) { - world = worldIn; - pos = posIn; - state = stateIn; - random = randomIn; - onRandomTick(); - } - - protected void onEntityWalk(){} - - - - @Override - public void onEntityWalk(World worldIn, BlockPos posIn, Entity entityIn) { - world = worldIn; - pos = posIn; - entity = entityIn; - onEntityWalk(); - super.onEntityWalk(worldIn, pos, entityIn); - } - - - - @Override - public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, IBlockAccess worldIn, BlockPos pos) { - AxisAlignedBB box = super.getCollisionBoundingBox(blockState, worldIn, pos); - AxisAlignedBB slightlySmaller = new AxisAlignedBB(box.minX + .1, box.minY + .1, box.minZ + .1, box.maxX - .1, box.maxY - .1, box.maxZ - .1); - return slightlySmaller; - } - - - - /** - * Generates a sound at the entities current position. Each call to this - * method generates a random pitch applied to the audio. - * - * @param sound - * The sound to generate - */ - protected void playSound(SoundEvent sound) { - if(entity == null) return; - world.playSound((EntityPlayer) null, entity.posX, entity.posY, entity.posZ, sound, SoundCategory.NEUTRAL, 0.5f, - 0.4F / ((float)Math.random() * 0.4F + 0.8F)); - } - - protected void onEntityCollidedWithBlock(){ - - } - - public QuickBlock getInstance(){ - return this; - } - - @Override - public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn) { - super.onEntityCollidedWithBlock(worldIn, pos, state, entityIn); - this.world = worldIn; - this.pos = pos; - this.state = state; - this.entity = entityIn; - onEntityCollidedWithBlock(); - } - - - - @Override - public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) { - super.onBlockAdded(worldIn, pos, state); - this.world = worldIn; - int nextTick = tickSpeed > 0 ? (int)(tickSpeed*20) : 20; - world.scheduleBlockUpdate(pos, this, nextTick, 1); - } - - - @Override - public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand) { - this.world = worldIn; - this.state = state; - this.random = rand; - this.pos = pos; - tick(); - int nextTick = tickSpeed > 0 ? (int)(tickSpeed*20) : 20; - world.scheduleBlockUpdate(pos, this, nextTick, 1); - } - - protected void tick(){ - - } - - protected void spawnItem(Item toSpawn){ - world.spawnEntity(new EntityItem(world, pos.getX(), pos.getY()+1, pos.getZ(), new ItemStack(toSpawn))); - - } - - protected void spawnParticles(EnumParticleTypes type, int intensity){ - for (int i = 0; i < intensity; i++) { - double x = pos.getX(); - double y = pos.getY(); - double z = pos.getZ(); - world.setBlockState(new BlockPos(x, y, z), state); - System.out.println(x + " , " + y + ", " + z); - world.spawnParticle(type, x, y, z, 0, 0, 0); - } - } - - /** - * This method creates the JSON Files necessary for the Minecraft mod. If - * you want to use your own custom JSON files @Override this method. - */ - public void createJSONFiles() { - createModelJSON(); - createBlockStatesJSON(); - createItemJSON(); - } - - // Code below this point is used to generate JSON - - private final void createModelJSON() { - File f = Paths.get(".").resolve(JSONManager.assetsDir + "/models/block/" + JSONManager.jsonName(name) + ".json") - .toFile(); - - if (f.exists()) { - f.delete(); - } - - StringBuilder builder = new StringBuilder(); - - builder.append("{"); - builder.append("\"parent\": \"block/" + type + "\","); - - builder.append("\"textures\": {"); - // TODO: Need to figure out how to apply textures properly for various - // types - builder.append(" \"all\": \"" + BaseMod.MODID + ":blocks/" + texture + "\""); - builder.append("}"); - builder.append("}"); - - try { - FileUtils.writeStringToFile(f, builder.toString()); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private final void createBlockStatesJSON() { - File f = Paths.get(".").resolve(JSONManager.assetsDir + "/blockStates/" + JSONManager.jsonName(name) + ".json") - .toFile(); - - if (f.exists()) { - f.delete(); - } - - StringBuilder builder = new StringBuilder(); - - builder.append("{"); - builder.append("\"variants\": {"); - builder.append("\"normal\": {"); - builder.append("\"model\": \"" + BaseMod.MODID + ":" + name + "\""); - builder.append("}"); - builder.append("}"); - builder.append("}"); - - try { - FileUtils.writeStringToFile(f, builder.toString()); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private final void createItemJSON() { - File f = Paths.get(".").resolve(JSONManager.assetsDir + "/models/item/" + JSONManager.jsonName(name) + ".json") - .toFile(); - - if (f.exists()) { - f.delete(); - } - - StringBuilder builder = new StringBuilder(); - - builder.append("{"); - builder.append("\"parent\": \"" + BaseMod.MODID + ":block/" + name + "\""); - builder.append("}"); - - try { - FileUtils.writeStringToFile(f, builder.toString()); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public String getName() { - return name; - } -} +package com.idtech.block; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Field; +import java.nio.file.Paths; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import org.apache.commons.io.FileUtils; + +import com.idtech.BaseMod; +import com.idtech.JSONManager; +import com.idtech.PackageManager; + +import net.minecraft.block.Block; +import net.minecraft.block.material.Material; +import net.minecraft.block.state.IBlockState; +import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.entity.Entity; +import net.minecraft.entity.item.EntityItem; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; +import net.minecraft.item.ItemBlock; +import net.minecraft.item.ItemStack; +import net.minecraft.util.EnumParticleTypes; +import net.minecraft.util.SoundCategory; +import net.minecraft.util.SoundEvent; +import net.minecraft.util.math.AxisAlignedBB; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.World; +import net.minecraftforge.fml.common.registry.GameRegistry; + +public class QuickBlock extends Block { + + + public static enum BlockType { + CUBE_ALL, HALF_SLAB; + } + + protected static final String PICKAXE = "pickaxe"; + protected static final String SHOVEL = "shovel"; + + protected static final int WOOD = 0; + protected static final int STONE = 1; + protected static final int IRON = 2; + protected static final int DIAMOND = 3; + + + + private static final Material defaultMaterial = Material.ROCK; + private static Set registry = new HashSet(); + private static Map lookup = new HashMap(); + + protected String name = "Undefined"; + protected BlockType type = BlockType.CUBE_ALL; + protected CreativeTabs tab = CreativeTabs.MISC; + protected String texture = "undefined"; + protected Material material = defaultMaterial; + protected Item itemDropped = null; + + protected World world; + protected BlockPos pos; + protected IBlockState state; + protected Entity entity; + protected Random random; + protected double tickSpeed; + + // When QuickBlock is loaded, it will scan the block directory for all sub + // classes of QuickBlock + // and create a newInstance of them. This allows us to simply use the + // initialization block to specify each parameter + // This invokes the default super constructor of QuickBlock and registers + // it. Therefore, you should almost never + // need to call the constructor for QuickBlock manually. + static { + // Loads all of the QuickBlocks that are in this package + Set classes = PackageManager.loadClassesInPackage("com.idtech.block"); + for (Class klass : classes) { + if (QuickBlock.class.isAssignableFrom(klass) && QuickBlock.class != klass) { + try { + QuickBlock block = (QuickBlock)klass.newInstance(); + lookup.put(block.name, block); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } + + public static QuickBlock getBlock(String name){ + return lookup.get(name); + } + + /** + * Returns an immutable view of the Registry + * + * @return + */ + public static Set getRegistry() { + return Collections.unmodifiableSet(registry); + } + + /** + * Creates a new QuickBlock and adds it to the registry. (Note: You probably + * shouldn't be calling this directly) + */ + public QuickBlock() { + super(defaultMaterial); + this.registry.add(this); + + } + + + /** + * This method should be called once prior to the init method. + */ + public static void preInit() { + System.out.println("Quick Block Registry Size: " + registry.size()); + // Loops through all registered QuickBlocks and initializes them + for (QuickBlock block : registry) { + block.setUnlocalizedName(BaseMod.MODID + "_" + JSONManager.safeString(block.name)); + block.setCreativeTab(block.tab); + System.out.println("preInit: " + block.name); + + // Assigns the block material using reflection since we are avoiding + // using constructors + setBlockField(block, block.material, "blockMaterial"); + setBlockField(block, block.material.getMaterialMapColor(), "blockMapColor"); + setBlockField(block, !block.material.blocksLight(), "translucent"); + + // Register the block and an item for the block. + GameRegistry.register(block.setRegistryName(block.name)); + GameRegistry.register(new ItemBlock(block).setRegistryName(block.getRegistryName())); + + } + } + + /** + * Private helper method which allows the change of private member variables + * in the parent class + * + * @param block + * the QuickBlock we want to change + * @param value + * the value we want to be assigned + * @param fieldName + * the field we want to assign + */ + private static void setBlockField(QuickBlock block, Object value, String fieldName) { + try { + Field f = Block.class.getDeclaredField(fieldName); + f.setAccessible(true); + f.set(block, value); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * This method should be run once before the mod launches + */ + public static void init() { + for (QuickBlock block : registry) { + System.out.println("init: " + block.name); + BaseMod.proxy.registerBlockInventoryRender(block, block.name); + } + } + + @Override + public Item getItemDropped(IBlockState state, Random rand, int fortune) { + if (itemDropped == null) + return Item.getItemFromBlock(this); + return itemDropped; + } + + protected void onRandomTick(){} + + + + /** + * Returns a random neighboring block position. + * @return a random neighboring block position. + */ + protected BlockPos findNeighborBlock(){ + int spreadX = random.nextInt(3) - 1; + int spreadY = spreadX == 0 ? random.nextInt(3) - 1 : 0 ; + int spreadZ = spreadX == 0 && spreadY == 0 ? random.nextInt(3) - 1 : 0; + BlockPos spreadPos = pos.add(spreadX, spreadY, spreadZ); + return spreadPos; + } + + protected void cloneAt(BlockPos pos){ + world.setBlockState(pos, this.getDefaultState()); + } + + + @Override + public void randomTick(World worldIn, BlockPos posIn, IBlockState stateIn, Random randomIn) { + world = worldIn; + pos = posIn; + state = stateIn; + random = randomIn; + onRandomTick(); + } + + protected void onEntityWalk(){} + + + + @Override + public void onEntityWalk(World worldIn, BlockPos posIn, Entity entityIn) { + world = worldIn; + pos = posIn; + entity = entityIn; + onEntityWalk(); + super.onEntityWalk(worldIn, pos, entityIn); + } + + + + @Override + public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, IBlockAccess worldIn, BlockPos pos) { + AxisAlignedBB box = super.getCollisionBoundingBox(blockState, worldIn, pos); + AxisAlignedBB slightlySmaller = new AxisAlignedBB(box.minX + .1, box.minY + .1, box.minZ + .1, box.maxX - .1, box.maxY - .1, box.maxZ - .1); + return slightlySmaller; + } + + + + /** + * Generates a sound at the entities current position. Each call to this + * method generates a random pitch applied to the audio. + * + * @param sound + * The sound to generate + */ + protected void playSound(SoundEvent sound) { + if(entity == null) return; + world.playSound((EntityPlayer) null, entity.posX, entity.posY, entity.posZ, sound, SoundCategory.NEUTRAL, 0.5f, + 0.4F / ((float)Math.random() * 0.4F + 0.8F)); + } + + protected void onEntityCollidedWithBlock(){ + + } + + public QuickBlock getInstance(){ + return this; + } + + @Override + public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, IBlockState state, Entity entityIn) { + super.onEntityCollidedWithBlock(worldIn, pos, state, entityIn); + this.world = worldIn; + this.pos = pos; + this.state = state; + this.entity = entityIn; + onEntityCollidedWithBlock(); + } + + + + @Override + public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) { + super.onBlockAdded(worldIn, pos, state); + this.world = worldIn; + int nextTick = tickSpeed > 0 ? (int)(tickSpeed*20) : 20; + world.scheduleBlockUpdate(pos, this, nextTick, 1); + } + + + @Override + public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand) { + this.world = worldIn; + this.state = state; + this.random = rand; + this.pos = pos; + tick(); + int nextTick = tickSpeed > 0 ? (int)(tickSpeed*20) : 20; + world.scheduleBlockUpdate(pos, this, nextTick, 1); + } + + protected void tick(){ + + } + + protected void spawnItem(Item toSpawn){ + world.spawnEntity(new EntityItem(world, pos.getX(), pos.getY()+1, pos.getZ(), new ItemStack(toSpawn))); + + } + + protected void spawnParticles(EnumParticleTypes type, int intensity){ + for (int i = 0; i < intensity; i++) { + double x = pos.getX(); + double y = pos.getY(); + double z = pos.getZ(); + world.setBlockState(new BlockPos(x, y, z), state); + System.out.println(x + " , " + y + ", " + z); + world.spawnParticle(type, x, y, z, 0, 0, 0); + } + } + + /** + * This method creates the JSON Files necessary for the Minecraft mod. If + * you want to use your own custom JSON files @Override this method. + */ + public void createJSONFiles() { + createModelJSON(); + createBlockStatesJSON(); + createItemJSON(); + } + + // Code below this point is used to generate JSON + + protected final void createModelJSON() { + File f = Paths.get(".").resolve(JSONManager.assetsDir + "/models/block/" + JSONManager.jsonName(name) + ".json") + .toFile(); + + if (f.exists()) { + f.delete(); + } + + StringBuilder builder = new StringBuilder(); + + builder.append("{"); + builder.append("\"parent\": \"block/" + type + "\","); + + builder.append("\"textures\": {"); + // TODO: Need to figure out how to apply textures properly for various + // types + builder.append(" \"all\": \"" + BaseMod.MODID + ":blocks/" + texture + "\""); + builder.append("}"); + builder.append("}"); + + try { + FileUtils.writeStringToFile(f, builder.toString()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + protected final void createBlockStatesJSON() { + File f = Paths.get(".").resolve(JSONManager.assetsDir + "/blockStates/" + JSONManager.jsonName(name) + ".json") + .toFile(); + + if (f.exists()) { + f.delete(); + } + + StringBuilder builder = new StringBuilder(); + + builder.append("{"); + builder.append("\"variants\": {"); + builder.append("\"normal\": {"); + builder.append("\"model\": \"" + BaseMod.MODID + ":" + name + "\""); + builder.append("}"); + builder.append("}"); + builder.append("}"); + + try { + FileUtils.writeStringToFile(f, builder.toString()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + protected final void createItemJSON() { + File f = Paths.get(".").resolve(JSONManager.assetsDir + "/models/item/" + JSONManager.jsonName(name) + ".json") + .toFile(); + + if (f.exists()) { + f.delete(); + } + + StringBuilder builder = new StringBuilder(); + + builder.append("{"); + builder.append("\"parent\": \"" + BaseMod.MODID + ":block/" + name + "\""); + builder.append("}"); + + try { + FileUtils.writeStringToFile(f, builder.toString()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public String getName() { + return name; + } +} diff --git a/src/main/java/com/idtech/item/QuickArmor.java b/src/main/java/com/idtech/item/QuickArmor.java new file mode 100755 index 0000000..20d02c8 --- /dev/null +++ b/src/main/java/com/idtech/item/QuickArmor.java @@ -0,0 +1,75 @@ +package com.idtech.item; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; + +import org.apache.commons.io.FileUtils; + +import com.idtech.BaseMod; +import com.idtech.JSONManager; + +import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.entity.Entity; +import net.minecraft.inventory.EntityEquipmentSlot; +import net.minecraft.item.ItemArmor; +import net.minecraft.item.ItemStack; +import net.minecraftforge.fml.common.registry.GameRegistry; + +public class QuickArmor extends ItemArmor { + + public final String name; + public final String textureName; + + public QuickArmor(ArmorMaterial materialIn, EntityEquipmentSlot equipmentSlotIn, String itemTextureName, String armorTextureName) { + super(materialIn, 0, equipmentSlotIn); + + this.name = itemTextureName; + this.textureName = armorTextureName; + this.setUnlocalizedName(BaseMod.MODID + "_" + itemTextureName); + this.setCreativeTab(CreativeTabs.COMBAT); + + createJSONFile(); + + GameRegistry.register(this.setRegistryName(itemTextureName)); + } + + public String getArmorTexture(ItemStack stack, Entity entity, EntityEquipmentSlot slot, + String type) { + + if(slot == EntityEquipmentSlot.LEGS){ + return "examplemod:textures/models/armor/" + textureName + "_layer_2.png"; + } + return "examplemod:textures/models/armor/" + textureName + "_layer_1.png"; + } + + public void registerRenderers() + { + System.out.println("init: " + name); + BaseMod.proxy.registerItemInventoryRender(this, name); + } + + public void createJSONFile() { + File f = Paths.get(".").resolve(JSONManager.assetsDir + "/models/item/" + JSONManager.jsonName(name) + ".json") + .toFile(); + + if (f.exists()) { + f.delete(); + } + + StringBuilder builder = new StringBuilder(); + + builder.append("{"); + builder.append("\"parent\": \"item/generated\","); + builder.append("\"textures\": {"); + builder.append(" \"layer0\": \"" + BaseMod.MODID + ":items/" + JSONManager.jsonName(name) + "\""); + builder.append("}"); + builder.append("}"); + + try { + FileUtils.writeStringToFile(f, builder.toString()); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/idtech/item/QuickFood.java b/src/main/java/com/idtech/item/QuickFood.java new file mode 100755 index 0000000..72a1103 --- /dev/null +++ b/src/main/java/com/idtech/item/QuickFood.java @@ -0,0 +1,95 @@ +package com.idtech.item; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; + +import org.apache.commons.io.FileUtils; + +import com.idtech.BaseMod; +import com.idtech.JSONManager; + +import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; +import net.minecraft.item.ItemFood; +import net.minecraft.item.ItemStack; +import net.minecraft.world.World; +import net.minecraftforge.fml.common.registry.GameRegistry; + +public class QuickFood extends ItemFood { + + public final String name; + + public OnEatEvent onEat; + + public static abstract class OnEatEvent { + ItemStack itemStack = null; + Item item = null; + World world = null; + EntityPlayer player = null; + + public abstract void onEat(); + } + + public QuickFood(String name, int amount, float saturation, boolean isAlwaysEdible, boolean isWolfFood) { + super(amount, isWolfFood); + + this.name = name; + + this.setUnlocalizedName(BaseMod.MODID + "_" + name); + this.setCreativeTab(CreativeTabs.FOOD); + + if (isAlwaysEdible) + this.setAlwaysEdible(); + + createJSONFile(); + + GameRegistry.register(this.setRegistryName(name)); + } + + public void registerRenderers() + { + System.out.println("init: " + name); + BaseMod.proxy.registerItemInventoryRender(this, name); + } + + protected void onFoodEaten(ItemStack stack, World worldIn, EntityPlayer player) { + super.onFoodEaten(stack, worldIn, player); + + if (onEat != null) { + + onEat.item = stack.getItem(); + onEat.itemStack = stack; + onEat.world = worldIn; + onEat.player = player; + + onEat.onEat(); + } + } + + public void createJSONFile() { + File f = Paths.get(".").resolve(JSONManager.assetsDir + "/models/item/" + JSONManager.jsonName(name) + ".json") + .toFile(); + + if (f.exists()) { + f.delete(); + } + + StringBuilder builder = new StringBuilder(); + + builder.append("{"); + builder.append("\"parent\": \"item/generated\","); + builder.append("\"textures\": {"); + builder.append(" \"layer0\": \"" + BaseMod.MODID + ":items/" + JSONManager.jsonName(name) + "\""); + builder.append("}"); + builder.append("}"); + + try { + FileUtils.writeStringToFile(f, builder.toString()); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/src/main/java/com/idtech/item/QuickTool.java b/src/main/java/com/idtech/item/QuickTool.java new file mode 100755 index 0000000..124924c --- /dev/null +++ b/src/main/java/com/idtech/item/QuickTool.java @@ -0,0 +1,146 @@ +package com.idtech.item; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.Set; + +import org.apache.commons.io.FileUtils; + +import com.google.common.collect.Sets; +import com.idtech.BaseMod; +import com.idtech.JSONManager; + +import net.minecraft.item.Item.ToolMaterial; +import net.minecraft.block.Block; +import net.minecraft.block.material.Material; +import net.minecraft.block.state.IBlockState; +import net.minecraft.init.Blocks; +import net.minecraft.item.ItemPickaxe; +import net.minecraft.item.ItemStack; +import net.minecraft.item.ItemTool; +import net.minecraftforge.fml.common.registry.GameRegistry; + +public class QuickTool extends ItemTool { + + protected String name = "Undefined"; + + enum ToolType { + // Taken from ItemPickaxe + PICKAXE(Sets.newHashSet(new Block[] { Blocks.ACTIVATOR_RAIL, Blocks.COAL_ORE, Blocks.COBBLESTONE, + Blocks.DETECTOR_RAIL, Blocks.DIAMOND_BLOCK, Blocks.DIAMOND_ORE, Blocks.DOUBLE_STONE_SLAB, + Blocks.GOLDEN_RAIL, Blocks.GOLD_BLOCK, Blocks.GOLD_ORE, Blocks.ICE, Blocks.IRON_BLOCK, Blocks.IRON_ORE, + Blocks.LAPIS_BLOCK, Blocks.LAPIS_ORE, Blocks.LIT_REDSTONE_ORE, Blocks.MOSSY_COBBLESTONE, + Blocks.NETHERRACK, Blocks.PACKED_ICE, Blocks.RAIL, Blocks.REDSTONE_ORE, Blocks.SANDSTONE, + Blocks.RED_SANDSTONE, Blocks.STONE, Blocks.STONE_SLAB, Blocks.STONE_BUTTON, + Blocks.STONE_PRESSURE_PLATE })), SHOVEL(Sets.newHashSet(new Block[] { Blocks.CLAY, Blocks.DIRT, + Blocks.FARMLAND, Blocks.GRASS, Blocks.GRAVEL, Blocks.MYCELIUM, Blocks.SAND, Blocks.SNOW, + Blocks.SNOW_LAYER, Blocks.SOUL_SAND, Blocks.GRASS_PATH })), AXE( + Sets.newHashSet(new Block[] { Blocks.PLANKS, Blocks.BOOKSHELF, Blocks.LOG, Blocks.LOG2, + Blocks.CHEST, Blocks.PUMPKIN, Blocks.LIT_PUMPKIN, Blocks.MELON_BLOCK, + Blocks.LADDER, Blocks.WOODEN_BUTTON, Blocks.WOODEN_PRESSURE_PLATE })), SWORD( + Sets.newHashSet(new Block[] {})); + + Set effectiveOn; + + ToolType(Set effectiveOn) { + this.effectiveOn = effectiveOn; + } + } + + ToolType toolType; + + public QuickTool(String name, ToolMaterial mat, float attackDamage, float attackSpeed, ToolType toolType) { + super(attackSpeed, attackSpeed, mat, toolType.effectiveOn); + + this.name = name; + this.toolType = toolType; + + this.setUnlocalizedName(BaseMod.MODID + "_" + JSONManager.safeString(name)); + System.out.println("preInit: " + name); + GameRegistry.register(this.setRegistryName(name)); + + createJSONFile(); + } + + public void registerRenderers() { + System.out.println("init: " + name); + BaseMod.proxy.registerItemInventoryRender(this, name); + } + + public void createJSONFile() { + File f = Paths.get(".").resolve(JSONManager.assetsDir + "/models/item/" + JSONManager.jsonName(name) + ".json") + .toFile(); + + if (f.exists()) { + f.delete(); + } + + StringBuilder builder = new StringBuilder(); + + builder.append("{"); + builder.append("\"parent\": \"item/handheld\","); + builder.append("\"textures\": {"); + builder.append(" \"layer0\": \"" + BaseMod.MODID + ":items/" + JSONManager.jsonName(name) + "\""); + builder.append("}"); + builder.append("}"); + + try { + FileUtils.writeStringToFile(f, builder.toString()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public boolean canHarvestBlock(IBlockState blockIn) { + if (toolType == ToolType.PICKAXE) { + Block block = blockIn.getBlock(); + + if (block == Blocks.OBSIDIAN) { + return this.toolMaterial.getHarvestLevel() == 3; + } else if (block != Blocks.DIAMOND_BLOCK && block != Blocks.DIAMOND_ORE) { + if (block != Blocks.EMERALD_ORE && block != Blocks.EMERALD_BLOCK) { + if (block != Blocks.GOLD_BLOCK && block != Blocks.GOLD_ORE) { + if (block != Blocks.IRON_BLOCK && block != Blocks.IRON_ORE) { + if (block != Blocks.LAPIS_BLOCK && block != Blocks.LAPIS_ORE) { + if (block != Blocks.REDSTONE_ORE && block != Blocks.LIT_REDSTONE_ORE) { + Material material = blockIn.getMaterial(); + return material == Material.ROCK ? true + : (material == Material.IRON ? true : material == Material.ANVIL); + } else { + return this.toolMaterial.getHarvestLevel() >= 2; + } + } else { + return this.toolMaterial.getHarvestLevel() >= 1; + } + } else { + return this.toolMaterial.getHarvestLevel() >= 1; + } + } else { + return this.toolMaterial.getHarvestLevel() >= 2; + } + } else { + return this.toolMaterial.getHarvestLevel() >= 2; + } + } else { + return this.toolMaterial.getHarvestLevel() >= 2; + } + } else + return super.canHarvestBlock(blockIn); + } + + public float getStrVsBlock(ItemStack stack, IBlockState state) { + if (toolType == ToolType.PICKAXE) { + Material material = state.getMaterial(); + return material != Material.IRON && material != Material.ANVIL && material != Material.ROCK + ? super.getStrVsBlock(stack, state) : this.efficiencyOnProperMaterial; + } + if (toolType == ToolType.AXE) { + Material material = state.getMaterial(); + return material != Material.WOOD && material != Material.PLANTS && material != Material.VINE + ? super.getStrVsBlock(stack, state) : this.efficiencyOnProperMaterial; + } + return super.getStrVsBlock(stack, state); + } + +}