mirror of
https://github.com/GTNewHorizons/TinkersConstruct.git
synced 2026-11-27 23:45:48 +00:00
Co-authored-by: Nikolay S. <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
423 lines
15 KiB
Java
423 lines
15 KiB
Java
package tconstruct.weaponry.weapons;
|
|
|
|
import java.util.List;
|
|
|
|
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 baubles.common.lib.PlayerHandler;
|
|
import mods.battlegear2.api.core.IBattlePlayer;
|
|
import tconstruct.compat.Battlegear2Compat;
|
|
import tconstruct.compat.LoadedMods;
|
|
import tconstruct.library.TConstructRegistry;
|
|
import tconstruct.library.crafting.ToolBuilder;
|
|
import tconstruct.library.tools.AbilityHelper;
|
|
import tconstruct.library.weaponry.AmmoItem;
|
|
import tconstruct.library.weaponry.IAmmo;
|
|
import tconstruct.library.weaponry.ProjectileWeapon;
|
|
import tconstruct.tools.TinkerTools;
|
|
import tconstruct.util.Reference;
|
|
import tconstruct.weaponry.TinkerWeaponry;
|
|
import tconstruct.weaponry.ammo.BoltAmmo;
|
|
import tconstruct.weaponry.entity.BoltEntity;
|
|
|
|
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;
|
|
|
|
ItemStack ammo = ItemStack.loadItemStackFromNBT(tags.getCompoundTag("LoadedItem"));
|
|
|
|
// prevent crashes from remapping corruptions..
|
|
if (ammo != null && ammo.getItem() != null && ammo.getItem() != TinkerWeaponry.boltAmmo) {
|
|
tags.removeTag("LoadedItem");
|
|
ammo = null;
|
|
}
|
|
|
|
return ammo;
|
|
}
|
|
|
|
@Override
|
|
public float getMinWindupProgress(ItemStack itemStack) {
|
|
return 1.0f;
|
|
}
|
|
|
|
@Override
|
|
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
|
|
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
|
|
|
|
// unload on sneak right-click
|
|
if (player.isSneaking() && 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 player)) return;
|
|
|
|
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");
|
|
|
|
if (tags.getBoolean("Loaded")) { // loaded, full accuracy
|
|
return 1.0f;
|
|
} else if (tags.hasKey("Reloading")) { // not loaded, but reloading -> progress
|
|
return 1.0f - ((float) tags.getInteger("Reloading")) / ((float) getWindupTime(itemStack));
|
|
} else { // not loaded and not reloading -> no accuracy!
|
|
return 0.0f;
|
|
}
|
|
}
|
|
|
|
public void initiateReload(ItemStack stack, EntityPlayer player, NBTTagCompound tags) {
|
|
if (tags.getBoolean("Broken")) return;
|
|
|
|
// has ammo?
|
|
if (searchForAmmo(player, stack) != null && !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 (!world.isRemote) {
|
|
if (ammo.getItem() instanceof IAmmo ammoItem) {
|
|
if (ammo.hasTagCompound()) {
|
|
int ammoReinforced = ammo.getTagCompound().getCompoundTag("InfiTool").getInteger("Unbreaking");
|
|
if (random.nextInt(10) < 10 - ammoReinforced) ammoItem.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, 1.0f);
|
|
|
|
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) {
|
|
// bolt priority: offhand > bauble slots > hotbar > inventory
|
|
if (LoadedMods.battlegear2 && player instanceof IBattlePlayer battlePlayer
|
|
&& battlePlayer.battlegear2$isBattlemode()) {
|
|
ItemStack offhand = Battlegear2Compat.getBattlegear2Offhand(player);
|
|
if (checkTinkerBolt(offhand)) return offhand;
|
|
}
|
|
|
|
if (LoadedMods.baubles) {
|
|
for (ItemStack bauble : PlayerHandler.getPlayerBaubles(player).stackList) {
|
|
if (checkTinkerBolt(bauble)) return bauble;
|
|
}
|
|
}
|
|
|
|
ItemStack[] inventory = player.inventory.mainInventory;
|
|
|
|
// check hotbar for bolts
|
|
for (ItemStack stack : inventory) {
|
|
if (checkTinkerBolt(stack)) return stack;
|
|
}
|
|
|
|
if (player.capabilities.isCreativeMode && TinkerWeaponry.creativeBolt != null)
|
|
return TinkerWeaponry.creativeBolt.copy();
|
|
|
|
return null;
|
|
}
|
|
|
|
private static boolean checkTinkerBolt(ItemStack stack) {
|
|
return stack != null && (stack.getItem() instanceof BoltAmmo bolt) && bolt.getAmmoCount(stack) > 0;
|
|
}
|
|
|
|
@Override
|
|
protected Entity createProjectile(ItemStack ammo, World world, EntityPlayer player, float speed, float accuracy,
|
|
float windup) {
|
|
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("crossbowReload"), 1.0f, 1.0f);
|
|
}
|
|
|
|
@Override
|
|
public void playFiringSound(World world, EntityPlayer player, ItemStack weapon, ItemStack ammo, float speed,
|
|
float accuracy) {
|
|
world.playSoundAtEntity(player, "random.bow", 1.0F, 0.5F);
|
|
}
|
|
|
|
@Override
|
|
public IIcon getIcon(ItemStack stack, int renderPass) {
|
|
if (!animateLayer(renderPass) && renderPass < getPartAmount()) return super.getIcon(stack, renderPass);
|
|
|
|
if (!stack.hasTagCompound()) return super.getIcon(stack, renderPass);
|
|
|
|
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
|
|
if (tags == null || renderPass > 10) return super.getIcon(stack, renderPass);
|
|
|
|
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"));
|
|
|
|
// are we drawing an effect?
|
|
if (renderPass >= getPartAmount()) {
|
|
// is the effect animated?
|
|
String effect = "Effect" + (1 + renderPass - getPartAmount());
|
|
if (tags.hasKey(effect)) {
|
|
int index = tags.getInteger(effect);
|
|
if (animationEffectIcons.get(index) != null)
|
|
return getCorrectAnimationIcon(animationEffectIcons, index, progress);
|
|
else
|
|
// non-animated
|
|
return effectIcons.get(index);
|
|
}
|
|
return super.getIcon(stack, renderPass);
|
|
}
|
|
|
|
// get the correct icon
|
|
return switch (renderPass) {
|
|
case 0 -> getCorrectAnimationIcon(animationHandleIcons, tags.getInteger("RenderHandle"), progress);
|
|
case 1 -> getCorrectAnimationIcon(animationHeadIcons, tags.getInteger("RenderHead"), progress);
|
|
case 2 -> getCorrectAnimationIcon(animationAccessoryIcons, tags.getInteger("RenderAccessory"), progress);
|
|
case 3 -> getCorrectAnimationIcon(animationExtraIcons, tags.getInteger("RenderExtra"), progress);
|
|
default -> emptyIcon;
|
|
};
|
|
}
|
|
|
|
@Override
|
|
public String getIconSuffix(int partType) {
|
|
return switch (partType) {
|
|
case 0 -> "_crossbow_bow"; // head
|
|
case 1 -> ""; // broken
|
|
case 2 -> "_crossbow_body"; // handle
|
|
case 3 -> "_crossbow_string"; // accessory
|
|
case 4 -> "_crossbow_binding"; // extra
|
|
default -> "";
|
|
};
|
|
}
|
|
|
|
@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 TinkerWeaponry.bowstring;
|
|
}
|
|
|
|
@Override
|
|
public Item getExtraItem() {
|
|
return TinkerTools.toughBinding;
|
|
}
|
|
|
|
@Override
|
|
public String[] getTraits() {
|
|
return new String[] { "weapon", "bow", "windup" };
|
|
}
|
|
|
|
@Override
|
|
public void buildTool(int id, String name, List<ItemStack> list) {
|
|
// does the material have a bow material?
|
|
if (TConstructRegistry.getBowMaterial(id) == null) return;
|
|
|
|
// 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");
|
|
return switch (mat) {
|
|
case 0 -> 0xffffffff; // string = white
|
|
case 1 -> 0xffccccff; // magic string = light blue
|
|
case 2 -> 0xffffcccc; // flamestring = light red
|
|
default -> super.getColorFromItemStack(stack, renderPass);
|
|
};
|
|
}
|
|
}
|