Skip to content

Commit

Permalink
Clean up cauldron model state mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed Jun 30, 2018
1 parent 35f00cd commit 7be5fd9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 47 deletions.
@@ -1,16 +1,26 @@
package knightminer.inspirations.recipes;

import java.util.LinkedHashMap;
import javax.annotation.Nonnull;

import com.google.common.collect.Maps;

import knightminer.inspirations.common.ClientProxy;
import knightminer.inspirations.common.Config;
import knightminer.inspirations.library.Util;
import knightminer.inspirations.library.client.NameStateMapper;
import knightminer.inspirations.recipes.block.BlockEnhancedCauldron;
import knightminer.inspirations.recipes.tileentity.TileCauldron;
import net.minecraft.block.BlockCauldron;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
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.client.renderer.texture.TextureMap;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.ColorHandlerEvent;
import net.minecraftforge.client.event.ModelBakeEvent;
Expand All @@ -21,16 +31,10 @@
public class RecipesClientProxy extends ClientProxy {
private static final ResourceLocation POTION_MODEL = new ResourceLocation("bottle_drinkable");
public static final ResourceLocation CAULDRON_MODEL = Util.getResource("cauldron");
public static final ResourceLocation CAULDRON_MODEL_BIGGER = Util.getResource("cauldron_bigger");

@SubscribeEvent
public void registerModels(ModelRegistryEvent event) {
if(Config.enableBiggerCauldron) {
setModelStateMapper(InspirationsRecipes.cauldron, new NameStateMapper(CAULDRON_MODEL_BIGGER, BlockCauldron.LEVEL));
} else {
setModelStateMapper(InspirationsRecipes.cauldron, new NameStateMapper(CAULDRON_MODEL));
}

setModelStateMapper(InspirationsRecipes.cauldron, new CauldronStateMapper(CAULDRON_MODEL));
if(Config.enableCauldronDyeing) {
registerItemModel(InspirationsRecipes.dyedWaterBottle, POTION_MODEL);
}
Expand Down Expand Up @@ -76,23 +80,11 @@ public void onModelBake(ModelBakeEvent event) {
return;
}

String level;
int max;
ResourceLocation base;
if(Config.enableBiggerCauldron) {
level = "levels";
max = 4;
base = CAULDRON_MODEL_BIGGER;
} else {
level = "level";
max = 3;
base = CAULDRON_MODEL;
}

boolean boiling = false;
do {
for(int i = 1; i <= max; i++) {
replaceTexturedModel(event, new ModelResourceLocation(base, String.format("boiling=%s,contents=fluid,%s=%s", boiling, level, i)), "water", false);
replaceTexturedModel(event, new ModelResourceLocation(CAULDRON_MODEL, String.format("boiling=%s,contents=fluid,level=empty", boiling)), "water", false);
for(int i = (Config.enableBiggerCauldron ? 0 : 1); i <= 3; i++) {
replaceTexturedModel(event, new ModelResourceLocation(CAULDRON_MODEL, String.format("boiling=%s,contents=fluid,level=%s", boiling, i)), "water", false);
}
boiling = !boiling;
} while(boiling);
Expand All @@ -103,4 +95,60 @@ public void registerTextures(TextureStitchEvent.Pre event) {
TextureMap map = event.getMap();
registerFluidTexture(map, InspirationsRecipes.milk);
}

/**
* Mapper for redstone torch levers, to combine the two blocks as if its all one block
*/
public static class CauldronStateMapper extends StateMapperBase {
private static final PropertyEnum<CauldronLevel> LEVEL = PropertyEnum.create("level", CauldronLevel.class);
private ResourceLocation location;
public CauldronStateMapper(ResourceLocation location) {
this.location = location;
}

@Nonnull
@Override
protected ModelResourceLocation getModelResourceLocation(@Nonnull IBlockState state) {
LinkedHashMap<IProperty<?>, Comparable<?>> map = Maps.newLinkedHashMap(state.getProperties());
if(Config.enableBiggerCauldron) {
map.put(LEVEL, CauldronLevel.forLevel(state.getValue(BlockEnhancedCauldron.LEVELS)));
map.remove(BlockCauldron.LEVEL);
map.remove(BlockEnhancedCauldron.LEVELS);
} else {
map.put(LEVEL, CauldronLevel.forLevel(state.getValue(BlockCauldron.LEVEL)));
map.remove(BlockCauldron.LEVEL);
}

return new ModelResourceLocation(location, this.getPropertyString(map));
}

private static enum CauldronLevel implements IStringSerializable {
EMPTY,
LEVEL0,
LEVEL1,
LEVEL2,
LEVEL3;

public static CauldronLevel forLevel(int level) {
// validate meta
if(level < 0 || level > 4) {
level = 0;
}
// if a regular cauldron, add one to the index, as the second spot is 0 instead of 1
if(!Config.enableBiggerCauldron && level != 0) {
level = level + 1;
}
return values()[level];
}

@Override
public String getName() {
if(this == EMPTY) {
return "empty";
}
// removes 1 so the meta matches up with the vanilla levels
return "" + (this.ordinal() - 1);
}
}
}
}
Expand Up @@ -3,7 +3,8 @@
"defaults": { "model": "inspirations:cauldron/empty" },
"variants": {
"level": {
"0": {},
"empty": {},
"0": { "model": "inspirations:cauldron/level_0" },
"1": { "model": "inspirations:cauldron/level_1" },
"2": { "model": "inspirations:cauldron/level_2" },
"3": { "model": "inspirations:cauldron/level_3" }
Expand Down

This file was deleted.

0 comments on commit 7be5fd9

Please sign in to comment.