Skip to content

Instantly share code, notes, and snippets.

@Lothrazar
Forked from Vazkii/ModBlockDispenser.java
Created August 23, 2016 03:06
Show Gist options
  • Save Lothrazar/6bf51668c18afdf71b7e5caa6f5292f5 to your computer and use it in GitHub Desktop.
Save Lothrazar/6bf51668c18afdf71b7e5caa6f5292f5 to your computer and use it in GitHub Desktop.
package vazkii.blockdispenser;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import com.google.common.collect.ImmutableSet;
import net.minecraft.block.Block;
import net.minecraft.block.BlockDirectional;
import net.minecraft.block.BlockDispenser;
import net.minecraft.block.BlockHorizontal;
import net.minecraft.block.BlockLog;
import net.minecraft.block.BlockLog.EnumAxis;
import net.minecraft.block.BlockQuartz;
import net.minecraft.block.BlockRotatedPillar;
import net.minecraft.block.BlockStairs;
import net.minecraft.block.SoundType;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.IBlockState;
import net.minecraft.dispenser.BehaviorDefaultDispenseItem;
import net.minecraft.dispenser.IBlockSource;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumFacing.Axis;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
@Mod(modid = "BlockDispenser", name = "BlockDispenser", version = "1.0")
public class ModBlockDispenser {
List<String> blacklist;
@EventHandler
public void preInit(FMLPreInitializationEvent event) {
Configuration config = new Configuration(event.getSuggestedConfigurationFile());
String[] blacklist = config.getStringList("blacklist", Configuration.CATEGORY_GENERAL, new String[] {
"minecraft:water",
"minecraft:flowing_water",
"minecraft:lava",
"minecraft:flowing_lava",
"minecraft:fire",
"minecraft:web"
}, "");
this.blacklist = Arrays.asList(blacklist);
if(config.hasChanged())
config.save();
}
@EventHandler
public void postInit(FMLPostInitializationEvent event) {
for(ResourceLocation r : Block.REGISTRY.getKeys()) {
Block block = Block.REGISTRY.getObject(r);
Item item = Item.getItemFromBlock(block);
if(block == null || item == null || !(item instanceof ItemBlock) || blacklist.contains(r.toString()))
continue;
if(!BlockDispenser.DISPENSE_BEHAVIOR_REGISTRY.containsKey(item))
BlockDispenser.DISPENSE_BEHAVIOR_REGISTRY.putObject(item, new BehaviourBlock((ItemBlock) item, block));
}
}
public class BehaviourBlock extends BehaviorDefaultDispenseItem {
ItemBlock item;
Block block;
public BehaviourBlock(ItemBlock item, Block block) {
this.item = item;
this.block = block;
}
@Override
public ItemStack dispenseStack(IBlockSource par1IBlockSource, ItemStack par2ItemStack) {
EnumFacing facing = par1IBlockSource.func_189992_e().getValue(BlockDispenser.FACING);
Axis axis = facing.getAxis();
BlockPos pos = par1IBlockSource.getBlockPos().offset(facing);
World world = par1IBlockSource.getWorld();
if(world.isAirBlock(pos) && block.canPlaceBlockAt(world, pos)) {
int meta = item.getMetadata(par2ItemStack.getItemDamage());
IBlockState state = block.getStateFromMeta(meta);
Collection<IProperty<?>> props = state.getPropertyNames();
if(props.contains(BlockDirectional.FACING))
state = state.withProperty(BlockDirectional.FACING, facing);
else if(props.contains(BlockHorizontal.FACING) && axis != Axis.Y)
state = state.withProperty(BlockHorizontal.FACING, facing);
else if(props.contains(BlockRotatedPillar.AXIS))
state = state.withProperty(BlockRotatedPillar.AXIS, axis);
// fuck yeah special cases
if(block instanceof BlockStairs)
state = state.withProperty(BlockHorizontal.FACING, facing.getOpposite());
if(block instanceof BlockLog)
state = state.withProperty(BlockLog.LOG_AXIS, BlockLog.LOG_AXIS.parseValue(axis.getName()).or(EnumAxis.NONE));
if(block instanceof BlockQuartz) {
BlockQuartz.EnumType type = state.getValue(BlockQuartz.VARIANT);
if(ImmutableSet.of(BlockQuartz.EnumType.LINES_X, BlockQuartz.EnumType.LINES_Y, BlockQuartz.EnumType.LINES_Z).contains(type))
state = state.withProperty(BlockQuartz.VARIANT, BlockQuartz.VARIANT.parseValue("lines_" + axis.getName()).or(BlockQuartz.EnumType.LINES_Y));
}
world.setBlockState(pos, state);
SoundType soundtype = block.getSoundType();
world.playSound(null, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
par2ItemStack.stackSize--;
return par2ItemStack;
}
return super.dispenseStack(par1IBlockSource, par2ItemStack);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment