diff --git a/src/main/java/resonantinduction/transport/transformer/PartTransformer.java b/src/main/java/resonantinduction/transport/transformer/PartTransformer.java index 49c6564ca..23a6762d6 100644 --- a/src/main/java/resonantinduction/transport/transformer/PartTransformer.java +++ b/src/main/java/resonantinduction/transport/transformer/PartTransformer.java @@ -1,12 +1,15 @@ package resonantinduction.transport.transformer; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.ChatMessageComponent; import net.minecraft.util.MovingObjectPosition; import net.minecraftforge.common.ForgeDirection; import resonantinduction.ResonantInduction; @@ -31,193 +34,339 @@ import cpw.mods.fml.relauncher.SideOnly; public class PartTransformer extends JCuboidPart implements JNormalOcclusion, TFacePart, IVoltageOutput, IEnergyInterface { - public static Cuboid6[][] oBoxes = new Cuboid6[6][2]; + public static Cuboid6[][] oBoxes = new Cuboid6[6][2]; - static - { - oBoxes[0][0] = new Cuboid6(1 / 8D, 0, 0, 7 / 8D, 1 / 8D, 1); - oBoxes[0][1] = new Cuboid6(0, 0, 1 / 8D, 1, 1 / 8D, 7 / 8D); - for (int s = 1; s < 6; s++) - { - Transformation t = Rotation.sideRotations[s].at(Vector3.center); - oBoxes[s][0] = oBoxes[0][0].copy().apply(t); - oBoxes[s][1] = oBoxes[0][1].copy().apply(t); - } - } - /** Side of the block this is placed on */ - public int placementSide; - /** Direction this block faces */ - public int face = 0; - /** Step the voltage up */ - private boolean stepUp; - /** Amount to mulitply the step by (up x2. down /2) */ - public int multiplier = 2; + static + { + oBoxes[0][0] = new Cuboid6(1 / 8D, 0, 0, 7 / 8D, 1 / 8D, 1); + oBoxes[0][1] = new Cuboid6(0, 0, 1 / 8D, 1, 1 / 8D, 7 / 8D); + for (int s = 1; s < 6; s++) + { + Transformation t = Rotation.sideRotations[s].at(Vector3.center); + oBoxes[s][0] = oBoxes[0][0].copy().apply(t); + oBoxes[s][1] = oBoxes[0][1].copy().apply(t); + } + } + /** Side of the block this is placed on */ + public ForgeDirection placementSide; + /** Direction this block faces */ + public byte face = 0; + /** Step the voltage up */ + private boolean stepUp; + /** Amount to mulitply the step by (up x2. down /2) */ + public int multiplier = 2; - public void preparePlacement(int side, int itemDamage) - { - this.placementSide = (byte) (side ^ 1); - } + public void preparePlacement(int side, int itemDamage) + { + this.placementSide = ForgeDirection.getOrientation((byte) (side ^ 1)); + } - @Override - public void readDesc(MCDataInput packet) - { - this.placementSide = packet.readByte(); - this.face = packet.readByte(); - } + @Override + public void readDesc(MCDataInput packet) + { + this.placementSide = ForgeDirection.getOrientation(packet.readByte()); + this.face = packet.readByte(); + } - @Override - public void writeDesc(MCDataOutput packet) - { - packet.writeByte(this.placementSide); - packet.writeByte(this.face); - } + @Override + public void writeDesc(MCDataOutput packet) + { + packet.writeByte(this.placementSide.ordinal()); + packet.writeByte(this.face); + } - public boolean stepUp() - { - return this.stepUp; - } + public boolean stepUp() + { + return this.stepUp; + } - @Override - public int getSlotMask() - { - return 1 << this.placementSide; - } + @Override + public int getSlotMask() + { + return 1 << this.placementSide.ordinal(); + } - @Override - public Cuboid6 getBounds() - { - return FaceMicroClass.aBounds()[0x10 | this.placementSide]; - } + @Override + public Cuboid6 getBounds() + { + return FaceMicroClass.aBounds()[0x10 | this.placementSide.ordinal()]; + } - @Override - public int redstoneConductionMap() - { - return 0; - } + @Override + public int redstoneConductionMap() + { + return 0; + } - @Override - public boolean solid(int arg0) - { - return true; - } + @Override + public boolean solid(int arg0) + { + return true; + } - @Override - public Iterable getOcclusionBoxes() - { - return Arrays.asList(oBoxes[this.placementSide]); - } + @Override + public Iterable getOcclusionBoxes() + { + return Arrays.asList(oBoxes[this.placementSide.ordinal()]); + } - protected ItemStack getItem() - { - return new ItemStack(ResonantInduction.itemTransformer); - } + protected ItemStack getItem() + { + return new ItemStack(ResonantInduction.itemTransformer); + } - @Override - public Iterable getDrops() - { - List drops = new ArrayList(); - drops.add(getItem()); - return drops; - } + @Override + public Iterable getDrops() + { + List drops = new ArrayList(); + drops.add(getItem()); + return drops; + } - @Override - public ItemStack pickItem(MovingObjectPosition hit) - { - return getItem(); - } + @Override + public ItemStack pickItem(MovingObjectPosition hit) + { + return getItem(); + } - @Override - @SideOnly(Side.CLIENT) - public void renderDynamic(Vector3 pos, float frame, int pass) - { - if (pass == 0) - { - RenderTransformer.render(this, pos.x, pos.y, pos.z); - } - } + @Override + @SideOnly(Side.CLIENT) + public void renderDynamic(Vector3 pos, float frame, int pass) + { + if (pass == 0) + { + RenderTransformer.render(this, pos.x, pos.y, pos.z); + } + } - @Override - public void load(NBTTagCompound nbt) - { - super.load(nbt); - this.placementSide = nbt.getByte("side"); - this.stepUp = nbt.getBoolean("stepUp"); - } + @Override + public void load(NBTTagCompound nbt) + { + super.load(nbt); + this.placementSide = ForgeDirection.getOrientation(nbt.getByte("side")); + this.stepUp = nbt.getBoolean("stepUp"); + } - @Override - public void save(NBTTagCompound nbt) - { - super.save(nbt); - nbt.setByte("side", (byte) this.placementSide); - nbt.setBoolean("stepUp", this.stepUp); - } + @Override + public void save(NBTTagCompound nbt) + { + super.save(nbt); + nbt.setByte("side", (byte) this.placementSide.ordinal()); + nbt.setBoolean("stepUp", this.stepUp); + } - @Override - public String getType() - { - return "resonant_induction_transformer"; - } + @Override + public String getType() + { + return "resonant_induction_transformer"; + } - protected ForgeDirection getFacing() - { - return ForgeDirection.NORTH; - } + protected ForgeDirection getFacing() + { + if (this.placementSide != ForgeDirection.UP && this.placementSide != ForgeDirection.DOWN) + { + switch (this.face) + { + case 0: + return ForgeDirection.UP; + case 1: + switch (this.placementSide) + { + case NORTH: + return ForgeDirection.EAST; + case SOUTH: + return ForgeDirection.WEST; + case EAST: + return ForgeDirection.SOUTH; + case WEST: + return ForgeDirection.NORTH; + } + case 2: + return ForgeDirection.DOWN; + case 3: + switch (this.placementSide) + { + case NORTH: + return ForgeDirection.WEST; + case SOUTH: + return ForgeDirection.EAST; + case EAST: + return ForgeDirection.NORTH; + case WEST: + return ForgeDirection.SOUTH; + } + } + } + else + { + switch (this.face) + { + case 0: + return ForgeDirection.NORTH; + case 1: + return ForgeDirection.EAST; + case 2: + return ForgeDirection.SOUTH; + case 3: + return ForgeDirection.WEST; + } + } + return ForgeDirection.NORTH; + } - @Override - public boolean canConnect(ForgeDirection direction) - { - return direction == getFacing() || direction == getFacing().getOpposite(); - } + @Override + public boolean canConnect(ForgeDirection direction) + { + return direction == getFacing() || direction == getFacing().getOpposite(); + } - @Override - public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive) - { - if (from == this.getFacing().getOpposite()) - { - TileEntity entity = VectorHelper.getTileEntityFromSide(this.world(), new universalelectricity.api.vector.Vector3(this.x(), this.y(), this.z()), this.getFacing()); - if (entity instanceof IEnergyInterface) - { - if (entity instanceof IVoltageInput) - { - long voltage = this.getVoltageOutput(from.getOpposite()); - if (voltage != ((IVoltageInput) entity).getVoltageInput(from)) - { - ((IVoltageInput) entity).onWrongVoltage(from, voltage); - } - } - return ((IEnergyInterface) entity).onReceiveEnergy(from, receive, doReceive); - } + @Override + public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive) + { + if (from == this.getFacing().getOpposite()) + { + TileEntity entity = VectorHelper.getTileEntityFromSide(this.world(), new universalelectricity.api.vector.Vector3(this.x(), this.y(), this.z()), this.getFacing()); + if (entity instanceof IEnergyInterface) + { + if (entity instanceof IVoltageInput) + { + long voltage = this.getVoltageOutput(from.getOpposite()); + if (voltage != ((IVoltageInput) entity).getVoltageInput(from)) + { + ((IVoltageInput) entity).onWrongVoltage(from, voltage); + } + } + return ((IEnergyInterface) entity).onReceiveEnergy(from, receive, doReceive); + } - } - return 0; - } + } + return 0; + } - @Override - public long onExtractEnergy(ForgeDirection from, long extract, boolean doExtract) - { - return 0; - } + @Override + public long onExtractEnergy(ForgeDirection from, long extract, boolean doExtract) + { + return 0; + } - @Override - public long getVoltageOutput(ForgeDirection side) - { - TileEntity entity = VectorHelper.getTileEntityFromSide(this.world(), new universalelectricity.api.vector.Vector3(this.x(), this.y(), this.z()), this.getFacing().getOpposite()); - if (entity instanceof IConductor && ((IConductor) entity).getNetwork() instanceof IElectricalNetwork) - { - long voltage = ((IElectricalNetwork) ((IConductor) entity).getNetwork()).getVoltage(); - if (this.stepUp()) - { - return voltage * this.multiplier; - } - else if (voltage > 0) - { - return voltage / this.multiplier; - } - } - else if (entity instanceof IVoltageOutput) - { - return ((IVoltageOutput) entity).getVoltageOutput(side); - } - return 0; - } + @Override + public long getVoltageOutput(ForgeDirection side) + { + if (side == this.getFacing()) + { + TileEntity entity = VectorHelper.getTileEntityFromSide(this.world(), new universalelectricity.api.vector.Vector3(this.x(), this.y(), this.z()), this.getFacing().getOpposite()); + if (entity instanceof IConductor && ((IConductor) entity).getNetwork() instanceof IElectricalNetwork) + { + long voltage = ((IElectricalNetwork) ((IConductor) entity).getNetwork()).getVoltage(); + if (this.stepUp()) + { + return voltage * this.multiplier; + } + else if (voltage > 0) + { + return voltage / this.multiplier; + } + } + else if (entity instanceof IVoltageOutput) + { + return ((IVoltageOutput) entity).getVoltageOutput(side); + } + } + return 0; + } + + @Override + public boolean activate(EntityPlayer player, MovingObjectPosition hit, ItemStack item) + { + + if (this.isUsableWrench(player, player.inventory.getCurrentItem(), x(), y(), z())) + { + if (this.world().isRemote) + { + return true; + } + this.damageWrench(player, player.inventory.getCurrentItem(), x(), y(), z()); + if (this.face < 3) + this.face++; + else + this.face = 0; + this.sendDescUpdate(); + player.sendChatToPlayer(ChatMessageComponent.createFromText("Face:" + this.face)); + return true; + } + return false; + } + + public boolean isUsableWrench(EntityPlayer entityPlayer, ItemStack itemStack, int x, int y, int z) + { + if (entityPlayer != null && itemStack != null) + { + Class wrenchClass = itemStack.getItem().getClass(); + + /** UE and Buildcraft */ + try + { + Method methodCanWrench = wrenchClass.getMethod("canWrench", EntityPlayer.class, Integer.TYPE, Integer.TYPE, Integer.TYPE); + return (Boolean) methodCanWrench.invoke(itemStack.getItem(), entityPlayer, x, y, z); + } + catch (NoClassDefFoundError e) + { + } + catch (Exception e) + { + } + + /** Industrialcraft */ + try + { + if (wrenchClass == Class.forName("ic2.core.item.tool.ItemToolWrench") || wrenchClass == Class.forName("ic2.core.item.tool.ItemToolWrenchElectric")) + { + return itemStack.getItemDamage() < itemStack.getMaxDamage(); + } + } + catch (Exception e) + { + } + } + + return false; + } + + /** This function damages a wrench. Works with Buildcraft and Industrialcraft wrenches. + * + * @return True if damage was successfull. */ + public boolean damageWrench(EntityPlayer entityPlayer, ItemStack itemStack, int x, int y, int z) + { + if (this.isUsableWrench(entityPlayer, itemStack, x, y, z)) + { + Class wrenchClass = itemStack.getItem().getClass(); + + /** UE and Buildcraft */ + try + { + Method methodWrenchUsed = wrenchClass.getMethod("wrenchUsed", EntityPlayer.class, Integer.TYPE, Integer.TYPE, Integer.TYPE); + methodWrenchUsed.invoke(itemStack.getItem(), entityPlayer, x, y, z); + return true; + } + catch (Exception e) + { + } + + /** Industrialcraft */ + try + { + if (wrenchClass == Class.forName("ic2.core.item.tool.ItemToolWrench") || wrenchClass == Class.forName("ic2.core.item.tool.ItemToolWrenchElectric")) + { + Method methodWrenchDamage = wrenchClass.getMethod("damage", ItemStack.class, Integer.TYPE, EntityPlayer.class); + methodWrenchDamage.invoke(itemStack.getItem(), itemStack, 1, entityPlayer); + return true; + } + } + catch (Exception e) + { + } + } + + return false; + } } \ No newline at end of file diff --git a/src/main/java/resonantinduction/transport/transformer/RenderTransformer.java b/src/main/java/resonantinduction/transport/transformer/RenderTransformer.java index ec1364c13..1cd231ba4 100644 --- a/src/main/java/resonantinduction/transport/transformer/RenderTransformer.java +++ b/src/main/java/resonantinduction/transport/transformer/RenderTransformer.java @@ -18,54 +18,142 @@ import cpw.mods.fml.relauncher.SideOnly; @SideOnly(Side.CLIENT) public class RenderTransformer { - public static final ModelTransformer MODEL = new ModelTransformer(); - public static final ResourceLocation TEXTURE = new ResourceLocation(ResonantInduction.DOMAIN, ResonantInduction.MODEL_TEXTURE_DIRECTORY + "transformer.png"); + public static final ModelTransformer MODEL = new ModelTransformer(); + public static final ResourceLocation TEXTURE = new ResourceLocation(ResonantInduction.DOMAIN, ResonantInduction.MODEL_TEXTURE_DIRECTORY + "transformer.png"); - public static void render(PartTransformer part, double x, double y, double z) - { - String status = TranslationHelper.getLocal((part.stepUp() ? "tooltip.transformer.stepUp" : "tooltip.transformer.stepDown")); + public static void render(PartTransformer part, double x, double y, double z) + { + String status = TranslationHelper.getLocal((part.stepUp() ? "tooltip.transformer.stepUp" : "tooltip.transformer.stepDown")); - EntityPlayer player = Minecraft.getMinecraft().thePlayer; - MovingObjectPosition movingPosition = player.rayTrace(5, 1f); + EntityPlayer player = Minecraft.getMinecraft().thePlayer; + MovingObjectPosition movingPosition = player.rayTrace(5, 1f); - if (movingPosition != null) - { - if (new Vector3(part.x(), part.y(), part.z()).equals(new Vector3(movingPosition))) - { - CalclaviaRenderHelper.renderFloatingText(status, (float) (x + 0.5), (float) (y - 1), (float) (z + 0.5)); - } - } + if (movingPosition != null) + { + if (new Vector3(part.x(), part.y(), part.z()).equals(new Vector3(movingPosition))) + { + CalclaviaRenderHelper.renderFloatingText(status, (float) (x + 0.5), (float) (y - 1), (float) (z + 0.5)); + } + } - GL11.glPushMatrix(); - GL11.glTranslatef((float) x + 0.5F, (float) y + 1.5F, (float) z + 0.5F); - GL11.glScalef(1.0F, -1F, -1F); + GL11.glPushMatrix(); + GL11.glTranslatef((float) x + 0.5F, (float) y + 1.5F, (float) z + 0.5F); + GL11.glScalef(1.0F, -1F, -1F); - switch (part.placementSide) - { - case 1: - GL11.glRotatef(180, 0, 0, 1); - GL11.glTranslatef(0, -2, 0); - break; - case 2: - GL11.glRotatef(90, 1, 0, 0); - GL11.glTranslatef(0, -1, -1); - break; - case 3: - GL11.glRotatef(-90, 1, 0, 0); - GL11.glTranslatef(0, -1, 1); - break; - case 4: - GL11.glRotatef(90, 0, 0, 1); - GL11.glTranslatef(1, -1, 0); - break; - case 5: - GL11.glRotatef(-90, 0, 0, 1); - GL11.glTranslatef(-1, -1, 0); - break; - } + switch (part.placementSide) + { + case DOWN: - FMLClientHandler.instance().getClient().renderEngine.bindTexture(TEXTURE); - MODEL.render(0.0625F); - GL11.glPopMatrix(); - } + switch (part.face) + { + case 0: + GL11.glRotatef(90, 0, 1, 0); + break; + case 1: + GL11.glRotatef(180, 0, 1, 0); + break; + case 2: + GL11.glRotatef(-90, 0, 1, 0); + break; + case 3: + break; + } + break; + case UP: + GL11.glRotatef(180, 0, 0, 1); + GL11.glTranslatef(0, -2, 0); + switch (part.face) + { + case 0: + GL11.glRotatef(90, 0, 1, 0); + break; + case 1: + GL11.glRotatef(180, 0, 1, 0); + break; + case 2: + GL11.glRotatef(-90, 0, 1, 0); + break; + case 3: + break; + } + break; + case NORTH: + GL11.glRotatef(90, 1, 0, 0); + GL11.glTranslatef(0, -1, -1); + switch (part.face) + { + case 0: + GL11.glRotatef(90, 0, 1, 0); + break; + case 1: + GL11.glRotatef(180, 0, 1, 0); + break; + case 2: + GL11.glRotatef(-90, 0, 1, 0); + break; + case 3: + break; + } + break; + case SOUTH: + GL11.glRotatef(-90, 1, 0, 0); + GL11.glTranslatef(0, -1, 1); + switch (part.face) + { + case 0: + GL11.glRotatef(-90, 0, 1, 0); + break; + case 1: + break; + case 2: + GL11.glRotatef(90, 0, 1, 0); + break; + case 3: + GL11.glRotatef(180, 0, 1, 0); + break; + + } + break; + case WEST: + GL11.glRotatef(90, 0, 0, 1); + GL11.glTranslatef(1, -1, 0); + switch (part.face) + { + case 0: + break; + case 1: + GL11.glRotatef(90, 0, 1, 0); + break; + case 2: + GL11.glRotatef(180, 0, 1, 0); + break; + case 3: + GL11.glRotatef(-90, 0, 1, 0); + break; + } + break; + case EAST: + GL11.glRotatef(-90, 0, 0, 1); + GL11.glTranslatef(-1, -1, 0); + switch (part.face) + { + case 0: + GL11.glRotatef(180, 0, 1, 0); + break; + case 1: + GL11.glRotatef(-90, 0, 1, 0); + break; + case 2: + break; + case 3: + GL11.glRotatef(90, 0, 1, 0); + break; + } + break; + } + + FMLClientHandler.instance().getClient().renderEngine.bindTexture(TEXTURE); + MODEL.render(0.0625F); + GL11.glPopMatrix(); + } } \ No newline at end of file