Added PotionBrewEvent.Pre/Post. To allow for modification and cancelation of Brewing. Closes #1248

This commit is contained in:
Lex Manos committed 2014-12-07 03:30:52 -08:00
1 parent c9fe5ded9b
commit 24213a8ca7
4 files changed
+132 -16

No files matched your search

@@ -18,6 +18,7 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntityBrewingStand;
import net.minecraft.world.Explosion;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
@@ -28,6 +29,8 @@ import net.minecraft.world.storage.SaveHandler;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.BlockSnapshot;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.event.brewing.PotionBrewEvent;
import net.minecraftforge.event.brewing.PotionBrewedEvent;
import net.minecraftforge.event.entity.EntityStruckByLightningEvent;
import net.minecraftforge.event.entity.living.LivingHealEvent;
import net.minecraftforge.event.entity.living.LivingPackSizeEvent;
@@ -253,4 +256,31 @@ public class ForgeEventFactory
LivingHealEvent event = new LivingHealEvent(entity, amount);
return (MinecraftForge.EVENT_BUS.post(event) ? 0 : event.amount);
}
public static boolean onPotionAttemptBreaw(ItemStack[] stacks)
{
ItemStack[] tmp = new ItemStack[stacks.length];
for (int x = 0; x < tmp.length; x++)
tmp[x] = stacks[x].copy();
PotionBrewEvent.Pre event = new PotionBrewEvent.Pre(tmp);
if (MinecraftForge.EVENT_BUS.post(event))
{
boolean changed = false;
for (int x = 0; x < stacks.length; x++)
{
changed |= ItemStack.areItemStacksEqual(tmp[x], stacks[x]);
stacks[x] = event.getItem(x);
}
if (changed)
onPotionBrewed(stacks);
return true;
}
return false;
}
public static void onPotionBrewed(ItemStack[] brewingItemStacks)
{
MinecraftForge.EVENT_BUS.post(new PotionBrewEvent.Post(brewingItemStacks));
}
}
@@ -0,0 +1,83 @@
package net.minecraftforge.event.brewing;
import net.minecraft.item.ItemStack;
import cpw.mods.fml.common.eventhandler.Cancelable;
import cpw.mods.fml.common.eventhandler.Event;
import cpw.mods.fml.common.eventhandler.Event.HasResult;
public class PotionBrewEvent extends Event
{
private ItemStack[] stacks;
protected PotionBrewEvent(ItemStack[] stacks)
{
this.stacks = stacks;
}
public ItemStack getItem(int index)
{
if (index >= stacks.length) return null;
return stacks[index];
}
public void setItem(int index, ItemStack stack)
{
if (index < stacks.length)
{
stacks[index] = stack;
}
}
public int getLength()
{
return stacks.length;
}
/**
* PotionBrewEvent.Pre is fired before vanilla brewing takes place.
* All changes made to the event's array will be made to the TileEntity if the event is canceled.
* <br>
* The event is fired during the TileEntityBrewingStand#brewPotions() method invocation.<br>
* <br>
* {@link #brewingStacks} contains the itemstack array from the TileEntityBrewer holding all items in Brewer.<br>
* <br>
* This event is {@link Cancelable}.<br>
* If the event is not canceled, the vanilla brewing will take place instead of modded brewing.
* <br>
* This event does not have a result. {@link HasResult}<br>
* <br>
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.<br>
* <br>
* If this event is canceled, and items have been modified, PotionBrewEvent.Post will automatically be fired.
**/
@Cancelable
public static class Pre extends PotionBrewEvent
{
public Pre(ItemStack[] stacks)
{
super(stacks);
}
}
/**
* PotionBrewEvent.Post is fired when a potion is brewed in the brewing stand.
* <br>
* The event is fired during the TileEntityBrewingStand#brewPotions() method invocation.<br>
* <br>
* {@link #brewingStacks} contains the itemstack array from the TileEntityBrewer holding all items in Brewer.<br>
* <br>
* This event is not {@link Cancelable}.<br>
* <br>
* This event does not have a result. {@link HasResult}<br>
* <br>
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.<br>
**/
public static class Post extends PotionBrewedEvent
{
public Post(ItemStack[] stacks)
{
super(stacks);
}
}
}
@@ -1,5 +1,6 @@
package net.minecraftforge.event.brewing;
import cpw.mods.fml.common.eventhandler.Cancelable;
import cpw.mods.fml.common.eventhandler.Event;
import net.minecraft.item.ItemStack;
@@ -16,14 +17,18 @@ import net.minecraft.item.ItemStack;
* <br>
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.<br>
**/
public class PotionBrewedEvent extends Event
@Deprecated //Remove in 1.8.1
public class PotionBrewedEvent extends PotionBrewEvent
{
/**
* The brewing stacks in the brewing stand. Each index has the possibility to be null, so make sure you check.
* Changing this array to another one has no effect.
*/
@Deprecated
public ItemStack[] brewingStacks;
public PotionBrewedEvent(ItemStack[] brewingStacks)
{
super(brewingStacks);
this.brewingStacks = brewingStacks;
}
}