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
|
/*
* Copyright (C) 2022 NotEnoughUpdates contributors
*
* This file is part of NotEnoughUpdates.
*
* NotEnoughUpdates is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* NotEnoughUpdates is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
*/
package io.github.moulberry.notenoughupdates.miscfeatures.world;
import io.github.moulberry.notenoughupdates.core.util.render.RenderUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.util.BlockPos;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import java.util.HashSet;
import java.util.Set;
public abstract class GenericBlockHighlighter {
protected abstract boolean isEnabled();
protected abstract boolean isValidHighlightSpot(BlockPos key);
protected abstract int getColor(BlockPos blockPos);
public final Set<BlockPos> highlightedBlocks = new HashSet<>();
@SubscribeEvent
public void onWorldRenderLast(RenderWorldLastEvent event) {
if (!isEnabled()) return;
World w = Minecraft.getMinecraft().theWorld;
if (w == null) return;
for (BlockPos blockPos : highlightedBlocks) {
RenderUtils.renderBoundingBox(blockPos, getColor(blockPos), event.partialTicks, true);
}
}
@SubscribeEvent
public void onTick(TickEvent.ClientTickEvent ev) {
if (ev.phase != TickEvent.Phase.END) return;
highlightedBlocks.removeIf(it -> !isValidHighlightSpot(it) ||
!canPlayerSeeNearBlocks(it.getX(), it.getY(), it.getZ()));
}
protected boolean canPlayerSeeBlock(double xCoord, double yCoord, double zCoord) {
EntityPlayerSP p = Minecraft.getMinecraft().thePlayer;
if (p == null) return false;
Vec3 playerPosition = new Vec3(p.posX, p.posY + p.eyeHeight, p.posZ);
MovingObjectPosition hitResult = rayTraceBlocks(p.worldObj, playerPosition, xCoord + 0.5, yCoord + 0.5, zCoord + 0.5);
return canSee(hitResult, new BlockPos(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5));
}
protected boolean canPlayerSeeNearBlocks(double x, double y, double z) {
EntityPlayerSP p = Minecraft.getMinecraft().thePlayer;
if (p == null) return false;
World world = p.worldObj;
Vec3 playerPosition = new Vec3(p.posX, p.posY + p.eyeHeight, p.posZ);
BlockPos blockPos = new BlockPos(x, y, z);
MovingObjectPosition hitResult1 = rayTraceBlocks(world, playerPosition, x, y, z);
if (canSee(hitResult1, blockPos)) return true;
MovingObjectPosition hitResult2 = rayTraceBlocks(world, playerPosition, x + 1, y, z);
if (canSee(hitResult2, blockPos.add(1, 0, 1))) return true;
MovingObjectPosition hitResult3 = rayTraceBlocks(world, playerPosition, x + 1, y + 1, z);
if (canSee(hitResult3, blockPos.add(1, 1, 0))) return true;
MovingObjectPosition hitResult4 = rayTraceBlocks(world, playerPosition, x + 1, y + 1, z + 1);
if (canSee(hitResult4, blockPos.add(1, 1, 1))) return true;
MovingObjectPosition hitResult5 = rayTraceBlocks(world, playerPosition, x, y + 1, z + 1);
if (canSee(hitResult5, blockPos.add(0, 1, 1))) return true;
MovingObjectPosition hitResult6 = rayTraceBlocks(world, playerPosition, x, y + 1, z);
if (canSee(hitResult6, blockPos.add(0, 1, 0))) return true;
MovingObjectPosition hitResult7 = rayTraceBlocks(world, playerPosition, x + 1, y, z + 1);
if (canSee(hitResult7, blockPos.add(1, 0, 1))) return true;
MovingObjectPosition hitResult8 = rayTraceBlocks(world, playerPosition, x, y + 1, z);
if (canSee(hitResult8, blockPos.add(0, 1, 0))) return true;
return false;
}
private static boolean canSee(MovingObjectPosition hitResult, BlockPos bp) {
return hitResult == null
|| hitResult.typeOfHit != MovingObjectPosition.MovingObjectType.BLOCK
|| bp.equals(hitResult.getBlockPos());
}
private static MovingObjectPosition rayTraceBlocks(World world, Vec3 playerPosition, double x, double y, double z) {
return world.rayTraceBlocks(playerPosition, new Vec3(x, y, z), false, true, true);
}
@SubscribeEvent
public void onWorldChange(WorldEvent.Unload event) {
highlightedBlocks.clear();
}
public boolean tryRegisterInterest(BlockPos pos) {
return tryRegisterInterest(pos.getX(), pos.getY(), pos.getZ());
}
public boolean tryRegisterInterest(double x, double y, double z) {
BlockPos blockPos = new BlockPos(x, y, z);
boolean contains = highlightedBlocks.contains(blockPos);
if (!contains) {
boolean canSee = canPlayerSeeNearBlocks(blockPos.getX(), blockPos.getY(), blockPos.getZ());
if (isValidHighlightSpot(blockPos) && canSee) {
highlightedBlocks.add(blockPos);
}
}
return contains;
}
}
|