Search in sources :

Example 1 with PlayerWrapper

use of me.deecaad.weaponmechanics.wrappers.PlayerWrapper in project MechanicsMain by WeaponMechanics.

the class WeaponMechanics method getPlayerWrapper.

/**
 * This method can't return null because new PlayerWrapper is created if not found.
 * Use mainly getEntityWrapper() instead of this unless you especially need something from PlayerWrapper.
 *
 * @param player the player wrapper to get
 * @return the player wrapper
 */
public static PlayerWrapper getPlayerWrapper(Player player) {
    EntityWrapper wrapper = plugin.entityWrappers.get(player);
    if (wrapper == null) {
        wrapper = new PlayerWrapper(player);
        plugin.entityWrappers.put(player, wrapper);
    }
    if (!(wrapper instanceof PlayerWrapper)) {
        // Exception is better in this case as we need to know where this mistake happened
        throw new IllegalArgumentException("Tried to get PlayerWrapper from player which didn't have PlayerWrapper (only EntityWrapper)...?");
    }
    return (PlayerWrapper) wrapper;
}
Also used : EntityWrapper(me.deecaad.weaponmechanics.wrappers.EntityWrapper) PlayerWrapper(me.deecaad.weaponmechanics.wrappers.PlayerWrapper)

Example 2 with PlayerWrapper

use of me.deecaad.weaponmechanics.wrappers.PlayerWrapper in project MechanicsMain by WeaponMechanics.

the class WeaponListeners method equip.

@EventHandler
public void equip(EntityEquipmentEvent e) {
    if (e.isArmor())
        return;
    LivingEntity entity = (LivingEntity) e.getEntity();
    EntityWrapper entityWrapper = WeaponMechanics.getEntityWrapper(entity);
    ItemStack weaponStack = e.getEquipped();
    // Also try auto converting to weapon
    String weaponTitle = weaponHandler.getInfoHandler().getWeaponTitle(weaponStack, true);
    boolean alreadyUsedEquipMechanics = false;
    boolean mainhand = e.getSlot() == EquipmentSlot.HAND;
    HandData handData = mainhand ? entityWrapper.getMainHandData() : entityWrapper.getOffHandData();
    handData.setCurrentWeaponTitle(weaponTitle);
    if (weaponTitle != null) {
        if (e.getEntityType() == EntityType.PLAYER) {
            WeaponInfoDisplay weaponInfoDisplay = getConfigurations().getObject(weaponTitle + ".Info.Weapon_Info_Display", WeaponInfoDisplay.class);
            if (weaponInfoDisplay != null)
                weaponInfoDisplay.send((PlayerWrapper) entityWrapper, e.getSlot());
        }
        weaponHandler.getSkinHandler().tryUse(entityWrapper, weaponTitle, weaponStack, e.getSlot());
        Mechanics equipMechanics = getConfigurations().getObject(weaponTitle + ".Info.Weapon_Equip_Mechanics", Mechanics.class);
        if (equipMechanics != null) {
            equipMechanics.use(new CastData(entityWrapper, weaponTitle, weaponStack));
            alreadyUsedEquipMechanics = true;
        }
        handData.setLastEquipTime(System.currentTimeMillis());
        if (getConfigurations().getBool(weaponTitle + ".Info.Show_Cooldown.Weapon_Equip_Delay") && e.getEntityType() == EntityType.PLAYER) {
            CompatibilityAPI.getEntityCompatibility().setCooldown((Player) entity, weaponStack.getType(), getConfigurations().getInt(weaponTitle + ".Info.Weapon_Equip_Delay") / 50);
        } else if (CompatibilityAPI.getEntityCompatibility().hasCooldown((Player) entity, weaponStack.getType())) {
            CompatibilityAPI.getEntityCompatibility().setCooldown((Player) entity, weaponStack.getType(), 0);
        }
        Bukkit.getPluginManager().callEvent(new WeaponEquipEvent(weaponTitle, weaponStack, entity, e.getSlot() == EquipmentSlot.HAND));
    }
    ItemStack dequipped = e.getDequipped();
    String dequippedWeapon = weaponHandler.getInfoHandler().getWeaponTitle(dequipped, false);
    if (dequippedWeapon != null) {
        // Don't use holster mechanics is equip mechanics were already used
        if (!alreadyUsedEquipMechanics) {
            Mechanics holsterMechanics = getConfigurations().getObject(dequippedWeapon + ".Info.Weapon_Holster_Mechanics", Mechanics.class);
            if (holsterMechanics != null)
                holsterMechanics.use(new CastData(entityWrapper, dequippedWeapon, dequipped));
        }
        if (weaponTitle == null && CompatibilityAPI.getEntityCompatibility().hasCooldown((Player) entity, dequipped.getType())) {
            CompatibilityAPI.getEntityCompatibility().setCooldown((Player) entity, dequipped.getType(), 0);
        }
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) CastData(me.deecaad.weaponmechanics.mechanics.CastData) Player(org.bukkit.entity.Player) WeaponEquipEvent(me.deecaad.weaponmechanics.weapon.weaponevents.WeaponEquipEvent) EntityWrapper(me.deecaad.weaponmechanics.wrappers.EntityWrapper) Mechanics(me.deecaad.weaponmechanics.mechanics.Mechanics) WeaponMechanics(me.deecaad.weaponmechanics.WeaponMechanics) WeaponInfoDisplay(me.deecaad.weaponmechanics.weapon.info.WeaponInfoDisplay) PlayerWrapper(me.deecaad.weaponmechanics.wrappers.PlayerWrapper) ItemStack(org.bukkit.inventory.ItemStack) HandData(me.deecaad.weaponmechanics.wrappers.HandData) EventHandler(org.bukkit.event.EventHandler)

Example 3 with PlayerWrapper

use of me.deecaad.weaponmechanics.wrappers.PlayerWrapper in project MechanicsMain by WeaponMechanics.

the class ReloadHandler method getCloseTask.

private ChainTask getCloseTask(int firearmCloseTime, FirearmAction firearmAction, ItemStack weaponStack, HandData handData, EntityWrapper entityWrapper, String weaponTitle, boolean mainhand, EquipmentSlot slot) {
    return new ChainTask(firearmCloseTime) {

        @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);
            }
            firearmAction.changeState(taskReference, FirearmState.READY);
            finishReload(entityWrapper, weaponTitle, taskReference, handData, slot);
        }

        @Override
        public void setup() {
            handData.addReloadTask(getTaskId());
            firearmAction.changeState(weaponStack, FirearmState.CLOSE);
            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());
            firearmAction.useMechanics(castData, false);
            if (entityWrapper instanceof PlayerWrapper) {
                WeaponInfoDisplay weaponInfoDisplay = getConfigurations().getObject(weaponTitle + ".Info.Weapon_Info_Display", WeaponInfoDisplay.class);
                if (weaponInfoDisplay != null)
                    weaponInfoDisplay.send((PlayerWrapper) entityWrapper, slot);
            }
            weaponHandler.getSkinHandler().tryUse(entityWrapper, weaponTitle, weaponStack, slot);
        }
    };
}
Also used : CastData(me.deecaad.weaponmechanics.mechanics.CastData) WeaponInfoDisplay(me.deecaad.weaponmechanics.weapon.info.WeaponInfoDisplay) PlayerWrapper(me.deecaad.weaponmechanics.wrappers.PlayerWrapper) ItemStack(org.bukkit.inventory.ItemStack)

Example 4 with PlayerWrapper

use of me.deecaad.weaponmechanics.wrappers.PlayerWrapper in project MechanicsMain by WeaponMechanics.

the class ReloadHandler method finishReload.

public void finishReload(EntityWrapper entityWrapper, String weaponTitle, ItemStack weaponStack, HandData handData, EquipmentSlot slot) {
    handData.finishReload();
    Mechanics reloadFinishMechanics = getConfigurations().getObject(weaponTitle + ".Reload.Finish_Mechanics", Mechanics.class);
    if (reloadFinishMechanics != null)
        reloadFinishMechanics.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, slot);
    }
    weaponHandler.getSkinHandler().tryUse(entityWrapper, weaponTitle, weaponStack, slot);
}
Also used : CastData(me.deecaad.weaponmechanics.mechanics.CastData) Mechanics(me.deecaad.weaponmechanics.mechanics.Mechanics) WeaponMechanics(me.deecaad.weaponmechanics.WeaponMechanics) WeaponInfoDisplay(me.deecaad.weaponmechanics.weapon.info.WeaponInfoDisplay) PlayerWrapper(me.deecaad.weaponmechanics.wrappers.PlayerWrapper)

Example 5 with PlayerWrapper

use of me.deecaad.weaponmechanics.wrappers.PlayerWrapper in project MechanicsMain by WeaponMechanics.

the class TriggerPlayerListeners method interact.

@EventHandler
public void interact(PlayerInteractEvent e) {
    Action action = e.getAction();
    Player player = e.getPlayer();
    EntityEquipment playerEquipment = player.getEquipment();
    // I don't think ignoreCancelled = true works in this event properly
    if (player.getGameMode() == GameMode.SPECTATOR || playerEquipment == null)
        return;
    if (action == Action.PHYSICAL || e.useItemInHand() == Event.Result.DENY)
        return;
    if (getBasicConfigurations().getBool("Disabled_Trigger_Checks.Right_And_Left_Click"))
        return;
    // Basically this just cancel double call to player interact event
    EquipmentSlot hand = e.getHand();
    // Only if main hand is air (off hand can be whatever)
    if (playerEquipment.getItemInMainHand().getType() == Material.AIR) {
        // -> Cancel
        if (action == Action.RIGHT_CLICK_BLOCK && hand == EquipmentSlot.HAND) {
            return;
        }
    // This basically means that hand is now OFF_HAND, but it doesn't let HAND calls pass
    } else if (hand == EquipmentSlot.OFF_HAND) {
        // If main hand had item, then we can always just cancel OFF_HAND call since HAND is guaranteed to be used
        return;
    }
    PlayerWrapper playerWrapper = getPlayerWrapper(player);
    boolean rightClick = action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK;
    if (rightClick) {
        getPlayerWrapper(player).rightClicked();
    } else if (CompatibilityAPI.getVersion() >= 1.15 && !NumberUtil.hasMillisPassed(playerWrapper.getLastDropWeaponTime(), 25)) {
        // Basically checks if less than 25 millis has passed since weapon item drop
        return;
    }
    ItemStack mainStack = playerEquipment.getItemInMainHand();
    String mainWeapon = weaponHandler.getInfoHandler().getWeaponTitle(mainStack, true);
    ItemStack offStack = playerEquipment.getItemInOffHand();
    String offWeapon = weaponHandler.getInfoHandler().getWeaponTitle(offStack, true);
    if (mainWeapon == null && offWeapon == null)
        return;
    if ((mainWeapon != null && getConfigurations().getBool(mainWeapon + ".Info.Cancel.Block_Interactions") || offWeapon != null && getConfigurations().getBool(offWeapon + ".Info.Cancel.Block_Interactions"))) {
        e.setUseInteractedBlock(Event.Result.DENY);
    }
    if ((mainWeapon != null && getConfigurations().getBool(mainWeapon + ".Info.Cancel.Item_Interactions") || offWeapon != null && getConfigurations().getBool(offWeapon + ".Info.Cancel.Item_Interactions"))) {
        e.setUseItemInHand(Event.Result.DENY);
    }
    boolean dualWield = mainWeapon != null && offWeapon != null;
    if (rightClick) {
        if (weaponHandler.getInfoHandler().denyDualWielding(TriggerType.RIGHT_CLICK, player, mainWeapon, offWeapon))
            return;
        if (mainWeapon != null)
            weaponHandler.tryUses(playerWrapper, mainWeapon, mainStack, EquipmentSlot.HAND, TriggerType.RIGHT_CLICK, dualWield, null);
        if (offWeapon != null)
            weaponHandler.tryUses(playerWrapper, offWeapon, offStack, EquipmentSlot.OFF_HAND, TriggerType.RIGHT_CLICK, dualWield, null);
        return;
    }
    if (weaponHandler.getInfoHandler().denyDualWielding(TriggerType.LEFT_CLICK, player, mainWeapon, offWeapon))
        return;
    if (mainWeapon != null) {
        weaponHandler.tryUses(playerWrapper, mainWeapon, mainStack, EquipmentSlot.HAND, TriggerType.LEFT_CLICK, dualWield, null);
        if (mainStack.getAmount() != 0) {
            // Try melee if main weapon isn't null
            weaponHandler.tryUses(playerWrapper, mainWeapon, mainStack, EquipmentSlot.HAND, TriggerType.MELEE, dualWield, null);
        }
    }
    if (offWeapon != null)
        weaponHandler.tryUses(playerWrapper, offWeapon, offStack, EquipmentSlot.OFF_HAND, TriggerType.LEFT_CLICK, dualWield, null);
}
Also used : Action(org.bukkit.event.block.Action) Player(org.bukkit.entity.Player) EntityEquipment(org.bukkit.inventory.EntityEquipment) EquipmentSlot(org.bukkit.inventory.EquipmentSlot) PlayerWrapper(me.deecaad.weaponmechanics.wrappers.PlayerWrapper) ItemStack(org.bukkit.inventory.ItemStack) EventHandler(org.bukkit.event.EventHandler)

Aggregations

PlayerWrapper (me.deecaad.weaponmechanics.wrappers.PlayerWrapper)19 CastData (me.deecaad.weaponmechanics.mechanics.CastData)11 WeaponInfoDisplay (me.deecaad.weaponmechanics.weapon.info.WeaponInfoDisplay)10 ItemStack (org.bukkit.inventory.ItemStack)9 Mechanics (me.deecaad.weaponmechanics.mechanics.Mechanics)7 LivingEntity (org.bukkit.entity.LivingEntity)7 Player (org.bukkit.entity.Player)7 WeaponMechanics (me.deecaad.weaponmechanics.WeaponMechanics)6 HandData (me.deecaad.weaponmechanics.wrappers.HandData)6 Configuration (me.deecaad.core.file.Configuration)5 EventHandler (org.bukkit.event.EventHandler)5 EntityEquipment (org.bukkit.inventory.EntityEquipment)4 FirearmAction (me.deecaad.weaponmechanics.weapon.firearm.FirearmAction)3 FirearmState (me.deecaad.weaponmechanics.weapon.firearm.FirearmState)3 EntityWrapper (me.deecaad.weaponmechanics.wrappers.EntityWrapper)3 ArrayList (java.util.ArrayList)2 WorldGuardCompatibility (me.deecaad.core.compatibility.worldguard.WorldGuardCompatibility)2 AmmoTypes (me.deecaad.weaponmechanics.weapon.reload.ammo.AmmoTypes)2 Recoil (me.deecaad.weaponmechanics.weapon.shoot.recoil.Recoil)2 Trigger (me.deecaad.weaponmechanics.weapon.trigger.Trigger)2