aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/shedaniel/rei/api/Renderer.java
blob: 823a9ceb5c2e76efc736b61935c39119bd79aeb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * Roughly Enough Items by Danielshe.
 * Licensed under the MIT License.
 */

package me.shedaniel.rei.api;

import me.shedaniel.rei.api.annotations.ToBeRemoved;
import me.shedaniel.rei.gui.renderers.EmptyRenderer;
import me.shedaniel.rei.gui.renderers.FluidRenderer;
import me.shedaniel.rei.gui.renderers.ItemStackRenderer;
import me.shedaniel.rei.gui.renderers.SimpleRecipeRenderer;
import me.shedaniel.rei.gui.widget.QueuedTooltip;
import net.minecraft.client.gui.DrawableHelper;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.MathHelper;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

@Deprecated
public abstract class Renderer extends DrawableHelper {
    /**
     * Gets an item stack renderer by an item stack supplier
     *
     * @param supplier the supplier for getting the item stack
     * @return the item stack renderer
     */
    public static ItemStackRenderer fromItemStackSupplier(Supplier<ItemStack> supplier) {
        return fromItemStacks(() -> Collections.singletonList(supplier.get()), true, null);
    }
    
    /**
     * Gets an item stack renderer by an item stack supplier
     *
     * @param supplier the supplier for getting the item stack
     * @return the item stack renderer
     */
    public static ItemStackRenderer fromItemStackSupplierNoCounts(Supplier<ItemStack> supplier) {
        return fromItemStacks(() -> Collections.singletonList(supplier.get()), false, null);
    }
    
    /**
     * Gets an item stack renderer by an item stack
     *
     * @param stack the item stack to be displayed
     * @return the item stack renderer
     */
    public static ItemStackRenderer fromItemStack(ItemStack stack) {
        return fromItemStacks(() -> Collections.singletonList(stack), true, null);
    }
    
    public static FluidRenderer fromFluid(Fluid fluid) {
        return fromFluid(() -> fluid, null);
    }
    
    public static FluidRenderer fromFluid(Supplier<Fluid> fluidSupplier, @Nullable Function<Fluid, List<String>> extraTooltipSupplier) {
        return new FluidRenderer() {
            @Override
            public Fluid getFluid() {
                return fluidSupplier.get();
            }
            
            @Override
            protected List<String> getExtraToolTips(Fluid fluid) {
                if (extraTooltipSupplier == null)
                    return super.getExtraToolTips(fluid);
                List<String> apply = extraTooltipSupplier.apply(fluid);
                if (apply == null)
                    return super.getExtraToolTips(fluid);
                return apply;
            }
        };
    }
    
    /**
     * Gets an item stack renderer by an item stack
     *
     * @param stack the item stack to be displayed
     * @return the item stack renderer
     */
    public static ItemStackRenderer fromItemStackNoCounts(ItemStack stack) {
        return fromItemStacks(() -> Collections.singletonList(stack), false, null);
    }
    
    /**
     * Gets an empty renderer
     *
     * @return an empty renderer
     */
    public static EmptyRenderer empty() {
        return EmptyRenderer.INSTANCE;
    }
    
    /**
     * Gets a simple recipe renderer from inputs and outputs
     *
     * @param input  the list of input items
     * @param output the list of output items
     * @return the recipe renderer
     */
    @ToBeRemoved
    @Deprecated
    public static SimpleRecipeRenderer fromRecipe(Supplier<List<List<ItemStack>>> input, Supplier<List<ItemStack>> output) {
        return new SimpleRecipeRenderer(input, output);
    }
    
    public static SimpleRecipeRenderer fromRecipeEntries(Supplier<List<List<EntryStack>>> input, Supplier<List<EntryStack>> output) {
        return new SimpleRecipeRenderer(input, output, 0);
    }
    
    public static ItemStackRenderer fromItemStacks(List<ItemStack> stacks) {
        return fromItemStacks(() -> stacks, true, null);
    }
    
    public static ItemStackRenderer fromItemStacks(Supplier<List<ItemStack>> stacksSupplier, boolean renderCounts, @Nullable Function<ItemStack, List<String>> extraTooltipSupplier) {
        return fromItemStacks(stacksSupplier, stack -> renderCounts ? null : "", extraTooltipSupplier);
    }
    
    public static ItemStackRenderer fromItemStacks(Supplier<List<ItemStack>> stacksSupplier, @Nullable Function<ItemStack, String> countsFunction, @Nullable Function<ItemStack, List<String>> extraTooltipSupplier) {
        return fromItemStacks(stacksSupplier, countsFunction, extraTooltipSupplier, true);
    }
    
    public static ItemStackRenderer fromItemStacks(Supplier<List<ItemStack>> stacksSupplier, @Nullable Function<ItemStack, String> countsFunction, @Nullable Function<ItemStack, List<String>> extraTooltipSupplier, boolean renderOverlay) {
        List<ItemStack> stacks = stacksSupplier.get().stream().map(ItemStack::copy).collect(Collectors.toList());
        return new ItemStackRenderer() {
            @Override
            public ItemStack getItemStack() {
                if (stacks.isEmpty())
                    return ItemStack.EMPTY;
                return stacks.get(MathHelper.floor((System.currentTimeMillis() / 500 % (double) stacks.size()) / 1f));
            }
            
            @Override
            protected String getCounts() {
                return countsFunction == null ? null : countsFunction.apply(getItemStack());
            }
            
            @Override
            protected boolean renderOverlay() {
                return renderOverlay;
            }
            
            @Override
            protected List<String> getExtraToolTips(ItemStack stack) {
                if (extraTooltipSupplier == null)
                    return super.getExtraToolTips(stack);
                List<String> apply = extraTooltipSupplier.apply(stack);
                if (apply == null)
                    return super.getExtraToolTips(stack);
                return apply;
            }
        };
    }
    
    public static ItemStackRenderer fromItemStacksNoCounts(List<ItemStack> stacks) {
        return fromItemStacks(() -> stacks, false, null);
    }
    
    /**
     * Gets the current blit offset
     *
     * @return the blit offset
     */
    @Override
    public int getBlitOffset() {
        return super.getBlitOffset();
    }
    
    /**
     * Sets the current blit offset
     *
     * @param offset the new blit offset
     */
    @Override
    public void setBlitOffset(int offset) {
        super.setBlitOffset(offset);
    }
    
    /**
     * Renders of the renderable
     *
     * @param x      the x coordinate of the renderable
     * @param y      the y coordinate of the renderable
     * @param mouseX the x coordinate of the mouse
     * @param mouseY the y coordinate of the mouse
     * @param delta  the delta
     */
    public abstract void render(int x, int y, double mouseX, double mouseY, float delta);
    
    public EntryStack getEntry() {
        if (this instanceof ItemStackRenderer)
            return EntryStack.create(((ItemStackRenderer) this).getItemStack());
        if (this instanceof FluidRenderer)
            return EntryStack.create(((FluidRenderer) this).getFluid());
        return EntryStack.empty();
    }
    
    @Nullable
    public QueuedTooltip getQueuedTooltip(float delta) {
        return null;
    }
    
}