Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: spear #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/combat/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
require('./pushing');
require('./spear');
76 changes: 76 additions & 0 deletions src/combat/spear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Material } from 'org.bukkit';
import { Attribute } from 'org.bukkit.attribute';
import {
AbstractHorse,
Donkey,
EntityType,
Horse,
Mule,
Player,
} from 'org.bukkit.entity';
import { EntityDamageByEntityEvent } from 'org.bukkit.event.entity';
import { CustomItem } from '../common/items/CustomItem';

const MAX_DISTANCE = 4;
const MIN_DISTANCE = 2;
const HORSE_MAX_SPEED = 0.3375;
const MULTIPLIER = 8;

const Spear = new CustomItem({
id: 7,
name: 'Keihäs',
type: Material.IRON_HOE,
modelId: 7,
});

registerEvent(EntityDamageByEntityEvent, (event) => {
const damager = event.damager;
if (!damager) return;
if (damager.type !== EntityType.PLAYER) return;
const attacker = damager as Player;

const item = attacker.itemInHand;
if (!item) return;
if (!Spear.check(item)) return;

// The weapon has cooled down if the value is 1
if (attacker.attackCooldown < 1) return;

const target = event.entity;
if (target.type !== EntityType.PLAYER) return;

const vehicle = target.vehicle;
if (!vehicle) return;
if (!(vehicle instanceof AbstractHorse)) return;

// Calculate the chance of dropping the enemy
const distance = attacker.location.distance(target.location);
const chance1 = 1 - minMaxScale(distance, MIN_DISTANCE, MAX_DISTANCE);
const chance2 = getSpeed(attacker);
const total = 1 - Math.pow(1 - chance1 * chance2, MULTIPLIER);
if (Math.random() > total) return;

// Drop the target
vehicle.eject();
});

function getSpeed(player: Player) {
const vehicle = player.vehicle;

let speed = 0;
if (vehicle instanceof AbstractHorse) {
// Max speed of the horse
speed =
vehicle.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED)?.value || speed;
} else {
// Max speed of the player (but only 30% effective)
0.3 * (player.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED)?.value || 0);
}
return minMaxScale(speed, 0, HORSE_MAX_SPEED);
}

function minMaxScale(val: number, min: number, max: number) {
if (val < min) return 0;
if (val > max) return 1;
return (val - min) / (max - min);
}