Skip to content

Instantly share code, notes, and snippets.

@AlexIIL
Last active August 29, 2015 14:15
Show Gist options
  • Save AlexIIL/af55d022e99e3e636fd0 to your computer and use it in GitHub Desktop.
Save AlexIIL/af55d022e99e3e636fd0 to your computer and use it in GitHub Desktop.
package alexiil.mods.test;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.crafting.BenchFindMatchingRecipeEvent;
import net.minecraftforge.event.crafting.FindMatchingRecipeEvent;
import net.minecraftforge.event.crafting.FindMatchingRecipeEvent.EType;
import net.minecraftforge.event.crafting.FurnaceFindMatchingRecipeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
@Mod(modid = CraftBlocker.NAME, version = "a version")
public class CraftBlocker {
public static final String NAME = "craftBlockerTest";
@Mod.Instance(NAME)
public static CraftBlocker INSTANCE;
@Mod.EventHandler
public static void init(FMLPreInitializationEvent event)
{
MinecraftForge.EVENT_BUS.register(INSTANCE);
}
@SubscribeEvent
public void trySmelt(FurnaceFindMatchingRecipeEvent event)
{
BlockPos pos = event.pos;
if (event.input[0] == null) return;
// if they are not trying to smelt anything, don't give them anything
if (pos.getY() == 70) event.setOutput(new ItemStack(Items.cake));
// Give them cake if they are at y-level 70
}
@SubscribeEvent
public void tryCraft(FindMatchingRecipeEvent event)
{
if (event.type != EType.CRAFT) return;
// If they are NOT crafting (so smelting or other), then we don't want
// to block them
if (event.output == null)
{
long time = event.world.getWorldTime();
if (time > 23000) event.setOutput(new ItemStack(Items.diamond));
// If it is really late give them free diamonds
}
else if (event.output.getItem() == Items.book)
{
BlockPos current = null;
// Essentially, if there are 10 bookshelves nearby, they can craft
// more books than normal
if (event instanceof FindMatchingRecipeEvent.Block)
{
current = ((FindMatchingRecipeEvent.Block) event).pos;
}
else if (event instanceof FindMatchingRecipeEvent.Player)
{
EntityPlayer player = ((FindMatchingRecipeEvent.Player) event).player;
current = new BlockPos((int) player.posX, (int) player.posY, (int) player.posZ);
}
else
return;
if (event instanceof BenchFindMatchingRecipeEvent && event.world.isRemote())
{
// Bug workaround, so that the client gets it *about* right.
// This BlockPos position is better than the origin however.
EntityPlayer player = Minecraft.getMinecraft().thePlayer;
current = new BlockPos((int) player.posX, (int) player.posY, (int) player.posZ);
}
int shelvesFound = 0;
int r = 5; // Range of search
for (int x = -r; x < r; x++)
for (int y = -r; y < r; y++)
for (int z = -r; z < r; z++)
{
BlockPos pos = new BlockPos(current.getX() + x, current.getY() + y, current.getZ() + z);
IBlockState state = event.world.getBlockState(pos);
if (state == null) continue;
Block block = state.getBlock();
if (block == Blocks.bookshelf) shelvesFound++;
}
event.setOutput(new ItemStack(Items.book, shelvesFound / 10 + 1));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment