diff options
| author | shedaniel <daniel@shedaniel.me> | 2023-02-19 02:05:30 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2024-04-16 00:38:18 +0900 |
| commit | ab7a2bb72b9ee9591049d4ec3c8815d57986bd2c (patch) | |
| tree | a8c0c0fb76ac58537f5d4bafdc55a416f3a31b43 /runtime/src/main/java/me/shedaniel/rei/plugin | |
| parent | eb2b1d1ecc00ba435e97b5d89879ebe379fd7390 (diff) | |
| download | RoughlyEnoughItems-ab7a2bb72b9ee9591049d4ec3c8815d57986bd2c.tar.gz RoughlyEnoughItems-ab7a2bb72b9ee9591049d4ec3c8815d57986bd2c.tar.bz2 RoughlyEnoughItems-ab7a2bb72b9ee9591049d4ec3c8815d57986bd2c.zip | |
Mini Displays Favorites
Diffstat (limited to 'runtime/src/main/java/me/shedaniel/rei/plugin')
| -rw-r--r-- | runtime/src/main/java/me/shedaniel/rei/plugin/client/runtime/DefaultClientRuntimePlugin.java | 171 |
1 files changed, 163 insertions, 8 deletions
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 index ee5dc388c..cdbec40ac 100644 --- 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 @@ -36,13 +36,14 @@ 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.config.RecipeBorderType; +import me.shedaniel.rei.api.client.gui.drag.component.DraggableComponent; import me.shedaniel.rei.api.client.gui.drag.component.DraggableComponentProviderWidget; import me.shedaniel.rei.api.client.gui.drag.component.DraggableComponentVisitorWidget; -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.TooltipContext; -import me.shedaniel.rei.api.client.gui.widgets.Widgets; +import me.shedaniel.rei.api.client.gui.widgets.*; import me.shedaniel.rei.api.client.plugins.REIClientPlugin; +import me.shedaniel.rei.api.client.registry.category.CategoryRegistry; +import me.shedaniel.rei.api.client.registry.display.DisplayCategory; import me.shedaniel.rei.api.client.registry.display.DisplayRegistry; import me.shedaniel.rei.api.client.registry.entry.EntryRegistry; import me.shedaniel.rei.api.client.registry.screen.ExclusionZones; @@ -50,18 +51,25 @@ import me.shedaniel.rei.api.client.registry.screen.ScreenRegistry; import me.shedaniel.rei.api.client.registry.transfer.TransferHandlerRegistry; import me.shedaniel.rei.api.client.search.method.InputMethodRegistry; import me.shedaniel.rei.api.client.util.ClientEntryStacks; +import me.shedaniel.rei.api.common.category.CategoryIdentifier; +import me.shedaniel.rei.api.common.display.Display; +import me.shedaniel.rei.api.common.display.DisplaySerializerRegistry; +import me.shedaniel.rei.api.common.entry.EntryIngredient; import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.plugins.PluginManager; import me.shedaniel.rei.api.common.plugins.REIPlugin; import me.shedaniel.rei.api.common.registry.Reloadable; +import me.shedaniel.rei.api.common.util.CollectionUtils; import me.shedaniel.rei.api.common.util.EntryStacks; import me.shedaniel.rei.impl.client.ClientHelperImpl; import me.shedaniel.rei.impl.client.REIRuntimeImpl; import me.shedaniel.rei.impl.client.gui.ScreenOverlayImpl; import me.shedaniel.rei.impl.client.gui.screen.DefaultDisplayViewingScreen; +import me.shedaniel.rei.impl.client.gui.widget.DisplayCompositeWidget; import me.shedaniel.rei.impl.client.gui.widget.favorites.FavoritesListWidget; import me.shedaniel.rei.impl.client.search.method.DefaultInputMethod; import me.shedaniel.rei.impl.client.search.method.unihan.*; +import me.shedaniel.rei.impl.common.InternalLogger; import me.shedaniel.rei.impl.common.entry.type.EntryRegistryImpl; import me.shedaniel.rei.impl.common.entry.type.EntryRegistryListener; import me.shedaniel.rei.impl.common.util.HNEntryStackWrapper; @@ -75,8 +83,7 @@ import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; -import java.util.Collections; -import java.util.List; +import java.util.*; import java.util.function.Function; @Environment(EnvType.CLIENT) @@ -101,7 +108,7 @@ public class DefaultClientRuntimePlugin implements REIClientPlugin { 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"); + private final ResourceLocation id = new ResourceLocation("roughlyenoughitems", "textures/gui/kirb.png"); @Override public void render(PoseStack matrices, Rectangle bounds, int mouseX, int mouseY, float delta) { @@ -162,6 +169,7 @@ public class DefaultClientRuntimePlugin implements REIClientPlugin { @Override public void registerFavorites(FavoriteEntryType.Registry registry) { registry.register(EntryStackFavoriteType.INSTANCE.id, EntryStackFavoriteType.INSTANCE); + registry.register(DisplayFavoriteType.INSTANCE.id, DisplayFavoriteType.INSTANCE); } @Override @@ -183,7 +191,7 @@ public class DefaultClientRuntimePlugin implements REIClientPlugin { INSTANCE(FavoriteEntryType.ENTRY_STACK); private final String key = "data"; - private ResourceLocation id; + private final ResourceLocation id; EntryStackFavoriteType(ResourceLocation id) { this.id = id; @@ -263,4 +271,151 @@ public class DefaultClientRuntimePlugin implements REIClientPlugin { return EntryStacks.equalsExact(stack, that.stack); } } + + private enum DisplayFavoriteType implements FavoriteEntryType<DisplayFavoriteEntry> { + INSTANCE(FavoriteEntryType.DISPLAY); + + private final String key = "data"; + private final ResourceLocation id; + + DisplayFavoriteType(ResourceLocation id) { + this.id = id; + } + + @Override + public DataResult<DisplayFavoriteEntry> read(CompoundTag object) { + try { + if (object.contains("Data")) { + Display display = DisplaySerializerRegistry.getInstance().read(CategoryIdentifier.of(object.getString("CategoryID")), object.getCompound("Data")); + return DataResult.success(new DisplayFavoriteEntry(display, UUID.fromString(object.getString("UUID"))), Lifecycle.stable()); + } else { + return DataResult.success(new DisplayFavoriteEntry(null, UUID.fromString(object.getString("UUID"))), Lifecycle.stable()); + } + } catch (Throwable throwable) { + return DataResult.error(throwable.getMessage()); + } + } + + @Override + public DataResult<DisplayFavoriteEntry> fromArgs(Object... args) { + if (args.length == 0) return DataResult.error("Cannot create DisplayFavoriteEntry from empty args!"); + if (!(args[0] instanceof Display display)) + return DataResult.error("Creation of DisplayFavoriteEntry from args expected Display as the first argument!"); + return DataResult.success(new DisplayFavoriteEntry(display, UUID.randomUUID()), Lifecycle.stable()); + } + + @Override + public CompoundTag save(DisplayFavoriteEntry entry, CompoundTag tag) { + boolean hasSerializer = DisplaySerializerRegistry.getInstance().hasSerializer(entry.display.getCategoryIdentifier()); + tag.putString("CategoryID", entry.display.getCategoryIdentifier().toString()); + tag.putString("UUID", entry.uuid.toString()); + + if (hasSerializer) { + try { + tag.put("Data", DisplaySerializerRegistry.getInstance().save(entry.display, new CompoundTag())); + } catch (Exception e) { + InternalLogger.getInstance().warn("Failed to save display favorite entry", e); + } + } + + return tag; + } + } + + private static class DisplayFavoriteEntry extends FavoriteEntry { + private static final Function<EntryStack<?>, String> CANCEL_FLUID_AMOUNT = s -> null; + private final Display display; + private final UUID uuid; + private final long hash; + + public DisplayFavoriteEntry(Display display, UUID uuid) { + this.display = display; + this.uuid = uuid; + this.hash = uuid.hashCode(); + } + + @Override + public UUID getUuid() { + return uuid; + } + + @Override + public boolean isInvalid() { + return this.display == null; + } + + @Override + public Renderer getRenderer(boolean showcase) { + Panel panel = Widgets.createRecipeBase(new Rectangle(0, 0, 18, 18)) + .yTextureOffset(RecipeBorderType.LIGHTER.getYOffset()); + Slot slot = Widgets.createSlot(new Rectangle()) + .disableBackground() + .disableHighlight() + .disableTooltips(); + for (EntryIngredient ingredient : display.getOutputEntries()) { + slot.entries(ingredient); + } + return new AbstractRenderer() { + @Override + public void render(PoseStack matrices, Rectangle bounds, int mouseX, int mouseY, float delta) { + matrices.pushPose(); + matrices.translate(bounds.getX(), bounds.getY(), 1); + matrices.scale(bounds.width / (float) panel.getBounds().getWidth(), bounds.height / (float) panel.getBounds().getHeight(), 1); + panel.render(matrices, mouseX, mouseY, delta); + matrices.popPose(); + if (bounds.width > 4 && bounds.height > 4) { + matrices.pushPose(); + matrices.translate(0, 0.5, 0); + slot.getBounds().setBounds(bounds.x + 2, bounds.y + 2, bounds.width - 4, bounds.height - 4); + slot.render(matrices, mouseX, mouseY, delta); + matrices.popPose(); + } + } + }; + } + + @Override + public boolean doAction(int button) { + ClientHelperImpl.getInstance() + .openDisplayViewingScreen(Map.of(CategoryRegistry.getInstance().get(display.getCategoryIdentifier()).getCategory(), List.of(display)), + null, List.of(), List.of()); + Widgets.produceClickSound(); + return true; + } + + @Override + public long hashIgnoreAmount() { + return hash; + } + + @Override + public FavoriteEntry copy() { + return new DisplayFavoriteEntry(this.display, this.uuid); + } + + @Override + public ResourceLocation getType() { + return DisplayFavoriteType.INSTANCE.id; + } + + @Override + public boolean isSame(FavoriteEntry other) { + if (!(other instanceof DisplayFavoriteEntry that)) return false; + return Objects.equals(this.uuid, that.uuid); + } + + @Override + @Nullable + public DraggableComponent<?> asDraggableComponent(Slot slot) { + CategoryRegistry.CategoryConfiguration<Display> configuration = CategoryRegistry.getInstance().get((CategoryIdentifier<Display>) display.getCategoryIdentifier()); + DisplayCategory<Display> category = configuration.getCategory(); + Rectangle displayBounds = new Rectangle(0, 0, category.getDisplayWidth(display), category.getDisplayHeight()); + List<Widget> widgets = configuration.getView(display).setupDisplay(display, displayBounds); + DisplayCompositeWidget.DisplayDraggableComponent component = new DisplayCompositeWidget.DisplayDraggableComponent( + Widgets.concat(CollectionUtils.filterToList(widgets, w -> !(w instanceof Panel))), + display, slot.getInnerBounds(), displayBounds); + component.onFavoritesRegion = true; + return component; + } + } } |
