Skip to content

Commit

Permalink
Dual wield support
Browse files Browse the repository at this point in the history
  • Loading branch information
DeeCaaD committed Feb 25, 2022
1 parent 03257ac commit dcfc775
Show file tree
Hide file tree
Showing 51 changed files with 356 additions and 240 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import me.deecaad.core.utils.LogLevel;
import me.deecaad.core.utils.StringUtil;
import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -49,10 +50,11 @@ public static void addPlaceholderHandler(PlaceholderHandler placeholderHandler)
* @param player the player involved in event or null
* @param itemStack the item stack involved in event or null
* @param weaponTitle the weapon title involved in this request, can be null
* @param slot the weapon slot involved in this request, can be null
* @return the string with applied placeholders
*/
public static String applyPlaceholders(String to, @Nullable Player player, @Nullable ItemStack itemStack, @Nullable String weaponTitle) {
return applyPlaceholders(to, player, itemStack, weaponTitle, null);
public static String applyPlaceholders(String to, @Nullable Player player, @Nullable ItemStack itemStack, @Nullable String weaponTitle, @Nullable EquipmentSlot slot) {
return applyPlaceholders(to, player, itemStack, weaponTitle, slot, null);
}

/**
Expand All @@ -63,10 +65,11 @@ public static String applyPlaceholders(String to, @Nullable Player player, @Null
* @param player the player involved in event or null
* @param itemStack the item stack involved in event or null
* @param weaponTitle the weapon title involved in this request, can be null
* @param slot the weapon slot involved in this request, can be null
* @param temp the temporary placeholders to be used
* @return the string with applied placeholders
*/
public static String applyPlaceholders(String to, @Nullable Player player, @Nullable ItemStack itemStack, @Nullable String weaponTitle, @Nullable Map<String, String> temp) {
public static String applyPlaceholders(String to, @Nullable Player player, @Nullable ItemStack itemStack, @Nullable String weaponTitle, @Nullable EquipmentSlot slot, @Nullable Map<String, String> temp) {
if (to == null) {
return null;
}
Expand All @@ -90,9 +93,9 @@ public static String applyPlaceholders(String to, @Nullable Player player, @Null
}
String request = null;
try {
request = placeholderHandler.onRequest(player, itemStack, weaponTitle);
request = placeholderHandler.onRequest(player, itemStack, weaponTitle, slot);
} catch (Exception e) {
debug.log(LogLevel.WARN, "Placeholder using keyword %" + placeholderHandler.getPlaceholderName() + "% caused this exception!", e);
debug.log(LogLevel.WARN, "Placeholder using keyword " + placeholderHandler.getPlaceholderName() + " caused this exception!", e);
}
if (request == null) {
continue;
Expand All @@ -110,9 +113,9 @@ public static String applyPlaceholders(String to, @Nullable Player player, @Null
/**
* Creates new list based on given collection.
*
* @see PlaceholderAPI#applyPlaceholders(String, Player, ItemStack, String)
* @see PlaceholderAPI#applyPlaceholders(String, Player, ItemStack, String, EquipmentSlot)
*/
public static List<String> applyPlaceholders(Collection<String> to, @Nullable Player player, @Nullable ItemStack itemStack, @Nullable String weaponTitle) {
public static List<String> applyPlaceholders(Collection<String> to, @Nullable Player player, @Nullable ItemStack itemStack, @Nullable String weaponTitle, @Nullable EquipmentSlot slot) {
if (to == null)
return null;
else if (to.isEmpty())
Expand All @@ -122,7 +125,7 @@ else if (to.isEmpty())

List<String> tempList = new ArrayList<>();
while (iterator.hasNext()) {
tempList.add(applyPlaceholders(iterator.next(), player, itemStack, weaponTitle));
tempList.add(applyPlaceholders(iterator.next(), player, itemStack, weaponTitle, slot));
}
return tempList;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.deecaad.core.placeholder;

import org.bukkit.entity.Player;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -34,13 +35,15 @@ public String getPlaceholderName() {
}

/**
* Used to modify placeholder result when this specific placeholder is being requested. Remember to check nulls of player and itemstack!
* Used to modify placeholder result when this specific placeholder is being requested.
* Remember to check nulls!
*
* @param player the player involved in this request, can be null
* @param itemStack the item stack involved in this request, can be null
* @param weaponTitle the weapon title involved in this request, can be null
* @param slot the weapon slot involved in this request, can be null
* @return the result for placeholder or null
*/
@Nullable
public abstract String onRequest(@Nullable Player player, @Nullable ItemStack itemStack, @Nullable String weaponTitle);
public abstract String onRequest(@Nullable Player player, @Nullable ItemStack itemStack, @Nullable String weaponTitle, @Nullable EquipmentSlot slot);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import me.deecaad.core.file.TaskChain;
import me.deecaad.weaponmechanics.WeaponMechanics;
import me.deecaad.weaponmechanics.WeaponMechanicsAPI;
import me.deecaad.weaponmechanics.utils.CrackShotConvert.Converter;
import me.deecaad.weaponmechanics.lib.CrackShotConvert.Converter;
import org.bukkit.command.CommandSender;

import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.deecaad.weaponmechanics.utils.CrackShotConvert;
package me.deecaad.weaponmechanics.lib.CrackShotConvert;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.deecaad.weaponmechanics.utils.CrackShotConvert;
package me.deecaad.weaponmechanics.lib.CrackShotConvert;

import com.shampaggon.crackshot.MaterialManager;
import com.shampaggon.crackshot.SoundManager;
Expand All @@ -22,10 +22,8 @@ public void convertOneKey(YamlConfiguration configuration, String key, YamlConfi
if (configuration.get(key + ".Reload.Reload_Amount") != null) {
if (configuration.get(key + ".Firearm_Action.Type") != null) {
outputConfiguration.set(key + ".Info.Weapon_Info_Display.Action_Bar.Message", "&6" + key + "%firearm-state% &7«&6%ammo-left%&7»&6%reload%");
outputConfiguration.set(key + ".Info.Weapon_Info_Display.Action_Bar.Time", 40);
} else {
outputConfiguration.set(key + ".Info.Weapon_Info_Display.Action_Bar.Message", "&6" + key + " &7«&6%ammo-left%&7»&6%reload%");
outputConfiguration.set(key + ".Info.Weapon_Info_Display.Action_Bar.Time", 40);
}
}
for (Paths path : Paths.values()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.deecaad.weaponmechanics.utils.CrackShotConvert;
package me.deecaad.weaponmechanics.lib.CrackShotConvert;

import me.DeeCaaD.CrackShotPlus.CSPapi;
import me.deecaad.core.utils.NumberUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,16 @@ public void equip(EntityEquipmentEvent e) {
String weaponTitle = weaponHandler.getInfoHandler().getWeaponTitle(weaponStack, true);
boolean alreadyUsedEquipMechanics = false;

if (e.getSlot() == EquipmentSlot.HAND) {
entityWrapper.getMainHandData().setCurrentWeaponTitle(weaponTitle);
} else {
entityWrapper.getOffHandData().setCurrentWeaponTitle(weaponTitle);
}
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, weaponStack, e.getSlot());
if (weaponInfoDisplay != null) weaponInfoDisplay.send((PlayerWrapper) entityWrapper, e.getSlot());
}

weaponHandler.getSkinHandler().tryUse(entityWrapper, weaponTitle, weaponStack, e.getSlot());
Expand All @@ -68,7 +67,6 @@ public void equip(EntityEquipmentEvent e) {
alreadyUsedEquipMechanics = true;
}

HandData handData = e.getSlot() == EquipmentSlot.HAND ? entityWrapper.getMainHandData() : entityWrapper.getOffHandData();
handData.setLastEquipTime(System.currentTimeMillis());

if (getConfigurations().getBool(weaponTitle + ".Info.Show_Cooldown.Weapon_Equip_Delay") && e.getEntityType() == EntityType.PLAYER) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,13 @@ public void damage(EntityDamageByEntityEvent e) {
ItemStack offStack = entityEquipment.getItemInOffHand();
String offWeapon = weaponHandler.getInfoHandler().getWeaponTitle(offStack, false);

if (mainWeapon == null && offWeapon == null) {
entityWrapper.getMainHandData().setCurrentWeaponTitle(null);
entityWrapper.getOffHandData().setCurrentWeaponTitle(null);
return;
}
if (mainWeapon == null && offWeapon == null) return;

if (mainWeapon != null) {
// Cancel melee with weapons by default
e.setCancelled(true);
}

entityWrapper.getMainHandData().setCurrentWeaponTitle(mainWeapon);
entityWrapper.getOffHandData().setCurrentWeaponTitle(offWeapon);

// When sweep hit we don't want to do actual melee casts
if (cause == EntityDamageEvent.DamageCause.ENTITY_SWEEP_ATTACK) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

public class TriggerPlayerListeners implements Listener {

private WeaponHandler weaponHandler;
private final WeaponHandler weaponHandler;

public TriggerPlayerListeners(WeaponHandler weaponHandler) {
this.weaponHandler = weaponHandler;
Expand Down Expand Up @@ -140,11 +140,7 @@ public void interact(PlayerInteractEvent e) {
ItemStack offStack = playerEquipment.getItemInOffHand();
String offWeapon = weaponHandler.getInfoHandler().getWeaponTitle(offStack, true);

if (mainWeapon == null && offWeapon == null) {
playerWrapper.getMainHandData().setCurrentWeaponTitle(null);
playerWrapper.getOffHandData().setCurrentWeaponTitle(null);
return;
}
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"))) {
Expand All @@ -158,9 +154,6 @@ public void interact(PlayerInteractEvent e) {

boolean dualWield = mainWeapon != null && offWeapon != null;

playerWrapper.getMainHandData().setCurrentWeaponTitle(mainWeapon);
playerWrapper.getOffHandData().setCurrentWeaponTitle(offWeapon);

if (rightClick) {
if (weaponHandler.getInfoHandler().denyDualWielding(TriggerType.RIGHT_CLICK, player, mainWeapon, offWeapon)) return;

Expand Down Expand Up @@ -240,11 +233,7 @@ public void dropItem(PlayerDropItemEvent e) {
ItemStack offStack = playerEquipment.getItemInOffHand();
String offWeapon = weaponHandler.getInfoHandler().getWeaponTitle(offStack, false);

if (mainWeapon == null && offWeapon == null) {
playerWrapper.getMainHandData().setCurrentWeaponTitle(null);
playerWrapper.getOffHandData().setCurrentWeaponTitle(null);
return;
}
if (mainWeapon == null && offWeapon == null) return;

// Cancel reload (and other tasks) since drop item will most of the time cause
// itemstack reference change which will cause other bugs (e.g. infinite reload bug)
Expand All @@ -261,9 +250,6 @@ public void dropItem(PlayerDropItemEvent e) {

boolean dualWield = mainWeapon != null && offWeapon != null;

playerWrapper.getMainHandData().setCurrentWeaponTitle(mainWeapon);
playerWrapper.getOffHandData().setCurrentWeaponTitle(offWeapon);

if (mainWeapon != null) {
playerWrapper.droppedWeapon();

Expand All @@ -276,7 +262,8 @@ public void dropItem(PlayerDropItemEvent e) {

if (offWeapon != null) {
playerWrapper.droppedWeapon();
weaponHandler.tryUses(playerWrapper, offWeapon, offStack, EquipmentSlot.OFF_HAND, TriggerType.DROP_ITEM, dualWield, null);
Bukkit.getScheduler().runTask(WeaponMechanics.getPlugin(), () -> weaponHandler.tryUses(playerWrapper, offWeapon,
playerEquipment.getItemInOffHand(), EquipmentSlot.OFF_HAND, TriggerType.DROP_ITEM, dualWield, null));
}
}

Expand All @@ -299,11 +286,7 @@ public void swapHandItems(PlayerSwapHandItemsEvent e) {

ItemStack toOff = e.getOffHandItem();
String toOffWeapon = weaponHandler.getInfoHandler().getWeaponTitle(toOff, false);
if (toMainWeapon == null && toOffWeapon == null) {
playerWrapper.getMainHandData().setCurrentWeaponTitle(null);
playerWrapper.getOffHandData().setCurrentWeaponTitle(null);
return;
}
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")) {
Expand All @@ -316,22 +299,25 @@ public void swapHandItems(PlayerSwapHandItemsEvent e) {

boolean dualWield = toMainWeapon != null && toOffWeapon != null;

playerWrapper.getMainHandData().setCurrentWeaponTitle(toMainWeapon);
playerWrapper.getOffHandData().setCurrentWeaponTitle(toOffWeapon);

if (isValid(toMain)) {
// SWAP_TO_MAIN_HAND
if (weaponHandler.getInfoHandler().denyDualWielding(TriggerType.SWAP_TO_MAIN_HAND, player, toMainWeapon, toOffWeapon)) return;

// Only check off hand going to main hand
if (toMainWeapon != null) weaponHandler.tryUses(playerWrapper, toMainWeapon, toMain, EquipmentSlot.OFF_HAND, TriggerType.SWAP_TO_MAIN_HAND, dualWield, null);
if (toMainWeapon != null) {
Bukkit.getScheduler().runTask(WeaponMechanics.getPlugin(), () -> weaponHandler.tryUses(playerWrapper, toMainWeapon,
playerEquipment.getItemInMainHand(), EquipmentSlot.OFF_HAND, TriggerType.SWAP_TO_MAIN_HAND, dualWield, null));
}
}
if (isValid(toOff)) {
// SWAP_TO_OFF_HAND
if (weaponHandler.getInfoHandler().denyDualWielding(TriggerType.SWAP_TO_OFF_HAND, player, toMainWeapon, toOffWeapon)) return;

// Only check main hand going to off hand
if (toOffWeapon != null) weaponHandler.tryUses(playerWrapper, toOffWeapon, toOff, EquipmentSlot.HAND, TriggerType.SWAP_TO_OFF_HAND, dualWield, null);
if (toOffWeapon != null) {
Bukkit.getScheduler().runTask(WeaponMechanics.getPlugin(), () -> weaponHandler.tryUses(playerWrapper, toOffWeapon,
playerEquipment.getItemInOffHand(), EquipmentSlot.HAND, TriggerType.SWAP_TO_OFF_HAND, dualWield, null));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void use(CastData castData) {

Player player = castData.getCaster() instanceof Player ? (Player) castData.getCaster() : null;
for (CommandData commandData : commandList) {
String command = PlaceholderAPI.applyPlaceholders(commandData.getCommand(), player, castData.getWeaponStack(), castData.getWeaponTitle(), tempPlaceholders);
String command = PlaceholderAPI.applyPlaceholders(commandData.getCommand(), player, castData.getWeaponStack(), castData.getWeaponTitle(), null, tempPlaceholders);
if (commandData.isConsole()) {
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
} else if (player != null) {
Expand Down
Loading

0 comments on commit dcfc775

Please sign in to comment.