Search in sources :

Example 1 with Trigger

use of me.deecaad.weaponmechanics.weapon.trigger.Trigger in project MechanicsMain by WeaponMechanics.

the class ReloadHandler method validate.

@Override
public void validate(Configuration configuration, File file, ConfigurationSection configurationSection, String path) {
    Trigger trigger = configuration.getObject(path + ".Trigger", Trigger.class);
    if (trigger == null) {
        debug.log(LogLevel.ERROR, "Tried to use shoot without defining trigger for it.", StringUtil.foundAt(file, path + ".Trigger"));
    }
    int magazineSize = configurationSection.getInt(path + ".Magazine_Size");
    debug.validate(magazineSize > 0, "Magazine_Size must be positive", SerializerException.forValue(magazineSize), StringUtil.foundAt(file, path + ".Magazine_Size"));
    int reloadDuration = configurationSection.getInt(path + ".Reload_Duration");
    debug.validate(reloadDuration > 0, "Reload_Duration must be positive", SerializerException.forValue(reloadDuration), StringUtil.foundAt(file, path + ".Reload_Duration"));
    int ammoPerReload = configurationSection.getInt(path + ".Ammo_Per_Reload", -99);
    if (ammoPerReload != -99) {
        debug.validate(ammoPerReload > 0, "Ammo_Per_Reload must be positive", SerializerException.forValue(ammoPerReload), StringUtil.foundAt(file, path + ".Ammo_Per_Reload"));
    }
    boolean unloadAmmoOnReload = configurationSection.getBoolean(path + ".Unload_Ammo_On_Reload");
    if (unloadAmmoOnReload && ammoPerReload != -99) {
        // Using ammo per reload and unload ammo on reload at same time is considered as error
        debug.error("Cannot use Ammo_Per_Reload and Unload_Ammo_On_Reload at the same time", StringUtil.foundAt(file, path + ".Unload_Ammo_On_Reload"));
    }
}
Also used : Trigger(me.deecaad.weaponmechanics.weapon.trigger.Trigger)

Example 2 with Trigger

use of me.deecaad.weaponmechanics.weapon.trigger.Trigger in project MechanicsMain by WeaponMechanics.

the class ScopeHandler method tryUse.

/**
 * Tries to use scope
 *
 * @param entityWrapper the entity who used trigger
 * @param weaponTitle the weapon title
 * @param weaponStack the weapon stack
 * @param slot the slot used on trigger
 * @param triggerType the trigger type trying to activate scope
 * @return true if the scope used successfully to zoom in, our or stack
 */
public boolean tryUse(EntityWrapper entityWrapper, String weaponTitle, ItemStack weaponStack, EquipmentSlot slot, TriggerType triggerType, boolean dualWield) {
    Configuration config = getConfigurations();
    // Don't try to scope if either one of the hands is reloading
    if (entityWrapper.getMainHandData().isReloading() || entityWrapper.getOffHandData().isReloading()) {
        return false;
    }
    ZoomData zoomData;
    // Only allow using zoom at one hand at time
    if (slot == EquipmentSlot.HAND) {
        if (entityWrapper.getOffHandData().getZoomData().isZooming()) {
            return false;
        }
        zoomData = entityWrapper.getMainHandData().getZoomData();
    } else {
        if (entityWrapper.getMainHandData().getZoomData().isZooming()) {
            return false;
        }
        zoomData = entityWrapper.getOffHandData().getZoomData();
    }
    Trigger trigger = config.getObject(weaponTitle + ".Scope.Trigger", Trigger.class);
    if (trigger == null)
        return false;
    // Check if entity is already zooming
    if (zoomData.isZooming()) {
        Trigger offTrigger = config.getObject(weaponTitle + ".Scope.Zoom_Off.Trigger", Trigger.class);
        // If off trigger is valid -> zoom out even if stacking hasn't reached maximum stacks
        if (offTrigger != null && offTrigger.check(triggerType, slot, entityWrapper)) {
            return zoomOut(weaponStack, weaponTitle, entityWrapper, zoomData, slot);
        }
        // If trigger is valid zoom in or out depending on situation
        if (trigger.check(triggerType, slot, entityWrapper)) {
            int maximumStacks = config.getInt(weaponTitle + ".Scope.Zoom_Stacking.Maximum_Stacks");
            if (maximumStacks <= 0) {
                // Should turn off
                return zoomOut(weaponStack, weaponTitle, entityWrapper, zoomData, slot);
            }
            if (zoomData.getZoomStacks() < maximumStacks) {
                // Zoom in handles stacking on its own
                return zoomIn(weaponStack, weaponTitle, entityWrapper, zoomData, slot);
            }
            // Should turn off (because zoom stacks have reached maximum stacks)
            return zoomOut(weaponStack, weaponTitle, entityWrapper, zoomData, slot);
        }
    } else if (trigger.check(triggerType, slot, entityWrapper)) {
        // Try zooming in since entity is not zooming
        return zoomIn(weaponStack, weaponTitle, entityWrapper, zoomData, slot);
    }
    return false;
}
Also used : Trigger(me.deecaad.weaponmechanics.weapon.trigger.Trigger) Configuration(me.deecaad.core.file.Configuration) ZoomData(me.deecaad.weaponmechanics.wrappers.ZoomData)

Example 3 with Trigger

use of me.deecaad.weaponmechanics.weapon.trigger.Trigger in project MechanicsMain by WeaponMechanics.

the class ShootHandler method tryUse.

/**
 * Tries to use shoot
 *
 * @param entityWrapper the entity who used trigger
 * @param weaponTitle   the weapon title
 * @param weaponStack   the weapon stack
 * @param slot          the slot used on trigger
 * @param triggerType   the trigger type trying to activate shoot
 * @param dualWield     whether this was dual wield
 * @return true if was able to shoot
 */
public boolean tryUse(EntityWrapper entityWrapper, String weaponTitle, ItemStack weaponStack, EquipmentSlot slot, TriggerType triggerType, boolean dualWield, @Nullable LivingEntity knownVictim) {
    if (triggerType == TriggerType.MELEE && slot == EquipmentSlot.HAND) {
        return weaponHandler.getMeleeHandler().tryUse(entityWrapper, weaponTitle, weaponStack, slot, triggerType, dualWield, knownVictim);
    }
    Trigger trigger = getConfigurations().getObject(weaponTitle + ".Shoot.Trigger", Trigger.class);
    if (trigger == null || !trigger.check(triggerType, slot, entityWrapper))
        return false;
    MCTiming shootHandlerTiming = WeaponMechanics.timing("Shoot Handler").startTiming();
    boolean result = shootWithoutTrigger(entityWrapper, weaponTitle, weaponStack, slot, triggerType, dualWield);
    shootHandlerTiming.stopTiming();
    return result;
}
Also used : Trigger(me.deecaad.weaponmechanics.weapon.trigger.Trigger) MCTiming(co.aikar.timings.lib.MCTiming)

Example 4 with Trigger

use of me.deecaad.weaponmechanics.weapon.trigger.Trigger 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 5 with Trigger

use of me.deecaad.weaponmechanics.weapon.trigger.Trigger in project MechanicsMain by WeaponMechanics.

the class ShootHandler method validate.

@Override
public void validate(Configuration configuration, File file, ConfigurationSection configurationSection, String path) {
    Trigger trigger = configuration.getObject(path + ".Trigger", Trigger.class);
    if (trigger == null) {
        debug.log(LogLevel.ERROR, "Tried to use shoot without defining trigger for it.", "Located at file " + file + " in " + path + ".Trigger in configurations.");
    }
    double projectileSpeed = configuration.getDouble(path + ".Projectile_Speed", 80);
    debug.validate(projectileSpeed > 0, "Projectile_Speed must be a positive number!", StringUtil.foundAt(file, path + ".Projectile_Speed"));
    // Convert from more config friendly speed to normal
    // E.g. 80 -> 4.0
    configuration.set(path + ".Projectile_Speed", projectileSpeed / 20);
    int delayBetweenShots = configuration.getInt(path + ".Delay_Between_Shots");
    if (delayBetweenShots != 0) {
        // Convert to millis
        configuration.set(path + ".Delay_Between_Shots", delayBetweenShots * 50);
    }
    int projectilesPerShot = configuration.getInt(path + ".Projectiles_Per_Shot");
    if (projectilesPerShot == 0) {
        configuration.set(path + ".Projectiles_Per_Shot", 1);
    } else if (projectilesPerShot < 1) {
        debug.log(LogLevel.ERROR, "Tried to use shoot where projectiles per shot was less than 1.", "Located at file " + file + " in " + path + ".Projectiles_Per_Shot in configurations.");
    }
    boolean hasBurst = false;
    boolean hasAuto = false;
    int shotsPerBurst = configuration.getInt(path + ".Burst.Shots_Per_Burst");
    int ticksBetweenEachShot = configuration.getInt(path + ".Burst.Ticks_Between_Each_Shot");
    if (shotsPerBurst != 0 || ticksBetweenEachShot != 0) {
        hasBurst = true;
        if (shotsPerBurst < 1) {
            debug.log(LogLevel.ERROR, "Tried to use shots per burst with value less than 1.", "Located at file " + file + " in " + path + ".Burst.Shots_Per_Burst in configurations.");
        }
        if (ticksBetweenEachShot < 1) {
            debug.log(LogLevel.ERROR, "Tried to use ticks between each shot with value less than 1.", "Located at file " + file + " in " + path + ".Burst.Ticks_Between_Each_Shot in configurations.");
        }
    }
    int fullyAutomaticShotsPerSecond = configuration.getInt(path + ".Fully_Automatic_Shots_Per_Second");
    if (fullyAutomaticShotsPerSecond != 0) {
        hasAuto = true;
        if (fullyAutomaticShotsPerSecond < 1) {
            debug.log(LogLevel.ERROR, "Tried to use full auto with value less than 1.", "Located at file " + file + " in " + path + ".Fully_Automatic_Shots_Per_Second in configurations.");
        }
    }
    boolean usesSelectiveFire = configuration.getObject(path + ".Selective_Fire.Trigger", Trigger.class) != null;
    if (usesSelectiveFire && !hasBurst && !hasAuto) {
        debug.log(LogLevel.ERROR, "Tried to use selective fire without defining full auto or burst.", "You need to define at least other of them.", "Located at file " + file + " in " + path + " in configurations.");
    }
    String defaultSelectiveFire = configuration.getString(path + ".Selective_Fire.Default");
    if (defaultSelectiveFire != null) {
        if (!defaultSelectiveFire.equalsIgnoreCase("SINGLE") && !defaultSelectiveFire.equalsIgnoreCase("BURST") && !defaultSelectiveFire.equalsIgnoreCase("AUTO")) {
            debug.log(LogLevel.ERROR, "Tried to use selective fire default with invalid type.", "You need to use one of the following: SINGLE, BURST or AUTO, now there was " + defaultSelectiveFire, "Located at file " + file + " in " + path + " in configurations.");
        }
    }
}
Also used : Trigger(me.deecaad.weaponmechanics.weapon.trigger.Trigger)

Aggregations

Trigger (me.deecaad.weaponmechanics.weapon.trigger.Trigger)6 Configuration (me.deecaad.core.file.Configuration)2 MCTiming (co.aikar.timings.lib.MCTiming)1 CastData (me.deecaad.weaponmechanics.mechanics.CastData)1 Mechanics (me.deecaad.weaponmechanics.mechanics.Mechanics)1 WeaponInfoDisplay (me.deecaad.weaponmechanics.weapon.info.WeaponInfoDisplay)1 AmmoTypes (me.deecaad.weaponmechanics.weapon.reload.ammo.AmmoTypes)1 PlayerWrapper (me.deecaad.weaponmechanics.wrappers.PlayerWrapper)1 ZoomData (me.deecaad.weaponmechanics.wrappers.ZoomData)1