mirror of
https://github.com/GTNewHorizons/TinkersConstruct.git
synced 2026-11-20 04:09:40 +00:00
First merging of weaponry into tinkers
This commit is contained in:
1 parent
6e1ea0e736
commit
73b25d19f0
191 files changed
+5325
-1
No files matched your search
@@ -0,0 +1,442 @@
|
||||
package boni.tinkersweaponry.weapons;
|
||||
|
||||
import boni.tinkersweaponry.TinkerWeaponry;
|
||||
import boni.tinkersweaponry.ammo.BoltAmmo;
|
||||
import boni.tinkersweaponry.entity.BoltEntity;
|
||||
import boni.tinkersweaponry.library.weaponry.AmmoItem;
|
||||
import boni.tinkersweaponry.library.weaponry.ProjectileWeapon;
|
||||
import boni.tinkersweaponry.util.Reference;
|
||||
import boni.tinkersweaponry.library.weaponry.IAmmo;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.projectile.EntityArrow;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.world.World;
|
||||
import tconstruct.library.crafting.ToolBuilder;
|
||||
import tconstruct.library.tools.AbilityHelper;
|
||||
import tconstruct.tools.TinkerTools;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Crossbow extends ProjectileWeapon {
|
||||
public Crossbow() {
|
||||
super(0, "Crossbow");
|
||||
this.setMaxDamage(100);
|
||||
}
|
||||
|
||||
public boolean isLoaded(ItemStack itemStack)
|
||||
{
|
||||
if(itemStack.hasTagCompound())
|
||||
return isLoaded(itemStack.getTagCompound().getCompoundTag("InfiTool"));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isLoaded(NBTTagCompound tags)
|
||||
{
|
||||
return tags.getBoolean("Loaded");
|
||||
}
|
||||
|
||||
public ItemStack getLoadedAmmo(ItemStack itemStack)
|
||||
{
|
||||
NBTTagCompound tags = itemStack.getTagCompound().getCompoundTag("InfiTool");
|
||||
if(!isLoaded(tags))
|
||||
return null;
|
||||
|
||||
return ItemStack.loadItemStackFromNBT(tags.getCompoundTag("LoadedItem"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWindupTime(ItemStack itemStack) {
|
||||
NBTTagCompound toolTag = itemStack.getTagCompound().getCompoundTag("InfiTool");
|
||||
return (int)((float)toolTag.getInteger("DrawSpeed") * 2.5f);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getMinWindupProgress(ItemStack itemStack) {
|
||||
return 1.0f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getProjectileSpeed(ItemStack itemStack) {
|
||||
return super.getProjectileSpeed(itemStack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
|
||||
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
|
||||
|
||||
// unload on shift-rightclick
|
||||
if(player.isSneaking())
|
||||
if(unload(stack, player, tags))
|
||||
return stack;
|
||||
|
||||
// loaded
|
||||
if(tags.getBoolean("Loaded"))
|
||||
fire(stack, world, player);
|
||||
|
||||
// reload (automatically after firing)
|
||||
initiateReload(stack, player, tags);
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5) {
|
||||
super.onUpdate(stack, world, entity, par4, par5);
|
||||
|
||||
if(!stack.hasTagCompound())
|
||||
return;
|
||||
|
||||
if(!(entity instanceof EntityPlayer))
|
||||
return;
|
||||
|
||||
EntityPlayer player = (EntityPlayer) entity;
|
||||
if(player.inventory.getCurrentItem() != stack)
|
||||
return;
|
||||
|
||||
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
|
||||
if(tags.hasKey("Reloading"))
|
||||
{
|
||||
int timeLeft = tags.getInteger("Reloading");
|
||||
timeLeft--;
|
||||
if(timeLeft > 0)
|
||||
tags.setInteger("Reloading", timeLeft);
|
||||
else {
|
||||
tags.removeTag("Reloading");
|
||||
reload(stack, player, world, tags);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getWindupProgress(ItemStack itemStack, EntityPlayer player) {
|
||||
NBTTagCompound tags = itemStack.getTagCompound().getCompoundTag("InfiTool");
|
||||
|
||||
// loaded, full accuracy
|
||||
if(tags.getBoolean("Loaded"))
|
||||
return 1.0f;
|
||||
// not loaded, but reloading -> progress
|
||||
else if(tags.hasKey("Reloading"))
|
||||
return 1.0f - ((float)tags.getInteger("Reloading"))/((float)getWindupTime(itemStack));
|
||||
// not loaded and not reloading -> no accuracy!
|
||||
else
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
public void initiateReload(ItemStack stack, EntityPlayer player, NBTTagCompound tags) {
|
||||
if(tags.getBoolean("Broken"))
|
||||
return;
|
||||
|
||||
// has ammo?
|
||||
if(searchForAmmo(player, stack) != null)
|
||||
if(!tags.hasKey("Reloading"))
|
||||
// start reloading
|
||||
tags.setInteger("Reloading", getWindupTime(stack));
|
||||
}
|
||||
|
||||
// called after the reloading is done. Does the actual loading
|
||||
public boolean reload(ItemStack weapon, EntityPlayer player, World world, NBTTagCompound tags)
|
||||
{
|
||||
ItemStack ammo = searchForAmmo(player, weapon);
|
||||
// no ammo present
|
||||
if(ammo == null)
|
||||
return false;
|
||||
|
||||
// already loaded
|
||||
if(tags.getBoolean("Loaded"))
|
||||
return false;
|
||||
|
||||
ItemStack copy = ammo.copy();
|
||||
copy.getTagCompound().getCompoundTag("InfiTool").setInteger("Ammo", 1);
|
||||
|
||||
|
||||
// load ammo into nbt
|
||||
NBTTagCompound ammotag = new NBTTagCompound();
|
||||
copy.writeToNBT(ammotag);
|
||||
//ammotag.getCompoundTag("tag"; // set ammo count to 1
|
||||
tags.setTag("LoadedItem", ammotag);
|
||||
tags.setBoolean("Loaded", true);
|
||||
|
||||
// remove loaded item
|
||||
if(ammo.getItem() instanceof IAmmo)
|
||||
((IAmmo) ammo.getItem()).consumeAmmo(1, ammo);
|
||||
else
|
||||
player.inventory.consumeInventoryItem(ammo.getItem());
|
||||
|
||||
playReloadSound(world, player, weapon, ammo);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// stops reloading, or unloads the ammo if it has been loaded
|
||||
public boolean unload(ItemStack weapon, EntityPlayer player, NBTTagCompound tags)
|
||||
{
|
||||
// is reloading?
|
||||
if(tags.hasKey("Reloading"))
|
||||
{
|
||||
// stop it
|
||||
tags.removeTag("Reloading");
|
||||
return true;
|
||||
}
|
||||
|
||||
// loaded?
|
||||
if(tags.getBoolean("Loaded"))
|
||||
{
|
||||
// unload
|
||||
ItemStack loadedItem = ItemStack.loadItemStackFromNBT(tags.getCompoundTag("LoadedItem"));
|
||||
AmmoItem ammo = (AmmoItem) loadedItem.getItem();
|
||||
// try to pick it up
|
||||
ammo.pickupAmmo(loadedItem, searchForAmmo(player, weapon), player);
|
||||
|
||||
// unloaded
|
||||
tags.setBoolean("Loaded", false);
|
||||
tags.removeTag("LoadedItem");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// nothing to unload
|
||||
return false;
|
||||
}
|
||||
|
||||
// called if loaded to SHOOOOOOOT
|
||||
public void fire(ItemStack weapon, World world, EntityPlayer player)
|
||||
{
|
||||
NBTTagCompound tags = weapon.getTagCompound().getCompoundTag("InfiTool");
|
||||
// not loaded
|
||||
if(!tags.getBoolean("Loaded"))
|
||||
return;
|
||||
|
||||
// get ammo
|
||||
ItemStack ammo = ItemStack.loadItemStackFromNBT(tags.getCompoundTag("LoadedItem"));
|
||||
|
||||
// unload
|
||||
tags.setBoolean("Loaded", false);
|
||||
tags.removeTag("LoadedItem");
|
||||
|
||||
// safety
|
||||
if(ammo == null)
|
||||
return;
|
||||
|
||||
float projectileSpeed = getProjectileSpeed(weapon);
|
||||
float windup = getWindupTime(weapon); // max windup time
|
||||
float accuracy = getAccuracy(weapon, (int)windup);
|
||||
|
||||
Entity projectile = createProjectile(ammo, world, player, projectileSpeed, accuracy);
|
||||
|
||||
int reinforced = 0;
|
||||
|
||||
if (tags.hasKey("Unbreaking"))
|
||||
reinforced = tags.getInteger("Unbreaking");
|
||||
|
||||
if (random.nextInt(10) < 10 - reinforced)
|
||||
AbilityHelper.damageTool(weapon, 1, player, false);
|
||||
|
||||
playFiringSound(world, player, weapon, ammo, projectileSpeed, accuracy);
|
||||
|
||||
// thwock
|
||||
if (!world.isRemote)
|
||||
world.spawnEntityInWorld(projectile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack searchForAmmo(EntityPlayer player, ItemStack weapon) {
|
||||
// arrow priority: hotbar > inventory, tinker arrows > regular arrows
|
||||
ItemStack[] inventory = player.inventory.mainInventory;
|
||||
|
||||
// check hotbar for tinker arrows
|
||||
for(int i = 0; i < inventory.length; i++)
|
||||
{
|
||||
ItemStack stack = inventory[i];
|
||||
if(stack == null)
|
||||
continue;
|
||||
if(!(stack.getItem() instanceof BoltAmmo))
|
||||
continue;
|
||||
if(((IAmmo) stack.getItem()).getAmmoCount(stack) <= 0)
|
||||
continue;
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Entity createProjectile(ItemStack ammo, World world, EntityPlayer player, float speed, float accuracy) {
|
||||
EntityArrow arrow;
|
||||
|
||||
ItemStack reference = ammo.copy();
|
||||
reference.stackSize = 1;
|
||||
reference.getTagCompound().getCompoundTag("InfiTool").setInteger("Ammo", 1);
|
||||
arrow = new BoltEntity(world, player, speed, accuracy, reference);
|
||||
|
||||
if(player.capabilities.isCreativeMode)
|
||||
arrow.canBePickedUp = 2;
|
||||
|
||||
return arrow;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float minAccuracy(ItemStack itemStack) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float maxAccuracy(ItemStack itemStack) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void playReloadSound(World world, EntityPlayer player, ItemStack weapon, ItemStack ammo) {
|
||||
world.playSoundAtEntity(player, Reference.resource("woodHit"), 1.0f, 1.0f);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playFiringSound(World world, EntityPlayer player, ItemStack weapon, ItemStack ammo, float speed, float accuracy) {
|
||||
// todo: THWOCK
|
||||
}
|
||||
|
||||
@Override
|
||||
public IIcon getIcon(ItemStack stack, int renderPass) {
|
||||
if(!animateLayer(renderPass))
|
||||
return super.getIcon(stack, renderPass);
|
||||
|
||||
if(!stack.hasTagCompound())
|
||||
return super.getIcon(stack, renderPass);
|
||||
|
||||
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
|
||||
|
||||
float progress;
|
||||
|
||||
if(tags.getBoolean("Loaded"))
|
||||
progress = 1;
|
||||
else if(!tags.hasKey("Reloading"))
|
||||
return super.getIcon(stack, renderPass);
|
||||
else
|
||||
progress = getWindupProgress(stack, getWindupTime(stack) - tags.getInteger("Reloading"));
|
||||
|
||||
// effects aren't animated
|
||||
// todo: make effects animated
|
||||
if(renderPass >= getPartAmount())
|
||||
return super.getIcon(stack, renderPass);
|
||||
|
||||
// get the correct icon
|
||||
switch (renderPass)
|
||||
{
|
||||
case 0: return getCorrectAnimationIcon(animationHandleIcons, tags.getInteger("RenderHandle"), progress);
|
||||
case 1: return getCorrectAnimationIcon(animationHeadIcons, tags.getInteger("renderHead"), progress);
|
||||
case 2: return getCorrectAnimationIcon(animationAccessoryIcons, tags.getInteger("renderAccessory"), progress);
|
||||
case 3: return getCorrectAnimationIcon(animationExtraIcons, tags.getInteger("renderExtra"), progress);
|
||||
}
|
||||
|
||||
return emptyIcon;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIconSuffix (int partType)
|
||||
{
|
||||
switch (partType)
|
||||
{
|
||||
case 0:
|
||||
return "_crossbow_bow"; // head
|
||||
case 1:
|
||||
return ""; // broken
|
||||
case 2:
|
||||
return "_crossbow_body"; // handle
|
||||
case 3:
|
||||
return "_crossbow_string"; // accessory
|
||||
case 4:
|
||||
return "_crossbow_binding"; // extra
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEffectSuffix ()
|
||||
{
|
||||
return "_crossbow_effect";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDefaultFolder ()
|
||||
{
|
||||
return "crossbow";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPartAmount() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean animateLayer(int renderPass) {
|
||||
return renderPass == 1 || renderPass == 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item getHeadItem ()
|
||||
{
|
||||
return TinkerWeaponry.partCrossbowLimb;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item getHandleItem ()
|
||||
{
|
||||
return TinkerWeaponry.partCrossbowBody;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item getAccessoryItem ()
|
||||
{
|
||||
return TinkerTools.bowstring;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item getExtraItem() {
|
||||
return TinkerTools.toughBinding;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getTraits() {
|
||||
return new String[] {"weapon", "ranged", "crossbow"};
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void buildTool (int id, String name, List list)
|
||||
{
|
||||
// all creative bows use regular bowstring
|
||||
ItemStack headStack = new ItemStack(getHeadItem(), 1, id);
|
||||
ItemStack handleStack = new ItemStack(getHandleItem(), 1, id);
|
||||
ItemStack accessoryStack = new ItemStack(getAccessoryItem(), 1, 0); // bowstring
|
||||
ItemStack extraStack = getPartAmount() > 3 ? new ItemStack(getExtraItem(), 1, id) : null;
|
||||
|
||||
ItemStack tool = ToolBuilder.instance.buildTool(headStack, handleStack, accessoryStack, extraStack, "");
|
||||
if (tool != null)
|
||||
{
|
||||
tool.getTagCompound().getCompoundTag("InfiTool").setBoolean("Built", true);
|
||||
list.add(tool);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getColorFromItemStack(ItemStack stack, int renderPass) {
|
||||
if(renderPass != 2 || !stack.hasTagCompound())
|
||||
return super.getColorFromItemStack(stack, renderPass);
|
||||
|
||||
// bowstring: custom material -> custom coloring
|
||||
// todo: move this into the custom material itself
|
||||
int mat = stack.getTagCompound().getCompoundTag("InfiTool").getInteger("Accessory");
|
||||
switch(mat)
|
||||
{
|
||||
case 0: return 0xffffffff; // string = white
|
||||
case 1: return 0xffccccff; // macig string = light blue
|
||||
case 2: return 0xffffcccc; // flamestring = light red
|
||||
}
|
||||
return super.getColorFromItemStack(stack, renderPass);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user