Skip to content

Instantly share code, notes, and snippets.

@dazsim
Created July 6, 2018 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dazsim/56f9f0c6c0dea8575b9f1393c001c68f to your computer and use it in GitHub Desktop.
Save dazsim/56f9f0c6c0dea8575b9f1393c001c68f to your computer and use it in GitHub Desktop.
package com.workshopcraft.SimpleHarvest;
import java.util.Collection;
import java.util.Iterator;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.block.BlockBush;
import net.minecraft.block.BlockCrops;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.IPlantable;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.common.Loader;
//import net.minecraftforge.event.entity.player.PlayerInteractEvent.Action;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
public class CropHandler {
public static final PropertyInteger AGE7 = PropertyInteger.create("age", 0, 7);
public static final PropertyInteger AGE3 = PropertyInteger.create("age", 0, 3);
public static final PropertyInteger AGE11 = PropertyInteger.create("age", 0, 11);
public boolean isHarvestcraftInstalled()
{
return (Loader.isModLoaded("harvestcraft"));
}
public boolean isFlowercraftInstalled()
{
return (Loader.isModLoaded("flowercraft"));
}
public boolean isSimpleCornInstalled()
{
return (Loader.isModLoaded("simplecorn"));
}
public boolean isMoreFoodsInstalled()
{
return (Loader.isModLoaded("morefood"));
}
public boolean isIEInstalled()
{
return (Loader.isModLoaded("immersiveengineering"));
}
@SubscribeEvent
public void onPlayerUse(PlayerInteractEvent event)
{
//event.entityPlayer.getHeldItem().
//Action a = event.getAction();
World w = event.getWorld();
BlockPos p = event.getPos();
String un = "";
String smod = "";
EntityPlayer e = event.getEntityPlayer();
boolean updated = false;
if (event.getHand() == EnumHand.OFF_HAND)
{
//System.out.println(w.getBlockState(p).getBlock().getClass().getName());
if (w.getBlockState(p).getBlock() instanceof BlockBush )
{
//you just right clicked a bush
IBlockState b = w.getBlockState(p);
Block tmpBlock;
int stage = 0;
boolean fail = false;
un = b.getBlock().getUnlocalizedName().substring(5);
if (un.equals("tallgrass"))
{
return; //do nothing.
}
//smod = w.getBlockState(p).getBlock().getUnlocalizedName().substring(0,12);
//System.out.println(un);
//System.out.println(un.substring(0,9));
if ((isHarvestcraftInstalled()) || (isFlowercraftInstalled()))
{
if (b.getBlock().getUnlocalizedName().substring(5, 8).equals("pam"))
{
{
stage = (Integer)b.getValue(AGE3).intValue();
//System.out.println("PAM CROP");
}
} else
if (un.length()>9)
{
if (un.substring(0,9).equals("immersive"))
{
stage = b.getBlock().getMetaFromState(b);
}
} else
if (un.equals("beetroots"))
{
stage = (Integer)b.getValue(AGE3).intValue();
}
else
{
//System.out.println(un);
if ((un.equals("tallgrass")) || (un.equals("flower") || (un.equals("flower2"))))
{
return;
}
stage = (Integer)b.getValue(AGE7).intValue();
}
//System.out.println(stage);
}if (isSimpleCornInstalled())
{
if (un.equals("simplecorn.corn"))
{
stage = (Integer)b.getValue(AGE11).intValue();
if (stage>8)
{
//delete all crops and set lowest crop to default state
tmpBlock = w.getBlockState(p).getBlock();
if (w.getBlockState(p).getBlock().getUnlocalizedName().substring(5).equals("simplecorn.corn"))
{
w.getBlockState(p).getBlock().breakBlock(w, p, w.getBlockState(p));
}
if (w.getBlockState(p.down()).getBlock().canSustainPlant(w.getBlockState(p.down()), w, p,EnumFacing.UP,(IPlantable)tmpBlock))
{
//System.out.println("broke bottom block");
w.setBlockState(p, tmpBlock.getDefaultState());
} else if (w.getBlockState(p.down(2)).getBlock().canSustainPlant(w.getBlockState(p.down(2)), w, p.down(),EnumFacing.UP,(IPlantable)tmpBlock))
{
//System.out.println("broke middle block");
w.setBlockState(p.down(), tmpBlock.getDefaultState());
}else if (w.getBlockState(p.down(3)).getBlock().canSustainPlant(w.getBlockState(p.down(3)), w, p.down(2),EnumFacing.UP,(IPlantable)tmpBlock))
{
//System.out.println("broke top block");
w.setBlockState(p.down(2), tmpBlock.getDefaultState());
}
/*
Collection<ItemStack> items =b.getBlock().getDrops(w, p, b, 0);
Iterator<ItemStack> i = items.iterator();
while (i.hasNext())
{
ItemStack s = i.next();
e.inventory.addItemStackToInventory(s);
updated = true;
}
w.setBlockState(p, w.getBlockState(p).getBlock().getDefaultState());
*/
}
//System.out.println("corn "+stage);
}
} else
{
if (un.equals("beetroots"))
{
stage = (Integer)b.getValue(AGE3).intValue();
}
else if (un.equals("immersiveengineering.hemp"))
{
stage = b.getBlock().getMetaFromState(b);
//System.out.println("State: "+stage);
}
else
{
if (w.getBlockState(p).getBlock() instanceof BlockCrops )
{
stage = (Integer)b.getValue(AGE7).intValue();
} else
{
return;
}
}
//System.out.println(stage);
}
if (!w.isRemote)
{
//System.out.println(stage);
if ((isHarvestcraftInstalled()) || (isFlowercraftInstalled()))
{
if (b.getBlock().getUnlocalizedName().length()>9)
{
if (b.getBlock().getUnlocalizedName().substring(5, 8).equals("pam"))
{
Collection<ItemStack> items =b.getBlock().getDrops(w, p, b, 0);
Iterator<ItemStack> i = items.iterator();
Random rand = new Random();
while (i.hasNext())
{
ItemStack s = i.next();
if (!e.inventory.addItemStackToInventory(s))
{
//failed to insert itemstack into inventory.
//drop on floor.
float f = rand.nextFloat() * 0.8F + 0.1F;
float f1 = rand.nextFloat() * 0.8F + 0.1F;
float f2 = rand.nextFloat() * 0.8F + 0.1F;
EntityItem item = new EntityItem(e.world, e.getPosition().getX() + f, e.getPosition().getY() + f1, e.getPosition().getZ() + f2, s);
}
updated = true;
}
w.setBlockState(p, w.getBlockState(p).getBlock().getDefaultState());
e.inventory.markDirty();
return;
}
}
}
if ((un.equals("beetroots") || (isMoreFoodsInstalled() && b.getBlock().getUnlocalizedName().substring(0, 7).equals("morefood"))) && (stage == 3))
{
//special case for beetroot as it has 3 stages rather than 7.
Collection<ItemStack> items =b.getBlock().getDrops(w, p, b, 0);
Iterator<ItemStack> i = items.iterator();
Random rand = new Random();
while (i.hasNext())
{
ItemStack s = i.next();
if (!e.inventory.addItemStackToInventory(s))
{
//failed to insert itemstack into inventory.
//drop on floor.
float f = rand.nextFloat() * 0.8F + 0.1F;
float f1 = rand.nextFloat() * 0.8F + 0.1F;
float f2 = rand.nextFloat() * 0.8F + 0.1F;
EntityItem item = new EntityItem(e.world, e.getPosition().getX() + f, e.getPosition().getY() + f1, e.getPosition().getZ() + f2, s);
}
updated = true;
}
w.setBlockState(p, w.getBlockState(p).getBlock().getDefaultState());
e.inventory.markDirty();
return;
}
if (stage == 7)
{
//reset stage to 0
//getdrops
//System.out.println("MEH FOO");
Collection<ItemStack> items =b.getBlock().getDrops(w, p, b, 0);
Iterator<ItemStack> i = items.iterator();
Random rand = new Random();
while (i.hasNext())
{
ItemStack s = i.next();
if (!e.inventory.addItemStackToInventory(s))
{
//failed to insert itemstack into inventory.
//drop on floor.
float f = rand.nextFloat() * 0.8F + 0.1F;
float f1 = rand.nextFloat() * 0.8F + 0.1F;
float f2 = rand.nextFloat() * 0.8F + 0.1F;
EntityItem item = new EntityItem(e.world, e.getPosition().getX() + f, e.getPosition().getY() + f1, e.getPosition().getZ() + f2, s);
}
updated = true;
}
w.setBlockState(p, w.getBlockState(p).getBlock().getDefaultState());
e.inventory.markDirty();
//if crop is ender crop then check if on tilled endstone and trigger endermite check
if (un.equals("endercrop.ender_crop"))
{
if (w.getBlockState(p.add(0,-1,0)).getBlock().getUnlocalizedName().equals("endercrop.tilled_end_stone"))
{
//System.out.println("ATTEMPT TO SPAWN ENDERMITE");
w.getBlockState(p.add(0,-1,0)).getBlock().harvestBlock(w, event.getEntityPlayer(), p, b, null, null);
}
}
return;
}
if (isIEInstalled() && (un.equals("immersiveengineering.hemp")))
{
if (stage >= 4)
{
//this is a valid hemp crop.
//check block above to see if it is 2 blocks tall
IBlockState b2 = w.getBlockState(p.add(0,1,0));
//System.out.println(b2.getBlock().getUnlocalizedName());
boolean twice = false;
if (b2.getBlock().getUnlocalizedName().equals("tile.immersiveengineering.hemp"))
{
if (b2.getBlock().getMetaFromState(b2)==5)
{
//get drops
/*System.out.println(b2.getBlock().getMetaFromState(b2));
NonNullList<ItemStack> drops = null;
drops.create();
b2.getBlock().getDrops(drops,w, p.add(0,1,0) , b2, 0);
//add to inventory
/Iterator<ItemStack> i = drops.iterator();
while (i.hasNext())
{
ItemStack s = i.next();
e.inventory.addItemStackToInventory(s);
updated = true;
}*/
twice = true;
w.setBlockToAir(p.add(0,1,0));
//w.setBlockState(p, w.getBlockState(p.add(0,1,0)).getBlock().getDefaultState());
//set to air
}
}
//get drops
NonNullList<ItemStack> drops = null;
drops = drops.create();
b.getBlock().getDrops(drops,w, p , b, 0);
if (twice)
b.getBlock().getDrops(drops,w, p , b, 0);
//add to inventory
Iterator<ItemStack> i = drops.iterator();
Random rand = new Random();
while (i.hasNext())
{
ItemStack s = i.next();
if (!e.inventory.addItemStackToInventory(s))
{
//failed to insert itemstack into inventory.
//drop on floor.
float f = rand.nextFloat() * 0.8F + 0.1F;
float f1 = rand.nextFloat() * 0.8F + 0.1F;
float f2 = rand.nextFloat() * 0.8F + 0.1F;
EntityItem item = new EntityItem(e.world, e.getPosition().getX() + f, e.getPosition().getY() + f1, e.getPosition().getZ() + f2, s);
}
updated = true;
}
//set to base state
w.setBlockState(p, w.getBlockState(p).getBlock().getDefaultState());
e.inventory.markDirty();
//if 2 blocks tall drop double crop value
//tada #BlameTage
}
}
}
if (updated)
{
e.inventory.markDirty();
//e.inventory.inventoryChanged=updated;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment