Skip to content

Commit

Permalink
Add custom portal colors
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed Jul 26, 2019
1 parent e98fb29 commit a248516
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/main/java/knightminer/inspirations/common/Config.java
Expand Up @@ -246,6 +246,7 @@ public class Config {
public static short milkCooldownTime = 600;
public static boolean caveSpiderDrops = true;
public static boolean skeletonSkull = true;
public static boolean customPortalColor = true;
// heartbeet
public static boolean enableHeartbeet = true;
public static boolean brewHeartbeet = true;
Expand Down Expand Up @@ -527,6 +528,8 @@ public static void preInit(FMLPreInitializationEvent event) {
milkCooldown = configFile.getBoolean("milkCooldown", "tweaks", milkCooldown, "Adds a cooldown to milking cows, prevents practically infinite milk in modded worlds where milk is more useful.");
milkCooldownTime = (short)configFile.getInt("time", "tweaks.milkCooldown", milkCooldownTime, 1, Short.MAX_VALUE, "Delay in seconds after milking a cow before it can be milked again.");

// custom portal color
customPortalColor = configFile.getBoolean("customPortalColor", "tweaks", customPortalColor, "Allows the portal color to be changed by placing colored blocks under the portal. Any block that tints a beacon beam will work for the color.");
}

// compatibility
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/knightminer/inspirations/library/Util.java
Expand Up @@ -22,6 +22,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -157,6 +158,17 @@ public static float[] getColorComponents(int color) {
return new float[] {i / 255.0f, j / 255.0f, k / 255.0f};
}

/**
* Splits a hex color integer into three float color components between 0 and 1
* @param component float color component array, must be length 3
* @return Color integer value
*/
public static int getColorInteger(@Nonnull float[] component) {
return ((int)(component[0] * 255) & 0xFF) << 16
| ((int)(component[1] * 255) & 0xFF) << 8
| ((int)(component[2] * 255) & 0xFF);
}

/**
* Adds the tooltips for the potion type into the given string list
* @param potionType Potion type input
Expand Down
@@ -1,18 +1,17 @@
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.NameStateMapper;
import knightminer.inspirations.library.client.PropertyStateMapper;
import knightminer.inspirations.recipes.RecipesClientProxy;
import knightminer.inspirations.tweaks.block.BlockBetterFlowerPot;
import knightminer.inspirations.tweaks.block.BlockFittedCarpet;
import knightminer.inspirations.tweaks.client.PortalColorHandler;
import net.minecraft.block.BlockCarpet;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.IBlockState;
Expand All @@ -37,12 +36,15 @@
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.util.LinkedHashMap;

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 ResourceLocation PORTAL = Util.getResource("portal");
private static final ModelResourceLocation FLOWER_POT_MODEL = new ModelResourceLocation(Util.getResource("flower_pot"), "normal");

@SubscribeEvent
Expand All @@ -52,6 +54,9 @@ public void registerModels(ModelRegistryEvent event) {
BlockFittedCarpet.NORTHWEST, BlockFittedCarpet.NORTHEAST, BlockFittedCarpet.SOUTHWEST, BlockFittedCarpet.SOUTHEAST
));
setModelStateMapper(InspirationsTweaks.flowerPot, new FlowerPotStateMapper());
if (Config.customPortalColor) {
setModelStateMapper(Blocks.PORTAL, new NameStateMapper(PORTAL));
}

registerItemModel(InspirationsTweaks.cactusSeeds);
registerItemModel(InspirationsTweaks.carrotSeeds);
Expand Down Expand Up @@ -94,6 +99,11 @@ public void registerBlockColors(ColorHandlerEvent.Block event) {
}, Blocks.FLOWER_POT);
}

// portal tinting
if (Config.customPortalColor) {
registerBlockColors(colors, new PortalColorHandler(), Blocks.PORTAL);
}

// coloring on sugar cane crop to match reeds
registerBlockColors(colors, (state, world, pos, index) -> {
if(world == null || pos == null) {
Expand Down
@@ -0,0 +1,87 @@
package knightminer.inspirations.tweaks.client;

import knightminer.inspirations.Inspirations;
import knightminer.inspirations.library.Util;
import net.minecraft.block.Block;
import net.minecraft.block.BlockStainedGlass;
import net.minecraft.block.BlockStainedGlassPane;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.color.IBlockColor;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.ChunkCache;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

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<>();

@Override
public int colorMultiplier(IBlockState state, @Nullable IBlockAccess world, @Nullable BlockPos pos, int tintIndex) {
if (world == null || pos == null) {
return DEFAULT_COLOR;
}

// 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(2);
IBlockState colorState = world.getBlockState(pos);
Block block = colorState.getBlock();
while(block == Blocks.PORTAL || block == Blocks.OBSIDIAN) {
// update iterator
pos = pos.down(block == Blocks.PORTAL ? 2 : 1);
colorState = world.getBlockState(pos);
block = colorState.getBlock();
}

return getColorValue(world, pos);
}

/**
* 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
* @return
*/
private static int getColorValue(IBlockAccess access, BlockPos pos) {
IBlockState state = access.getBlockState(pos);
Block block = state.getBlock();
// stained glass
if (block == Blocks.STAINED_GLASS) {
return state.getValue(BlockStainedGlass.COLOR).colorValue;
}
if (block == Blocks.STAINED_GLASS_PANE) {
return state.getValue(BlockStainedGlassPane.COLOR).colorValue;
}
// beacon color fallback
if (!BEACON_COLOR_BLACKLIST.contains(block)) {
World world = null;
if (access instanceof World) {
world = (World)access;
} else if (access instanceof ChunkCache) {
world = ((ChunkCache)access).world;
}
if (world != null) {
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;
}
}
5 changes: 4 additions & 1 deletion src/main/resources/META-INF/inspirations_at.cfg
Expand Up @@ -10,4 +10,7 @@ public net.minecraft.item.EnumDyeColor field_193351_w # colorValue
public net.minecraft.tileentity.TileEntityBeacon field_146015_k # isComplete

# Sheild enchantments
public net.minecraft.entity.EntityLivingBase func_184583_d(Lnet/minecraft/util/DamageSource;)Z # canBlockDamageSource
public net.minecraft.entity.EntityLivingBase func_184583_d(Lnet/minecraft/util/DamageSource;)Z # canBlockDamageSource

# Portal color
public net.minecraft.world.ChunkCache field_72815_e # world
@@ -0,0 +1,6 @@
{
"variants": {
"axis=z": { "model": "inspirations:portal_ew" },
"axis=x": { "model": "inspirations:portal_ns" }
}
}
15 changes: 15 additions & 0 deletions src/main/resources/assets/inspirations/models/block/portal_ew.json
@@ -0,0 +1,15 @@
{
"textures": {
"particle": "blocks/portal",
"portal": "inspirations:blocks/portal"
},
"elements": [
{ "from": [ 6, 0, 0 ],
"to": [ 10, 16, 16 ],
"faces": {
"east": { "uv": [ 0, 0, 16, 16 ], "texture": "#portal", "tintindex": 0 },
"west": { "uv": [ 0, 0, 16, 16 ], "texture": "#portal", "tintindex": 0 }
}
}
]
}
15 changes: 15 additions & 0 deletions src/main/resources/assets/inspirations/models/block/portal_ns.json
@@ -0,0 +1,15 @@
{
"textures": {
"particle": "blocks/portal",
"portal": "inspirations:blocks/portal"
},
"elements": [
{ "from": [ 0, 0, 6 ],
"to": [ 16, 16, 10 ],
"faces": {
"north": { "uv": [ 0, 0, 16, 16 ], "texture": "#portal", "tintindex": 0 },
"south": { "uv": [ 0, 0, 16, 16 ], "texture": "#portal", "tintindex": 0 }
}
}
]
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,3 @@
{
"animation": {}
}

0 comments on commit a248516

Please sign in to comment.