use of me.deecaad.weaponmechanics.weapon.reload.ReloadHandler in project MechanicsMain by WeaponMechanics.
the class ShootHandler method burstShot.
private boolean burstShot(EntityWrapper entityWrapper, String weaponTitle, ItemStack weaponStack, HandData handData, EquipmentSlot slot, boolean dualWield) {
Configuration config = getConfigurations();
int shotsPerBurst = config.getInt(weaponTitle + ".Shoot.Burst.Shots_Per_Burst");
int ticksBetweenEachShot = config.getInt(weaponTitle + ".Shoot.Burst.Ticks_Between_Each_Shot");
// Not used
if (shotsPerBurst == 0 || ticksBetweenEachShot == 0)
return false;
boolean mainhand = slot == EquipmentSlot.HAND;
boolean consumeItemOnShoot = getConfigurations().getBool(weaponTitle + ".Shoot.Consume_Item_On_Shoot");
handData.setBurstTask(new BukkitRunnable() {
int shots = 0;
@Override
public void run() {
ItemStack taskReference = mainhand ? entityWrapper.getEntity().getEquipment().getItemInMainHand() : entityWrapper.getEntity().getEquipment().getItemInOffHand();
if (taskReference == weaponStack)
taskReference = weaponStack;
// START RELOAD STUFF
ReloadHandler reloadHandler = weaponHandler.getReloadHandler();
if (entityWrapper.getMainHandData().isReloading() || entityWrapper.getOffHandData().isReloading()) {
handData.setBurstTask(0);
cancel();
return;
}
if (!reloadHandler.consumeAmmo(taskReference, weaponTitle, 1)) {
handData.setBurstTask(0);
cancel();
reloadHandler.startReloadWithoutTrigger(entityWrapper, weaponTitle, taskReference, slot, dualWield, false);
return;
}
// END RELOAD STUFF
// Only make the first projectile of burst modify spread change if its used
shoot(entityWrapper, weaponTitle, taskReference, getShootLocation(entityWrapper.getEntity(), dualWield, mainhand), mainhand, shots == 0, false);
if (consumeItemOnShoot && handleConsumeItemOnShoot(taskReference)) {
handData.setBurstTask(0);
cancel();
return;
}
if (++shots >= shotsPerBurst) {
handData.setBurstTask(0);
cancel();
if (reloadHandler.getAmmoLeft(taskReference, weaponTitle) == 0) {
reloadHandler.startReloadWithoutTrigger(entityWrapper, weaponTitle, taskReference, slot, dualWield, false);
} else {
doShootFirearmActions(entityWrapper, weaponTitle, taskReference, handData, slot);
}
}
}
}.runTaskTimer(WeaponMechanics.getPlugin(), 0, ticksBetweenEachShot).getTaskId());
return true;
}
use of me.deecaad.weaponmechanics.weapon.reload.ReloadHandler in project MechanicsMain by WeaponMechanics.
the class ShootHandler method fullAutoShot.
private boolean fullAutoShot(EntityWrapper entityWrapper, String weaponTitle, ItemStack weaponStack, HandData handData, EquipmentSlot slot, TriggerType triggerType, boolean dualWield) {
Configuration config = getConfigurations();
int fullyAutomaticShotsPerSecond = config.getInt(weaponTitle + ".Shoot.Fully_Automatic_Shots_Per_Second");
// Not used
if (fullyAutomaticShotsPerSecond == 0)
return false;
int baseAmountPerTick = fullyAutomaticShotsPerSecond / 20;
int extra = fullyAutomaticShotsPerSecond % 20;
boolean mainhand = slot == EquipmentSlot.HAND;
boolean consumeItemOnShoot = getConfigurations().getBool(weaponTitle + ".Shoot.Consume_Item_On_Shoot");
ReloadHandler reloadHandler = weaponHandler.getReloadHandler();
handData.setFullAutoTask(new BukkitRunnable() {
int tick = 0;
public void run() {
ItemStack taskReference = mainhand ? entityWrapper.getEntity().getEquipment().getItemInMainHand() : entityWrapper.getEntity().getEquipment().getItemInOffHand();
if (taskReference == weaponStack)
taskReference = weaponStack;
if (entityWrapper.getMainHandData().isReloading() || entityWrapper.getOffHandData().isReloading()) {
handData.setFullAutoTask(0);
cancel();
return;
}
int ammoLeft = reloadHandler.getAmmoLeft(taskReference, weaponTitle);
if (!keepFullAutoOn(entityWrapper, triggerType)) {
handData.setFullAutoTask(0);
cancel();
if (ammoLeft == 0) {
reloadHandler.startReloadWithoutTrigger(entityWrapper, weaponTitle, taskReference, slot, dualWield, false);
} else {
doShootFirearmActions(entityWrapper, weaponTitle, taskReference, handData, slot);
}
return;
}
int shootAmount;
if (extra != 0) {
shootAmount = (baseAmountPerTick + AUTO[extra - 1][tick]);
} else {
shootAmount = baseAmountPerTick;
}
if (ammoLeft != -1) {
// Check whether shoot amount of this tick should be changed
if (ammoLeft - shootAmount < 0) {
shootAmount = ammoLeft;
}
if (!reloadHandler.consumeAmmo(taskReference, weaponTitle, shootAmount)) {
handData.setFullAutoTask(0);
cancel();
reloadHandler.startReloadWithoutTrigger(entityWrapper, weaponTitle, taskReference, slot, dualWield, false);
return;
}
}
if (shootAmount == 1) {
shoot(entityWrapper, weaponTitle, taskReference, getShootLocation(entityWrapper.getEntity(), dualWield, mainhand), mainhand, true, false);
if (consumeItemOnShoot && handleConsumeItemOnShoot(taskReference)) {
handData.setFullAutoTask(0);
cancel();
return;
}
} else if (shootAmount > 1) {
// Don't try to shoot in this tick if shoot amount is 0
for (int i = 0; i < shootAmount; ++i) {
shoot(entityWrapper, weaponTitle, taskReference, getShootLocation(entityWrapper.getEntity(), dualWield, mainhand), mainhand, true, false);
if (consumeItemOnShoot && handleConsumeItemOnShoot(taskReference)) {
handData.setFullAutoTask(0);
cancel();
return;
}
}
}
if (++tick >= 20) {
tick = 0;
}
}
}.runTaskTimer(WeaponMechanics.getPlugin(), 0, 0).getTaskId());
return true;
}
use of me.deecaad.weaponmechanics.weapon.reload.ReloadHandler in project MechanicsMain by WeaponMechanics.
the class ShootHandler method shootWithoutTrigger.
/**
* @return true if was able to shoot
*/
public boolean shootWithoutTrigger(EntityWrapper entityWrapper, String weaponTitle, ItemStack weaponStack, EquipmentSlot slot, TriggerType triggerType, boolean dualWield) {
HandData handData = slot == EquipmentSlot.HAND ? entityWrapper.getMainHandData() : entityWrapper.getOffHandData();
// Don't even try if slot is already being used for full auto or burst
if (handData.isUsingFullAuto() || handData.isUsingBurst())
return false;
Configuration config = getConfigurations();
WeaponPreShootEvent preShootEvent = new WeaponPreShootEvent(weaponTitle, weaponStack, entityWrapper.getEntity());
Bukkit.getPluginManager().callEvent(preShootEvent);
if (preShootEvent.isCancelled())
return false;
// Cancel shooting if we can only shoot while scoped.
if (config.getBool(weaponTitle + ".Shoot.Only_Shoot_While_Scoped") && !handData.getZoomData().isZooming())
return false;
boolean isMelee = triggerType == TriggerType.MELEE;
// Handle worldguard flags
WorldGuardCompatibility worldGuard = CompatibilityAPI.getWorldGuardCompatibility();
Location loc = entityWrapper.getEntity().getLocation();
if (!worldGuard.testFlag(loc, entityWrapper instanceof PlayerWrapper ? ((PlayerWrapper) entityWrapper).getPlayer() : null, "weapon-shoot")) {
Object obj = worldGuard.getValue(loc, "weapon-shoot-message");
if (obj != null && !obj.toString().isEmpty()) {
entityWrapper.getEntity().sendMessage(StringUtil.color(obj.toString()));
}
return false;
}
ReloadHandler reloadHandler = weaponHandler.getReloadHandler();
if (!getConfigurations().getBool(weaponTitle + ".Shoot.Consume_Item_On_Shoot")) {
reloadHandler.handleWeaponStackAmount(entityWrapper, weaponStack);
}
int ammoLeft = reloadHandler.getAmmoLeft(weaponStack, weaponTitle);
// Check if other hand is reloading and deny shooting if it is
if (slot == EquipmentSlot.HAND) {
if (entityWrapper.getOffHandData().isReloading()) {
return false;
}
} else if (entityWrapper.getMainHandData().isReloading()) {
return false;
}
// FIREARM START
FirearmAction firearmAction = config.getObject(weaponTitle + ".Firearm_Action", FirearmAction.class);
if (firearmAction != null) {
FirearmState state = firearmAction.getState(weaponStack);
if (state != FirearmState.READY) {
if (ammoLeft > 0) {
// Since weapon still has ammo, only CLOSE weapon and let it shoot AFTER that
// Cancel reload if its running
handData.stopReloadingTasks();
// Call shoot firearm actions, so they can complete firearm actions
doShootFirearmActions(entityWrapper, weaponTitle, weaponStack, handData, slot);
} else {
// Else continue to reload from where it left on...
reloadHandler.startReloadWithoutTrigger(entityWrapper, weaponTitle, weaponStack, slot, dualWield, false);
}
// Return false since firearm state wasn't ready, and they need to be completed
return false;
}
}
// If no ammo left, start reloading
if (ammoLeft == 0) {
reloadHandler.startReloadWithoutTrigger(entityWrapper, weaponTitle, weaponStack, slot, dualWield, false);
return false;
} else if (handData.isReloading()) {
// Else if reloading, cancel it
handData.stopReloadingTasks();
}
// RELOAD END
boolean usesSelectiveFire = config.getObject(weaponTitle + ".Shoot.Selective_Fire.Trigger", Trigger.class) != null;
boolean isSelectiveFireAuto = false;
int selectiveFire = 0;
if (usesSelectiveFire) {
selectiveFire = CustomTag.SELECTIVE_FIRE.getInteger(weaponStack);
if (CustomTag.SELECTIVE_FIRE.hasInteger(weaponStack) && selectiveFire == SelectiveFireState.AUTO.getId()) {
isSelectiveFireAuto = true;
}
}
// Only check if selective fire doesn't have auto selected and it isn't melee
if (!isSelectiveFireAuto && !isMelee) {
int delayBetweenShots = config.getInt(weaponTitle + ".Shoot.Delay_Between_Shots");
if (delayBetweenShots != 0 && !NumberUtil.hasMillisPassed(handData.getLastShotTime(), delayBetweenShots))
return false;
}
int weaponEquipDelay = config.getInt(weaponTitle + ".Info.Weapon_Equip_Delay");
if (weaponEquipDelay != 0 && !NumberUtil.hasMillisPassed(handData.getLastEquipTime(), weaponEquipDelay))
return false;
int shootDelayAfterScope = config.getInt(weaponTitle + ".Scope.Shoot_Delay_After_Scope");
if (shootDelayAfterScope != 0 && !NumberUtil.hasMillisPassed(handData.getLastScopeTime(), shootDelayAfterScope))
return false;
if (isMelee) {
return singleShot(entityWrapper, weaponTitle, weaponStack, handData, slot, dualWield, isMelee);
}
if (usesSelectiveFire) {
switch(selectiveFire) {
case // 1 = burst, can't use SelectiveFireState.BURST.getId() here
(1):
return burstShot(entityWrapper, weaponTitle, weaponStack, handData, slot, dualWield);
case // 2 = auto, can't use SelectiveFireState.AUTO.getId() here
(2):
return fullAutoShot(entityWrapper, weaponTitle, weaponStack, handData, slot, triggerType, dualWield);
default:
return singleShot(entityWrapper, weaponTitle, weaponStack, handData, slot, dualWield, isMelee);
}
}
// First try full auto, then burst then single fire
return fullAutoShot(entityWrapper, weaponTitle, weaponStack, handData, slot, triggerType, dualWield) || burstShot(entityWrapper, weaponTitle, weaponStack, handData, slot, dualWield) || singleShot(entityWrapper, weaponTitle, weaponStack, handData, slot, dualWield, isMelee);
}
use of me.deecaad.weaponmechanics.weapon.reload.ReloadHandler in project MechanicsMain by WeaponMechanics.
the class ShootHandler method singleShot.
private boolean singleShot(EntityWrapper entityWrapper, String weaponTitle, ItemStack weaponStack, HandData handData, EquipmentSlot slot, boolean dualWield, boolean isMelee) {
boolean mainhand = slot == EquipmentSlot.HAND;
boolean consumeItemOnShoot = getConfigurations().getBool(weaponTitle + ".Shoot.Consume_Item_On_Shoot");
// START RELOAD STUFF
ReloadHandler reloadHandler = weaponHandler.getReloadHandler();
reloadHandler.consumeAmmo(weaponStack, weaponTitle, 1);
// END RELOAD STUFF
shoot(entityWrapper, weaponTitle, weaponStack, getShootLocation(entityWrapper.getEntity(), dualWield, mainhand), mainhand, true, isMelee);
if (consumeItemOnShoot && handleConsumeItemOnShoot(weaponStack)) {
return true;
}
if (reloadHandler.getAmmoLeft(weaponStack, weaponTitle) == 0) {
reloadHandler.startReloadWithoutTrigger(entityWrapper, weaponTitle, weaponStack, slot, dualWield, false);
} else {
doShootFirearmActions(entityWrapper, weaponTitle, weaponStack, handData, slot);
}
return true;
}
Aggregations