Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bauble equipment change event #292

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/main/java/baubles/common/container/SlotBauble.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import baubles.api.IBauble;
import baubles.api.cap.BaublesCapabilities;
import baubles.api.cap.IBaublesItemHandler;
import baubles.common.event.BaubleEquipmentChangeEvent;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.eventhandler.EventBus;
import net.minecraftforge.items.SlotItemHandler;

public class SlotBauble extends SlotItemHandler
Expand Down Expand Up @@ -45,6 +48,7 @@ public ItemStack onTake(EntityPlayer playerIn, ItemStack stack) {
stack.getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null).onUnequipped(stack, playerIn);
}
super.onTake(playerIn, stack);
MinecraftForge.EVENT_BUS.post(new BaubleEquipmentChangeEvent(playerIn, baubleSlot, stack, ItemStack.EMPTY));
return stack;
}

Expand All @@ -58,12 +62,15 @@ public void putStack(ItemStack stack) {

ItemStack oldstack = getStack().copy();
super.putStack(stack);

if (getHasStack() && !ItemStack.areItemStacksEqual(oldstack,getStack())
&& !((IBaublesItemHandler)getItemHandler()).isEventBlocked() &&
getStack().hasCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null)) {
getStack().getCapability(BaublesCapabilities.CAPABILITY_ITEM_BAUBLE, null).onEquipped(getStack(), player);
}
if (!ItemStack.areItemStacksEqual(oldstack,getStack())){
MinecraftForge.EVENT_BUS.post(new BaubleEquipmentChangeEvent(player, baubleSlot, oldstack, getStack()));
}

}

@Override
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/baubles/common/event/BaubleEquipmentChangeEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package baubles.common.event;

import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.entity.living.LivingEvent;

import javax.annotation.Nonnull;

public class BaubleEquipmentChangeEvent extends LivingEvent {
private final int slot;
private final ItemStack from;
private final ItemStack to;

public BaubleEquipmentChangeEvent(EntityLivingBase entity, int slot, @Nonnull ItemStack from, @Nonnull ItemStack to) {
super(entity);
this.slot = slot;
this.from = from;
this.to = to;
}

public int getSlot() {
return this.slot;
}

@Nonnull
public ItemStack getFrom() {
return this.from;
}

@Nonnull
public ItemStack getTo() {
return this.to;
}
}