-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for dragging items from jei
- Loading branch information
Showing
9 changed files
with
114 additions
and
14 deletions.
There are no files selected for viewing
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
55 changes: 55 additions & 0 deletions
55
src/main/java/mods/railcraft/integrations/jei/GhostIngredientHandler.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,55 @@ | ||
package mods.railcraft.integrations.jei; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import mezz.jei.api.gui.handlers.IGhostIngredientHandler; | ||
import mezz.jei.api.ingredients.ITypedIngredient; | ||
import mods.railcraft.client.gui.screen.inventory.RailcraftMenuScreen; | ||
import mods.railcraft.network.NetworkChannel; | ||
import mods.railcraft.network.play.SetFilterSlotMessage; | ||
import mods.railcraft.world.inventory.slot.RailcraftSlot; | ||
import net.minecraft.client.renderer.Rect2i; | ||
import net.minecraft.world.item.ItemStack; | ||
|
||
public class GhostIngredientHandler<T extends RailcraftMenuScreen<?>> | ||
implements IGhostIngredientHandler<T> { | ||
|
||
@Override | ||
public <I> List<Target<I>> getTargetsTyped(T gui, ITypedIngredient<I> ingredient, | ||
boolean doStart) { | ||
var targets = new ArrayList<Target<I>>(); | ||
|
||
for (var slot : gui.getMenu().slots) { | ||
if (!slot.isActive()) { | ||
continue; | ||
} | ||
|
||
var bounds = new Rect2i(gui.getGuiLeft() + slot.x, gui.getGuiTop() + slot.y, 17, 17); | ||
|
||
if (ingredient.getIngredient() instanceof ItemStack itemStack) { | ||
if ((slot instanceof RailcraftSlot railcraftSlot && railcraftSlot.isPhantom())) { | ||
if (railcraftSlot.mayPlace(itemStack)) { | ||
targets.add(new Target<>() { | ||
@Override | ||
public Rect2i getArea() { | ||
return bounds; | ||
} | ||
|
||
@Override | ||
public void accept(I ingredient) { | ||
var itemStack = ((ItemStack) ingredient).copy(); | ||
NetworkChannel.GAME.sendToServer(new SetFilterSlotMessage(slot.index, itemStack)); | ||
slot.set(itemStack); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
return targets; | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
} | ||
} |
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
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
39 changes: 39 additions & 0 deletions
39
src/main/java/mods/railcraft/network/play/SetFilterSlotMessage.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,39 @@ | ||
package mods.railcraft.network.play; | ||
|
||
import java.util.function.Supplier; | ||
import mods.railcraft.world.inventory.slot.RailcraftSlot; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraftforge.network.NetworkEvent; | ||
|
||
public record SetFilterSlotMessage(int slotIndex, ItemStack stack) { | ||
|
||
public static SetFilterSlotMessage decode(FriendlyByteBuf in) { | ||
var slotIndex = in.readVarInt(); | ||
var stack = in.readItem(); | ||
return new SetFilterSlotMessage(slotIndex, stack); | ||
} | ||
|
||
public void encode(FriendlyByteBuf out) { | ||
out.writeVarInt(this.slotIndex); | ||
out.writeItem(this.stack); | ||
} | ||
|
||
public boolean handle(Supplier<NetworkEvent.Context> context) { | ||
var player = context.get().getSender(); | ||
if (player == null) { | ||
return false; | ||
} | ||
if (!this.stack.isEmpty() && this.stack.getCount() <= this.stack.getMaxStackSize()) { | ||
var container = player.containerMenu; | ||
if (container != null && this.slotIndex >= 0 && this.slotIndex < container.slots.size()) { | ||
if (container.getSlot(this.slotIndex) instanceof RailcraftSlot slot && slot.isPhantom()) { | ||
if (slot.mayPlace(this.stack)) { | ||
slot.set(this.stack); | ||
} | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
} |
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
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
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
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