Skip to content

Commit ffb703a

Browse files
authored
Merge pull request #703 from Lorenzo0111/copilot/suggest-use-q-to-unload-weapon
Add weapon unload feature using Q key with sneak modifier
2 parents 9c2bf15 + e1e9f76 commit ffb703a

File tree

3 files changed

+87
-7
lines changed

3 files changed

+87
-7
lines changed

src/main/java/me/zombie_striker/qg/QAMain.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public class QAMain extends JavaPlugin {
135135
public static boolean reloadOnQ = true;
136136
public static boolean reloadOnF = true;
137137
public static boolean reloadOnFOnly = true;
138+
public static boolean unloadOnQ = false;
138139
public static boolean disableHotBarMessageOnShoot = false;
139140
public static boolean disableHotBarMessageOnReload = false;
140141
public static boolean disableHotBarMessageOnOutOfAmmo = false;
@@ -864,6 +865,7 @@ public void reloadVals() {
864865
reloadOnQ = (boolean) a("enableReloadingOnDrop", false);
865866
reloadOnF = (boolean) a("enableReloadingWhenSwapToOffhand", true);
866867
reloadOnFOnly = (boolean) a("enableReloadOnlyWhenSwapToOffhand", false);
868+
unloadOnQ = (boolean) a("enableUnloadingOnDrop", false);
867869

868870
allowGunHitEntities = (boolean) a("allowGunHitEntities", true);
869871
preventHiddenPlayers = (boolean) a("preventHiddenPlayers", true);

src/main/java/me/zombie_striker/qg/listener/QAListener.java

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import me.zombie_striker.qg.guns.Gun;
1414
import me.zombie_striker.qg.guns.utils.GunRefillerRunnable;
1515
import me.zombie_striker.qg.guns.utils.GunUtil;
16+
import me.zombie_striker.qg.guns.utils.WeaponSounds;
1617
import me.zombie_striker.qg.handlers.BulletWoundHandler;
1718
import me.zombie_striker.qg.handlers.IronsightsHandler;
1819
import me.zombie_striker.qg.handlers.Update19OffhandChecker;
@@ -465,12 +466,35 @@ public void onPickup(PlayerPickupItemEvent e) {
465466

466467
@EventHandler(priority = EventPriority.LOW)
467468
public void onDropReload(PlayerDropItemEvent e) {
468-
if (QAMain.reloadOnQ && !QAMain.reloadOnFOnly) {
469-
Gun g = QualityArmory.getGun(e.getItemDrop().getItemStack());
470-
if (g != null) {
471-
e.setCancelled(true);
472-
reload(e.getPlayer(),g);
473-
}
469+
ItemStack droppedItem = e.getItemDrop().getItemStack();
470+
471+
Gun g = QualityArmory.getGun(droppedItem);
472+
if (g == null && QualityArmory.isIronSights(droppedItem))
473+
g = QualityArmory.getGun(e.getPlayer().getInventory().getItemInOffHand());
474+
475+
if (g == null) return;
476+
477+
Gun finalG = g;
478+
if (QAMain.unloadOnQ) {
479+
e.setCancelled(true);
480+
481+
ignoreClick.add(e.getPlayer().getUniqueId());
482+
483+
Bukkit.getScheduler().runTaskLater(QAMain.getInstance(), () -> {
484+
ignoreClick.remove(e.getPlayer().getUniqueId());
485+
486+
if (e.getPlayer().isSneaking()) unloadAll(e.getPlayer(), finalG);
487+
else unloadOne(e.getPlayer(), finalG);
488+
}, 2L);
489+
} else if (QAMain.reloadOnQ && !QAMain.reloadOnFOnly) {
490+
e.setCancelled(true);
491+
492+
ignoreClick.add(e.getPlayer().getUniqueId());
493+
494+
Bukkit.getScheduler().runTaskLater(QAMain.getInstance(), () -> {
495+
ignoreClick.remove(e.getPlayer().getUniqueId());
496+
reload(e.getPlayer(), finalG);
497+
}, 2L);
474498
}
475499
}
476500

@@ -480,6 +504,54 @@ public static void reload(Player player, Gun g) {
480504
}
481505
}
482506

507+
public static void unloadOne(Player player, Gun g) {
508+
int currentAmmo = Gun.getAmount(player);
509+
if (currentAmmo <= 0) {
510+
QAMain.DEBUG("No ammo to unload");
511+
return;
512+
}
513+
514+
QAMain.DEBUG("Unloading one bullet from " + g.getDisplayName());
515+
Gun.updateAmmo(g, player, currentAmmo - 1);
516+
517+
if (g.getAmmoType() != null) {
518+
QAMain.DEBUG("Returning one bullet to player inventory");
519+
QualityArmory.addAmmoToInventory(player, g.getAmmoType(), 1);
520+
}
521+
522+
playUnloadSound(player);
523+
}
524+
525+
public static void unloadAll(Player player, Gun g) {
526+
int currentAmmo = Gun.getAmount(player);
527+
if (currentAmmo <= 0) {
528+
QAMain.DEBUG("No ammo to unload");
529+
return;
530+
}
531+
532+
QAMain.DEBUG("Unloading all ammo from " + g.getDisplayName());
533+
Gun.updateAmmo(g, player, 0);
534+
535+
if (g.getAmmoType() != null) {
536+
QAMain.DEBUG("Returning all ammo to player inventory");
537+
QualityArmory.addAmmoToInventory(player, g.getAmmoType(), currentAmmo);
538+
}
539+
540+
playUnloadSound(player);
541+
}
542+
543+
private static void playUnloadSound(Player player) {
544+
try {
545+
player.getWorld().playSound(player.getLocation(), WeaponSounds.RELOAD_MAG_OUT.getSoundName(), 1, 0.8f);
546+
} catch (Error e2) {
547+
try {
548+
player.getWorld().playSound(player.getLocation(), Sound.valueOf("CLICK"), 1, 0.8f);
549+
} catch (Error | Exception e3) {
550+
player.getWorld().playSound(player.getLocation(), Sound.valueOf("BLOCK_LEVER_CLICK"), 1, 0.8f);
551+
}
552+
}
553+
}
554+
483555
@EventHandler
484556
public void onMove(PlayerMoveEvent e) {
485557
QAMain.recoilHelperMovedLocation.put(e.getPlayer().getUniqueId(), e.getTo());
@@ -678,6 +750,11 @@ public void onAnvilClick(final PlayerInteractEvent e) {
678750
@SuppressWarnings({"deprecation"})
679751
@EventHandler(priority = EventPriority.LOWEST)
680752
public void onClick(final PlayerInteractEvent e) {
753+
// Prevent shooting/interaction immediately after unload/reload
754+
if (ignoreClick.contains(e.getPlayer().getUniqueId())) {
755+
return;
756+
}
757+
681758
QAMain.DEBUG("InteractEvent Called. Custom item used = " + QualityArmory.isCustomItem(e.getPlayer().getItemInHand()));
682759
if (!CustomItemManager.isUsingCustomData()) {
683760
QAMain.DEBUG("Custom Data Check");

wiki/config/main.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,5 @@ Below you can find a list of all the options that you can configure and their ex
9898
* **weaponSwitchDelay**: `0` - The delay in seconds before a player can fire after switching weapon. A value of `0` means no delay.
9999
* **DefaultResourcepack**: Defines the resource pack URLs. Refer to [ResourcePack Configuration](resourcepack.md) for more information.
100100
* **restoreOffHand**: `false` - If `true`, restores the off-hand item after iron sights are unaimed.
101-
* **hitDistance**: `5` - The maximum distance (in blocks) at which a gun can register melee hits on entities when using the gun to hit directly.
101+
* **hitDistance**: `5` - The maximum distance (in blocks) at which a gun can register melee hits on entities when using the gun to hit directly.
102+
* **enableUnloadingOnDrop**: `false` - If `true`, allows players to unload their gun's magazine by pressing their drop key (default 'Q') while holding a gun.

0 commit comments

Comments
 (0)