Skip to content

Instantly share code, notes, and snippets.

@Vazkii
Last active November 14, 2023 22:25
Show Gist options
  • Save Vazkii/2c1dce5ce5684d2745e9615ef54a2037 to your computer and use it in GitHub Desktop.
Save Vazkii/2c1dce5ce5684d2745e9615ef54a2037 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.getBlockState().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.getPropertyKeys();
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.shrink(1);;
return par2ItemStack;
}
return super.dispenseStack(par1IBlockSource, par2ItemStack);
}
}
}
@hottentottententententoonstelling

Hi Vazkii, I get the following error when places a strongbox from thermal expansion in 1.10.2 but I believe this happens in 1.11.2 too:

https://pastebin.com/AgJ2Wjh6

Hopefully you can fix this, so that I can use the strongbox mechanics as an ideal replacement for shulkerboxes.

Also: the strongbox is placed strange regarding to the dispenser. The strongbox "lock" is place toward the dispenser, maybe you can tweak this too :) So that the front of a tile-entity is placed opposite to the dispenser.

Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment