Search in sources :

Example 1 with AmmoTypes

use of me.deecaad.weaponmechanics.weapon.reload.ammo.AmmoTypes in project MechanicsMain by WeaponMechanics.

the class WeaponHandler method tryUses.

/**
 * Tries all uses in this exact order
 * <pre>{@code
 * 1) Shoot
 * 2) Reload
 * 3) Scope
 * 4) Selective fire
 * 5) Ammo type switch
 * }</pre>
 *
 * @param entityWrapper the entity which caused trigger
 * @param weaponTitle the weapon title involved
 * @param weaponStack the weapon stack involved
 * @param slot the weapon slot used
 * @param triggerType the trigger which caused this
 * @param dualWield whether or not this was dual wield
 */
public void tryUses(EntityWrapper entityWrapper, String weaponTitle, ItemStack weaponStack, EquipmentSlot slot, TriggerType triggerType, boolean dualWield, @Nullable LivingEntity victim) {
    if (!weaponStack.hasItemMeta())
        return;
    // Try shooting (and melee)
    if (shootHandler.tryUse(entityWrapper, weaponTitle, weaponStack, slot, triggerType, dualWield, victim)) {
        if (triggerType.isSprintType())
            getSkinHandler().tryUse(triggerType, entityWrapper, weaponTitle, weaponStack, slot);
        return;
    }
    // Shooting wasn't valid, try reloading
    if (reloadHandler.tryUse(entityWrapper, weaponTitle, weaponStack, slot, triggerType, dualWield)) {
        if (triggerType.isSprintType())
            getSkinHandler().tryUse(triggerType, entityWrapper, weaponTitle, weaponStack, slot);
        return;
    }
    // Reloading wasn't valid, try scoping
    if (scopeHandler.tryUse(entityWrapper, weaponTitle, weaponStack, slot, triggerType, dualWield)) {
        if (triggerType.isSprintType())
            getSkinHandler().tryUse(triggerType, entityWrapper, weaponTitle, weaponStack, slot);
        return;
    }
    if (triggerType.isSprintType())
        getSkinHandler().tryUse(triggerType, entityWrapper, weaponTitle, weaponStack, slot);
    // Scoping wasn't valid, try selective fire
    Configuration config = getConfigurations();
    Trigger selectiveFireTrigger = config.getObject(weaponTitle + ".Shoot.Selective_Fire.Trigger", Trigger.class);
    if (selectiveFireTrigger != null && selectiveFireTrigger.check(triggerType, slot, entityWrapper)) {
        boolean hasBurst = config.getInt(weaponTitle + ".Shoot.Burst.Shots_Per_Burst") != 0 && config.getInt(weaponTitle + ".Shoot.Burst.Ticks_Between_Each_Shot") != 0;
        boolean hasAuto = config.getInt(weaponTitle + ".Shoot.Fully_Automatic_Shots_Per_Second") != 0;
        // 3) Auto
        if (!CustomTag.SELECTIVE_FIRE.hasInteger(weaponStack)) {
            if (hasBurst) {
                CustomTag.SELECTIVE_FIRE.setInteger(weaponStack, BURST.getId());
            } else if (hasAuto) {
                CustomTag.SELECTIVE_FIRE.setInteger(weaponStack, AUTO.getId());
            }
        } else {
            int currentSelectiveFire = CustomTag.SELECTIVE_FIRE.getInteger(weaponStack);
            switch(currentSelectiveFire) {
                case // 1 = burst, can't use SelectiveFireState.BURST.getId() here
                (1):
                    if (hasAuto) {
                        CustomTag.SELECTIVE_FIRE.setInteger(weaponStack, AUTO.getId());
                    } else {
                        CustomTag.SELECTIVE_FIRE.setInteger(weaponStack, SINGLE.getId());
                    }
                    break;
                case // 2 = auto, can't use SelectiveFireState.AUTO.getId() here
                (2):
                    CustomTag.SELECTIVE_FIRE.setInteger(weaponStack, SINGLE.getId());
                    break;
                default:
                    if (hasBurst) {
                        CustomTag.SELECTIVE_FIRE.setInteger(weaponStack, BURST.getId());
                    } else if (hasAuto) {
                        CustomTag.SELECTIVE_FIRE.setInteger(weaponStack, AUTO.getId());
                    }
                    break;
            }
        }
        Mechanics selectiveFireMechanics = config.getObject(weaponTitle + ".Shoot.Selective_Fire.Mechanics", Mechanics.class);
        if (selectiveFireMechanics != null)
            selectiveFireMechanics.use(new CastData(entityWrapper, weaponTitle, weaponStack));
        WeaponInfoDisplay weaponInfoDisplay = getConfigurations().getObject(weaponTitle + ".Info.Weapon_Info_Display", WeaponInfoDisplay.class);
        if (weaponInfoDisplay != null)
            weaponInfoDisplay.send((PlayerWrapper) entityWrapper, slot);
        entityWrapper.getMainHandData().cancelTasks();
        entityWrapper.getOffHandData().cancelTasks();
        return;
    }
    // Selective fire wasn't valid, try ammo type switch
    Trigger ammoTypeSwitchTrigger = config.getObject(weaponTitle + ".Reload.Ammo.Ammo_Type_Switch.Trigger", Trigger.class);
    if (ammoTypeSwitchTrigger != null && entityWrapper instanceof PlayerWrapper && ammoTypeSwitchTrigger.check(triggerType, slot, entityWrapper)) {
        AmmoTypes ammoTypes = config.getObject(weaponTitle + ".Reload.Ammo.Ammo_Types", AmmoTypes.class);
        if (ammoTypes != null) {
            // First empty the current ammo
            int ammoLeft = CustomTag.AMMO_LEFT.getInteger(weaponStack);
            if (ammoLeft > 0) {
                ammoTypes.giveAmmo(weaponStack, (PlayerWrapper) entityWrapper, ammoLeft, config.getInt(weaponTitle + ".Reload.Magazine_Size"));
                CustomTag.AMMO_LEFT.setInteger(weaponStack, 0);
            }
            // Then do the switch
            ammoTypes.updateToNextAmmoType(weaponStack);
            Mechanics ammoTypeSwitchMechanics = getConfigurations().getObject(weaponTitle + ".Reload.Ammo.Ammo_Type_Switch.Mechanics", Mechanics.class);
            if (ammoTypeSwitchMechanics != null)
                ammoTypeSwitchMechanics.use(new CastData(entityWrapper, weaponTitle, weaponStack));
            WeaponInfoDisplay weaponInfoDisplay = getConfigurations().getObject(weaponTitle + ".Info.Weapon_Info_Display", WeaponInfoDisplay.class);
            if (weaponInfoDisplay != null)
                weaponInfoDisplay.send((PlayerWrapper) entityWrapper, slot);
            entityWrapper.getMainHandData().cancelTasks();
            entityWrapper.getOffHandData().cancelTasks();
        // Here has to be return if new triggers gets added
        }
    }
}
Also used : CastData(me.deecaad.weaponmechanics.mechanics.CastData) AmmoTypes(me.deecaad.weaponmechanics.weapon.reload.ammo.AmmoTypes) Trigger(me.deecaad.weaponmechanics.weapon.trigger.Trigger) Configuration(me.deecaad.core.file.Configuration) Mechanics(me.deecaad.weaponmechanics.mechanics.Mechanics) WeaponInfoDisplay(me.deecaad.weaponmechanics.weapon.info.WeaponInfoDisplay) PlayerWrapper(me.deecaad.weaponmechanics.wrappers.PlayerWrapper)

Example 2 with AmmoTypes

use of me.deecaad.weaponmechanics.weapon.reload.ammo.AmmoTypes in project MechanicsMain by WeaponMechanics.

the class ReloadHandler method startReloadWithoutTriggerAndWithoutTiming.

private boolean startReloadWithoutTriggerAndWithoutTiming(EntityWrapper entityWrapper, String weaponTitle, ItemStack weaponStack, EquipmentSlot slot, boolean dualWield, boolean isReloadLoop) {
    // Don't try to reload if either one of the hands is already reloading / full autoing
    HandData mainHandData = entityWrapper.getMainHandData();
    HandData offHandData = entityWrapper.getOffHandData();
    if (mainHandData.isReloading() || mainHandData.isUsingFullAuto() || mainHandData.isUsingBurst() || offHandData.isReloading() || offHandData.isUsingFullAuto() || offHandData.isUsingBurst()) {
        return false;
    }
    WeaponPreReloadEvent preReloadEvent = new WeaponPreReloadEvent(weaponTitle, weaponStack, entityWrapper.getEntity());
    Bukkit.getPluginManager().callEvent(preReloadEvent);
    if (preReloadEvent.isCancelled())
        return false;
    Configuration config = getConfigurations();
    int reloadDuration = config.getInt(weaponTitle + ".Reload.Reload_Duration");
    int tempMagazineSize = config.getInt(weaponTitle + ".Reload.Magazine_Size");
    if (tempMagazineSize <= 0 || reloadDuration <= 0) {
        // This ensures that non intended reloads doesn't occur from ShootHandler for example
        return false;
    }
    int ammoLeft = getAmmoLeft(weaponStack, weaponTitle);
    if (ammoLeft == -1) {
        // This shouldn't be -1 at this point since reload should be used, perhaps ammo was added for weapon in configs later in server...
        CustomTag.AMMO_LEFT.setInteger(weaponStack, 0);
        ammoLeft = 0;
    }
    // On reload force zoom out
    entityWrapper.getMainHandData().ifZoomingForceZoomOut();
    entityWrapper.getOffHandData().ifZoomingForceZoomOut();
    boolean mainhand = slot == EquipmentSlot.HAND;
    HandData handData = mainhand ? entityWrapper.getMainHandData() : entityWrapper.getOffHandData();
    int ammoPerReload = config.getInt(weaponTitle + ".Reload.Ammo_Per_Reload", -1);
    // Check how much ammo should be added during this reload iteration
    int tempAmmoToAdd;
    if (ammoPerReload != -1) {
        tempAmmoToAdd = ammoPerReload;
        if (ammoLeft + tempAmmoToAdd > tempMagazineSize) {
            tempAmmoToAdd = tempMagazineSize - ammoLeft;
        }
    } else {
        tempAmmoToAdd = tempMagazineSize - ammoLeft;
    }
    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;
    FirearmAction firearmAction = config.getObject(weaponTitle + ".Firearm_Action", FirearmAction.class);
    FirearmState state = null;
    boolean isRevolver = false;
    boolean isPump = false;
    int firearmOpenTime = 0;
    int firearmCloseTime = 0;
    if (firearmAction != null) {
        state = firearmAction.getState(weaponStack);
        isRevolver = firearmAction.getFirearmType() == FirearmType.REVOLVER;
        isPump = firearmAction.getFirearmType() == FirearmType.PUMP;
        // Is revolver or ammo is 0
        if (!isReloadLoop && (isRevolver || ammoLeft <= 0)) {
            firearmOpenTime = firearmAction.getOpenTime();
            firearmCloseTime = firearmAction.getCloseTime();
            switch(state) {
                case OPEN:
                    if (isPump)
                        reloadDuration = 0;
                    break;
                case CLOSE:
                    firearmOpenTime = 0;
                    reloadDuration = 0;
                    break;
                default:
                    break;
            }
        }
    }
    WeaponReloadEvent reloadEvent = new WeaponReloadEvent(weaponTitle, weaponStack, entityWrapper.getEntity(), reloadDuration, tempAmmoToAdd, tempMagazineSize, firearmOpenTime, firearmCloseTime);
    Bukkit.getPluginManager().callEvent(reloadEvent);
    reloadDuration = reloadEvent.getReloadTime();
    tempAmmoToAdd = reloadEvent.getReloadAmount();
    tempMagazineSize = reloadEvent.getMagazineSize();
    firearmOpenTime = reloadEvent.getFirearmOpenTime();
    firearmCloseTime = reloadEvent.getFirearmCloseTime();
    final int finalAmmoToAdd = tempAmmoToAdd;
    final int magazineSize = tempMagazineSize;
    if (ammoLeft >= magazineSize || reloadDuration == 0) {
        if (state != null && state != FirearmState.READY) {
            if (!isPump) {
                // Since with pump we want to first OPEN and then CLOSE
                if (state != FirearmState.CLOSE)
                    firearmAction.changeState(weaponStack, FirearmState.CLOSE);
            }
            // Simply CLOSE weapon or OPEN CLOSE if pump
            weaponHandler.getShootHandler().doShootFirearmActions(entityWrapper, weaponTitle, weaponStack, handData, slot);
            // Here true because firearm actions started
            return true;
        }
        return false;
    }
    AmmoTypes ammoTypes = playerWrapper != null ? config.getObject(weaponTitle + ".Reload.Ammo.Ammo_Types", AmmoTypes.class) : null;
    if (ammoTypes != null && !ammoTypes.hasAmmo(weaponTitle, weaponStack, playerWrapper)) {
        Mechanics outOfAmmoMechanics = getConfigurations().getObject(weaponTitle + ".Reload.Ammo.Out_Of_Ammo", Mechanics.class);
        if (outOfAmmoMechanics != null)
            outOfAmmoMechanics.use(new CastData(entityWrapper, weaponTitle, weaponStack));
        return false;
    }
    boolean unloadAmmoOnReload = config.getBool(weaponTitle + ".Reload.Unload_Ammo_On_Reload");
    // This is necessary for events to be used correctly
    handData.setReloadData(weaponTitle, weaponStack);
    ChainTask reloadTask = new ChainTask(reloadDuration) {

        private int unloadedAmount;

        @Override
        public void task() {
            ItemStack taskReference = mainhand ? entityWrapper.getEntity().getEquipment().getItemInMainHand() : entityWrapper.getEntity().getEquipment().getItemInOffHand();
            if (taskReference == weaponStack) {
                taskReference = weaponStack;
            } else {
                handData.setReloadData(weaponTitle, taskReference);
            }
            int ammoLeft = getAmmoLeft(taskReference, weaponTitle);
            // Here creating this again since this may change if there isn't enough ammo...
            int ammoToAdd = finalAmmoToAdd + unloadedAmount;
            if (ammoTypes != null) {
                int removedAmount = ammoTypes.removeAmmo(taskReference, playerWrapper, ammoToAdd, magazineSize);
                // Just check if for some reason ammo disappeared from entity before reaching reload "complete" state
                if (removedAmount <= 0) {
                    Mechanics outOfAmmoMechanics = getConfigurations().getObject(weaponTitle + ".Reload.Ammo.Out_Of_Ammo", Mechanics.class);
                    if (outOfAmmoMechanics != null)
                        outOfAmmoMechanics.use(new CastData(entityWrapper, weaponTitle, taskReference));
                    // Remove next task as reload can't be finished
                    setNextTask(null);
                    handData.stopReloadingTasks();
                    return;
                }
                // Else simply set ammo to add value to removed amount
                // Removed amount will be less than ammo to add amount IF player didn't have that much ammo
                ammoToAdd = removedAmount;
            }
            int finalAmmoSet = ammoLeft + ammoToAdd;
            handleWeaponStackAmount(entityWrapper, taskReference);
            CustomTag.AMMO_LEFT.setInteger(taskReference, finalAmmoSet);
            finishReload(entityWrapper, weaponTitle, taskReference, handData, slot);
            if (ammoPerReload != -1) {
                // Start the loop
                startReloadWithoutTrigger(entityWrapper, weaponTitle, taskReference, slot, dualWield, true);
            }
        }

        @Override
        public void setup() {
            handData.addReloadTask(getTaskId());
            int ammoLeft = CustomTag.AMMO_LEFT.getInteger(weaponStack);
            if (unloadAmmoOnReload && ammoLeft > 0) {
                if (ammoTypes != null)
                    ammoTypes.giveAmmo(weaponStack, playerWrapper, ammoLeft, magazineSize);
                unloadedAmount = ammoLeft;
                handleWeaponStackAmount(entityWrapper, weaponStack);
                CustomTag.AMMO_LEFT.setInteger(weaponStack, 0);
            }
            CastData castData = new CastData(entityWrapper, weaponTitle, weaponStack);
            // Set the extra data so SoundMechanic knows to save task id to hand's reload tasks
            castData.setData(ReloadSound.getDataKeyword(), mainhand ? ReloadSound.MAIN_HAND.getId() : ReloadSound.OFF_HAND.getId());
            Mechanics reloadStartMechanics = config.getObject(weaponTitle + ".Reload.Start_Mechanics", Mechanics.class);
            if (reloadStartMechanics != null)
                reloadStartMechanics.use(castData);
            weaponInfoDisplay.send(playerWrapper, slot);
            weaponHandler.getSkinHandler().tryUse(entityWrapper, weaponTitle, weaponStack, slot);
        }
    };
    if (getConfigurations().getBool(weaponTitle + ".Info.Show_Cooldown.Reload_Time") && entityWrapper.getEntity().getType() == EntityType.PLAYER) {
        CompatibilityAPI.getEntityCompatibility().setCooldown((Player) entityWrapper.getEntity(), weaponStack.getType(), reloadEvent.getReloadCompleteTime());
    }
    // OR ammo left is above 0 and revolver isn't used (when using revolver firearm actions should always occur)
    if (isReloadLoop || state == null || (ammoLeft > 0 && !isRevolver)) {
        reloadTask.startChain();
        return true;
    }
    ChainTask closeTask = getCloseTask(firearmCloseTime, firearmAction, weaponStack, handData, entityWrapper, weaponTitle, mainhand, slot);
    if (state == FirearmState.CLOSE) {
        closeTask.startChain();
        return true;
    }
    ChainTask openTask = getOpenTask(firearmOpenTime, firearmAction, weaponStack, handData, entityWrapper, weaponTitle, mainhand, slot);
    if (isPump) {
        firearmAction.changeState(weaponStack, FirearmState.OPEN);
        if (ammoPerReload != -1) {
            reloadTask.startChain();
        } else {
            reloadTask.setNextTask(openTask).setNextTask(closeTask);
            reloadTask.startChain();
        }
    } else {
        if (ammoPerReload != -1) {
            openTask.setNextTask(reloadTask);
            openTask.startChain();
        } else {
            openTask.setNextTask(reloadTask).setNextTask(closeTask);
            openTask.startChain();
        }
    }
    return true;
}
Also used : AmmoTypes(me.deecaad.weaponmechanics.weapon.reload.ammo.AmmoTypes) CastData(me.deecaad.weaponmechanics.mechanics.CastData) Configuration(me.deecaad.core.file.Configuration) PlayerWrapper(me.deecaad.weaponmechanics.wrappers.PlayerWrapper) FirearmAction(me.deecaad.weaponmechanics.weapon.firearm.FirearmAction) WeaponPreReloadEvent(me.deecaad.weaponmechanics.weapon.weaponevents.WeaponPreReloadEvent) LivingEntity(org.bukkit.entity.LivingEntity) Mechanics(me.deecaad.weaponmechanics.mechanics.Mechanics) WeaponMechanics(me.deecaad.weaponmechanics.WeaponMechanics) WeaponInfoDisplay(me.deecaad.weaponmechanics.weapon.info.WeaponInfoDisplay) ItemStack(org.bukkit.inventory.ItemStack) WeaponReloadEvent(me.deecaad.weaponmechanics.weapon.weaponevents.WeaponReloadEvent) HandData(me.deecaad.weaponmechanics.wrappers.HandData) FirearmState(me.deecaad.weaponmechanics.weapon.firearm.FirearmState)

Example 3 with AmmoTypes

use of me.deecaad.weaponmechanics.weapon.reload.ammo.AmmoTypes in project MechanicsMain by WeaponMechanics.

the class WeaponMechanicsAPI method getCurrentAmmoName.

/**
 * Returns the ammo name which is currently used in weapon. If the weapon
 * doesn't use ammo, this method will return <code>null</code>.
 *
 * @param weaponTitle The non-null weapon-title of the weapon.
 * @param weaponStack The non-null weapon item stack.
 * @return The current ammo name, or null.
 */
@Nullable
public static String getCurrentAmmoName(@Nonnull String weaponTitle, @Nonnull ItemStack weaponStack) {
    checkState();
    notNull(weaponTitle);
    notNull(weaponStack);
    AmmoTypes ammoTypes = plugin.configurations.getObject(weaponTitle + ".Reload.Ammo.Ammo_Types", AmmoTypes.class);
    if (ammoTypes == null)
        return null;
    return ammoTypes.getCurrentAmmoName(weaponStack);
}
Also used : AmmoTypes(me.deecaad.weaponmechanics.weapon.reload.ammo.AmmoTypes) Nullable(javax.annotation.Nullable)

Aggregations

AmmoTypes (me.deecaad.weaponmechanics.weapon.reload.ammo.AmmoTypes)3 Configuration (me.deecaad.core.file.Configuration)2 CastData (me.deecaad.weaponmechanics.mechanics.CastData)2 Mechanics (me.deecaad.weaponmechanics.mechanics.Mechanics)2 WeaponInfoDisplay (me.deecaad.weaponmechanics.weapon.info.WeaponInfoDisplay)2 PlayerWrapper (me.deecaad.weaponmechanics.wrappers.PlayerWrapper)2 Nullable (javax.annotation.Nullable)1 WeaponMechanics (me.deecaad.weaponmechanics.WeaponMechanics)1 FirearmAction (me.deecaad.weaponmechanics.weapon.firearm.FirearmAction)1 FirearmState (me.deecaad.weaponmechanics.weapon.firearm.FirearmState)1 Trigger (me.deecaad.weaponmechanics.weapon.trigger.Trigger)1 WeaponPreReloadEvent (me.deecaad.weaponmechanics.weapon.weaponevents.WeaponPreReloadEvent)1 WeaponReloadEvent (me.deecaad.weaponmechanics.weapon.weaponevents.WeaponReloadEvent)1 HandData (me.deecaad.weaponmechanics.wrappers.HandData)1 LivingEntity (org.bukkit.entity.LivingEntity)1 ItemStack (org.bukkit.inventory.ItemStack)1