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

This commit is contained in:
Lex Manos 2014-12-07 03:30:52 -08:00
parent c9fe5ded9b
commit 24213a8ca7
4 changed files with 132 additions and 16 deletions

View File

@ -1,15 +1,6 @@
--- ../src-base/minecraft/net/minecraft/tileentity/TileEntityBrewingStand.java
+++ ../src-work/minecraft/net/minecraft/tileentity/TileEntityBrewingStand.java
@@ -12,6 +12,8 @@
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.potion.PotionHelper;
+import net.minecraftforge.common.MinecraftForge;
+import net.minecraftforge.event.brewing.PotionBrewedEvent;
public class TileEntityBrewingStand extends TileEntity implements ISidedInventory
{
@@ -104,7 +106,7 @@
@@ -104,7 +104,7 @@
for (int i = 0; i < 3; ++i)
{
@ -18,7 +9,14 @@
{
int j = this.field_145945_j[i].func_77960_j();
int k = this.func_145936_c(j, itemstack);
@@ -143,7 +145,7 @@
@@ -137,13 +137,14 @@
private void func_145940_l()
{
+ if (net.minecraftforge.event.ForgeEventFactory.onPotionAttemptBreaw(field_145945_j)) return;
if (this.func_145934_k())
{
ItemStack itemstack = this.field_145945_j[3];
for (int i = 0; i < 3; ++i)
{
@ -27,7 +25,7 @@
{
int j = this.field_145945_j[i].func_77960_j();
int k = this.func_145936_c(j, itemstack);
@@ -164,9 +166,9 @@
@@ -164,9 +165,9 @@
}
}
@ -39,15 +37,15 @@
}
else
{
@@ -177,6 +179,7 @@
@@ -177,6 +178,7 @@
this.field_145945_j[3] = null;
}
}
+ MinecraftForge.EVENT_BUS.post(new PotionBrewedEvent(field_145945_j));
+ net.minecraftforge.event.ForgeEventFactory.onPotionBrewed(field_145945_j);
}
}
@@ -292,7 +295,7 @@
@@ -292,7 +294,7 @@
public boolean func_94041_b(int p_94041_1_, ItemStack p_94041_2_)
{

View File

@ -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));
}
}

View File

@ -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);
}
}
}

View File

@ -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;
}
}