/
BuildingClientEvents.java
175 lines (154 loc) · 6.97 KB
/
BuildingClientEvents.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
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
package knightminer.inspirations.building;
import knightminer.inspirations.Inspirations;
import knightminer.inspirations.building.block.type.BushType;
import knightminer.inspirations.building.block.type.ShelfType;
import knightminer.inspirations.building.tileentity.BookshelfTileEntity;
import knightminer.inspirations.common.ClientEvents;
import knightminer.inspirations.library.Util;
import knightminer.inspirations.library.client.ClientUtil;
import knightminer.inspirations.shared.client.BackgroundContainerScreen;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.RenderTypeLookup;
import net.minecraft.client.renderer.color.BlockColors;
import net.minecraft.client.renderer.color.ItemColors;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.FoliageColors;
import net.minecraft.world.biome.BiomeColors;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.ColorHandlerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import slimeknights.mantle.item.RetexturedBlockItem;
import slimeknights.mantle.tileentity.IRetexturedTileEntity;
import slimeknights.mantle.util.TileEntityHelper;
import java.util.function.Consumer;
@SuppressWarnings("unused")
@EventBusSubscriber(modid = Inspirations.modID, value = Dist.CLIENT, bus = Bus.MOD)
public class BuildingClientEvents extends ClientEvents {
private static final Minecraft mc = Minecraft.getInstance();
@SubscribeEvent
static void clientSetup(FMLClientSetupEvent event) {
// set render types
RenderType cutout = RenderType.getCutout();
Consumer<Block> setCutout = (block) -> RenderTypeLookup.setRenderLayer(block, cutout);
RenderType cutoutMipped = RenderType.getCutoutMipped();
Consumer<Block> setCutoutMipped = (block) -> RenderTypeLookup.setRenderLayer(block, cutoutMipped);
// general
InspirationsBuilding.bookshelf.forEach(setCutout);
InspirationsBuilding.enlightenedBush.forEach(setCutoutMipped);
// ropes
setRenderLayer(InspirationsBuilding.rope, cutout);
setRenderLayer(InspirationsBuilding.vine, cutout);
setRenderLayer(InspirationsBuilding.ironBars, cutoutMipped);
// doors
setRenderLayer(InspirationsBuilding.glassDoor, cutoutMipped);
setRenderLayer(InspirationsBuilding.glassTrapdoor, cutoutMipped);
// flower
InspirationsBuilding.flower.forEach(setCutout);
InspirationsBuilding.flowerPot.forEach(setCutout);
}
@SubscribeEvent
static void commonSetup(FMLCommonSetupEvent event) {
// Register GUIs.
registerScreenFactory(InspirationsBuilding.contBookshelf, new BackgroundContainerScreen.Factory<>(156, "bookshelf"));
}
@SubscribeEvent
static void registerBlockColors(ColorHandlerEvent.Block event) {
BlockColors blockColors = event.getBlockColors();
// coloring of books for normal bookshelf
registerBlockColors(blockColors, (state, world, pos, tintIndex) -> {
if (tintIndex > 0 && tintIndex <= 14 && world != null && pos != null) {
TileEntity te = world.getTileEntity(pos);
if (te instanceof BookshelfTileEntity) {
ItemStack stack = ((BookshelfTileEntity)te).getStackInSlot(tintIndex - 1);
if (!stack.isEmpty()) {
int color = ClientUtil.getItemColor(stack.getItem());
int itemColors = mc.getItemColors().getColor(stack, 0);
if (itemColors > -1) {
// combine twice to make sure the item colors result is dominant
color = Util.combineColors(color, itemColors, 3);
}
return color;
}
}
}
return -1;
}, InspirationsBuilding.bookshelf.getOrNull(ShelfType.NORMAL));
// rope vine coloring
registerBlockColors(blockColors, (state, world, pos, tintIndex) -> {
if (world != null && pos != null) {
return BiomeColors.getFoliageColor(world, pos);
}
return FoliageColors.getDefault();
}, InspirationsBuilding.vine);
// bush block coloring
// First the three which never change tint.
InspirationsBuilding.enlightenedBush.forEach((type, bush) -> {
if (type != BushType.WHITE) {
int color = type.getColor(); // Make closure capture just the int.
blockColors.register((state, world, pos, tintIndex) -> tintIndex == 0 ? color : -1, bush);
}
});
// white copies the default leaf colors
registerBlockColors(blockColors, (state, world, pos, tintIndex) -> {
if (tintIndex != 0 || world == null || pos == null) {
return -1;
}
// TODO: should probably pass block directly here
IRetexturedTileEntity te = TileEntityHelper.getTileEntity(IRetexturedTileEntity.class, world, pos);
if (te != null) {
Block block = te.getTexture();
if (block != Blocks.AIR) {
return ClientUtil.getStackBlockColorsSafe(new ItemStack(block), world, pos, 0);
}
}
return FoliageColors.getDefault();
}, InspirationsBuilding.enlightenedBush.getOrNull(BushType.WHITE));
}
@SubscribeEvent
static void registerItemColors(ColorHandlerEvent.Item event) {
ItemColors itemColors = event.getItemColors();
// coloring of books for normal bookshelf
registerItemColors(itemColors, (stack, tintIndex) -> {
if (tintIndex > 0 && tintIndex <= 14) {
return 0x654B17;
}
return -1;
}, InspirationsBuilding.bookshelf.getOrNull(ShelfType.NORMAL));
// book covers, too lazy to make 16 cover textures
InspirationsBuilding.coloredBooks.forEach((color, book) -> {
int hexColor = color.colorValue;
itemColors.register((stack, tintIndex) -> (tintIndex == 0) ? hexColor : -1, book);
});
// bush block colors
// First the three blocks which never change tint.
InspirationsBuilding.enlightenedBush.forEach((type, bush) -> {
if (type != BushType.WHITE) {
int color = type.getColor();
itemColors.register((stack, tintIndex) -> tintIndex == 0 ? color : -1, bush);
}
});
// The main one uses the tint of the textured stack
registerItemColors(itemColors, (stack, tintIndex) -> {
if (tintIndex != 0) {
return -1;
}
// redirect to block for colors
Block block = RetexturedBlockItem.getTexture(stack);
if (block != Blocks.AIR) {
return itemColors.getColor(new ItemStack(block), 0);
} else {
return FoliageColors.getDefault();
}
}, InspirationsBuilding.enlightenedBush.getOrNull(BushType.WHITE));
// We can't get the world position of the item, so use the default tint.
registerItemColors(itemColors, (stack, tintIndex) -> FoliageColors.getDefault(), InspirationsBuilding.vine);
}
}