-
Notifications
You must be signed in to change notification settings - Fork 18
/
PortalColorHandler.java
84 lines (75 loc) · 2.91 KB
/
PortalColorHandler.java
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
package knightminer.inspirations.tweaks.client;
import knightminer.inspirations.Inspirations;
import knightminer.inspirations.common.Config;
import knightminer.inspirations.library.Util;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.IBeaconBeamColorProvider;
import net.minecraft.client.renderer.chunk.ChunkRenderCache;
import net.minecraft.client.renderer.color.IBlockColor;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IEnviromentBlockReader;
import net.minecraft.world.IWorldReader;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.Set;
/**
* Logic to handle getting the color for a portal from stained glass below
*/
public class PortalColorHandler implements IBlockColor {
private static final int DEFAULT_COLOR = 0x9928FF;
private static final Set<Block> BEACON_COLOR_BLACKLIST = new HashSet<>();
public static final PortalColorHandler INSTANCE = new PortalColorHandler();
private PortalColorHandler() {}
@Override
public int getColor(@Nonnull BlockState state, @Nullable IEnviromentBlockReader world, @Nullable BlockPos pos, int tintValue) {
if (!Config.customPortalColor.get()) {
return -1;
}
if (world == null || pos == null) {
return DEFAULT_COLOR;
}
// Get the real world, not the fake one so we can look at the blocks far enough below us.
if (world instanceof ChunkRenderCache) {
world = ((ChunkRenderCache) world).world;
}
// iterate down until the first non-portal block
// can skip every other block as it takes at least 2 from a portal to below a portal
pos = pos.down();
while(world.getBlockState(pos).getBlock() == Blocks.NETHER_PORTAL) {
pos = pos.down();
}
return getColorValue(world, pos.down());
}
/**
* Gets the color for a block in the world, uses the same logic as beacon beam colors
* @param access Block access
* @param pos Block pos
*/
private static int getColorValue(IEnviromentBlockReader access, BlockPos pos) {
BlockState state = access.getBlockState(pos);
Block block = state.getBlock();
// stained glass
if (block instanceof IBeaconBeamColorProvider) {
return ((IBeaconBeamColorProvider) block).getColor().colorValue;
}
// beacon color fallback
if (!BEACON_COLOR_BLACKLIST.contains(block)) {
if (access instanceof IWorldReader) {
IWorldReader world = (IWorldReader)access;
try {
float[] color = block.getBeaconColorMultiplier(state, world, pos, pos);
if (color != null && color.length == 3) {
return Util.getColorInteger(color);
}
} catch (ClassCastException e) {
Inspirations.log.error("Error getting beacon color for block", e);
BEACON_COLOR_BLACKLIST.add(block);
}
}
}
return DEFAULT_COLOR;
}
}