Jump to content

[SOLVED] [1.8] Making a Sword hit faster


JimiIT92

Recommended Posts

Hi guys :) I'm trying to make a Katana, wich will basically be a sword but faster, but i don't know how to change it's speed.

This is the code for my katana (it's just a copy of the ItemSword class), how can i make it faster (i mean if you click with a sword it does an animation to attack, so you can't attack every second until the animation is over, how can i speed up this animation?)

package blaze.items;

import com.google.common.collect.Multimap;
import com.sun.webkit.SharedBuffer;

import blaze.core.BLItems;
import blaze.core.BLTabs;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.attributes.AttributeModifier;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.EnumAction;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import sun.corba.SharedSecrets;

public class ItemKatana extends Item
{
private float attackDamage;
private final Item.ToolMaterial material;

public ItemKatana(Item.ToolMaterial material)
{
	this.material = material;
	this.maxStackSize = 1;
	this.setMaxDamage(material.getMaxUses());
	this.setCreativeTab(BLTabs.tabCombat);
	this.attackDamage = 2.0F + material.getDamageVsEntity();
}

/**
 * Returns the amount of damage this item will deal. One heart of damage is equal to 2 damage points.
 */
public float getDamageVsEntity()
{
	return this.material.getDamageVsEntity();
}

public float getStrVsBlock(ItemStack stack, Block block)
{
	Material material = block.getMaterial();
	return material != Material.plants && material != Material.vine && material != Material.coral && material != Material.leaves && material != Material.gourd ? 1.0F : 1.5F;
}

/**
 * Current implementations of this method in child classes do not use the entry argument beside ev. They just raise
 * the damage on the stack.
 *  
 * @param target The Entity being hit
 * @param attacker the attacking entity
 */
public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker)
{
	stack.damageItem(1, attacker);
	return true;
}

/**
 * Called when a Block is destroyed using this Item. Return true to trigger the "Use Item" statistic.
 */
public boolean onBlockDestroyed(ItemStack stack, World worldIn, Block blockIn, BlockPos pos, EntityLivingBase playerIn)
{
	if ((double)blockIn.getBlockHardness(worldIn, pos) != 0.0D)
	{
		stack.damageItem(2, playerIn);
	}

	return true;
}

/**
 * Returns True is the item is renderer in full 3D when hold.
 */
@SideOnly(Side.CLIENT)
public boolean isFull3D()
{
	return true;
}

/**
 * returns the action that specifies what animation to play when the items is being used
 */
public EnumAction getItemUseAction(ItemStack stack)
{
	return EnumAction.BLOCK;
}

/**
 * How long it takes to use or consume an item
 */
public int getMaxItemUseDuration(ItemStack stack)
{
	return 72000;
}

/**
 * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
 */
public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn)
{
	playerIn.setItemInUse(itemStackIn, this.getMaxItemUseDuration(itemStackIn));
	return itemStackIn;
}

/**
 * Return the enchantability factor of the item, most of the time is based on material.
 */
public int getItemEnchantability()
{
	return this.material.getEnchantability();
}

/**
 * Return the name for this tool's material.
 */
public String getToolMaterialName()
{
	return this.material.toString();
}

public boolean getIsRepairable(ItemStack toRepair, ItemStack repair)
{
	if(this.material.equals("ruby") && repair.getItem().equals(BLItems.ruby))
		return true;
	else
		return false;
}

/**
 * Gets a map of item attribute modifiers, used by ItemSword to increase hit damage.
 */
public Multimap getItemAttributeModifiers()
{
	Multimap multimap = super.getItemAttributeModifiers();
	multimap.put(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName(), new AttributeModifier(itemModifierUUID, "Weapon modifier", (double)this.attackDamage, 0));
	return multimap;
}
}

Don't blame me if i always ask for your help. I just want to learn to be better :)

Link to comment
Share on other sites

That doesn't speed up the animation, it just lets the target get hurt immediately.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

The potion effect Haste speeds up the animation for hitting, though it also does for block breaking. Try taking a look at it to see how you can achieve the same effect - remember to consider the speeding-up of block breaking.

Development of Plugins [2012 - 2014] Development of Mods [2012 - Current]

Link to comment
Share on other sites

Use your IDE. In Eclipse, I found a couple pieces of code relating specifically to this.

 

1.) The Potion Effect Declaration - This is found in net.minecraft.potion.Potion


//Potion effect is declared below

public static final Potion digSpeed = (new Potion(3, new ResourceLocation("haste"), false, 14270531)).setPotionName("potion.digSpeed").setIconIndex(2, 0).setEffectiveness(1.5D);

 

2.) The Swing Animation Speed - This is found in net.minecraft.entity.EntityLivingBase


//Modify the below method

/**
     * Returns an integer indicating the end point of the swing animation, used by {@link #swingProgress} to provide a
     * progress indicator. Takes dig speed enchantments into account.
     */
    private int getArmSwingAnimationEnd()
    {
        return this.isPotionActive(Potion.digSpeed) ? 6 - (1 + this.getActivePotionEffect(Potion.digSpeed).getAmplifier()) * 1 : (this.isPotionActive(Potion.digSlowdown) ? 6 + (1 + this.getActivePotionEffect(Potion.digSlowdown).getAmplifier()) * 2 : 6);
    }

 

Looking at the above modified code found in the paths provided, the first example declares the effect. The second example is what you want to look at, as it returns an integer pertaining to the end of the "swing animation".

Development of Plugins [2012 - 2014] Development of Mods [2012 - Current]

Link to comment
Share on other sites

After a bit more searching, I came across this field, which you can modify using reflection.

 

Field SwingProgressInt - Found in net.minecraft.entity.EntityLivingBase


//Change this to your needed speed - be sure to pay attention to the methods regarding this field below

public int swingProgressInt;

Development of Plugins [2012 - 2014] Development of Mods [2012 - Current]

Link to comment
Share on other sites

Oh, that's why i didn't find the code, it was in the entity class. Also i tried do "Search in file" in eclipse but it gives me an error i don't know why :/ I'll try working with that parameters and let you know, thanks :)

Don't blame me if i always ask for your help. I just want to learn to be better :)

Link to comment
Share on other sites

Ok, so i've overrited this method

@Override
public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity)
{
	player.swingProgressInt = 1;
	return this.hitEntity(stack, (EntityLivingBase) entity, player);
}

and my katana is faster (wich is what i want), BUT if i hit an entity it doesn't deal damage :/

Don't blame me if i always ask for your help. I just want to learn to be better :)

Link to comment
Share on other sites

Ok, so i've looked into the super method wich is only a return false statement. So i changed my function to this and it worked :)

@Override
public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity)
{
	player.swingProgressInt = 3;
	return false;
}

 

Also fun fact: it seems that i can make my item only faster but not slower. xD

Don't blame me if i always ask for your help. I just want to learn to be better :)

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello im trying to make a mod and the past few days GSON has almost killed me, when i export the mod and then launch it through minecraft launcher i get java.lang.NoSuchMethodError: com.google.gson.JsonParser.parseString(Ljava/lang/String;)Lcom/google/gson/JsonElement; i have literally tried everything here is my build config   dependencies { minecraft("com.mojang:minecraft:1.8.9") mappings("de.oceanlabs.mcp:mcp_stable:22-1.8.9") forge("net.minecraftforge:forge:1.8.9-11.15.1.2318-1.8.9") compileOnly("cc.polyfrost:oneconfig-1.8.9-forge:0.2.2-alpha+") shadowImpl("cc.polyfrost:oneconfig-wrapper-launchwrapper:1.0.0-beta+") { isTransitive = false exclude(module = "gson") } shadowImpl("org.spongepowered:mixin:0.7.11-SNAPSHOT") { isTransitive = false exclude(module = "gson") } annotationProcessor("org.spongepowered:mixin:0.8.5-SNAPSHOT") shadowImpl("org.javassist:javassist:3.15.0-GA") { isTransitive = false exclude(module = "gson") } shadowImpl("com.neovisionaries:nv-i18n:1.28") { isTransitive = false exclude(module = "gson") } shadowImpl("org.apache.commons:commons-lang3:3.4") { isTransitive = false exclude(module = "gson") } shadowImpl("org.apache.httpcomponents:httpcore:4.4.5") { isTransitive = false exclude(module = "gson") } compileOnly("com.google.code.gson:gson:2.8.6") { isTransitive = false } configurations.all { resolutionStrategy { force("com.google.code.gson:gson:2.8.6") } } shadowImpl(fileTree( mapOf( "dir" to "libs", "include" to listOf("*.jar"), "exclude" to listOf( "asm", "asm-commons", "asm-tree", "gson", "unspecified", "nv-i18n" ) ) )) }  
    • @chxr Looks like you're making some sort of a crafting table / furnace hybrid? Are the inputs needing arranging like a shaped recipe, or is it shapeless? I'll assume it's shapeless since that just adds a lot more complexity. In that case I'd probably do something like this { "type": "relativedimensions:particle_rebound", "inputs": [ { "ingredient": { "item": "relativedimensions:aberrant_shard" }, "count": 8 } ], "fuel": { "tag": "relativedimensions:block/aberrant_fuel" }, "output": { "Count": 1, "id": "relativedimensions:aberrant_ingot" } } inputs: A list of ingredients and how many are needed. The count among each input adds up to 8. Since there's only 1 ingredient, the count is set to 8. fuel: Same thing as before but remove the list and just make it an object with a tag. output: Kept the same.   In this case the Codec I would make is public record ParticleReboundIngredient(Ingredient ingredient, int count) { public static final Codec<ParticleReboundIngredient> CODEC = RecordCodecBuilder.create( builder -> builder.group( Ingredient.CODEC.fieldOf("ingredient").forGetter((i) -> i.ingredient), Codec.INT.fieldOf("count").forGetter(i -> i.count) ).apply(builder, ParticleReboundIngredient::new) ); } public record ParticleReboundFuel(String tag) { public static final Codec<ParticleReboundFuel> CODEC = RecordCodecBuilder.create( builder -> builder.group(Codec.STRING.fieldOf("tag").forGetter(f -> f.tag)).apply(builder, ParticleReboundFuel::new) ); public boolean isFuel(ItemStack stack) { // TODO: Check if fuel item matches the tag } } public record ParticleReboundRecipe(List<ParticleReboundIngredient> inputs, ParticleReboundFuel fuel, ItemStack output) { public static final Codec<ParticleReboundRecipe> CODEC = RecordCodecBuilder.create( builder -> builder.group( ParticleReboundIngredient.CODEC.listOf().fieldOf("inputs").forGetter(r -> r.inputs), ParticleReboundFuel.CODEC.fieldOf("fuel").forGetter(r -> r.fuel), ItemStack.CODEC.fieldOf("output").forGetter(r -> r.output) ).apply(builder, ParticleReboundRecipe::new) ); }   There might be a more proper Codec for the fuel and the tag that's built into minecraft / forge, but I didn't look
    • Tested with 5900X, 64GB 3200 MHz, 3070 Ti, driver 551.86 GPU clock range: 200 - 1950 MHz, usually 210 MHz with 10-30% utilisation on desktop idle use Render settings: Vsync ON, Framerate unlimited, Render distance 32, Fullscreen ON (60hz monitor, so 60 fps with vsync) Vanilla minecraft: Main menu: 210mhz idle clock always, ingame clock around 400 - 1000 mhz with ~30% utilisation and after alt+tab stays at same. Modded install with curseforge app, only said forge added: Main menu: 210 mhz idle clock always, ingame clock around 400 - 1000 mhz with ~30% utilisation, but after alt+tab gpu clock shoots up to 1950 mhz and utilisation is at 50-60% After disabling all render and chunk related stuff in config, after alt+tab it goes up to 1500 mhz. So game turns gpu into space heater when its not even seen. GPU usage returns to expected when game comes back on top. If instead of unlimited fps, fps is set to ~60, then during alt+tab gpu stays at same range as vanilla, but minecraft has horrible limit logic and framerate limit causes lot of tearing, so its not an option. So it seems that when alt+tabbing, for some reason logic no longer takes vsync toggle into account. So expected behaviour would be for it to work like vanilla does. keep rendering in same manner, no matter if its seen and use same amount of resources. Essentially noticed this behaviour while playing Supersymmetry and after elimination testing, result was that only Forge was enough to cause it. And also confirmed it by doing fresh pack with only Forge added, so no other mods even in disabled state. Also happened with both singleplayer and remote multiplayer. Other offender was "Universal Tweaks" which introduced same effect to main menu with its # Removes the hardcoded 30 FPS limit in screens like the main menu B:"Uncap FPS"=true setting, meaning when game was on top, it still worked in limits of vsync cap, but after alt+tabbing even in main menu gpu usage shot up to max.
    • Add crash-reports with sites like https://paste.ee/ and paste the link to it here Make a test without the mod auditory
    • Funny enough, Chatgpt is confusing me even more. My BE works like a furnace, you need 8 items and two fuel to make a third item. Here is an example recipe. In this case, 8 aberrant shards, each one in one slot, and two of aberrant fuel, each one on one slot for a total of 10 slots. Supposedly any block with the tag aberrant_fuel should be accepted but im not sure this is how the json should look:   { "type": "relativedimensions:particle_rebound", "inputItem": [ { "item": "relativedimensions:aberrant_shard" } ], "fuelItem": [ { "tag": "relativedimensions:block/aberrant_fuel" } ], "output": { "Count": 1, "id": "relativedimensions:aberrant_ingot" } }  
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.