Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 6bee13c

Browse files
authored
Add DrawBlockHighlightEvent(DrawHighlightEvent) (#116)
* Add DrawBlockHighlightEvent(DrawHighlightEvent) * Update RenderEvents.java
1 parent de525e1 commit 6bee13c

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
archivesBaseName = "patchwork-client-colors"
22
version = getSubprojectVersion(project, "0.1.0")
3+
4+
dependencies {
5+
compile project(path: ':patchwork-fml', configuration: 'dev')
6+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.client.event;
21+
22+
import net.minecraftforge.eventbus.api.Event;
23+
24+
import net.minecraft.client.render.Camera;
25+
import net.minecraft.client.render.WorldRenderer;
26+
import net.minecraft.util.hit.BlockHitResult;
27+
import net.minecraft.util.hit.EntityHitResult;
28+
import net.minecraft.util.hit.HitResult;
29+
30+
/**
31+
* An event called whenever the selection highlight around blocks is about to be rendered.
32+
* Canceling this event stops the selection highlight from being rendered.
33+
*/
34+
//TODO: in 1.15 rename to DrawHighlightEvent
35+
public class DrawBlockHighlightEvent extends Event {
36+
private final WorldRenderer context;
37+
private final Camera info;
38+
private final HitResult target;
39+
private final int subID;
40+
private final float partialTicks;
41+
42+
public DrawBlockHighlightEvent(WorldRenderer context, Camera info, HitResult target, int subID, float partialTicks) {
43+
this.context = context;
44+
this.info = info;
45+
this.target = target;
46+
this.subID = subID;
47+
this.partialTicks = partialTicks;
48+
}
49+
50+
public WorldRenderer getContext() {
51+
return context;
52+
}
53+
54+
public Camera getInfo() {
55+
return info;
56+
}
57+
58+
public HitResult getTarget() {
59+
return target;
60+
}
61+
62+
public int getSubID() {
63+
return subID;
64+
}
65+
66+
public float getPartialTicks() {
67+
return partialTicks;
68+
}
69+
70+
@Override
71+
public boolean isCancelable() {
72+
return true;
73+
}
74+
75+
/**
76+
* A variant of the DrawHighlightEvent only called when a block is highlighted.
77+
*/
78+
public static class HighlightBlock extends DrawBlockHighlightEvent {
79+
public HighlightBlock(WorldRenderer context, Camera info, HitResult target, int subID, float partialTicks) {
80+
super(context, info, target, subID, partialTicks);
81+
}
82+
83+
@Override
84+
public BlockHitResult getTarget() {
85+
return (BlockHitResult) super.target;
86+
}
87+
}
88+
89+
/**
90+
* A variant of the DrawHighlightEvent only called when an entity is highlighted.
91+
* Canceling this event has no effect.
92+
*/
93+
public static class HighlightEntity extends DrawBlockHighlightEvent {
94+
public HighlightEntity(WorldRenderer context, Camera info, HitResult target, int subID, float partialTicks) {
95+
super(context, info, target, subID, partialTicks);
96+
}
97+
98+
@Override
99+
public EntityHitResult getTarget() {
100+
return (EntityHitResult) super.target;
101+
}
102+
}
103+
}

patchwork-events-rendering/src/main/java/net/patchworkmc/impl/event/render/RenderEvents.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@
2323
import java.util.function.Consumer;
2424

2525
import net.minecraftforge.client.event.ColorHandlerEvent;
26+
import net.minecraftforge.client.event.DrawBlockHighlightEvent;
2627
import net.minecraftforge.client.event.TextureStitchEvent;
28+
import net.minecraftforge.common.MinecraftForge;
2729
import net.minecraftforge.eventbus.api.Event;
2830

2931
import net.minecraft.client.color.block.BlockColors;
3032
import net.minecraft.client.color.item.ItemColors;
33+
import net.minecraft.client.render.Camera;
34+
import net.minecraft.client.render.WorldRenderer;
3135
import net.minecraft.client.texture.SpriteAtlasTexture;
3236
import net.minecraft.util.Identifier;
37+
import net.minecraft.util.hit.BlockHitResult;
38+
import net.minecraft.util.hit.EntityHitResult;
39+
import net.minecraft.util.hit.HitResult;
3340

3441
public class RenderEvents {
3542
private static Consumer<Event> eventDispatcher;
@@ -53,4 +60,21 @@ public static void onTextureStitchPre(SpriteAtlasTexture spriteAtlasTexture, Set
5360
public static void onTextureStitchPost(SpriteAtlasTexture spriteAtlasTexture) {
5461
eventDispatcher.accept(new TextureStitchEvent.Post(spriteAtlasTexture));
5562
}
63+
64+
/**
65+
* Called by ForgeHooksClient and MixinWorldRenderer.
66+
* @return true if the bounding box rendering is cancelled.
67+
*/
68+
public static boolean onDrawHighlightEvent(WorldRenderer context, Camera info, HitResult target, int subID, float partialTicks) {
69+
switch (target.getType()) {
70+
case BLOCK:
71+
if (!(target instanceof BlockHitResult)) return false;
72+
return MinecraftForge.EVENT_BUS.post(new DrawBlockHighlightEvent.HighlightBlock(context, info, target, subID, partialTicks));
73+
case ENTITY:
74+
if (!(target instanceof EntityHitResult)) return false;
75+
return MinecraftForge.EVENT_BUS.post(new DrawBlockHighlightEvent.HighlightEntity(context, info, target, subID, partialTicks));
76+
default:
77+
return MinecraftForge.EVENT_BUS.post(new DrawBlockHighlightEvent(context, info, target, subID, partialTicks));
78+
}
79+
}
5680
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.patchworkmc.mixin.event.render;
21+
22+
import org.spongepowered.asm.mixin.Mixin;
23+
import org.spongepowered.asm.mixin.injection.At;
24+
import org.spongepowered.asm.mixin.injection.Redirect;
25+
26+
import net.minecraft.client.MinecraftClient;
27+
import net.minecraft.client.render.Camera;
28+
import net.minecraft.client.render.GameRenderer;
29+
import net.minecraft.client.render.WorldRenderer;
30+
import net.minecraft.util.hit.HitResult;
31+
32+
import net.patchworkmc.impl.event.render.RenderEvents;
33+
34+
@Mixin(GameRenderer.class)
35+
public abstract class MixinGameRenderer {
36+
@Redirect(
37+
method = "renderCenter", at = @At(value = "INVOKE", ordinal = 0,
38+
target = "net/minecraft/client/render/WorldRenderer.drawHighlightedBlockOutline(Lnet/minecraft/client/render/Camera;Lnet/minecraft/util/hit/HitResult;I)V"
39+
)
40+
)
41+
private void patchwork_renderCenter_drawHighlightedBlockOutline(WorldRenderer worldRenderer, Camera camera, HitResult hit, int renderPass, float partialTicks, long nanoTime) {
42+
MinecraftClient client = ((GameRenderer) (Object) this).getClient();
43+
44+
if (!RenderEvents.onDrawHighlightEvent(worldRenderer, camera, hit, 0, partialTicks)) {
45+
worldRenderer.drawHighlightedBlockOutline(camera, client.crosshairTarget, 0);
46+
}
47+
}
48+
}

patchwork-events-rendering/src/main/resources/patchwork-events-rendering.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"client": [
66
"MixinItemColors",
77
"MixinBlockColors",
8+
"MixinGameRenderer",
89
"MixinSpriteAtlasTexture"
910
],
1011
"injectors": {

0 commit comments

Comments
 (0)