-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
src/main/java/com/cleanroommc/neverenoughanimations/animations/SwapHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.cleanroommc.neverenoughanimations.animations; | ||
|
||
import com.cleanroommc.neverenoughanimations.IItemLocation; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.entity.player.InventoryPlayer; | ||
import net.minecraft.inventory.Slot; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraftforge.items.SlotItemHandler; | ||
import net.minecraftforge.items.wrapper.PlayerInvWrapper; | ||
import net.minecraftforge.items.wrapper.PlayerMainInvWrapper; | ||
|
||
import java.util.List; | ||
|
||
public class SwapHolder { | ||
|
||
public static final SwapHolder INSTANCE = new SwapHolder(); | ||
|
||
private Slot targetSlot; | ||
private Slot hotbarSlot; | ||
private ItemStack targetStack; | ||
private ItemStack hotbarStack; | ||
|
||
public boolean init(Slot hoveredSlot, List<Slot> slots, int hotbarIndex) { | ||
this.targetSlot = hoveredSlot; | ||
this.hotbarSlot = findHotbarSlot(slots, hotbarIndex); | ||
if (this.hotbarSlot == null) { | ||
reset(); | ||
return false; | ||
} | ||
this.targetStack = this.targetSlot.getStack(); | ||
this.hotbarStack = this.hotbarSlot.getStack(); | ||
if (this.targetStack.isEmpty() && this.hotbarStack.isEmpty()) { | ||
reset(); | ||
return false; | ||
} | ||
this.targetStack = this.targetStack.copy(); | ||
this.hotbarStack = this.hotbarStack.copy(); | ||
return true; | ||
} | ||
|
||
public void performSwap() { | ||
IItemLocation hotbar = IItemLocation.of(this.hotbarSlot); | ||
IItemLocation hovering = IItemLocation.of(this.targetSlot); | ||
long time = Minecraft.getSystemTime(); | ||
if (this.targetStack.isEmpty()) { | ||
if (!hovering.nea$getStack().isEmpty()) { | ||
ItemMoveAnimation.queueAnimation(hotbar.nea$getSlotNumber(), | ||
new ItemMovePacket(time, hotbar, hovering, hovering.nea$getStack().copy())); | ||
ItemMoveAnimation.updateVirtualStack(hovering.nea$getSlotNumber(), ItemStack.EMPTY, 1); | ||
} | ||
} else if (this.hotbarStack.isEmpty()) { | ||
if (!hotbar.nea$getStack().isEmpty()) { | ||
ItemMoveAnimation.queueAnimation(hovering.nea$getSlotNumber(), | ||
new ItemMovePacket(time, hovering, hotbar, hotbar.nea$getStack().copy())); | ||
ItemMoveAnimation.updateVirtualStack(hotbar.nea$getSlotNumber(), ItemStack.EMPTY, 1); | ||
} | ||
} else { | ||
ItemMoveAnimation.queueAnimation(hotbar.nea$getSlotNumber(), | ||
new ItemMovePacket(time, hotbar, hovering, hovering.nea$getStack().copy())); | ||
ItemMoveAnimation.queueAnimation(hovering.nea$getSlotNumber(), | ||
new ItemMovePacket(time, hovering, hotbar, hotbar.nea$getStack().copy())); | ||
ItemMoveAnimation.updateVirtualStack(hovering.nea$getSlotNumber(), ItemStack.EMPTY, 1); | ||
ItemMoveAnimation.updateVirtualStack(hotbar.nea$getSlotNumber(), ItemStack.EMPTY, 1); | ||
} | ||
reset(); | ||
} | ||
|
||
public void reset() { | ||
this.targetSlot = null; | ||
this.hotbarSlot = null; | ||
this.targetStack = null; | ||
this.hotbarStack = null; | ||
} | ||
|
||
public ItemStack getTargetStack() { | ||
return targetStack; | ||
} | ||
|
||
public ItemStack getHotbarStack() { | ||
return hotbarStack; | ||
} | ||
|
||
public Slot getHotbarSlot() { | ||
return hotbarSlot; | ||
} | ||
|
||
public Slot getTargetSlot() { | ||
return targetSlot; | ||
} | ||
|
||
public static Slot findHotbarSlot(List<Slot> slots, int index) { | ||
for (Slot slot : slots) { | ||
if (slot.getSlotIndex() != index) continue; | ||
if (slot.inventory instanceof InventoryPlayer || | ||
(slot instanceof SlotItemHandler slotItemHandler && | ||
(slotItemHandler.getItemHandler() instanceof PlayerMainInvWrapper || | ||
slotItemHandler.getItemHandler() instanceof PlayerInvWrapper))) { | ||
return slot; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters