use of me.deecaad.weaponmechanics.wrappers.PlayerWrapper in project MechanicsMain by WeaponMechanics.
the class ShootHandler method shoot.
/**
* Shoots using weapon.
* Does not use ammo nor check for it.
*/
public void shoot(EntityWrapper entityWrapper, String weaponTitle, ItemStack weaponStack, Location shootLocation, boolean mainHand, boolean updateSpreadChange, boolean isMelee) {
Configuration config = getConfigurations();
LivingEntity livingEntity = entityWrapper.getEntity();
if (!isMelee) {
HandData handData = mainHand ? entityWrapper.getMainHandData() : entityWrapper.getOffHandData();
handData.setLastShotTime(System.currentTimeMillis());
if (getConfigurations().getBool(weaponTitle + ".Info.Show_Cooldown.Delay_Between_Shots") && entityWrapper.getEntity().getType() == EntityType.PLAYER) {
CompatibilityAPI.getEntityCompatibility().setCooldown((Player) entityWrapper, weaponStack.getType(), config.getInt(weaponTitle + ".Shoot.Delay_Between_Shots") / 50);
}
}
Mechanics shootMechanics = config.getObject(weaponTitle + ".Shoot.Mechanics", Mechanics.class);
if (shootMechanics != null)
shootMechanics.use(new CastData(entityWrapper, weaponTitle, weaponStack));
if (entityWrapper instanceof PlayerWrapper) {
WeaponInfoDisplay weaponInfoDisplay = getConfigurations().getObject(weaponTitle + ".Info.Weapon_Info_Display", WeaponInfoDisplay.class);
if (weaponInfoDisplay != null)
weaponInfoDisplay.send((PlayerWrapper) entityWrapper, mainHand ? EquipmentSlot.HAND : EquipmentSlot.OFF_HAND);
}
Projectile projectile = config.getObject(weaponTitle + ".Projectile", Projectile.class);
if (projectile == null || isMelee) {
// No projectile defined or was melee trigger
return;
}
Spread spread = config.getObject(weaponTitle + ".Shoot.Spread", Spread.class);
Recoil recoil = config.getObject(weaponTitle + ".Shoot.Recoil", Recoil.class);
double projectileSpeed = config.getDouble(weaponTitle + ".Shoot.Projectile_Speed");
for (int i = 0; i < config.getInt(weaponTitle + ".Shoot.Projectiles_Per_Shot"); ++i) {
// i == 0
// -> Only allow spread changing on first shot
Vector motion = spread != null ? spread.getNormalizedSpreadDirection(entityWrapper, mainHand, i == 0 && updateSpreadChange).multiply(projectileSpeed) : livingEntity.getLocation().getDirection().multiply(projectileSpeed);
if (recoil != null && i == 0 && livingEntity instanceof Player) {
recoil.start((Player) livingEntity, mainHand);
}
// Only create bullet first if WeaponShootEvent changes
WeaponProjectile bullet = projectile.create(livingEntity, shootLocation, motion, weaponStack, weaponTitle);
WeaponShootEvent shootEvent = new WeaponShootEvent(bullet);
Bukkit.getPluginManager().callEvent(shootEvent);
bullet = shootEvent.getProjectile();
// Shoot the given bullet
projectile.shoot(bullet, shootLocation);
}
}
use of me.deecaad.weaponmechanics.wrappers.PlayerWrapper in project MechanicsMain by WeaponMechanics.
the class ShootHandler method doShootFirearmActions.
public void doShootFirearmActions(EntityWrapper entityWrapper, String weaponTitle, ItemStack weaponStack, HandData handData, EquipmentSlot slot) {
FirearmAction firearmAction = getConfigurations().getObject(weaponTitle + ".Firearm_Action", FirearmAction.class);
if (firearmAction == null || handData.hasRunningFirearmAction())
return;
FirearmState state = firearmAction.getState(weaponStack);
// If state is ready, check if this shot should not cause firearm actions
if (state == FirearmState.READY && (weaponHandler.getReloadHandler().getAmmoLeft(weaponStack, weaponTitle) % firearmAction.getFirearmActionFrequency() != 0 || !firearmAction.getFirearmType().hasShootActions())) {
return;
}
boolean mainhand = slot == EquipmentSlot.HAND;
LivingEntity shooter = entityWrapper.getEntity();
WeaponInfoDisplay weaponInfoDisplay = shooter.getType() != EntityType.PLAYER ? null : getConfigurations().getObject(weaponTitle + ".Info.Weapon_Info_Display", WeaponInfoDisplay.class);
PlayerWrapper playerWrapper = weaponInfoDisplay == null ? null : (PlayerWrapper) entityWrapper;
// Initiate CLOSE task
BukkitRunnable closeRunnable = new BukkitRunnable() {
@Override
public void run() {
ItemStack taskReference = mainhand ? entityWrapper.getEntity().getEquipment().getItemInMainHand() : entityWrapper.getEntity().getEquipment().getItemInOffHand();
if (taskReference == weaponStack) {
taskReference = weaponStack;
}
firearmAction.changeState(taskReference, FirearmState.READY);
if (weaponInfoDisplay != null)
weaponInfoDisplay.send(playerWrapper, slot);
handData.stopFirearmActionTasks();
}
};
// Init cast data
CastData castData = new CastData(entityWrapper, weaponTitle, weaponStack);
castData.setData(FirearmSound.getDataKeyword(), mainhand ? FirearmSound.MAIN_HAND.getId() : FirearmSound.OFF_HAND.getId());
// Check if OPEN state was already completed
if (state == FirearmState.CLOSE) {
// Only do CLOSE state
// Set the extra data so SoundMechanic knows to save task id to hand's firearm action tasks
firearmAction.useMechanics(castData, false);
if (weaponInfoDisplay != null)
weaponInfoDisplay.send(playerWrapper, slot);
if (getConfigurations().getBool(weaponTitle + ".Info.Show_Cooldown.Firearm_Actions_Time") && playerWrapper != null) {
CompatibilityAPI.getEntityCompatibility().setCooldown(playerWrapper.getPlayer(), weaponStack.getType(), firearmAction.getCloseTime());
}
handData.addFirearmActionTask(closeRunnable.runTaskLater(WeaponMechanics.getPlugin(), firearmAction.getCloseTime()).getTaskId());
// Return since we only want to do close state
return;
}
// Update state
if (state != FirearmState.OPEN)
firearmAction.changeState(weaponStack, FirearmState.OPEN);
// Set the extra data so SoundMechanic knows to save task id to hand's firearm action tasks
firearmAction.useMechanics(castData, true);
if (weaponInfoDisplay != null)
weaponInfoDisplay.send(playerWrapper, slot);
if (getConfigurations().getBool(weaponTitle + ".Info.Show_Cooldown.Firearm_Actions_Time") && playerWrapper != null) {
CompatibilityAPI.getEntityCompatibility().setCooldown(playerWrapper.getPlayer(), weaponStack.getType(), firearmAction.getOpenTime() + firearmAction.getCloseTime());
}
// Add the task to shoot firearm action tasks
handData.addFirearmActionTask(new BukkitRunnable() {
@Override
public void run() {
ItemStack taskReference = mainhand ? entityWrapper.getEntity().getEquipment().getItemInMainHand() : entityWrapper.getEntity().getEquipment().getItemInOffHand();
if (taskReference == weaponStack)
taskReference = weaponStack;
firearmAction.changeState(taskReference, FirearmState.CLOSE);
// Set the extra data so SoundMechanic knows to save task id to hand's firearm action tasks
CastData castData = new CastData(entityWrapper, weaponTitle, taskReference);
castData.setData(FirearmSound.getDataKeyword(), mainhand ? FirearmSound.MAIN_HAND.getId() : FirearmSound.OFF_HAND.getId());
firearmAction.useMechanics(castData, false);
if (weaponInfoDisplay != null)
weaponInfoDisplay.send(playerWrapper, slot);
handData.addFirearmActionTask(closeRunnable.runTaskLater(WeaponMechanics.getPlugin(), firearmAction.getCloseTime()).getTaskId());
}
}.runTaskLater(WeaponMechanics.getPlugin(), firearmAction.getOpenTime()).getTaskId());
}
use of me.deecaad.weaponmechanics.wrappers.PlayerWrapper in project MechanicsMain by WeaponMechanics.
the class TriggerPlayerListeners method swapHandItems.
// Event priority LOW to ensure that this is ran before.
// Weapon listeners PlayerSwapHandItemsEvent is ran.
// Basically lower priority means that it will be one of the first EventHandlers to run.
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOW)
public void swapHandItems(PlayerSwapHandItemsEvent e) {
if (getBasicConfigurations().getBool("Disabled_Trigger_Checks.Swap_Main_And_Hand_Items"))
return;
Player player = e.getPlayer();
EntityEquipment playerEquipment = player.getEquipment();
if (player.getGameMode() == GameMode.SPECTATOR || playerEquipment == null)
return;
PlayerWrapper playerWrapper = getPlayerWrapper(player);
ItemStack toMain = e.getMainHandItem();
String toMainWeapon = weaponHandler.getInfoHandler().getWeaponTitle(toMain, false);
ItemStack toOff = e.getOffHandItem();
String toOffWeapon = weaponHandler.getInfoHandler().getWeaponTitle(toOff, false);
if (toMainWeapon == null && toOffWeapon == null)
return;
if (toMainWeapon != null && getConfigurations().getBool(toMainWeapon + ".Info.Cancel.Swap_Hands") || toOffWeapon != null && getConfigurations().getBool(toOffWeapon + ".Info.Cancel.Swap_Hands")) {
e.setCancelled(true);
toOff = playerEquipment.getItemInMainHand();
toMain = playerEquipment.getItemInOffHand();
}
boolean dualWield = toMainWeapon != null && toOffWeapon != null;
if (isValid(toMain)) {
// SWAP_TO_MAIN_HAND
if (weaponHandler.getInfoHandler().denyDualWielding(TriggerType.SWAP_HANDS, player, toMainWeapon, toOffWeapon))
return;
// Only check off hand going to main hand
if (toMainWeapon != null) {
Bukkit.getScheduler().runTask(WeaponMechanics.getPlugin(), () -> weaponHandler.tryUses(playerWrapper, toMainWeapon, playerEquipment.getItemInOffHand(), EquipmentSlot.OFF_HAND, TriggerType.SWAP_HANDS, dualWield, null));
}
}
if (isValid(toOff)) {
// SWAP_TO_OFF_HAND
if (weaponHandler.getInfoHandler().denyDualWielding(TriggerType.SWAP_HANDS, player, toMainWeapon, toOffWeapon))
return;
// Only check main hand going to off hand
if (toOffWeapon != null) {
Bukkit.getScheduler().runTask(WeaponMechanics.getPlugin(), () -> weaponHandler.tryUses(playerWrapper, toOffWeapon, playerEquipment.getItemInMainHand(), EquipmentSlot.HAND, TriggerType.SWAP_HANDS, dualWield, null));
}
}
}
use of me.deecaad.weaponmechanics.wrappers.PlayerWrapper in project MechanicsMain by WeaponMechanics.
the class RecoilCommand method execute.
@Override
public void execute(CommandSender sender, String[] args) {
long rotationTime = Long.parseLong(args[0]);
long recoverTime = Long.parseLong(args[1]);
List<Float> yaws = new ArrayList<>();
for (String yaw : args[2].split(",")) {
yaws.add(Float.parseFloat(yaw));
}
List<Float> pitches = new ArrayList<>();
for (String pitch : args[3].split(",")) {
pitches.add(Float.parseFloat(pitch));
}
int fireRate = Integer.parseInt(args[4]);
if (fireRate < 1 || fireRate > 20) {
sender.sendMessage(ChatColor.RED + "Only fire rates between 1-20 are allowed for testing.");
return;
}
int shootTime = Integer.parseInt(args[5]);
Recoil recoil = new Recoil(rotationTime, recoverTime, yaws, pitches, null, null);
PlayerWrapper playerWrapper = WeaponMechanics.getPlayerWrapper((Player) sender);
new BukkitRunnable() {
int ticks = 0;
@Override
public void run() {
if (playerWrapper.isRightClicking()) {
recoil.start((Player) sender, true);
}
ticks += fireRate;
if (ticks > shootTime) {
cancel();
}
}
}.runTaskTimer(WeaponMechanics.getPlugin(), 0, fireRate);
}
Aggregations