Created
November 2, 2014 16:17
-
-
Save airbreather/fdc110a2f7b35eb6c6f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2014 JPiston | |
* | |
* Licensed under The MIT License (MIT); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://opensource.org/licenses/MIT | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
/** | |
* Created by Alireza Khodakarami on 10/15/2014. | |
*/ | |
package com.jiraiyah.jpiston.items.tools; | |
import buildcraft.api.tools.IToolWrench; | |
import cofh.api.block.IDismantleable; | |
import cofh.api.energy.IEnergyContainerItem; | |
import cofh.api.energy.ItemEnergyContainer; | |
import com.jiraiyah.jpiston.JPiston; | |
import com.jiraiyah.jpiston.references.Names; | |
import com.jiraiyah.jpiston.references.Resources; | |
import com.jiraiyah.jpiston.utilities.PowerDisplayUtil; | |
import com.jiraiyah.jpiston.utilities.StringUtility; | |
import cpw.mods.fml.relauncher.Side; | |
import cpw.mods.fml.relauncher.SideOnly; | |
import net.minecraft.block.Block; | |
import net.minecraft.client.renderer.texture.IIconRegister; | |
import net.minecraft.creativetab.CreativeTabs; | |
import net.minecraft.entity.EntityLivingBase; | |
import net.minecraft.entity.player.EntityPlayer; | |
import net.minecraft.item.Item; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.nbt.NBTTagCompound; | |
import net.minecraft.world.World; | |
import net.minecraftforge.common.util.ForgeDirection; | |
import java.util.List; | |
public class DigitalWand extends ItemEnergyContainer implements IEnergyContainerItem, IToolWrench | |
{ | |
public DigitalWand () | |
{ | |
super(1200, 1200 / 1000, 0); | |
this.setUnlocalizedName( Names.Items.Tools.DIGITAL_WAND ); | |
this.setCreativeTab( JPiston.JPistonTab ); | |
this.setHasSubtypes( true ); | |
setMaxDamage(16); | |
} | |
@Override | |
public boolean doesSneakBypassUse ( World world, int x, int y, int z, EntityPlayer player ) | |
{ | |
return true; | |
} | |
@Override | |
public int receiveEnergy(ItemStack itemStack, int maxReceive, boolean simulate) { | |
int res = super.receiveEnergy( itemStack, maxReceive, simulate ); | |
if(res != 0 && !simulate) { | |
updateDamage(itemStack); | |
} | |
return res; | |
} | |
private void updateDamage(ItemStack stack) { | |
float r = (float) getEnergyStored(stack) / getMaxEnergyStored(stack); | |
int res = 16 - (int) (r * 16); | |
stack.setItemDamage( res ); | |
} | |
@Override | |
public int extractEnergy ( ItemStack itemStack, int amount, boolean simulate ) | |
{ | |
int res = super.extractEnergy(itemStack, maxExtract, simulate); | |
if(res != 0 && !simulate) { | |
updateDamage(itemStack); | |
} | |
return res; | |
} | |
public void extractInternal(ItemStack item, int powerUse) { | |
int res = Math.max(0, getEnergyStored(item) - powerUse); | |
setEnergy( item, res ); | |
} | |
@Override | |
@SideOnly(Side.CLIENT) | |
public void getSubItems(Item item, CreativeTabs par2CreativeTabs, List par3List) { | |
ItemStack is = new ItemStack(this); | |
setFull(is); | |
par3List.add(is); | |
is = new ItemStack(this); | |
setEnergy(is, 0); | |
par3List.add( is ); | |
} | |
void setEnergy(ItemStack container, int energy) { | |
if(container.stackTagCompound == null) { | |
container.stackTagCompound = new NBTTagCompound(); | |
} | |
container.stackTagCompound.setInteger("Energy", energy); | |
updateDamage(container); | |
} | |
public int getEnergy(ItemStack itemStack) | |
{ | |
return getEnergyStored( itemStack ); | |
} | |
public void setFull(ItemStack container) { | |
setEnergy(container, 1200); | |
} | |
@Override | |
public void onCreated(ItemStack itemStack, World world, EntityPlayer entityPlayer) { | |
setEnergy(itemStack, 0); | |
} | |
@Override | |
@SideOnly(Side.CLIENT) | |
public void addInformation(ItemStack itemStack, EntityPlayer par2EntityPlayer, List list, boolean par4) { | |
super.addInformation(itemStack, par2EntityPlayer, list, par4); | |
String str = PowerDisplayUtil.formatPower( getEnergyStored( itemStack ) ) + "/" | |
+ PowerDisplayUtil.formatPower(getMaxEnergyStored(itemStack)) + " " + PowerDisplayUtil.abrevation(); | |
list.add(str); | |
} | |
//region Localization | |
@Override | |
public String getUnlocalizedName () | |
{ | |
return String.format( "item.%s%s", Resources.Textures.RESOURCE_PREFIX, StringUtility.getUnwrappedUnlocalizedName( super.getUnlocalizedName().toLowerCase() ) ); | |
} | |
@Override | |
public String getUnlocalizedName ( ItemStack itemStack ) | |
{ | |
return String.format( "item.%s%s", Resources.Textures.RESOURCE_PREFIX,StringUtility.getUnwrappedUnlocalizedName( super.getUnlocalizedName().toLowerCase() ) ); | |
} | |
@Override | |
@SideOnly ( Side.CLIENT ) | |
public void registerIcons ( IIconRegister iconRegister ) | |
{ | |
itemIcon = iconRegister.registerIcon( this.getUnlocalizedName().substring( this.getUnlocalizedName().toLowerCase().indexOf( "." ) + 1) ); | |
} | |
//endregion | |
@Override | |
public boolean canWrench ( EntityPlayer player, int x, int y, int z ) | |
{ | |
return true; | |
} | |
@Override | |
public void wrenchUsed ( EntityPlayer player, int x, int y, int z ) | |
{ | |
} | |
@Override | |
public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ) | |
{ | |
if (world.isRemote) | |
{ | |
return false; | |
} | |
Block b = paramWorld.func_147439_a(x, y, z); | |
if (!(b instanceof IDismantleable)) | |
{ | |
// TODO: block rotating | |
return false; | |
} | |
IDismantleable dismantleable = (IDismantleable)b; | |
if (!dismantleable.canDismantle(player, world, x, y, z)) | |
{ | |
return false; | |
} | |
dismantleable.dismantleBlock(player, world, x, y, z, false); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment