From 63d9fca789075c729ccd733cf20333d177beee29 Mon Sep 17 00:00:00 2001 From: malte0811 Date: Fri, 11 Jan 2019 16:16:44 +0100 Subject: [PATCH] Panels can have any texture now, textures are changed by crafting the unfinished panel with the block it should "imitate". Closes #54 ToDo documentation --- .../industrialwires/CommonProxy.java | 5 + .../blocks/controlpanel/TileEntityPanel.java | 8 +- .../controlpanel/TileEntityPanelCreator.java | 7 +- .../industrialwires/client/ClientProxy.java | 24 ++++ .../controlpanel/PanelUtils.java | 11 +- .../controlpanel/PropertyComponents.java | 20 +-- .../crafting/RecipeComponentCopy.java | 2 +- .../crafting/RecipePanelTexture.java | 115 ++++++++++++++++++ .../industrialwires/crafting/Recipes.java | 3 +- 9 files changed, 177 insertions(+), 18 deletions(-) create mode 100644 src/main/java/malte0811/industrialwires/crafting/RecipePanelTexture.java diff --git a/src/main/java/malte0811/industrialwires/CommonProxy.java b/src/main/java/malte0811/industrialwires/CommonProxy.java index e7aa797..c73694b 100644 --- a/src/main/java/malte0811/industrialwires/CommonProxy.java +++ b/src/main/java/malte0811/industrialwires/CommonProxy.java @@ -26,6 +26,7 @@ import malte0811.industrialwires.containers.ContainerRenameKey; import malte0811.industrialwires.mech_mb.MechEnergy; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; +import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumHand; @@ -90,4 +91,8 @@ public class CommonProxy implements IGuiHandler { public boolean isSingleplayer() { return false; } + + public boolean isValidTextureSource(ItemStack stack) { + return stack.getItem() instanceof ItemBlock; + } } diff --git a/src/main/java/malte0811/industrialwires/blocks/controlpanel/TileEntityPanel.java b/src/main/java/malte0811/industrialwires/blocks/controlpanel/TileEntityPanel.java index 79e452e..e45e09e 100644 --- a/src/main/java/malte0811/industrialwires/blocks/controlpanel/TileEntityPanel.java +++ b/src/main/java/malte0811/industrialwires/blocks/controlpanel/TileEntityPanel.java @@ -34,11 +34,11 @@ import net.minecraft.nbt.NBTTagList; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; import net.minecraft.util.ITickable; -import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.RayTraceResult; import net.minecraft.util.math.Vec3d; +import net.minecraftforge.common.util.Constants; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; @@ -140,8 +140,8 @@ public class TileEntityPanel extends TileEntityGeneralCP implements IDirectional } } components.setHeight(nbt.getFloat("height")); - if (nbt.hasKey("texture")) { - components.setTexture(new ResourceLocation(nbt.getString("texture"))); + if (nbt.hasKey("texture", Constants.NBT.TAG_COMPOUND)) { + components.setTextureSource(new ItemStack(nbt.getCompoundTag("texture"))); } components.setAngle(nbt.getFloat("angle")); } @@ -158,7 +158,7 @@ public class TileEntityPanel extends TileEntityGeneralCP implements IDirectional nbt.setTag("components", comps); nbt.setFloat("height", components.getHeight()); nbt.setFloat("angle", components.getAngle()); - nbt.setString("texture", components.getTexture().toString()); + nbt.setTag("texture", components.getTextureSource().serializeNBT()); } @Nonnull diff --git a/src/main/java/malte0811/industrialwires/blocks/controlpanel/TileEntityPanelCreator.java b/src/main/java/malte0811/industrialwires/blocks/controlpanel/TileEntityPanelCreator.java index faf7987..8159a4b 100644 --- a/src/main/java/malte0811/industrialwires/blocks/controlpanel/TileEntityPanelCreator.java +++ b/src/main/java/malte0811/industrialwires/blocks/controlpanel/TileEntityPanelCreator.java @@ -115,7 +115,12 @@ public class TileEntityPanelCreator extends TileEntityIWBase implements INetGUI, } } if (valid) { - NBTTagCompound panelNBT = new NBTTagCompound(); + NBTTagCompound panelNBT; + if (inv.hasTagCompound()) { + panelNBT = inv.getTagCompound().copy(); + } else { + panelNBT = new NBTTagCompound(); + } writeToItemNBT(panelNBT, true); ItemStack panel = new ItemStack(IndustrialWires.panel, 1, BlockTypes_Panel.TOP.ordinal()); panel.setTagCompound(panelNBT); diff --git a/src/main/java/malte0811/industrialwires/client/ClientProxy.java b/src/main/java/malte0811/industrialwires/client/ClientProxy.java index b6b53c0..ad65169 100644 --- a/src/main/java/malte0811/industrialwires/client/ClientProxy.java +++ b/src/main/java/malte0811/industrialwires/client/ClientProxy.java @@ -59,6 +59,8 @@ import net.minecraft.client.audio.MovingSound; import net.minecraft.client.audio.PositionedSoundRecord; import net.minecraft.client.audio.SoundHandler; import net.minecraft.client.gui.Gui; +import net.minecraft.client.renderer.block.model.IBakedModel; +import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.resources.I18n; import net.minecraft.client.resources.IReloadableResourceManager; import net.minecraft.entity.player.EntityPlayer; @@ -457,6 +459,28 @@ public class ClientProxy extends CommonProxy { return Minecraft.getMinecraft().isSingleplayer(); } + @Override + public boolean isValidTextureSource(ItemStack stack) { + if (!super.isValidTextureSource(stack)) { + return false; + } + IBakedModel texModel = Minecraft.getMinecraft().getRenderItem().getItemModelWithOverrides(stack, + null, null); + TextureAtlasSprite sprite = texModel.getParticleTexture(); + if (sprite.hasAnimationMetadata()) { + return false; + } + int[][] data = sprite.getFrameTextureData(0); + for (int x = 0; x < data.length; x++) { + for (int y = 0; y < data[x].length; y++) { + if ((data[x][y] >>> 24) != 255) { + return false; + } + } + } + return true; + } + @Override public Gui getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { if (ID == 0) { diff --git a/src/main/java/malte0811/industrialwires/controlpanel/PanelUtils.java b/src/main/java/malte0811/industrialwires/controlpanel/PanelUtils.java index 35b53ff..ff75a11 100644 --- a/src/main/java/malte0811/industrialwires/controlpanel/PanelUtils.java +++ b/src/main/java/malte0811/industrialwires/controlpanel/PanelUtils.java @@ -24,6 +24,7 @@ import malte0811.industrialwires.client.RawQuad; import malte0811.industrialwires.controlpanel.PropertyComponents.PanelRenderProperties; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.block.model.BakedQuad; +import net.minecraft.client.renderer.block.model.IBakedModel; import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.renderer.texture.TextureMap; import net.minecraft.client.renderer.vertex.VertexFormat; @@ -62,11 +63,17 @@ public final class PanelUtils { @SideOnly(Side.CLIENT) public static List generateQuads(PanelRenderProperties components) { - TextureMap texMap = Minecraft.getMinecraft().getTextureMapBlocks(); if (PANEL_TEXTURE == null) { + TextureMap texMap = Minecraft.getMinecraft().getTextureMapBlocks(); PANEL_TEXTURE = texMap.getAtlasSprite(IndustrialWires.MODID + ":blocks/control_panel"); } - final TextureAtlasSprite mainTex = texMap.getAtlasSprite(components.getTexture().toString()); + ItemStack source = components.getTextureSource(); + IBakedModel texModel = null; + if (IndustrialWires.proxy.isValidTextureSource(source)) { + texModel = Minecraft.getMinecraft().getRenderItem().getItemModelWithOverrides(source, + null, null); + } + final TextureAtlasSprite mainTex = texModel != null ? texModel.getParticleTexture() : PANEL_TEXTURE; List ret = new ArrayList<>(); Matrix4 m4 = components.getPanelTopTransform(); Matrix4 m4RotOnly = m4.copy(); diff --git a/src/main/java/malte0811/industrialwires/controlpanel/PropertyComponents.java b/src/main/java/malte0811/industrialwires/controlpanel/PropertyComponents.java index 030f555..7765e55 100644 --- a/src/main/java/malte0811/industrialwires/controlpanel/PropertyComponents.java +++ b/src/main/java/malte0811/industrialwires/controlpanel/PropertyComponents.java @@ -17,11 +17,13 @@ package malte0811.industrialwires.controlpanel; import blusunrize.immersiveengineering.common.util.chickenbones.Matrix4; import malte0811.industrialwires.IndustrialWires; +import malte0811.industrialwires.blocks.controlpanel.BlockTypes_Panel; import net.minecraft.block.Block; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; +import net.minecraft.item.ItemBlock; +import net.minecraft.item.ItemStack; import net.minecraft.util.EnumFacing; -import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; @@ -64,7 +66,7 @@ public class PropertyComponents implements IUnlistedProperty imple boolean found = false; int foundX = -1; int foundY = -1; - boolean foundPanel = false; + boolean foundPanel; for (int x = 0; x < inv.getWidth(); x++) { for (int y = 0; y < inv.getHeight(); y++) { ItemStack here = inv.getStackInRowAndColumn(x, y); diff --git a/src/main/java/malte0811/industrialwires/crafting/RecipePanelTexture.java b/src/main/java/malte0811/industrialwires/crafting/RecipePanelTexture.java new file mode 100644 index 0000000..7e6d49e --- /dev/null +++ b/src/main/java/malte0811/industrialwires/crafting/RecipePanelTexture.java @@ -0,0 +1,115 @@ +/* + * This file is part of Industrial Wires. + * Copyright (C) 2016-2018 malte0811 + * Industrial Wires is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * Industrial Wires is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with Industrial Wires. If not, see . + */ + +package malte0811.industrialwires.crafting; + +import blusunrize.immersiveengineering.api.ApiUtils; +import blusunrize.immersiveengineering.common.util.ItemNBTHelper; +import malte0811.industrialwires.IndustrialWires; +import malte0811.industrialwires.blocks.controlpanel.BlockTypes_Panel; +import malte0811.industrialwires.util.NBTKeys; +import net.minecraft.inventory.InventoryCrafting; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.IRecipe; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.NonNullList; +import net.minecraft.world.World; +import net.minecraftforge.registries.IForgeRegistryEntry; + +import javax.annotation.Nonnull; + +public class RecipePanelTexture extends IForgeRegistryEntry.Impl implements IRecipe { + @Override + public boolean matches(@Nonnull InventoryCrafting inv, @Nonnull World worldIn) { + boolean foundTexture = false; + boolean foundPanel = false; + for (int x = 0; x < inv.getWidth(); x++) { + for (int y = 0; y < inv.getHeight(); y++) { + ItemStack here = inv.getStackInRowAndColumn(x, y); + if (isUnfinishedPanel(here)) { + if (foundPanel) { + return false; + } + foundPanel = true; + } else if (IndustrialWires.proxy.isValidTextureSource(here)) { + if (foundTexture || (here.getItem() == Item.getItemFromBlock(IndustrialWires.panel) + && here.getMetadata() != BlockTypes_Panel.DUMMY.ordinal())) { + return false; + } + foundTexture = true; + } else if (!here.isEmpty()) { + return false; + } + } + } + return foundPanel && foundTexture; + } + + private boolean isUnfinishedPanel(ItemStack stack) { + return stack.getItem() == Item.getItemFromBlock(IndustrialWires.panel) + && stack.getMetadata() == BlockTypes_Panel.UNFINISHED.ordinal(); + } + + @Nonnull + @Override + public ItemStack getCraftingResult(@Nonnull InventoryCrafting inv) { + ItemStack texture = null; + ItemStack panel = null; + for (int x = 0; x < inv.getWidth(); x++) { + for (int y = 0; y < inv.getHeight(); y++) { + ItemStack here = inv.getStackInRowAndColumn(x, y); + if (isUnfinishedPanel(here)) { + panel = here; + } else if (IndustrialWires.proxy.isValidTextureSource(here)) { + texture = here; + } + } + } + assert texture != null && panel != null; + NBTTagCompound texAsNBT = texture.serializeNBT(); + ItemStack ret = panel.copy(); + if (ret.getTagCompound() == null) { + ItemNBTHelper.setFloat(ret, NBTKeys.ANGLE, 0); + ItemNBTHelper.setFloat(ret, NBTKeys.HEIGHT, .5F); + } + ItemNBTHelper.setTagCompound(ret, "texture", texAsNBT); + return ret; + } + + @Override + public boolean canFit(int width, int height) { + return width * height >= 2; + } + + @Nonnull + @Override + public ItemStack getRecipeOutput() { + return new ItemStack(IndustrialWires.panel, 1, BlockTypes_Panel.TOP.ordinal()); + } + + @Nonnull + @Override + public NonNullList getRemainingItems(InventoryCrafting inv) { + NonNullList ret = IRecipe.super.getRemainingItems(inv); + for (int i = 0; i < inv.getSizeInventory(); i++) { + ItemStack here = inv.getStackInSlot(i); + if (!isUnfinishedPanel(here) && !here.isEmpty()) { + ret.set(i, ApiUtils.copyStackWithAmount(here, 1)); + } + } + return ret; + } +} diff --git a/src/main/java/malte0811/industrialwires/crafting/Recipes.java b/src/main/java/malte0811/industrialwires/crafting/Recipes.java index 82dee7c..4a1662c 100644 --- a/src/main/java/malte0811/industrialwires/crafting/Recipes.java +++ b/src/main/java/malte0811/industrialwires/crafting/Recipes.java @@ -31,6 +31,7 @@ public class Recipes { registry.register(new RecipeKeyRing(true).setRegistryName(MODID, "add_key_ring")); registry.register(new RecipeKeyRing(false).setRegistryName(MODID, "remove_key_ring")); registry.register(new RecipeKeyLock().setRegistryName(MODID, "key_lock")); + registry.register(new RecipePanelTexture().setRegistryName(MODID, "panel_texture")); registry.register(new RecipeComponentCopy().setRegistryName(MODID, "component_copy")); AssemblerHandler.registerRecipeAdapter(RecipeCoilLength.class, new Recipes.AllRecipeAdapter<>()); AssemblerHandler.registerRecipeAdapter(RecipeComponentCopy.class, new Recipes.AllRecipeAdapter<>()); @@ -55,7 +56,7 @@ public class Recipes { ret.add(new AssemblerHandler.RecipeQuery(in.get(i), 1)); } } - return ret.toArray(new AssemblerHandler.RecipeQuery[ret.size()]); + return ret.toArray(new AssemblerHandler.RecipeQuery[0]); } @Override