From b866becfb620c02a74fb990915001e154b5d2b0b Mon Sep 17 00:00:00 2001 From: shedaniel Date: Mon, 2 Aug 2021 19:54:29 +0800 Subject: Welcome, Forge 1.17 --- .../rei/impl/client/gui/ScreenOverlayImpl.java | 32 +-- .../rei/impl/client/gui/widget/QueuedTooltip.java | 13 ++ .../rei/impl/common/entry/AbstractEntryStack.java | 1 + .../plugin/client/DefaultClientRuntimePlugin.java | 252 --------------------- .../client/runtime/DefaultClientRuntimePlugin.java | 252 +++++++++++++++++++++ .../rei/plugin/common/DefaultRuntimePlugin.java | 60 ----- .../common/runtime/DefaultRuntimePlugin.java | 60 +++++ 7 files changed, 331 insertions(+), 339 deletions(-) delete mode 100644 runtime/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientRuntimePlugin.java create mode 100644 runtime/src/main/java/me/shedaniel/rei/plugin/client/runtime/DefaultClientRuntimePlugin.java delete mode 100644 runtime/src/main/java/me/shedaniel/rei/plugin/common/DefaultRuntimePlugin.java create mode 100644 runtime/src/main/java/me/shedaniel/rei/plugin/common/runtime/DefaultRuntimePlugin.java (limited to 'runtime/src/main/java') diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java index 628e823fc..b6dc4028d 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java @@ -28,6 +28,7 @@ import com.mojang.blaze3d.platform.Window; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.math.Vector4f; +import dev.architectury.injectables.annotations.ExpectPlatform; import me.shedaniel.math.Point; import me.shedaniel.math.Rectangle; import me.shedaniel.math.impl.PointHelper; @@ -551,35 +552,12 @@ public class ScreenOverlayImpl extends ScreenOverlay { } public void renderTooltip(PoseStack matrices, Tooltip tooltip) { - List lines = tooltip.entries().stream() - .flatMap(component -> { - if (component.isText()) { - return Minecraft.getInstance().font.getSplitter().splitLines(component.getAsText(), 100000, Style.EMPTY).stream() - .map(Language.getInstance()::getVisualOrder) - .map(ClientTooltipComponent::create); - } else { - return Stream.of(component.getAsComponent()); - } - }) - .collect(Collectors.toList()); - for (TooltipComponent component : tooltip.components()) { - try { - ClientInternals.getClientTooltipComponent(lines, component); - } catch (Throwable exception) { - throw new IllegalArgumentException("Failed to add tooltip component! " + component + ", Class: " + (component == null ? null : component.getClass().getCanonicalName()), exception); - } - } - renderTooltipInner(matrices, lines, tooltip.getX(), tooltip.getY()); + renderTooltipInner(minecraft.screen, matrices, tooltip, tooltip.getX(), tooltip.getY()); } - public void renderTooltipInner(PoseStack matrices, List lines, int mouseX, int mouseY) { - if (lines.isEmpty()) { - return; - } - matrices.pushPose(); - matrices.translate(0, 0, 500); - minecraft.screen.renderTooltipInternal(matrices, lines, mouseX, mouseY); - matrices.popPose(); + @ExpectPlatform + public static void renderTooltipInner(Screen screen, PoseStack matrices, Tooltip tooltip, int mouseX, int mouseY) { + throw new AssertionError(); } public void addTooltip(@Nullable Tooltip tooltip) { diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/QueuedTooltip.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/QueuedTooltip.java index 213305e18..aed5add29 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/QueuedTooltip.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/QueuedTooltip.java @@ -28,6 +28,7 @@ import com.google.common.collect.Lists; import me.shedaniel.math.Point; import me.shedaniel.math.impl.PointHelper; import me.shedaniel.rei.api.client.gui.widgets.Tooltip; +import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.util.CollectionUtils; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @@ -48,6 +49,7 @@ public class QueuedTooltip implements Tooltip { private Point location; private List entries; private List components; + private EntryStack stack = EntryStack.empty(); private QueuedTooltip(Point location, Collection entries) { this.location = location; @@ -110,6 +112,17 @@ public class QueuedTooltip implements Tooltip { Tooltip.super.queue(); } + @Override + public EntryStack getContextStack() { + return stack; + } + + @Override + public Tooltip withContextStack(EntryStack stack) { + this.stack = stack.copy(); + return this; + } + public record TooltipEntryImpl(Object obj) implements Tooltip.Entry { @Override public Component getAsText() { diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/AbstractEntryStack.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/AbstractEntryStack.java index a70f7c5d2..421422d4a 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/AbstractEntryStack.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/AbstractEntryStack.java @@ -181,6 +181,7 @@ public abstract class AbstractEntryStack implements EntryStack, Renderer { try { Mutable tooltip = new MutableObject<>(getRenderer().cast().getTooltip(this, mouse)); if (tooltip.getValue() == null) return null; + tooltip.getValue().withContextStack(this); tooltip.getValue().addAllTexts(get(Settings.TOOLTIP_APPEND_EXTRA).apply(this)); tooltip.setValue(get(Settings.TOOLTIP_PROCESSOR).apply(this, tooltip.getValue())); if (tooltip.getValue() == null) return null; diff --git a/runtime/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientRuntimePlugin.java b/runtime/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientRuntimePlugin.java deleted file mode 100644 index 5334fbea5..000000000 --- a/runtime/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientRuntimePlugin.java +++ /dev/null @@ -1,252 +0,0 @@ -/* - * This file is licensed under the MIT License, part of Roughly Enough Items. - * Copyright (c) 2018, 2019, 2020, 2021 shedaniel - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package me.shedaniel.rei.plugin.client; - -import com.mojang.blaze3d.systems.RenderSystem; -import com.mojang.blaze3d.vertex.PoseStack; -import com.mojang.serialization.DataResult; -import com.mojang.serialization.Lifecycle; -import dev.architectury.fluid.FluidStack; -import me.shedaniel.math.Point; -import me.shedaniel.math.Rectangle; -import me.shedaniel.rei.RoughlyEnoughItemsCoreClient; -import me.shedaniel.rei.api.client.ClientHelper; -import me.shedaniel.rei.api.client.REIRuntime; -import me.shedaniel.rei.api.client.favorites.FavoriteEntry; -import me.shedaniel.rei.api.client.favorites.FavoriteEntryType; -import me.shedaniel.rei.api.client.gui.AbstractRenderer; -import me.shedaniel.rei.api.client.gui.Renderer; -import me.shedaniel.rei.api.client.gui.drag.DraggableStackProviderWidget; -import me.shedaniel.rei.api.client.gui.drag.DraggableStackVisitorWidget; -import me.shedaniel.rei.api.client.gui.widgets.Panel; -import me.shedaniel.rei.api.client.gui.widgets.Tooltip; -import me.shedaniel.rei.api.client.gui.widgets.Widgets; -import me.shedaniel.rei.api.client.plugins.REIClientPlugin; -import me.shedaniel.rei.api.client.registry.entry.EntryRegistry; -import me.shedaniel.rei.api.client.registry.screen.DisplayBoundsProvider; -import me.shedaniel.rei.api.client.registry.screen.ExclusionZones; -import me.shedaniel.rei.api.client.registry.screen.ScreenRegistry; -import me.shedaniel.rei.api.client.registry.transfer.TransferHandlerRegistry; -import me.shedaniel.rei.api.client.util.ClientEntryStacks; -import me.shedaniel.rei.api.common.entry.EntryStack; -import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; -import me.shedaniel.rei.api.common.util.EntryStacks; -import me.shedaniel.rei.impl.client.ClientHelperImpl; -import me.shedaniel.rei.impl.client.gui.ScreenOverlayImpl; -import me.shedaniel.rei.impl.client.gui.screen.AbstractDisplayViewingScreen; -import me.shedaniel.rei.impl.client.gui.screen.DefaultDisplayViewingScreen; -import me.shedaniel.rei.impl.client.gui.widget.FavoritesListWidget; -import me.shedaniel.rei.plugin.autocrafting.DefaultCategoryHandler; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.minecraft.client.gui.screens.Screen; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.network.chat.TextComponent; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.InteractionResult; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.ItemStack; -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.Nullable; - -import java.util.Collections; -import java.util.function.Function; - -@Environment(EnvType.CLIENT) -@ApiStatus.Internal -public class DefaultClientRuntimePlugin implements REIClientPlugin { - @Override - public void registerEntries(EntryRegistry registry) { - if (ClientHelperImpl.getInstance().isAprilFools.get()) { - registry.addEntry(ClientEntryStacks.of(new AbstractRenderer() { - private ResourceLocation id = new ResourceLocation("roughlyenoughitems", "textures/gui/kirb.png"); - - @Override - public void render(PoseStack matrices, Rectangle bounds, int mouseX, int mouseY, float delta) { - RenderSystem.setShaderTexture(0, id); - innerBlit(matrices.last().pose(), bounds.x, bounds.getMaxX(), bounds.y, bounds.getMaxY(), getBlitOffset(), 0, 1, 0, 1); - } - - @Override - @Nullable - public Tooltip getTooltip(Point point) { - return Tooltip.create(new TextComponent("Kirby"), ClientHelper.getInstance().getFormattedModFromModId("Dream Land")); - } - })); - } - } - - @Override - public void registerScreens(ScreenRegistry registry) { - ExclusionZones zones = registry.exclusionZones(); - zones.register(DefaultDisplayViewingScreen.class, screen -> { - Panel widget = screen.getWorkingStationsBaseWidget(); - if (widget == null) - return Collections.emptyList(); - return Collections.singletonList(widget.getBounds().clone()); - }); - zones.register(Screen.class, screen -> { - FavoritesListWidget widget = ScreenOverlayImpl.getFavoritesListWidget(); - if (widget != null) { - if (widget.favoritePanelButton.isVisible()) { - return Collections.singletonList(widget.favoritePanelButton.bounds); - } - } - return Collections.emptyList(); - }); - registry.registerDecider(new DisplayBoundsProvider() { - @Override - public Rectangle getScreenBounds(AbstractDisplayViewingScreen screen) { - return screen.getBounds(); - } - - @Override - public boolean isHandingScreen(Class screen) { - return AbstractDisplayViewingScreen.class.isAssignableFrom(screen); - } - - @Override - public InteractionResult shouldScreenBeOverlaid(Class screen) { - return InteractionResult.SUCCESS; - } - }); - registry.registerDraggableStackProvider(DraggableStackProviderWidget.from(context -> { - if (RoughlyEnoughItemsCoreClient.shouldReturn(context.getScreen()) || !REIRuntime.getInstance().isOverlayVisible()) return Collections.emptyList(); - return Widgets.walk(REIRuntime.getInstance().getOverlay().get().children(), DraggableStackProviderWidget.class::isInstance); - })); - registry.registerDraggableStackVisitor(DraggableStackVisitorWidget.from(context -> { - if (RoughlyEnoughItemsCoreClient.shouldReturn(context.getScreen()) || !REIRuntime.getInstance().isOverlayVisible()) return Collections.emptyList(); - return Widgets.walk(REIRuntime.getInstance().getOverlay().get().children(), DraggableStackVisitorWidget.class::isInstance); - })); - } - - @Override - public void registerFavorites(FavoriteEntryType.Registry registry) { - registry.register(EntryStackFavoriteType.INSTANCE.id, EntryStackFavoriteType.INSTANCE); - } - - @Override - public void registerTransferHandlers(TransferHandlerRegistry registry) { - registry.register(new DefaultCategoryHandler()); - } - - private enum EntryStackFavoriteType implements FavoriteEntryType { - INSTANCE(FavoriteEntryType.ENTRY_STACK); - - private final String key = "data"; - private ResourceLocation id; - - EntryStackFavoriteType(ResourceLocation id) { - this.id = id; - } - - @Override - public DataResult readResult(CompoundTag object) { - EntryStack stack; - try { - stack = EntryStack.read(object.getCompound(key)); - } catch (Throwable throwable) { - return DataResult.error(throwable.getMessage()); - } - return DataResult.success(new EntryStackFavoriteEntry(stack), Lifecycle.stable()); - } - - @Override - public DataResult fromArgsResult(Object... args) { - if (args.length == 0) return DataResult.error("Cannot create EntryStackFavoriteEntry from empty args!"); - if (!(args[0] instanceof EntryStack stack)) - return DataResult.error("Creation of EntryStackFavoriteEntry from args expected EntryStack as the first argument!"); - if (!stack.supportSaving()) - return DataResult.error("Creation of EntryStackFavoriteEntry from an unserializable stack!"); - return DataResult.success(new EntryStackFavoriteEntry(stack), Lifecycle.stable()); - } - - @Override - public CompoundTag save(EntryStackFavoriteEntry entry, CompoundTag tag) { - tag.put(key, entry.stack.save()); - return tag; - } - } - - private static class EntryStackFavoriteEntry extends FavoriteEntry { - private static final Function, String> CANCEL_FLUID_AMOUNT = s -> null; - private final EntryStack stack; - private final long hash; - - public EntryStackFavoriteEntry(EntryStack stack) { - this.stack = stack.normalize(); - this.hash = EntryStacks.hashExact(this.stack); - } - - @Override - public boolean isInvalid() { - return this.stack.isEmpty(); - } - - @Override - public Renderer getRenderer(boolean showcase) { - return this.stack; - } - - @Override - public boolean doAction(int button) { - if (!ClientHelper.getInstance().isCheating()) return false; - EntryStack entry = stack.copy(); - if (!entry.isEmpty()) { - if (entry.getValueType() == FluidStack.class) { - Item bucketItem = ((FluidStack) entry.getValue()).getFluid().getBucket(); - if (bucketItem != null) { - entry = EntryStacks.of(bucketItem); - } - } - if (entry.getType() == VanillaEntryTypes.ITEM) - entry.castValue().setCount(button != 1 && !Screen.hasShiftDown() ? 1 : entry.castValue().getMaxStackSize()); - return ClientHelper.getInstance().tryCheatingEntry(entry); - } - - return false; - } - - @Override - public long hashIgnoreAmount() { - return hash; - } - - @Override - public FavoriteEntry copy() { - return new EntryStackFavoriteEntry(stack.normalize()); - } - - @Override - public ResourceLocation getType() { - return EntryStackFavoriteType.INSTANCE.id; - } - - @Override - public boolean isSame(FavoriteEntry other) { - if (!(other instanceof EntryStackFavoriteEntry that)) return false; - return EntryStacks.equalsExact(stack, that.stack); - } - } -} diff --git a/runtime/src/main/java/me/shedaniel/rei/plugin/client/runtime/DefaultClientRuntimePlugin.java b/runtime/src/main/java/me/shedaniel/rei/plugin/client/runtime/DefaultClientRuntimePlugin.java new file mode 100644 index 000000000..22bd239b4 --- /dev/null +++ b/runtime/src/main/java/me/shedaniel/rei/plugin/client/runtime/DefaultClientRuntimePlugin.java @@ -0,0 +1,252 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.plugin.client.runtime; + +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.serialization.DataResult; +import com.mojang.serialization.Lifecycle; +import dev.architectury.fluid.FluidStack; +import me.shedaniel.math.Point; +import me.shedaniel.math.Rectangle; +import me.shedaniel.rei.RoughlyEnoughItemsCoreClient; +import me.shedaniel.rei.api.client.ClientHelper; +import me.shedaniel.rei.api.client.REIRuntime; +import me.shedaniel.rei.api.client.favorites.FavoriteEntry; +import me.shedaniel.rei.api.client.favorites.FavoriteEntryType; +import me.shedaniel.rei.api.client.gui.AbstractRenderer; +import me.shedaniel.rei.api.client.gui.Renderer; +import me.shedaniel.rei.api.client.gui.drag.DraggableStackProviderWidget; +import me.shedaniel.rei.api.client.gui.drag.DraggableStackVisitorWidget; +import me.shedaniel.rei.api.client.gui.widgets.Panel; +import me.shedaniel.rei.api.client.gui.widgets.Tooltip; +import me.shedaniel.rei.api.client.gui.widgets.Widgets; +import me.shedaniel.rei.api.client.plugins.REIClientPlugin; +import me.shedaniel.rei.api.client.registry.entry.EntryRegistry; +import me.shedaniel.rei.api.client.registry.screen.DisplayBoundsProvider; +import me.shedaniel.rei.api.client.registry.screen.ExclusionZones; +import me.shedaniel.rei.api.client.registry.screen.ScreenRegistry; +import me.shedaniel.rei.api.client.registry.transfer.TransferHandlerRegistry; +import me.shedaniel.rei.api.client.util.ClientEntryStacks; +import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; +import me.shedaniel.rei.api.common.util.EntryStacks; +import me.shedaniel.rei.impl.client.ClientHelperImpl; +import me.shedaniel.rei.impl.client.gui.ScreenOverlayImpl; +import me.shedaniel.rei.impl.client.gui.screen.AbstractDisplayViewingScreen; +import me.shedaniel.rei.impl.client.gui.screen.DefaultDisplayViewingScreen; +import me.shedaniel.rei.impl.client.gui.widget.FavoritesListWidget; +import me.shedaniel.rei.plugin.autocrafting.DefaultCategoryHandler; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.TextComponent; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; + +import java.util.Collections; +import java.util.function.Function; + +@Environment(EnvType.CLIENT) +@ApiStatus.Internal +public class DefaultClientRuntimePlugin implements REIClientPlugin { + @Override + public void registerEntries(EntryRegistry registry) { + if (ClientHelperImpl.getInstance().isAprilFools.get()) { + registry.addEntry(ClientEntryStacks.of(new AbstractRenderer() { + private ResourceLocation id = new ResourceLocation("roughlyenoughitems", "textures/gui/kirb.png"); + + @Override + public void render(PoseStack matrices, Rectangle bounds, int mouseX, int mouseY, float delta) { + RenderSystem.setShaderTexture(0, id); + innerBlit(matrices.last().pose(), bounds.x, bounds.getMaxX(), bounds.y, bounds.getMaxY(), getBlitOffset(), 0, 1, 0, 1); + } + + @Override + @Nullable + public Tooltip getTooltip(Point point) { + return Tooltip.create(new TextComponent("Kirby"), ClientHelper.getInstance().getFormattedModFromModId("Dream Land")); + } + })); + } + } + + @Override + public void registerScreens(ScreenRegistry registry) { + ExclusionZones zones = registry.exclusionZones(); + zones.register(DefaultDisplayViewingScreen.class, screen -> { + Panel widget = screen.getWorkingStationsBaseWidget(); + if (widget == null) + return Collections.emptyList(); + return Collections.singletonList(widget.getBounds().clone()); + }); + zones.register(Screen.class, screen -> { + FavoritesListWidget widget = ScreenOverlayImpl.getFavoritesListWidget(); + if (widget != null) { + if (widget.favoritePanelButton.isVisible()) { + return Collections.singletonList(widget.favoritePanelButton.bounds); + } + } + return Collections.emptyList(); + }); + registry.registerDecider(new DisplayBoundsProvider() { + @Override + public Rectangle getScreenBounds(AbstractDisplayViewingScreen screen) { + return screen.getBounds(); + } + + @Override + public boolean isHandingScreen(Class screen) { + return AbstractDisplayViewingScreen.class.isAssignableFrom(screen); + } + + @Override + public InteractionResult shouldScreenBeOverlaid(Class screen) { + return InteractionResult.SUCCESS; + } + }); + registry.registerDraggableStackProvider(DraggableStackProviderWidget.from(context -> { + if (RoughlyEnoughItemsCoreClient.shouldReturn(context.getScreen()) || !REIRuntime.getInstance().isOverlayVisible()) return Collections.emptyList(); + return Widgets.walk(REIRuntime.getInstance().getOverlay().get().children(), DraggableStackProviderWidget.class::isInstance); + })); + registry.registerDraggableStackVisitor(DraggableStackVisitorWidget.from(context -> { + if (RoughlyEnoughItemsCoreClient.shouldReturn(context.getScreen()) || !REIRuntime.getInstance().isOverlayVisible()) return Collections.emptyList(); + return Widgets.walk(REIRuntime.getInstance().getOverlay().get().children(), DraggableStackVisitorWidget.class::isInstance); + })); + } + + @Override + public void registerFavorites(FavoriteEntryType.Registry registry) { + registry.register(EntryStackFavoriteType.INSTANCE.id, EntryStackFavoriteType.INSTANCE); + } + + @Override + public void registerTransferHandlers(TransferHandlerRegistry registry) { + registry.register(new DefaultCategoryHandler()); + } + + private enum EntryStackFavoriteType implements FavoriteEntryType { + INSTANCE(FavoriteEntryType.ENTRY_STACK); + + private final String key = "data"; + private ResourceLocation id; + + EntryStackFavoriteType(ResourceLocation id) { + this.id = id; + } + + @Override + public DataResult readResult(CompoundTag object) { + EntryStack stack; + try { + stack = EntryStack.read(object.getCompound(key)); + } catch (Throwable throwable) { + return DataResult.error(throwable.getMessage()); + } + return DataResult.success(new EntryStackFavoriteEntry(stack), Lifecycle.stable()); + } + + @Override + public DataResult fromArgsResult(Object... args) { + if (args.length == 0) return DataResult.error("Cannot create EntryStackFavoriteEntry from empty args!"); + if (!(args[0] instanceof EntryStack stack)) + return DataResult.error("Creation of EntryStackFavoriteEntry from args expected EntryStack as the first argument!"); + if (!stack.supportSaving()) + return DataResult.error("Creation of EntryStackFavoriteEntry from an unserializable stack!"); + return DataResult.success(new EntryStackFavoriteEntry(stack), Lifecycle.stable()); + } + + @Override + public CompoundTag save(EntryStackFavoriteEntry entry, CompoundTag tag) { + tag.put(key, entry.stack.save()); + return tag; + } + } + + private static class EntryStackFavoriteEntry extends FavoriteEntry { + private static final Function, String> CANCEL_FLUID_AMOUNT = s -> null; + private final EntryStack stack; + private final long hash; + + public EntryStackFavoriteEntry(EntryStack stack) { + this.stack = stack.normalize(); + this.hash = EntryStacks.hashExact(this.stack); + } + + @Override + public boolean isInvalid() { + return this.stack.isEmpty(); + } + + @Override + public Renderer getRenderer(boolean showcase) { + return this.stack; + } + + @Override + public boolean doAction(int button) { + if (!ClientHelper.getInstance().isCheating()) return false; + EntryStack entry = stack.copy(); + if (!entry.isEmpty()) { + if (entry.getValueType() == FluidStack.class) { + Item bucketItem = ((FluidStack) entry.getValue()).getFluid().getBucket(); + if (bucketItem != null) { + entry = EntryStacks.of(bucketItem); + } + } + if (entry.getType() == VanillaEntryTypes.ITEM) + entry.castValue().setCount(button != 1 && !Screen.hasShiftDown() ? 1 : entry.castValue().getMaxStackSize()); + return ClientHelper.getInstance().tryCheatingEntry(entry); + } + + return false; + } + + @Override + public long hashIgnoreAmount() { + return hash; + } + + @Override + public FavoriteEntry copy() { + return new EntryStackFavoriteEntry(stack.normalize()); + } + + @Override + public ResourceLocation getType() { + return EntryStackFavoriteType.INSTANCE.id; + } + + @Override + public boolean isSame(FavoriteEntry other) { + if (!(other instanceof EntryStackFavoriteEntry that)) return false; + return EntryStacks.equalsExact(stack, that.stack); + } + } +} diff --git a/runtime/src/main/java/me/shedaniel/rei/plugin/common/DefaultRuntimePlugin.java b/runtime/src/main/java/me/shedaniel/rei/plugin/common/DefaultRuntimePlugin.java deleted file mode 100644 index 103da1937..000000000 --- a/runtime/src/main/java/me/shedaniel/rei/plugin/common/DefaultRuntimePlugin.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * This file is licensed under the MIT License, part of Roughly Enough Items. - * Copyright (c) 2018, 2019, 2020, 2021 shedaniel - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package me.shedaniel.rei.plugin.common; - -import dev.architectury.event.CompoundEventResult; -import dev.architectury.fluid.FluidStack; -import me.shedaniel.rei.api.common.entry.EntryStack; -import me.shedaniel.rei.api.common.entry.type.EntryTypeRegistry; -import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; -import me.shedaniel.rei.api.common.fluid.FluidSupportProvider; -import me.shedaniel.rei.api.common.plugins.REIServerPlugin; -import me.shedaniel.rei.plugin.client.entry.FluidEntryDefinition; -import me.shedaniel.rei.plugin.client.entry.ItemEntryDefinition; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.minecraft.resources.ResourceLocation; -import org.jetbrains.annotations.ApiStatus; - -import java.util.Optional; -import java.util.stream.Stream; - -@ApiStatus.Internal -public class DefaultRuntimePlugin implements REIServerPlugin { - public static final ResourceLocation PLUGIN = new ResourceLocation("roughlyenoughitems", "default_runtime_plugin"); - - @Override - public void registerEntryTypes(EntryTypeRegistry registry) { - registry.register(VanillaEntryTypes.ITEM, new ItemEntryDefinition()); - registry.register(VanillaEntryTypes.FLUID, new FluidEntryDefinition()); - - registry.registerBridge(VanillaEntryTypes.ITEM, VanillaEntryTypes.FLUID, input -> { - Optional>> stream = FluidSupportProvider.getInstance().itemToFluids(input); - if (!stream.isPresent()) { - return CompoundEventResult.pass(); - } - return CompoundEventResult.interruptTrue(stream.get()); - }); - } -} diff --git a/runtime/src/main/java/me/shedaniel/rei/plugin/common/runtime/DefaultRuntimePlugin.java b/runtime/src/main/java/me/shedaniel/rei/plugin/common/runtime/DefaultRuntimePlugin.java new file mode 100644 index 000000000..8aeff8a20 --- /dev/null +++ b/runtime/src/main/java/me/shedaniel/rei/plugin/common/runtime/DefaultRuntimePlugin.java @@ -0,0 +1,60 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.plugin.common.runtime; + +import dev.architectury.event.CompoundEventResult; +import dev.architectury.fluid.FluidStack; +import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.entry.type.EntryTypeRegistry; +import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; +import me.shedaniel.rei.api.common.fluid.FluidSupportProvider; +import me.shedaniel.rei.api.common.plugins.REIServerPlugin; +import me.shedaniel.rei.plugin.client.entry.FluidEntryDefinition; +import me.shedaniel.rei.plugin.client.entry.ItemEntryDefinition; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.resources.ResourceLocation; +import org.jetbrains.annotations.ApiStatus; + +import java.util.Optional; +import java.util.stream.Stream; + +@ApiStatus.Internal +public class DefaultRuntimePlugin implements REIServerPlugin { + public static final ResourceLocation PLUGIN = new ResourceLocation("roughlyenoughitems", "default_runtime_plugin"); + + @Override + public void registerEntryTypes(EntryTypeRegistry registry) { + registry.register(VanillaEntryTypes.ITEM, new ItemEntryDefinition()); + registry.register(VanillaEntryTypes.FLUID, new FluidEntryDefinition()); + + registry.registerBridge(VanillaEntryTypes.ITEM, VanillaEntryTypes.FLUID, input -> { + Optional>> stream = FluidSupportProvider.getInstance().itemToFluids(input); + if (!stream.isPresent()) { + return CompoundEventResult.pass(); + } + return CompoundEventResult.interruptTrue(stream.get()); + }); + } +} -- cgit