Skip to content

Commit a524f48

Browse files
authored
Merge pull request #307 from Dragon-Seeker/1.21-ExclusionZoneAdjustments
[1.21] Adjust Exclusion Area code for REI and EMI
2 parents 6be8b83 + 2d7812f commit a524f48

File tree

3 files changed

+36
-48
lines changed

3 files changed

+36
-48
lines changed

src/main/java/io/wispforest/owo/compat/emi/OwoEmiPlugin.java

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@
55
import dev.emi.emi.api.widget.Bounds;
66
import io.wispforest.owo.itemgroup.OwoItemGroup;
77
import io.wispforest.owo.mixin.itemgroup.CreativeInventoryScreenAccessor;
8-
import io.wispforest.owo.mixin.ui.access.BaseOwoHandledScreenAccessor;
9-
import io.wispforest.owo.ui.core.Component;
10-
import io.wispforest.owo.ui.core.ParentComponent;
11-
import io.wispforest.owo.ui.core.Surface;
8+
import io.wispforest.owo.ui.base.BaseOwoHandledScreen;
129
import io.wispforest.owo.util.pond.OwoCreativeInventoryScreenExtensions;
1310
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
1411

15-
import java.util.ArrayList;
16-
1712
public class OwoEmiPlugin implements EmiPlugin {
1813
@Override
1914
public void register(EmiRegistry registry) {
@@ -36,22 +31,11 @@ public void register(EmiRegistry registry) {
3631
});
3732

3833
registry.addGenericExclusionArea((screen, consumer) -> {
39-
if (screen.children().isEmpty() || !(screen instanceof BaseOwoHandledScreenAccessor accessor)) return;
40-
41-
var adapter = accessor.owo$getUIAdapter();
42-
if (adapter == null) return;
43-
44-
var rootComponent = adapter.rootComponent;
45-
var children = new ArrayList<Component>();
46-
rootComponent.collectDescendants(children);
47-
children.remove(rootComponent);
48-
49-
children.forEach(component -> {
50-
if (component instanceof ParentComponent parent && parent.surface() == Surface.BLANK) return;
34+
if (!(screen instanceof BaseOwoHandledScreen<?, ?> owoHandledScreen)) return;
5135

52-
var size = component.fullSize();
53-
consumer.accept(new Bounds(component.x(), component.y(), size.width(), size.height()));
54-
});
36+
owoHandledScreen.componentsForExclusionAreas()
37+
.map(component -> new Bounds(component.x(), component.y(), component.width(), component.height()))
38+
.forEach(consumer);
5539
});
5640
}
5741
}

src/main/java/io/wispforest/owo/compat/rei/OwoReiPlugin.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import io.wispforest.owo.mixin.itemgroup.CreativeInventoryScreenAccessor;
77
import io.wispforest.owo.mixin.ui.access.BaseOwoHandledScreenAccessor;
88
import io.wispforest.owo.ui.base.BaseOwoHandledScreen;
9-
import io.wispforest.owo.ui.core.Component;
109
import io.wispforest.owo.ui.core.OwoUIDrawContext;
1110
import io.wispforest.owo.ui.core.ParentComponent;
1211
import io.wispforest.owo.ui.core.Surface;
@@ -28,7 +27,6 @@
2827

2928
import java.util.ArrayList;
3029
import java.util.Collections;
31-
import java.util.List;
3230

3331
public class OwoReiPlugin implements REIClientPlugin {
3432

@@ -59,24 +57,9 @@ public void registerExclusionZones(ExclusionZones zones) {
5957
});
6058

6159
zones.register(BaseOwoHandledScreen.class, screen -> {
62-
if (screen.children().isEmpty()) return List.of();
63-
64-
var adapter = ((BaseOwoHandledScreenAccessor) screen).owo$getUIAdapter();
65-
if (adapter == null) return List.of();
66-
67-
var rootComponent = adapter.rootComponent;
68-
var children = new ArrayList<Component>();
69-
rootComponent.collectDescendants(children);
70-
children.remove(rootComponent);
71-
72-
var rectangles = new ArrayList<Rectangle>();
73-
children.forEach(component -> {
74-
if (component instanceof ParentComponent parent && parent.surface() == Surface.BLANK) return;
75-
76-
var size = component.fullSize();
77-
rectangles.add(new Rectangle(component.x(), component.y(), size.width(), size.height()));
78-
});
79-
return rectangles;
60+
return ((BaseOwoHandledScreen<?, ?>) screen).componentsForExclusionAreas()
61+
.map(rect -> new Rectangle(rect.x(), rect.y(), rect.width(), rect.height()))
62+
.toList();
8063
});
8164
}
8265

src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
import net.minecraft.screen.ScreenHandler;
1616
import net.minecraft.screen.slot.Slot;
1717
import net.minecraft.text.Text;
18+
import org.jetbrains.annotations.ApiStatus;
1819
import org.jetbrains.annotations.NotNull;
1920
import org.jetbrains.annotations.Nullable;
2021
import org.lwjgl.glfw.GLFW;
2122
import org.lwjgl.opengl.GL11;
2223

24+
import java.util.ArrayList;
2325
import java.util.function.BiFunction;
26+
import java.util.stream.Stream;
2427

2528
public abstract class BaseOwoHandledScreen<R extends ParentComponent, S extends ScreenHandler> extends HandledScreen<S> implements DisposableScreen {
2629

@@ -162,6 +165,24 @@ protected <C extends Component> C component(Class<C> expectedClass, String id) {
162165
return this.uiAdapter.rootComponent.childById(expectedClass, id);
163166
}
164167

168+
/**
169+
* Compute a stream of all components for which to
170+
* generate exclusion areas in a recipe viewer overlay.
171+
* Called by the REI and EMI plugins
172+
*/
173+
@ApiStatus.OverrideOnly
174+
public Stream<Component> componentsForExclusionAreas() {
175+
if (this.children().isEmpty()) return Stream.of();
176+
177+
var rootComponent = uiAdapter.rootComponent;
178+
var children = new ArrayList<Component>();
179+
180+
rootComponent.collectDescendants(children);
181+
children.remove(rootComponent);
182+
183+
return children.stream().filter(component -> !(component instanceof ParentComponent parent) || parent.surface() != Surface.BLANK);
184+
}
185+
165186
@Override
166187
public void renderBackground(DrawContext context, int mouseX, int mouseY, float delta) {}
167188

@@ -179,12 +200,12 @@ public void render(DrawContext vanillaContext, int mouseX, int mouseY, float del
179200
if (!slot.isEnabled()) continue;
180201

181202
context.drawText(Text.literal("H:" + i),
182-
this.x + slot.x + 15, this.y + slot.y + 9, .5f, 0x0096FF,
183-
OwoUIDrawContext.TextAnchor.BOTTOM_RIGHT
203+
this.x + slot.x + 15, this.y + slot.y + 9, .5f, 0x0096FF,
204+
OwoUIDrawContext.TextAnchor.BOTTOM_RIGHT
184205
);
185206
context.drawText(Text.literal("I:" + slot.getIndex()),
186-
this.x + slot.x + 15, this.y + slot.y + 15, .5f, 0x5800FF,
187-
OwoUIDrawContext.TextAnchor.BOTTOM_RIGHT
207+
this.x + slot.x + 15, this.y + slot.y + 15, .5f, 0x5800FF,
208+
OwoUIDrawContext.TextAnchor.BOTTOM_RIGHT
188209
);
189210
}
190211

@@ -200,8 +221,8 @@ public void render(DrawContext vanillaContext, int mouseX, int mouseY, float del
200221
@Override
201222
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
202223
if ((modifiers & GLFW.GLFW_MOD_CONTROL) == 0
203-
&& this.uiAdapter.rootComponent.focusHandler().focused() instanceof GreedyInputComponent inputComponent
204-
&& inputComponent.onKeyPress(keyCode, scanCode, modifiers)) {
224+
&& this.uiAdapter.rootComponent.focusHandler().focused() instanceof GreedyInputComponent inputComponent
225+
&& inputComponent.onKeyPress(keyCode, scanCode, modifiers)) {
205226
return true;
206227
}
207228

@@ -252,7 +273,7 @@ public void draw(OwoUIDrawContext context, int mouseX, int mouseY, float partial
252273
GL11.glGetIntegerv(GL11.GL_SCISSOR_BOX, scissor);
253274

254275
((OwoSlotExtension) this.slot).owo$setScissorArea(PositionedRectangle.of(
255-
scissor[0], scissor[1], scissor[2], scissor[3]
276+
scissor[0], scissor[1], scissor[2], scissor[3]
256277
));
257278
}
258279

0 commit comments

Comments
 (0)