/
TweaksClientProxy.java
215 lines (191 loc) · 7.48 KB
/
TweaksClientProxy.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
package knightminer.inspirations.tweaks;
import java.util.LinkedHashMap;
import com.google.common.collect.Maps;
import knightminer.inspirations.common.ClientProxy;
import knightminer.inspirations.common.Config;
import knightminer.inspirations.common.PulseBase;
import knightminer.inspirations.library.Util;
import knightminer.inspirations.library.client.ClientUtil;
import knightminer.inspirations.library.client.PropertyStateMapper;
import knightminer.inspirations.recipes.RecipesClientProxy;
import knightminer.inspirations.tweaks.block.BlockBetterFlowerPot;
import knightminer.inspirations.tweaks.block.BlockFittedCarpet;
import net.minecraft.block.BlockCarpet;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.block.statemap.StateMapperBase;
import net.minecraft.client.renderer.color.BlockColors;
import net.minecraft.client.renderer.color.ItemColors;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemEnchantedBook;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityFlowerPot;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.biome.BiomeColorHelper;
import net.minecraftforge.client.event.ColorHandlerEvent;
import net.minecraftforge.client.event.ModelBakeEvent;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
public class TweaksClientProxy extends ClientProxy {
private static final ResourceLocation CARPET_MODEL = Util.getResource("carpet");
private static final ResourceLocation CAULDRON_ITEM_MODEL = Util.getResource("cauldron_item");
private static final ResourceLocation ENCHANTED_BOOK = Util.getResource("enchanted_book");
private static final ResourceLocation FIREWORKS = Util.getResource("fireworks");
private static final ModelResourceLocation FLOWER_POT_MODEL = new ModelResourceLocation(Util.getResource("flower_pot"), "normal");
@SubscribeEvent
public void registerModels(ModelRegistryEvent event) {
setModelStateMapper(InspirationsTweaks.carpet, new PropertyStateMapper(CARPET_MODEL,
BlockCarpet.COLOR,
BlockFittedCarpet.NORTHWEST, BlockFittedCarpet.NORTHEAST, BlockFittedCarpet.SOUTHWEST, BlockFittedCarpet.SOUTHEAST
));
setModelStateMapper(InspirationsTweaks.flowerPot, new FlowerPotStateMapper());
registerItemModel(InspirationsTweaks.cactusSeeds);
registerItemModel(InspirationsTweaks.carrotSeeds);
registerItemModel(InspirationsTweaks.potatoSeeds);
registerItemModel(InspirationsTweaks.sugarCaneSeeds);
if(Config.betterCauldronItem) {
// if recipes is loaded, pull that model as there is a chance the two are different
// the extended cauldron needed to replace the model to add tintindex's
ResourceLocation model = CAULDRON_ITEM_MODEL;
if(PulseBase.isRecipesLoaded() && Config.enableExtendedCauldron) {
model = RecipesClientProxy.CAULDRON_MODEL;
}
registerItemModel(Items.CAULDRON, 0, model);
}
if(Config.coloredEnchantedRibbons) {
registerItemModel(Items.ENCHANTED_BOOK, ENCHANTED_BOOK);
}
if(Config.coloredFireworkItems) {
registerItemModel(Items.FIREWORKS, 0, FIREWORKS);
}
}
@SubscribeEvent
public void registerBlockColors(ColorHandlerEvent.Block event) {
BlockColors colors = event.getBlockColors();
// the vanilla flower pot handler is pretty dumb, it only uses the item and not the specific stack with meta
if(Config.betterFlowerPot) {
registerBlockColors(colors, (state, world, pos, index) -> {
if (world != null && pos != null) {
TileEntity tileentity = world.getTileEntity(pos);
if (tileentity instanceof TileEntityFlowerPot) {
ItemStack stack = ((TileEntityFlowerPot)tileentity).getFlowerItemStack();
return ClientUtil.getStackBlockColorsSafe(stack, world, pos, 0);
}
}
return -1;
}, Blocks.FLOWER_POT);
}
// coloring on sugar cane crop to match reeds
registerBlockColors(colors, (state, world, pos, index) -> {
if(world == null || pos == null) {
return -1;
}
return BiomeColorHelper.getGrassColorAtPos(world, pos);
}, InspirationsTweaks.sugarCaneCrop);
}
@SubscribeEvent
public void registerItemColors(ColorHandlerEvent.Item event) {
ItemColors itemColors = event.getItemColors();
// colored ribbons on enchanted books
if(Config.coloredEnchantedRibbons) {
registerItemColors(itemColors, (stack, tintIndex) -> {
if(tintIndex == 0) {
// find the rarest enchantment we have
Enchantment.Rarity rarity = Enchantment.Rarity.COMMON;
for(NBTBase tag : ItemEnchantedBook.getEnchantments(stack)) {
if(tag.getId() == 10) {
int id = ((NBTTagCompound) tag).getShort("id");
Enchantment enchantment = Enchantment.getEnchantmentByID(id);
if(enchantment != null) {
Enchantment.Rarity newRarity = enchantment.getRarity();
if(newRarity != null && newRarity.getWeight() < rarity.getWeight()) {
rarity = newRarity;
}
}
}
}
// color by that rarity
switch(rarity) {
case COMMON: return 0xFF2151;
case UNCOMMON: return 0xE2882D;
case RARE: return 0x00FF21;
case VERY_RARE: return 0x9F7FFF;
}
}
return -1;
}, Items.ENCHANTED_BOOK);
}
if(Config.coloredFireworkItems) {
registerItemColors(itemColors, (stack, tintIndex) -> {
NBTTagCompound nbt = stack.getSubCompound("Fireworks");
// string is darker with more gunpowder
if(tintIndex == 2) {
if (nbt != null && nbt.hasKey("Flight", 99)) {
byte flight = nbt.getByte("Flight");
switch(flight) {
case 1:
return 0x808080;
case 2:
return 0x606060;
case 3:
return 0x303030;
}
if(flight > 3) {
return 0x000000;
}
}
return 0xA0A0A0;
}
// color the stripes and the top
if(tintIndex == 0 || tintIndex == 1) {
// no NBT?
int missing = tintIndex == 1 ? 0xCCA190 : 0xC0C0C0;
if(nbt == null) {
return missing;
}
NBTTagList stars = nbt.getTagList("Explosions", 10);
// not enough stars?
if(tintIndex >= stars.tagCount()) {
return missing;
}
// grab the proper star's first color
NBTTagCompound star = stars.getCompoundTagAt(tintIndex);
int[] colors = star.getIntArray("Colors");
if (colors.length > 0) {
return colors[0];
}
return missing;
}
return -1;
}, Items.FIREWORKS);
}
}
/**
* Replaces the bookshelf models with the dynamic texture model, which also handles books
*/
@SubscribeEvent
public void onModelBake(ModelBakeEvent event) {
if(InspirationsTweaks.flowerPot != null) {
replaceTexturedModel(event, FLOWER_POT_MODEL, "plant", false);
}
}
private static class FlowerPotStateMapper extends StateMapperBase {
@Override
protected ModelResourceLocation getModelResourceLocation(IBlockState state) {
if(state.getValue(BlockBetterFlowerPot.EXTRA)) {
return FLOWER_POT_MODEL;
}
LinkedHashMap<IProperty<?>, Comparable<?>> map = Maps.newLinkedHashMap(state.getProperties());
map.remove(BlockBetterFlowerPot.EXTRA);
map.remove(BlockBetterFlowerPot.LEGACY_DATA);
return new ModelResourceLocation(state.getBlock().getRegistryName(), this.getPropertyString(map));
}
}
}