Skip to content

Commit

Permalink
added a swapping delay when using the sword (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Trcx528 committed Mar 19, 2015
1 parent 3f11af6 commit 27be116
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/main/java/com/trcx/swapper/Common/Item/Swapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.*;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
Expand All @@ -39,7 +37,6 @@
*/

//TODO funny rendering with itemBlock
//TODO recipe

@Optional.Interface(iface = "cofh.api.energy.IEnergyContainerItem", modid = "ITA")
public class Swapper extends Item implements IEnergyContainerItem{
Expand All @@ -62,6 +59,7 @@ public class Swapper extends Item implements IEnergyContainerItem{
testTools.put("pickaxe", new ItemStack(Items.wooden_pickaxe));
testTools.put("shovel", new ItemStack(Items.wooden_shovel));
testTools.put("axe", new ItemStack(Items.wooden_axe));
testTools.put("sword", new ItemStack(Items.wooden_sword));
}

public Swapper(){
Expand All @@ -76,17 +74,19 @@ public Swapper(){
}
//region inventory stuff
public static ItemStack getLastStack(ItemStack swapper){
if (swapper.hasTagCompound()){
int slot = swapper.stackTagCompound.getInteger(stringLASTTOOL);
return getStack(slot, swapper);
if (!swapper.hasTagCompound()){
swapper.stackTagCompound = new NBTTagCompound();
}
return null;
int slot = swapper.stackTagCompound.getInteger(stringLASTTOOL);
return getStack(slot, swapper);
}

public static ItemStack getStack(int slot, ItemStack swapper){
ItemInventory inv = new ItemInventory(swapper, swapperSlots);
if (!swapper.hasTagCompound())
swapper.stackTagCompound = new NBTTagCompound();
if (swapper.stackTagCompound.hasKey("swung"))
slot = slotSword;
swapper.stackTagCompound.setInteger(stringLASTTOOL, slot);
if (inv.getStackInSlot(slot) != null && inv.getStackInSlot(slot).getItem() == Main.Swapper)
return null;
Expand Down Expand Up @@ -132,6 +132,7 @@ public boolean onBlockStartBreak(ItemStack swapper, int X, int Y, int Z, EntityP

@Override
public float getDigSpeed(ItemStack swapper, Block block, int metadata) {

String effTool = block.getHarvestTool(metadata);
ItemStack is;
if (effTool == null){
Expand All @@ -155,7 +156,7 @@ public float getDigSpeed(ItemStack swapper, Block block, int metadata) {
} else {
is = getStack(slotPICK, swapper);
}
} else { // have to handle vanilla not properly registering some blocks...
} else {
if (block == Blocks.web) {
is = getStack(slotSword, swapper);
} else {
Expand Down Expand Up @@ -211,6 +212,8 @@ public boolean canHarvestBlock(Block block, ItemStack swapper) {
public boolean onEntitySwing(EntityLivingBase player, ItemStack swapper) {
ItemStack is = getLastStack(swapper);
if(is != null) {
if (swapper.stackTagCompound.hasKey("swung") && swapper.stackTagCompound.getInteger(stringLASTTOOL) == slotSword)
swapper.stackTagCompound.setInteger("swung",0);
boolean ret = is.getItem().onEntitySwing(player, is);
putLastStack(swapper,is);
return ret;
Expand All @@ -221,6 +224,7 @@ public boolean onEntitySwing(EntityLivingBase player, ItemStack swapper) {
@Override
public boolean onLeftClickEntity(ItemStack swapper, EntityPlayer player, Entity mob) {
ItemStack is = getStack(slotSword,swapper);
swapper.stackTagCompound.setInteger("swung",0);
if(is != null) {
boolean ret = is.getItem().onLeftClickEntity(is, player, mob);
putLastStack(swapper,is);
Expand All @@ -232,6 +236,7 @@ public boolean onLeftClickEntity(ItemStack swapper, EntityPlayer player, Entity
@Override
public boolean itemInteractionForEntity(ItemStack swapper, EntityPlayer player, EntityLivingBase mob) {
ItemStack is = getStack(slotSword,swapper);
swapper.stackTagCompound.setInteger("swung",0);
if(is != null) {
boolean ret = is.getItem().itemInteractionForEntity(is, player, mob);
putLastStack(swapper,is);
Expand All @@ -243,6 +248,7 @@ public boolean itemInteractionForEntity(ItemStack swapper, EntityPlayer player,
@Override
public boolean hitEntity(ItemStack swapper, EntityLivingBase mob, EntityLivingBase player) {
ItemStack is = getStack(slotSword,swapper);
swapper.stackTagCompound.setInteger("swung",0);
if (is != null) {
boolean ret = is.getItem().hitEntity(is, mob, player);
putLastStack(swapper,is);
Expand Down Expand Up @@ -563,6 +569,16 @@ public void addInformation(ItemStack swapper, EntityPlayer player, List data, bo

@Override
public void onUpdate(ItemStack swapper, World world, Entity player, int par4, boolean par5) {
if (swapper.hasTagCompound()) {
if (swapper.stackTagCompound.hasKey("swung")){
int swingTime = swapper.stackTagCompound.getInteger("swung");
if (swingTime >= 4){
swapper.stackTagCompound.removeTag("swung");
} else {
swapper.stackTagCompound.setInteger("swung",swingTime + 1);
}
}
}
ItemStack is = getLastStack(swapper);
if (is != null) {
is.getItem().onUpdate(is, world, player, par4, par5);
Expand Down

0 comments on commit 27be116

Please sign in to comment.