Skip to content

Commit

Permalink
Custom bow support
Browse files Browse the repository at this point in the history
  • Loading branch information
IcarussOne committed Feb 24, 2024
1 parent aa743c8 commit 42f9944
Show file tree
Hide file tree
Showing 14 changed files with 301 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static void addRecipes() {
SmelterManager.addRecycleRecipe(energy, new ItemStack(MoCItems.katana), new ItemStack(MoCItems.ancientSilverIngot), 1);
SmelterManager.addRecycleRecipe(energy, new ItemStack(MoCItems.nunchaku), new ItemStack(MoCItems.ancientSilverIngot), 1);
SmelterManager.addRecycleRecipe(energy, new ItemStack(MoCItems.sai), new ItemStack(MoCItems.ancientSilverIngot), 1);
SmelterManager.addRecycleRecipe(energy, new ItemStack(MoCItems.silverBow), new ItemStack(MoCItems.ancientSilverIngot), 1);

// Pulverizer
energy = 3000;
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/drzhark/mocreatures/event/MoCEventHooksClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
package drzhark.mocreatures.event;

import drzhark.mocreatures.compat.CompatScreen;
import drzhark.mocreatures.item.MoCItemBow;
import net.minecraft.client.gui.GuiMainMenu;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraftforge.client.event.FOVUpdateEvent;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
Expand All @@ -20,6 +24,58 @@ public void displayCompatScreen(GuiOpenEvent event) {
}
}

// Courtesy of NeRdTheNed
@SubscribeEvent
public void FOV(FOVUpdateEvent event) {
final EntityPlayer eventPlayer = event.getEntity();
final Item eventItem = eventPlayer.getActiveItemStack().getItem();

if (eventItem instanceof MoCItemBow) {
float finalFov = event.getFov();
final float itemUseCount = ((MoCItemBow) eventItem).getMaxItemUseDuration(eventPlayer.getActiveItemStack()) - eventPlayer.getItemInUseCount();
/*
* First, we have to reverse the standard bow zoom.
* Minecraft helpfully applies the standard bow zoom
* to any item that is an instance of a ItemBow.
* However, our custom bows draw back at different speeds,
* so the standard zoom is not at the right speed.
* To compensate for this, we just calculate the standard bow zoom,
* and apply it in reverse.
*/
float realBow = itemUseCount / 20.0F;

if (realBow > 1.0F) {
realBow = 1.0F;
} else {
realBow *= realBow;
}

/*
* Minecraft uses finalFov *= 1.0F - (realBow * 0.15F)
* to calculate the standard bow zoom.
* To reverse this, we just divide it instead.
*/
finalFov /= 1.0F - (realBow * 0.15F);
/*
* We now calculate and apply our custom bow zoom.
* The only difference between standard bow zoom and custom bow zoom
* is that we change the hardcoded value of 20.0F to
* whatever drawTime is.
*/
float drawTime = 20 * ((MoCItemBow) eventItem).drawTimeMult;
float customBow = itemUseCount / drawTime;

if (customBow > 1.0F) {
customBow = 1.0F;
} else {
customBow *= customBow;
}

finalFov *= 1.0F - (customBow * 0.15F);
event.setNewfov(finalFov);
}
}

/* TODO: Fix rider rotation
@SubscribeEvent
public void renderClimbingRiderPre(RenderLivingEvent.Pre<EntityLivingBase> event) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/drzhark/mocreatures/init/MoCItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
import net.minecraft.item.Item.ToolMaterial;
import net.minecraft.item.ItemArmor.ArmorMaterial;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.potion.PotionEffect;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.common.util.EnumHelper;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.registries.IForgeRegistry;

import java.util.*;
Expand Down Expand Up @@ -127,6 +131,7 @@ public class MoCItems {
public static final MoCItemSword silversword = new MoCItemSword("silversword", SILVER);
public static final MoCItemMattock silverMattock = new MoCItemMattock("ancient_silver_mattock", SILVER, 6.0F, 1.3F);
public static final MoCItemAxe silveraxe = new MoCItemAxe("silveraxe", SILVER, 10.0F, 1.1F);
public static final MoCItemBow silverBow = new MoCItemBow("ancient_silver_bow", 720, 1.3F, 1.2F, 0.8F, 0.8F, Ingredient.fromStacks(new ItemStack(ancientSilverIngot)));
static ToolMaterial SCORPC = EnumHelper.addToolMaterial("SCORPC", 3, 371, 7.5F, 2.5F, 16).setRepairItem(new ItemStack(chitinCave));
public static final MoCItemSword scorpSwordCave = new MoCItemSword("scorpswordcave", SCORPC, 4);
public static final MoCItemMattock scorpMattockCave = new MoCItemMattock("dark_scorpion_mattock", SCORPC, 4.5F, 1.2F, 4);
Expand Down Expand Up @@ -199,6 +204,13 @@ public class MoCItems {
public static final MoCItemArmor helmetSilver = new MoCItemArmor("ancient_silver_helmet", silverARMOR, 4, EntityEquipmentSlot.HEAD);
public static final MoCItemArmor legsSilver = new MoCItemArmor("ancient_silver_leggings", silverARMOR, 4, EntityEquipmentSlot.LEGS);
public static final MoCItemArmor bootsSilver = new MoCItemArmor("ancient_silver_boots", silverARMOR, 4, EntityEquipmentSlot.FEET);

@SubscribeEvent
@SideOnly(Side.CLIENT)
public static void registerRenders(final ModelRegistryEvent modelRegistryEvent) {
// All bow items go here
ModelLoader.setCustomModelResourceLocation(silverBow, 0, new ModelResourceLocation(silverBow.delegate.name(), "inventory"));
}

@Mod.EventBusSubscriber(modid = MoCConstants.MOD_ID)
public static class RegistrationHandler {
Expand Down Expand Up @@ -251,6 +263,7 @@ public static void registerItems(final RegistryEvent.Register<Item> event) {
silversword,
silverMattock,
silveraxe,
silverBow,

essencedarkness,
essenceEternal,
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/drzhark/mocreatures/init/MoCRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void registerFuels(FurnaceFuelBurnTimeEvent event) {
event.setBurnTime(2400); // 12 items
} else if (event.getItemStack().getItem() == MoCItems.heartfire) {
event.setBurnTime(3200); // 16 items
} else if (event.getItemStack().getItem() == MoCItems.sharkaxe | event.getItemStack().getItem() == MoCItems.sharksword) {
} else if (event.getItemStack().getItem() == MoCItems.sharkMattock || event.getItemStack().getItem() == MoCItems.sharkaxe || event.getItemStack().getItem() == MoCItems.sharksword) {
event.setBurnTime(200); // 1 item
}
}
Expand Down Expand Up @@ -76,6 +76,11 @@ public static void registerRecipes(final RegistryEvent.Register<IRecipe> event)
GameRegistry.addSmelting(MoCItems.mocegg, new ItemStack(MoCItems.omelet), 0.35F);
GameRegistry.addSmelting(MoCItems.ostrichraw, new ItemStack(MoCItems.ostrichcooked), 0.35F);
GameRegistry.addSmelting(MoCItems.rawTurkey, new ItemStack(MoCItems.cookedTurkey), 0.35F);
GameRegistry.addSmelting(MoCItems.helmetSilver, new ItemStack(MoCItems.ancientSilverNugget), 0.25F);
GameRegistry.addSmelting(MoCItems.chestSilver, new ItemStack(MoCItems.ancientSilverNugget), 0.25F);
GameRegistry.addSmelting(MoCItems.legsSilver, new ItemStack(MoCItems.ancientSilverNugget), 0.25F);
GameRegistry.addSmelting(MoCItems.bootsSilver, new ItemStack(MoCItems.ancientSilverNugget), 0.25F);
GameRegistry.addSmelting(MoCItems.silverMattock, new ItemStack(MoCItems.ancientSilverNugget), 0.25F);
GameRegistry.addSmelting(MoCItems.silveraxe, new ItemStack(MoCItems.ancientSilverNugget), 0.25F);
GameRegistry.addSmelting(MoCItems.silversword, new ItemStack(MoCItems.ancientSilverNugget), 0.25F);
GameRegistry.addSmelting(MoCItems.turtleraw, new ItemStack(MoCItems.turtlecooked), 0.35F);
Expand Down
133 changes: 133 additions & 0 deletions src/main/java/drzhark/mocreatures/item/MoCItemBow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* GNU GENERAL PUBLIC LICENSE Version 3
*/
package drzhark.mocreatures.item;

import drzhark.mocreatures.MoCConstants;
import drzhark.mocreatures.MoCreatures;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.init.Enchantments;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemArrow;
import net.minecraft.item.ItemBow;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.stats.StatList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraft.world.World;
import net.minecraftforge.event.ForgeEventFactory;

public class MoCItemBow extends ItemBow {
public float damageMult;
public float velocityMult;
public float inaccuracy;
public float drawTimeMult;
public Ingredient repairMaterial;

public MoCItemBow(String name, int durability, float damageMult, float velocityMult, float drawTimeMult, float inaccuracy, Ingredient repairMaterial) {
this.setCreativeTab(MoCreatures.tabMoC);
this.setRegistryName(MoCConstants.MOD_ID, name);
this.setTranslationKey(name);
this.maxStackSize = 1;
this.setMaxDamage(durability);
this.damageMult = damageMult;
this.velocityMult = velocityMult;
this.drawTimeMult = drawTimeMult;
this.inaccuracy = inaccuracy;
this.repairMaterial = repairMaterial;
this.addPropertyOverride(new ResourceLocation("pull"), (ItemStack bow, World world, EntityLivingBase entity) -> {
if (entity == null)
return 0;

float drawTime = 20 * drawTimeMult;

return (this.getMaxItemUseDuration(bow) - entity.getItemInUseCount()) / drawTime;
});
}

@Override
public void onPlayerStoppedUsing(ItemStack itemStack, World world, EntityLivingBase entityLiving, int timeInUse) {
if (entityLiving instanceof EntityPlayer) {
EntityPlayer player = (EntityPlayer) entityLiving;
boolean isInfinityEnchant = player.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantments.INFINITY, itemStack) > 0;
ItemStack stack = this.findAmmo(player);

float chargeDivider = 1 * drawTimeMult;

int charge = (int) ((this.getMaxItemUseDuration(itemStack) - timeInUse) / chargeDivider);
charge = ForgeEventFactory.onArrowLoose(itemStack, world, player, charge, !stack.isEmpty() || isInfinityEnchant);
if (charge < 0) return;

if ((!stack.isEmpty() || isInfinityEnchant)) {
if (stack.isEmpty()) {
stack = new ItemStack(Items.ARROW);
}

float arrowVelocity = getArrowVelocity(charge);

if ((double) arrowVelocity >= 0.1D) {
boolean arrowInfinite = player.capabilities.isCreativeMode || (stack.getItem() instanceof ItemArrow && ((ItemArrow) stack.getItem()).isInfinite(stack, itemStack, player));

if (!world.isRemote) {
ItemArrow itemArrow = (ItemArrow) (stack.getItem() instanceof ItemArrow ? stack.getItem() : Items.ARROW);
EntityArrow entityArrow = itemArrow.createArrow(world, stack, player);
entityArrow = this.customizeArrow(entityArrow);
entityArrow.shoot(player, player.rotationPitch, player.rotationYaw, 0.0F, (arrowVelocity * 3.0F) * velocityMult, inaccuracy);

if (arrowVelocity == 1.0F) {
entityArrow.setIsCritical(true);
}

entityArrow.setDamage(entityArrow.getDamage() * damageMult);

int power = EnchantmentHelper.getEnchantmentLevel(Enchantments.POWER, itemStack);

if (power > 0) {
entityArrow.setDamage(entityArrow.getDamage() + (double) power * 0.5D + 0.5D);
}

int punch = EnchantmentHelper.getEnchantmentLevel(Enchantments.PUNCH, itemStack);

if (punch > 0) {
entityArrow.setKnockbackStrength(punch);
}

if (EnchantmentHelper.getEnchantmentLevel(Enchantments.FLAME, itemStack) > 0) {
entityArrow.setFire(100);
}

itemStack.damageItem(1, player);

if (arrowInfinite || player.capabilities.isCreativeMode && (stack.getItem() == Items.SPECTRAL_ARROW || stack.getItem() == Items.TIPPED_ARROW)) {
entityArrow.pickupStatus = EntityArrow.PickupStatus.CREATIVE_ONLY;
}

world.spawnEntity(entityArrow);
}

world.playSound(null, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_ARROW_SHOOT, SoundCategory.PLAYERS, 1, 1 / (itemRand.nextFloat() * 0.4F + 1.2F) + arrowVelocity * 0.5F);

if (!arrowInfinite && !player.capabilities.isCreativeMode) {
stack.shrink(1);

if (stack.isEmpty()) {
player.inventory.deleteStack(stack);
}
}

player.addStat(StatList.getObjectUseStats(this));
}
}
}
}

@Override
public boolean getIsRepairable(ItemStack toRepair, ItemStack repair) {
return repairMaterial.test(repair) || super.getIsRepairable(toRepair, repair);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"parent": "item/generated",
"textures": {
"layer0": "mocreatures:items/ancient_silver_bow_standby"
},
"display": {
"thirdperson_righthand": {
"rotation": [ -80, 260, -40 ],
"translation": [ -1, -2, 2.5 ],
"scale": [ 0.9, 0.9, 0.9 ]
},
"thirdperson_lefthand": {
"rotation": [ -80, -280, 40 ],
"translation": [ -1, -2, 2.5 ],
"scale": [ 0.9, 0.9, 0.9 ]
},
"firstperson_righthand": {
"rotation": [ 0, -90, 25 ],
"translation": [ 1.13, 3.2, 1.13],
"scale": [ 0.68, 0.68, 0.68 ]
},
"firstperson_lefthand": {
"rotation": [ 0, 90, -25 ],
"translation": [ 1.13, 3.2, 1.13],
"scale": [ 0.68, 0.68, 0.68 ]
}
},
"overrides": [
{
"predicate": {
"pulling": 1
},
"model": "mocreatures:item/ancient_silver_bow_pulling_0"
},
{
"predicate": {
"pulling": 1,
"pull": 0.65
},
"model": "mocreatures:item/ancient_silver_bow_pulling_1"
},
{
"predicate": {
"pulling": 1,
"pull": 0.9
},
"model": "mocreatures:item/ancient_silver_bow_pulling_2"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "mocreatures:item/ancient_silver_bow",
"textures": {
"layer0": "mocreatures:items/ancient_silver_bow_pulling_0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "mocreatures:item/ancient_silver_bow",
"textures": {
"layer0": "mocreatures:items/ancient_silver_bow_pulling_1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "mocreatures:item/ancient_silver_bow",
"textures": {
"layer0": "mocreatures:items/ancient_silver_bow_pulling_2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"type": "forge:ore_shaped",
"pattern": [
" S*",
"I *",
" S*"
],
"key": {
"S": {
"item": "mocreatures:ancientsilveringot"
},
"I": {
"type": "forge:ore_dict",
"ore": "ingotIron"
},
"*": {
"type": "forge:ore_dict",
"ore": "string"
}
},
"result": {
"item": "mocreatures:ancient_silver_bow"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 42f9944

Please sign in to comment.