Skip to content

Commit

Permalink
Tinker arrows/bolts/shuriken/throwingknives/javelins now work with di…
Browse files Browse the repository at this point in the history
…spensers :OOoo
  • Loading branch information
bonii-xx committed Dec 3, 2014
1 parent 429c3b7 commit d16acb6
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 1 deletion.
@@ -0,0 +1,74 @@
package tconstruct.library.util;

import net.minecraft.block.BlockDispenser;
import net.minecraft.dispenser.BehaviorDefaultDispenseItem;
import net.minecraft.dispenser.IBlockSource;
import net.minecraft.dispenser.IPosition;
import net.minecraft.entity.Entity;
import net.minecraft.entity.IProjectile;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.world.World;
import tconstruct.library.entity.ProjectileBase;
import tconstruct.library.weaponry.IAmmo;

public abstract class BehaviorProjectileBaseDispense extends BehaviorDefaultDispenseItem {
/**
* Dispense the specified stack, play the dispense sound and spawn particles.
*/
@Override
public ItemStack dispenseStack(IBlockSource blockSource, ItemStack stack)
{
World world = blockSource.getWorld();
IPosition iposition = BlockDispenser.func_149939_a(blockSource);
EnumFacing enumfacing = BlockDispenser.func_149937_b(blockSource.getBlockMetadata());

ItemStack reference;

if(stack.getItem() instanceof IAmmo)
{
IAmmo ammo = (IAmmo) stack.getItem();
// needs ammo to shoot
if(ammo.getAmmoCount(stack) <= 0)
return stack;
ammo.consumeAmmo(1, stack);
reference = stack.copy();
((IAmmo)reference.getItem()).setAmmo(1, reference);
}
else
reference = stack.splitStack(1);

ProjectileBase projectile = this.getProjectileEntity(world, iposition, reference);
projectile.setThrowableHeading((double)enumfacing.getFrontOffsetX(), (double)((float)enumfacing.getFrontOffsetY() + ballistic()), (double)enumfacing.getFrontOffsetZ(), this.accuraccy(), this.speed());
projectile.returnStack = reference;
projectile.canBePickedUp = 1;
world.spawnEntityInWorld(projectile);

return stack;
}

/**
* Play the dispense sound from the specified block.
*/
protected void playDispenseSound(IBlockSource p_82485_1_)
{
p_82485_1_.getWorld().playAuxSFX(1002, p_82485_1_.getXInt(), p_82485_1_.getYInt(), p_82485_1_.getZInt(), 0);
}

/**
* Return the projectile entity spawned by this dispense behavior.
*/
protected abstract ProjectileBase getProjectileEntity(World world, IPosition position, ItemStack stack);

protected float speed()
{
return 6.0F;
}

protected float accuraccy()
{
return 1.1F;
}

protected float ballistic() { return 0.1f; }
}
8 changes: 8 additions & 0 deletions src/main/java/tconstruct/library/weaponry/AmmoItem.java
Expand Up @@ -60,6 +60,14 @@ public int consumeAmmo(int toUse, ItemStack stack) {
return toUse - (oldCount - newCount);
}

@Override
public void setAmmo(int count, ItemStack stack) {
if(!stack.hasTagCompound()) return;

NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
tags.setInteger("Ammo", count);
}

public float getAmmoModifier() { return 0.1f; }

public boolean pickupAmmo(ItemStack stack, ItemStack candidate, EntityPlayer player)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/tconstruct/library/weaponry/AmmoWeapon.java
Expand Up @@ -105,7 +105,8 @@ protected void launchProjectile(ItemStack stack, World world, EntityPlayer playe
if(!world.isRemote) {
ItemStack reference = stack.copy();
reference.stackSize = 1;
reference.getTagCompound().getCompoundTag("InfiTool").setInteger("Ammo", 1);
((IAmmo)reference.getItem()).setAmmo(1, reference);

Entity projectile = createProjectile(reference, world, player, getAccuracy(stack, time), time);
world.spawnEntityInWorld(projectile);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/tconstruct/library/weaponry/IAmmo.java
Expand Up @@ -35,4 +35,11 @@ public interface IAmmo {
* @return The amount of ammo that couldn't be removed. If you try to remove 5, but only 3 are left, it'll return 2.
*/
int consumeAmmo(int count, ItemStack stack);

/**
* Sets the Ammo amount to that absolute value. Behaviour for values below 0 or above the max ammo is undefined.
* @param count How much
* @param stack The itemstack to set the ammo for. Has to have proper NBT.
*/
void setAmmo(int count, ItemStack stack);
}
57 changes: 57 additions & 0 deletions src/main/java/tconstruct/weaponry/TinkerWeaponry.java
Expand Up @@ -9,19 +9,24 @@
import cpw.mods.fml.common.registry.GameRegistry;
import mantle.pulsar.pulse.Handler;
import mantle.pulsar.pulse.Pulse;
import net.minecraft.block.BlockDispenser;
import net.minecraft.dispenser.IPosition;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import tconstruct.TConstruct;
import tconstruct.library.TConstructRegistry;
import tconstruct.library.crafting.*;
import tconstruct.library.entity.ProjectileBase;
import tconstruct.library.tools.DynamicToolPart;
import tconstruct.library.tools.FletchlingLeafMaterial;
import tconstruct.library.util.BehaviorProjectileBaseDispense;
import tconstruct.library.util.IPattern;
import tconstruct.library.util.IToolPart;
import tconstruct.modifiers.tools.ModAttack;
Expand All @@ -36,6 +41,7 @@
import tconstruct.weaponry.ammo.ArrowAmmo;
import tconstruct.weaponry.ammo.BoltAmmo;
import tconstruct.library.tools.DualMaterialToolPart;
import tconstruct.weaponry.entity.*;
import tconstruct.weaponry.items.GlassArrows;
import tconstruct.weaponry.items.WeaponryPattern;
import tconstruct.library.weaponry.AmmoItem;
Expand Down Expand Up @@ -114,6 +120,8 @@ public void init(FMLInitializationEvent event)
ModifyBuilder.registerModifier(TinkerTools.modAttack);

TConstructRegistry.registerActiveToolMod(new WeaponryActiveToolMod());

registerDispenserProjectiles();
}

@Handler
Expand Down Expand Up @@ -389,4 +397,53 @@ private void setupCreativeTab()

TConstructRegistry.weaponryTab.init(tool);
}

private void registerDispenserProjectiles() {
// shuriken
BlockDispenser.dispenseBehaviorRegistry.putObject(shuriken, new BehaviorProjectileBaseDispense() {

@Override
protected ProjectileBase getProjectileEntity(World world, IPosition position, ItemStack stack) {
return new ShurikenEntity(world, position.getX(), position.getY(), position.getZ());
}

@Override
protected float ballistic() {
// shurikens are shot out straight
return 0f;
}
});
// throwing knifes
BlockDispenser.dispenseBehaviorRegistry.putObject(throwingknife, new BehaviorProjectileBaseDispense() {

@Override
protected ProjectileBase getProjectileEntity(World world, IPosition position, ItemStack stack) {
return new ThrowingKnifeEntity(world, position.getX(), position.getY(), position.getZ());
}
});
// Javelin
BlockDispenser.dispenseBehaviorRegistry.putObject(javelin, new BehaviorProjectileBaseDispense() {

@Override
protected ProjectileBase getProjectileEntity(World world, IPosition position, ItemStack stack) {
return new JavelinEntity(world, position.getX(), position.getY(), position.getZ());
}
});

// arrows
BlockDispenser.dispenseBehaviorRegistry.putObject(arrowAmmo, new BehaviorProjectileBaseDispense() {
@Override
protected ProjectileBase getProjectileEntity(World world, IPosition position, ItemStack stack) {
return new ArrowEntity(world, position.getX(), position.getY(), position.getZ());
}
});

// bolts
BlockDispenser.dispenseBehaviorRegistry.putObject(boltAmmo, new BehaviorProjectileBaseDispense() {
@Override
protected ProjectileBase getProjectileEntity(World world, IPosition position, ItemStack stack) {
return new BoltEntity(world, position.getX(), position.getY(), position.getZ());
}
});
}
}

0 comments on commit d16acb6

Please sign in to comment.