Skip to content

Instantly share code, notes, and snippets.

@SuperJedi224
Last active October 22, 2019 11:19
Show Gist options
  • Save SuperJedi224/381453070ed10a4615bc839673811f1d to your computer and use it in GitHub Desktop.
Save SuperJedi224/381453070ed10a4615bc839673811f1d to your computer and use it in GitHub Desktop.
package sj224.mods.silk;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntityMobSpawner;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
@Mod(modid = SilkSpawners.MODID, version = SilkSpawners.VERSION, acceptableRemoteVersions = "*", acceptedMinecraftVersions = "[1.12,1.13)")
public class SilkSpawners
{
public static final String MODID = "silkspawner";
public static final String VERSION = "1.02";
@EventHandler
public void init(FMLInitializationEvent event)
{
MinecraftForge.EVENT_BUS.register(this);
}
@SubscribeEvent(priority=EventPriority.LOW)
public void mineSpawner(BlockEvent.BreakEvent b){
if(b.getWorld().isRemote)return;
if(b.getState()==null)return;
if(b.getPlayer()==null)return;
ItemStack i=(b.getPlayer().getHeldItemMainhand());
System.out.println(i.getTagCompound());
if(!b.getWorld().getGameRules().getBoolean("doTileDrops"))return;
if(b.getState().getBlock()==Blocks.MOB_SPAWNER){
if((i.getItem().getUnlocalizedName(i).indexOf("pickaxe")>-1)&&(i.getEnchantmentTagList().toString().indexOf("id:33s")>-1)){
NBTTagCompound tag=new NBTTagCompound();
b.getWorld().getTileEntity(b.getPos()).writeToNBT(tag);
System.out.println(tag);
ItemStack s=new ItemStack(Blocks.MOB_SPAWNER,1);
s.setTagCompound(new NBTTagCompound());
s.getTagCompound().setTag("BlockEntityTag",tag);
tag=tag.copy();
String t=tag.getCompoundTag("SpawnData").getString("id");
System.out.println(t);
String name=EntityList.getTranslationName(new ResourceLocation(t))+" Spawner";
s.setTranslatableName(name);
EntityItem e=new EntityItem(b.getWorld(),b.getPos().getX()+2*Math.random()-1,b.getPos().getY()+2*Math.random()-1,b.getPos().getZ()+2*Math.random()-1,s);
b.setExpToDrop(0);
if(!b.getPlayer().isCreative())b.getWorld().spawnEntity(e);
System.out.println(s.getTagCompound());
};
}
}
@SubscribeEvent(priority=EventPriority.LOW)
public void placeSpawner(PlayerInteractEvent.RightClickBlock p){
if(p.getWorld().isRemote)return;
if(p.getItemStack().getItem()==Item.getItemFromBlock(Blocks.MOB_SPAWNER)){
if(!p.getItemStack().hasTagCompound()||!p.getItemStack().getTagCompound().hasKey("BlockEntityTag"))return;
ItemBlock bk=(ItemBlock)p.getItemStack().getItem();
placeBlock(bk,p.getEntityPlayer(),p.getWorld(),p.getPos(),p.getHand(),p.getFace(),p.getItemStack().getTagCompound().getCompoundTag("BlockEntityTag"));
p.setCanceled(true);
}
}
public void placeBlock(ItemBlock item,EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, NBTTagCompound t)
{
IBlockState iblockstate = worldIn.getBlockState(pos);
Block block = iblockstate.getBlock();
if (!block.isReplaceable(worldIn, pos))
{
pos = pos.offset(facing);
}
ItemStack itemstack = player.getHeldItem(hand);
if (!itemstack.isEmpty() && player.canPlayerEdit(pos, facing, itemstack) && worldIn.mayPlace(item.getBlock(), pos, false, facing, (Entity)null))
{
int i = item.getMetadata(itemstack.getMetadata());
worldIn.setBlockState(pos,Blocks.MOB_SPAWNER.getDefaultState());
TileEntityMobSpawner sp=(TileEntityMobSpawner) worldIn.getTileEntity(pos);
sp.getSpawnerBaseLogic().readFromNBT(t);
itemstack.shrink(1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment