2017-04-10 16:20:39 +02:00
|
|
|
/*
|
2016-09-29 16:19:16 +02:00
|
|
|
* This file is part of Industrial Wires.
|
2017-04-10 16:20:39 +02:00
|
|
|
* Copyright (C) 2016-2017 malte0811
|
2016-09-29 16:19:16 +02:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
2017-04-10 16:20:39 +02:00
|
|
|
*/
|
2016-09-15 20:43:18 +02:00
|
|
|
package malte0811.industrialWires.client;
|
|
|
|
|
|
|
|
import blusunrize.immersiveengineering.api.Lib;
|
|
|
|
import blusunrize.immersiveengineering.client.ClientUtils;
|
|
|
|
import blusunrize.immersiveengineering.common.util.ItemNBTHelper;
|
|
|
|
import malte0811.industrialWires.IndustrialWires;
|
2017-08-23 22:30:24 +02:00
|
|
|
import malte0811.industrialWires.blocks.BlockIWBase;
|
2017-07-09 18:10:06 +02:00
|
|
|
import malte0811.industrialWires.blocks.IMetaEnum;
|
2017-03-24 17:40:05 +01:00
|
|
|
import malte0811.industrialWires.blocks.controlpanel.TileEntityPanel;
|
2017-04-28 20:20:52 +02:00
|
|
|
import malte0811.industrialWires.client.panelmodel.PanelModel;
|
|
|
|
import malte0811.industrialWires.controlpanel.PanelComponent;
|
2016-09-22 19:52:01 +02:00
|
|
|
import malte0811.industrialWires.items.ItemIC2Coil;
|
2017-07-09 18:10:06 +02:00
|
|
|
import malte0811.industrialWires.items.ItemKey;
|
|
|
|
import malte0811.industrialWires.items.ItemPanelComponent;
|
2016-09-15 20:43:18 +02:00
|
|
|
import malte0811.industrialWires.wires.IC2Wiretype;
|
2017-07-09 18:10:06 +02:00
|
|
|
import net.minecraft.block.Block;
|
2017-03-24 17:40:05 +01:00
|
|
|
import net.minecraft.client.Minecraft;
|
2017-07-09 18:10:06 +02:00
|
|
|
import net.minecraft.client.renderer.block.model.ModelBakery;
|
2017-04-28 20:20:52 +02:00
|
|
|
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
|
2016-09-15 20:43:18 +02:00
|
|
|
import net.minecraft.client.resources.I18n;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2017-07-09 18:10:06 +02:00
|
|
|
import net.minecraft.item.Item;
|
2016-09-15 20:43:18 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2017-03-24 17:40:05 +01:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2016-09-15 20:43:18 +02:00
|
|
|
import net.minecraft.util.EnumHand;
|
2017-07-09 18:10:06 +02:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
2016-12-20 17:05:20 +01:00
|
|
|
import net.minecraft.util.math.RayTraceResult;
|
2016-09-15 20:43:18 +02:00
|
|
|
import net.minecraftforge.client.GuiIngameForge;
|
2017-03-24 17:40:05 +01:00
|
|
|
import net.minecraftforge.client.event.DrawBlockHighlightEvent;
|
2017-04-28 20:20:52 +02:00
|
|
|
import net.minecraftforge.client.event.ModelBakeEvent;
|
2017-07-09 18:10:06 +02:00
|
|
|
import net.minecraftforge.client.event.ModelRegistryEvent;
|
2016-09-15 20:43:18 +02:00
|
|
|
import net.minecraftforge.client.event.RenderGameOverlayEvent;
|
2017-07-09 18:10:06 +02:00
|
|
|
import net.minecraftforge.client.model.ModelLoader;
|
|
|
|
import net.minecraftforge.fml.common.Mod;
|
2016-09-15 20:43:18 +02:00
|
|
|
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
2017-07-09 18:10:06 +02:00
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
2016-09-15 20:43:18 +02:00
|
|
|
import net.minecraftforge.oredict.OreDictionary;
|
2017-03-28 18:15:41 +02:00
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
2016-09-15 20:43:18 +02:00
|
|
|
|
2017-07-09 18:10:06 +02:00
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
@Mod.EventBusSubscriber(modid = IndustrialWires.MODID, value = Side.CLIENT)
|
2016-09-15 20:43:18 +02:00
|
|
|
public class ClientEventHandler {
|
|
|
|
@SubscribeEvent
|
2017-07-09 18:10:06 +02:00
|
|
|
public static void renderOverlayPost(RenderGameOverlayEvent.Post e) {
|
2017-05-10 17:56:05 +02:00
|
|
|
if (ClientUtils.mc().player != null && e.getType() == RenderGameOverlayEvent.ElementType.TEXT) {
|
|
|
|
EntityPlayer player = ClientUtils.mc().player;
|
2016-09-15 20:43:18 +02:00
|
|
|
|
2017-05-11 16:39:20 +02:00
|
|
|
for (EnumHand hand : EnumHand.values()) {
|
2017-05-10 17:56:05 +02:00
|
|
|
if (!player.getHeldItem(hand).isEmpty()) {
|
2016-09-15 20:43:18 +02:00
|
|
|
ItemStack equipped = player.getHeldItem(hand);
|
2017-05-11 16:39:20 +02:00
|
|
|
if (OreDictionary.itemMatches(new ItemStack(IndustrialWires.coil, 1, OreDictionary.WILDCARD_VALUE), equipped, false)) {
|
2016-12-20 17:05:20 +01:00
|
|
|
IC2Wiretype type = IC2Wiretype.IC2_TYPES[equipped.getItemDamage()];
|
|
|
|
int color = type.getColour(null);
|
2017-05-11 16:39:20 +02:00
|
|
|
String s = I18n.format(IndustrialWires.MODID + ".desc.wireLength", ItemIC2Coil.getLength(equipped));
|
|
|
|
ClientUtils.font().drawString(s, e.getResolution().getScaledWidth() / 2 - ClientUtils.font().getStringWidth(s) / 2, e.getResolution().getScaledHeight() - GuiIngameForge.left_height - 40, color, true);
|
|
|
|
if (ItemNBTHelper.hasKey(equipped, "linkingPos")) {
|
2016-09-15 20:43:18 +02:00
|
|
|
int[] link = ItemNBTHelper.getIntArray(equipped, "linkingPos");
|
2017-05-11 16:39:20 +02:00
|
|
|
if (link != null && link.length > 3) {
|
|
|
|
s = I18n.format(Lib.DESC_INFO + "attachedTo", link[1], link[2], link[3]);
|
2016-12-20 17:05:20 +01:00
|
|
|
RayTraceResult focussedBlock = ClientUtils.mc().objectMouseOver;
|
|
|
|
double distSquared;
|
2017-05-10 17:56:05 +02:00
|
|
|
if (focussedBlock != null && focussedBlock.typeOfHit == RayTraceResult.Type.BLOCK) {
|
2017-05-11 16:39:20 +02:00
|
|
|
distSquared = focussedBlock.getBlockPos().distanceSq(link[1], link[2], link[3]);
|
2016-12-20 17:05:20 +01:00
|
|
|
} else {
|
2017-05-11 16:39:20 +02:00
|
|
|
distSquared = player.getDistanceSq(link[1], link[2], link[3]);
|
2016-12-20 17:05:20 +01:00
|
|
|
}
|
|
|
|
int length = Math.min(ItemIC2Coil.getLength(equipped), type.getMaxLength());
|
2017-05-11 16:39:20 +02:00
|
|
|
if (length * length < distSquared) {
|
2016-12-20 17:05:20 +01:00
|
|
|
color = 0xdd3333;
|
|
|
|
}
|
2017-05-11 16:39:20 +02:00
|
|
|
ClientUtils.font().drawString(s, e.getResolution().getScaledWidth() / 2 - ClientUtils.font().getStringWidth(s) / 2, e.getResolution().getScaledHeight() - GuiIngameForge.left_height - 20, color, true);
|
2016-09-15 20:43:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-22 19:52:01 +02:00
|
|
|
}
|
2016-09-15 20:43:18 +02:00
|
|
|
}
|
|
|
|
}
|
2017-03-24 17:40:05 +01:00
|
|
|
|
|
|
|
@SubscribeEvent
|
2017-07-09 18:10:06 +02:00
|
|
|
public static void renderBoundingBoxes(DrawBlockHighlightEvent event) {
|
2017-03-24 17:40:05 +01:00
|
|
|
if (!event.isCanceled() && event.getSubID() == 0 && event.getTarget().typeOfHit == RayTraceResult.Type.BLOCK) {
|
2017-05-10 17:56:05 +02:00
|
|
|
TileEntity tile = event.getPlayer().world.getTileEntity(event.getTarget().getBlockPos());
|
2017-03-24 17:40:05 +01:00
|
|
|
if (tile instanceof TileEntityPanel) {
|
|
|
|
TileEntityPanel panel = (TileEntityPanel) tile;
|
2017-05-10 17:56:05 +02:00
|
|
|
Pair<PanelComponent, RayTraceResult> pc = panel.getSelectedComponent(Minecraft.getMinecraft().player, event.getTarget().hitVec, true);
|
2017-03-24 17:40:05 +01:00
|
|
|
if (pc != null) {
|
2017-03-28 18:15:41 +02:00
|
|
|
pc.getLeft().renderBox(panel);
|
2017-03-24 17:40:05 +01:00
|
|
|
event.setCanceled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-11 16:39:20 +02:00
|
|
|
|
2017-04-28 20:20:52 +02:00
|
|
|
@SubscribeEvent
|
2017-07-09 18:10:06 +02:00
|
|
|
public static void bakeModel(ModelBakeEvent event) {
|
2017-05-11 16:39:20 +02:00
|
|
|
event.getModelRegistry().putObject(new ModelResourceLocation(IndustrialWires.MODID + ":control_panel", "inventory,type=top"), new PanelModel());
|
2017-06-30 21:04:02 +02:00
|
|
|
event.getModelRegistry().putObject(new ModelResourceLocation(IndustrialWires.MODID + ":control_panel", "inventory,type=unfinished"), new PanelModel());
|
2017-04-28 20:20:52 +02:00
|
|
|
}
|
2017-07-09 18:10:06 +02:00
|
|
|
@SubscribeEvent
|
|
|
|
public static void registerModels(ModelRegistryEvent evt) {
|
2017-08-23 22:30:24 +02:00
|
|
|
if (IndustrialWires.coil!=null) {
|
|
|
|
for (int meta = 0; meta < ItemIC2Coil.subNames.length; meta++) {
|
|
|
|
ResourceLocation loc = new ResourceLocation(IndustrialWires.MODID, "ic2_wire_coil/" + ItemIC2Coil.subNames[meta]);
|
|
|
|
ModelBakery.registerItemVariants(IndustrialWires.coil, loc);
|
|
|
|
ModelLoader.setCustomModelResourceLocation(IndustrialWires.coil, meta, new ModelResourceLocation(loc, "inventory"));
|
|
|
|
}
|
2017-07-09 18:10:06 +02:00
|
|
|
}
|
|
|
|
for (int meta = 0; meta < ItemPanelComponent.types.length; meta++) {
|
|
|
|
ResourceLocation loc = new ResourceLocation(IndustrialWires.MODID, "panel_component/" + ItemPanelComponent.types[meta]);
|
|
|
|
ModelBakery.registerItemVariants(IndustrialWires.panelComponent, loc);
|
|
|
|
ModelLoader.setCustomModelResourceLocation(IndustrialWires.panelComponent, meta, new ModelResourceLocation(loc, "inventory"));
|
|
|
|
}
|
|
|
|
for (int meta = 0; meta < ItemKey.types.length; meta++) {
|
|
|
|
ResourceLocation loc = new ResourceLocation(IndustrialWires.MODID, "key/" + ItemKey.types[meta]);
|
|
|
|
ModelBakery.registerItemVariants(IndustrialWires.key, loc);
|
|
|
|
ModelLoader.setCustomModelResourceLocation(IndustrialWires.key, meta, new ModelResourceLocation(loc, "inventory"));
|
|
|
|
}
|
2017-08-23 22:30:24 +02:00
|
|
|
for (BlockIWBase b : IndustrialWires.blocks) {
|
|
|
|
Item blockItem = Item.getItemFromBlock(b);
|
|
|
|
final ResourceLocation loc = b.getRegistryName();
|
|
|
|
assert loc != null;
|
|
|
|
ModelLoader.setCustomMeshDefinition(blockItem, stack -> new ModelResourceLocation(loc, "inventory"));
|
|
|
|
Object[] v = ((IMetaEnum) b).getValues();
|
|
|
|
for (int meta = 0; meta < v.length; meta++) {
|
|
|
|
String location = loc.toString();
|
|
|
|
String prop = "inventory,type=" + v[meta].toString().toLowerCase(Locale.US);
|
|
|
|
try {
|
|
|
|
ModelLoader.setCustomModelResourceLocation(blockItem, meta, new ModelResourceLocation(location, prop));
|
|
|
|
} catch (NullPointerException npe) {
|
|
|
|
throw new RuntimeException(b + " lacks an item!", npe);
|
2017-07-09 18:10:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 20:43:18 +02:00
|
|
|
}
|