-
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.
Merge pull request #24 from refinedmods/release/0.3.0
Release v0.3.0
- Loading branch information
Showing
17 changed files
with
622 additions
and
33 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
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
98 changes: 98 additions & 0 deletions
98
.../src/main/java/com/refinedmods/refinedstorage/rei/fabric/FilterDraggableStackVisitor.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,98 @@ | ||
package com.refinedmods.refinedstorage.rei.fabric; | ||
|
||
import com.refinedmods.refinedstorage.common.support.AbstractBaseContainerMenu; | ||
import com.refinedmods.refinedstorage.common.support.AbstractBaseScreen; | ||
import com.refinedmods.refinedstorage.common.support.containermenu.FilterSlot; | ||
import com.refinedmods.refinedstorage.common.support.packet.c2s.C2SPackets; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import me.shedaniel.math.Rectangle; | ||
import me.shedaniel.rei.api.client.gui.drag.DraggableStack; | ||
import me.shedaniel.rei.api.client.gui.drag.DraggableStackVisitor; | ||
import me.shedaniel.rei.api.client.gui.drag.DraggedAcceptorResult; | ||
import me.shedaniel.rei.api.client.gui.drag.DraggingContext; | ||
import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; | ||
import net.minecraft.client.gui.screens.Screen; | ||
import net.minecraft.world.inventory.Slot; | ||
import net.minecraft.world.item.ItemStack; | ||
|
||
class FilterDraggableStackVisitor | ||
implements DraggableStackVisitor<AbstractBaseScreen<? extends AbstractBaseContainerMenu>> { | ||
@Override | ||
public Stream<BoundsProvider> getDraggableAcceptingBounds( | ||
final DraggingContext<AbstractBaseScreen<? extends AbstractBaseContainerMenu>> context, | ||
final DraggableStack stack | ||
) { | ||
final var screen = context.getScreen(); | ||
final var menu = screen.getMenu(); | ||
final List<BoundsProvider> bounds = new ArrayList<>(); | ||
if (stack.getStack().getType() == VanillaEntryTypes.ITEM) { | ||
addSlotBounds(stack, menu, bounds, screen); | ||
} | ||
return bounds.stream(); | ||
} | ||
|
||
private void addSlotBounds( | ||
final DraggableStack stack, | ||
final AbstractBaseContainerMenu menu, | ||
final List<BoundsProvider> bounds, | ||
final AbstractBaseScreen<? extends AbstractBaseContainerMenu> screen | ||
) { | ||
final ItemStack itemStack = (ItemStack) stack.getStack().getValue(); | ||
for (final Slot slot : menu.slots) { | ||
if (slot instanceof FilterSlot filterSlot && isValid(itemStack, filterSlot)) { | ||
bounds.add(BoundsProvider.ofRectangle(toRectangle(screen, slot))); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public DraggedAcceptorResult acceptDraggedStack( | ||
final DraggingContext<AbstractBaseScreen<? extends AbstractBaseContainerMenu>> context, | ||
final DraggableStack stack | ||
) { | ||
final var screen = context.getScreen(); | ||
final var menu = screen.getMenu(); | ||
if (stack.getStack().getValue() instanceof ItemStack itemStack) { | ||
return accept(context, menu, screen, itemStack); | ||
} | ||
return DraggedAcceptorResult.PASS; | ||
} | ||
|
||
private DraggedAcceptorResult accept( | ||
final DraggingContext<AbstractBaseScreen<? extends AbstractBaseContainerMenu>> context, | ||
final AbstractBaseContainerMenu menu, | ||
final AbstractBaseScreen<? extends AbstractBaseContainerMenu> screen, | ||
final ItemStack stack | ||
) { | ||
for (final Slot slot : menu.slots) { | ||
if (slot instanceof FilterSlot filterSlot && isValid(stack, filterSlot)) { | ||
final Rectangle slotBounds = toRectangle(screen, slot); | ||
if (!slotBounds.contains(context.getCurrentPosition())) { | ||
continue; | ||
} | ||
C2SPackets.sendFilterSlotChange(stack, filterSlot.index); | ||
return DraggedAcceptorResult.ACCEPTED; | ||
} | ||
} | ||
return DraggedAcceptorResult.PASS; | ||
} | ||
|
||
private static boolean isValid(final ItemStack stack, final FilterSlot slot) { | ||
return slot.isActive() && slot.mayPlace(stack); | ||
} | ||
|
||
private static Rectangle toRectangle(final AbstractBaseScreen<? extends AbstractBaseContainerMenu> screen, | ||
final Slot slot) { | ||
return new Rectangle(screen.getLeftPos() + slot.x, screen.getTopPos() + slot.y, 18, 18); | ||
} | ||
|
||
@Override | ||
public <R extends Screen> boolean isHandingScreen(final R screen) { | ||
return screen instanceof AbstractBaseScreen<?> | ||
&& ((AbstractBaseScreen<?>) screen).getMenu() instanceof AbstractBaseContainerMenu; | ||
} | ||
} |
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
113 changes: 113 additions & 0 deletions
113
...c/src/main/java/com/refinedmods/refinedstorage/rei/fabric/PatternGridTransferHandler.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,113 @@ | ||
package com.refinedmods.refinedstorage.rei.fabric; | ||
|
||
import com.refinedmods.refinedstorage.api.resource.ResourceAmount; | ||
import com.refinedmods.refinedstorage.common.api.RefinedStorageApi; | ||
import com.refinedmods.refinedstorage.common.autocrafting.PatternGridContainerMenu; | ||
import com.refinedmods.refinedstorage.common.support.resource.ItemResource; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import javax.annotation.Nullable; | ||
|
||
import me.shedaniel.rei.api.client.registry.transfer.TransferHandler; | ||
import me.shedaniel.rei.api.common.entry.EntryIngredient; | ||
import me.shedaniel.rei.api.common.entry.EntryStack; | ||
import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; | ||
import me.shedaniel.rei.api.common.util.CollectionUtils; | ||
import me.shedaniel.rei.plugin.common.BuiltinPlugin; | ||
import me.shedaniel.rei.plugin.common.displays.crafting.DefaultCraftingDisplay; | ||
import net.minecraft.world.item.ItemStack; | ||
|
||
class PatternGridTransferHandler implements TransferHandler { | ||
@Override | ||
public Result handle(final Context context) { | ||
if (!(context.getMenu() instanceof PatternGridContainerMenu containerMenu)) { | ||
return Result.createNotApplicable(); | ||
} | ||
final Result result = transferRegularRecipe(context, containerMenu); | ||
if (result != null) { | ||
return result; | ||
} | ||
return transferProcessingRecipe(context, containerMenu); | ||
} | ||
|
||
@Nullable | ||
private Result transferRegularRecipe(final Context context, final PatternGridContainerMenu containerMenu) { | ||
if (context.getDisplay().getCategoryIdentifier().equals(BuiltinPlugin.CRAFTING) | ||
&& context.getDisplay() instanceof DefaultCraftingDisplay<?> defaultCraftingDisplay) { | ||
return transferCraftingRecipe(context, containerMenu, defaultCraftingDisplay); | ||
} else if (context.getDisplay().getCategoryIdentifier().equals(BuiltinPlugin.STONE_CUTTING)) { | ||
return transferStonecutterRecipe(context, containerMenu); | ||
} else if (context.getDisplay().getCategoryIdentifier().equals(BuiltinPlugin.SMITHING)) { | ||
return transferSmithingTableRecipe(context, containerMenu); | ||
} | ||
return null; | ||
} | ||
|
||
private Result transferCraftingRecipe(final Context context, | ||
final PatternGridContainerMenu containerMenu, | ||
final DefaultCraftingDisplay<?> display) { | ||
final List<List<ItemResource>> inputs = getItems( | ||
display.getOrganisedInputEntries(3, 3) | ||
); | ||
if (context.isActuallyCrafting()) { | ||
containerMenu.transferCraftingRecipe(inputs); | ||
} | ||
return Result.createSuccessful().blocksFurtherHandling(); | ||
} | ||
|
||
private Result transferStonecutterRecipe(final Context context, final PatternGridContainerMenu menu) { | ||
final List<List<ItemResource>> inputs = getItems(context.getDisplay().getInputEntries()); | ||
final List<List<ItemResource>> outputs = getItems(context.getDisplay().getOutputEntries()); | ||
final boolean valid = !inputs.isEmpty() | ||
&& !outputs.isEmpty() | ||
&& !inputs.getFirst().isEmpty() | ||
&& !outputs.getFirst().isEmpty(); | ||
if (context.isActuallyCrafting() && valid) { | ||
menu.transferStonecutterRecipe(inputs.getFirst().getFirst(), outputs.getFirst().getFirst()); | ||
} | ||
return Result.createSuccessful().blocksFurtherHandling(); | ||
} | ||
|
||
private Result transferSmithingTableRecipe(final Context context, final PatternGridContainerMenu menu) { | ||
final List<List<ItemResource>> inputs = getItems(context.getDisplay().getInputEntries()); | ||
if (context.isActuallyCrafting() && inputs.size() == 3) { | ||
menu.transferSmithingTableRecipe(inputs.get(0), inputs.get(1), inputs.get(2)); | ||
} | ||
return Result.createSuccessful().blocksFurtherHandling(); | ||
} | ||
|
||
private Result transferProcessingRecipe(final Context context, final PatternGridContainerMenu menu) { | ||
final List<List<ResourceAmount>> inputs = getResources(context.getDisplay().getInputEntries()); | ||
final List<List<ResourceAmount>> outputs = getResources(context.getDisplay().getOutputEntries()); | ||
if (context.isActuallyCrafting()) { | ||
menu.transferProcessingRecipe(inputs, outputs); | ||
} | ||
return Result.createSuccessful().blocksFurtherHandling(); | ||
} | ||
|
||
private List<List<ItemResource>> getItems(final List<EntryIngredient> ingredients) { | ||
return ingredients.stream() | ||
.map(this::convertIngredientToItemStacks) | ||
.map(list -> list.stream().map(ItemResource::ofItemStack).collect(Collectors.toList())) | ||
.toList(); | ||
} | ||
|
||
private List<ItemStack> convertIngredientToItemStacks(final EntryIngredient ingredient) { | ||
return CollectionUtils.<EntryStack<?>, ItemStack>filterAndMap( | ||
ingredient, | ||
stack -> stack.getType() == VanillaEntryTypes.ITEM, | ||
EntryStack::castValue | ||
); | ||
} | ||
|
||
private List<List<ResourceAmount>> getResources(final List<EntryIngredient> ingredients) { | ||
return ingredients.stream() | ||
.map(ingredient -> ingredient.stream() | ||
.flatMap( | ||
item -> RefinedStorageApi.INSTANCE.getIngredientConverter().convertToResourceAmount(item.getValue()) | ||
.stream()) | ||
.collect(Collectors.toList())) | ||
.toList(); | ||
} | ||
} |
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
Oops, something went wrong.