Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package party.lemons.autoplant;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.IPlantable;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.event.entity.item.ItemExpireEvent;
import net.minecraftforge.event.entity.item.ItemTossEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import javax.annotation.Nullable;
@Mod(modid= AutoPlant.MODID, version= AutoPlant.VERSION, name=AutoPlant.NAME)
public class AutoPlant
{
public static final String MODID = "autoplant";
public static final String VERSION = "1.12-1.0.0";
public static final String NAME = "AutoPlant";
public static int despawntime;
public static String[] blockBlackList;
private static String[] defaultBlackList = new String[]
{
"minecraft:waterlily",
"minecraft:vine"
};
private static int chanceToPlant;
@Mod.EventHandler
public void preInit(FMLPreInitializationEvent event)
{
Configuration config = new Configuration(event.getSuggestedConfigurationFile());
config.load();
despawntime = config.getInt("despawnTime", config.CATEGORY_GENERAL, 1000, 0, Integer.MAX_VALUE, "How long a plant should take to despawn (and attempt to plant) Default Minecraft is 6000");
blockBlackList = config.getStringList("blacklist", config.CATEGORY_GENERAL, defaultBlackList, "blocks that should not be placed");
chanceToPlant = config.getInt("chanceToPlant", Configuration.CATEGORY_GENERAL, 100, 0, 100, "Percentage chance to plant");
config.save();
}
@Mod.EventHandler
public void load(FMLInitializationEvent event)
{
MinecraftForge.EVENT_BUS.register(this);
}
@SubscribeEvent
public void itemDecay(ItemExpireEvent event)
{
plant(event.getEntityItem());
}
@SubscribeEvent
public void itemToss(ItemTossEvent event)
{
if(getPlantable(event.getEntityItem().getEntityItem()) != null && !isStackBlacklisted(event.getEntityItem().getEntityItem()))
{
event.getEntityItem().lifespan = AutoPlant.despawntime;
}
}
public void plant(EntityItem entityItem)
{
plant(entityItem.getEntityItem(),entityItem.getPosition(), entityItem.world);
}
public void plant(ItemStack stack, BlockPos plantPosition, World world)
{
IPlantable plantable = getPlantable(stack);
if(plantable != null)
{
IBlockState state = getPlant(stack, world, plantPosition);
if(canPlace(state, plantPosition, world, plantable))
{
world.setBlockState(plantPosition, state);
state.getBlock().onBlockPlacedBy(world, plantPosition, state, null, stack);
}
}
}
public boolean canPlace(IBlockState state, BlockPos pos, World world, IPlantable plantable)
{
boolean flag1 = true;
boolean flag2 = state.getBlock().canPlaceBlockAt(world, pos);
boolean flag3 = world.isAirBlock(pos);
boolean flag4 = !isBlockBlacklisted(state.getBlock());
boolean flag5 = chanceToPlant == 100 || world.rand.nextInt(100) <= chanceToPlant;
return flag1 && flag2 && flag3 && flag4 && flag5;
}
public IBlockState getPlant(ItemStack stack, World world, BlockPos pos)
{
if(stack.getItem() instanceof ItemBlock)
{
Block block = Block.getBlockFromItem(stack.getItem());
return block.getStateFromMeta(stack.getItem().getMetadata(stack));
}
if(stack.getItem() instanceof IPlantable)
{
IPlantable plantable = (IPlantable) stack.getItem();
return plantable.getPlant(world, pos);
}
return null;
}
@Nullable
public IPlantable getPlantable(ItemStack stack)
{
Item item = stack.getItem();
IPlantable plantable = null;
if(item instanceof ItemBlock)
{
Block blk = Block.getBlockFromItem(item);
if(isPlantableBlock(blk))
{
plantable = (IPlantable) blk;
}
}
if(item instanceof IPlantable)
{
plantable = (IPlantable) item;
}
return plantable;
}
public boolean isPlantableBlock(Block block)
{
return block instanceof IPlantable;
}
public boolean isStackBlacklisted(ItemStack stack)
{
if(stack.getItem() instanceof ItemBlock)
{
return isBlockBlacklisted(Block.getBlockFromItem(stack.getItem()));
}
return false;
}
public boolean isBlockBlacklisted(Block block)
{
for(String id : blockBlackList)
{
if(id.equalsIgnoreCase(block.getRegistryName().toString()))
{
return true;
}
}
return false;
}
}
The MIT License (MIT)
Copyright (c) 2018
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

The MIT License (MIT)

Copyright (c) 2018

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.