/
BuildingClientEvents.java
220 lines (195 loc) · 8.64 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package knightminer.inspirations.building;
import knightminer.inspirations.Inspirations;
import knightminer.inspirations.building.block.BookshelfBlock;
import knightminer.inspirations.building.block.type.BushType;
import knightminer.inspirations.building.block.type.ShelfType;
import knightminer.inspirations.building.client.BookshelfModel;
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.library.util.TextureBlockUtil;
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.client.renderer.model.ModelResourceLocation;
import net.minecraft.item.ItemStack;
import net.minecraft.resources.IReloadableResourceManager;
import net.minecraft.resources.IResourceManager;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
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.client.event.ModelBakeEvent;
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 java.util.Objects;
import java.util.concurrent.CompletableFuture;
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) {
// listener to clear bookshelf model cache as its shared by all bookshelf model files
IResourceManager manager = Minecraft.getInstance().getResourceManager();
// should always be true, but just in case
if(manager instanceof IReloadableResourceManager) {
((IReloadableResourceManager) manager).addReloadListener(
(stage, resMan, prepProp, reloadProf, bgExec, gameExec) -> CompletableFuture
.runAsync(BookshelfModel.BOOK_CACHE::invalidateAll, gameExec)
.thenCompose(stage::markCompleteAwaitingOthers)
);
} else {
Inspirations.log.error("Failed to register resource reload listener, expected instance of IReloadableResourceManager but got {}", manager.getClass());
}
// 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;
}
TileEntity te = world.getTileEntity(pos);
if(te != null) {
Block block = TextureBlockUtil.getTextureBlock(te);
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 = TextureBlockUtil.getTextureBlock(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);
}
/**
* Replaces the bookshelf models with the dynamic texture model, which also handles books
*/
@SubscribeEvent
static void onModelBake(ModelBakeEvent event) {
for (BookshelfBlock block : InspirationsBuilding.bookshelf.values()) {
replaceBookshelfModel(event, block);
}
for (Block block : InspirationsBuilding.enlightenedBush.values()) {
replaceBothTexturedModels(event, Objects.requireNonNull(block.getRegistryName()), "leaves");
}
}
@Deprecated
private static void replaceBookshelfModel(ModelBakeEvent event, BookshelfBlock shelf) {
if (shelf.getRegistryName() == null) {
throw new AssertionError("Null registry name");
}
for(Direction facing : Direction.Plane.HORIZONTAL){
ModelResourceLocation location = new ModelResourceLocation(shelf.getRegistryName(), String.format("facing=%s", facing.getString()));
replaceModel(event, location, (loader, model) -> new BookshelfModel(location, loader, model));
}
replaceTexturedModel(event, new ModelResourceLocation(shelf.getRegistryName(), "inventory"), "texture",true);
}
}