Skip to content

Commit

Permalink
New shit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jazzkuh committed Aug 24, 2024
1 parent 4fa5d7a commit dda5f3f
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.jazzkuh.gunshell.api.objects;

import com.jazzkuh.gunshell.GunshellPlugin;
import com.jazzkuh.gunshell.api.interfaces.GunshellWeaponImpl;
import com.jazzkuh.gunshell.common.configuration.DefaultConfig;
import com.jazzkuh.gunshell.common.configuration.PlaceHolder;
import com.jazzkuh.gunshell.common.configuration.lang.MessagesConfig;
import com.jazzkuh.gunshell.utils.ChatUtils;
import com.jazzkuh.gunshell.utils.ItemBuilder;
import com.jazzkuh.gunshell.utils.PluginUtils;
import com.jazzkuh.gunshell.utils.NBTEditor;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
Expand All @@ -18,6 +23,12 @@
import java.util.List;

public class GunshellFireable implements GunshellWeaponImpl {
private final String GUN_AMMO_KEY = "gunshell_weapon_ammo";
private final String GUN_AMMOTYPE_KEY = "gunshell_weapon_ammotype";

private final String AMMUNITION_KEY = "gunshell_ammunition_key";
private final String AMMUNITION_AMMO_KEY = "gunshell_ammunition_ammo";

private final @NotNull @Getter String key;
private final @NotNull @Getter ConfigurationSection configuration;

Expand Down Expand Up @@ -121,4 +132,85 @@ public void updateItemMeta(ItemStack itemStack, int ammo) {
public ItemStack getItemStack(int durability) {
return getItem(durability).toItemStack();
}

public void reload(Player player, ItemStack itemStack, int durability) {
ItemStack ammoItem = PluginUtils.getInstance().getItemWithNBTTags(player, AMMUNITION_KEY, ammunitionKeys).get();
int ammoAmount = NBTEditor.getInt(ammoItem, AMMUNITION_AMMO_KEY);
GunshellPlugin.getInstance().getReloadingSet().add(player.getUniqueId());

for (Player target : player.getLocation().getWorld().getPlayers()) {
if (target.getLocation().distance(player.getLocation()) <= this.getSoundRange()) {
target.playSound(player.getLocation(), this.getReloadSound(), this.getSoundVolume(), 1F);
}
}

MessagesConfig.RELOADING_START.get(player,
new PlaceHolder("Durability", String.valueOf(durability)),
new PlaceHolder("Ammo", String.valueOf(ammoAmount > this.getMaxAmmo() ? this.getMaxAmmo() : ammoAmount)),
new PlaceHolder("MaxAmmo", String.valueOf(this.getMaxAmmo())));

int neededAmmo = this.getMaxAmmo();
int maxAmmoOnItem = NBTEditor.getInt(ammoItem, AMMUNITION_AMMO_KEY);

if (neededAmmo > maxAmmoOnItem && DefaultConfig.SMART_AMMO_LOADING.asBoolean()) {
ammoAmount = takeAmmoSmart(player, this, ammunitionKeys);
} else {
takeAmmoSingle(player, ammoItem);
}

int finalAmmoAmount = ammoAmount > this.getMaxAmmo() ? this.getMaxAmmo() : ammoAmount;
Bukkit.getScheduler().runTaskLater(GunshellPlugin.getInstance(), () -> {
PluginUtils.getInstance().applyNBTTag(itemStack, GUN_AMMO_KEY, finalAmmoAmount);
PluginUtils.getInstance().applyNBTTag(itemStack, GUN_AMMOTYPE_KEY, NBTEditor.getString(ammoItem, AMMUNITION_KEY));
this.updateItemMeta(itemStack, finalAmmoAmount);

MessagesConfig.SHOW_AMMO_DURABILITY.get(player,
new PlaceHolder("Durability", String.valueOf(durability)),
new PlaceHolder("Ammo", String.valueOf(finalAmmoAmount)),
new PlaceHolder("MaxAmmo", String.valueOf(this.getMaxAmmo())));

GunshellPlugin.getInstance().getReloadingSet().remove(player.getUniqueId());
MessagesConfig.RELOADING_FINISHED.get(player,
new PlaceHolder("Durability", String.valueOf(durability)),
new PlaceHolder("Ammo", String.valueOf(finalAmmoAmount)),
new PlaceHolder("MaxAmmo", String.valueOf(this.getMaxAmmo())));
}, this.getReloadTime());
}

private int takeAmmoSmart(Player player, GunshellFireable fireable, List<String> ammunitionKeys) {
List<ItemStack> ammoItems = PluginUtils.getInstance().getItemsWithNBTTags(player, AMMUNITION_KEY, ammunitionKeys).get();
int neededAmmo = fireable.getMaxAmmo();

int gatheredAmmo = 0;
for (ItemStack ammoItem : ammoItems) {
int ammoInClip = NBTEditor.getInt(ammoItem, AMMUNITION_AMMO_KEY);
while (gatheredAmmo < neededAmmo) {
takeAmmoSingle(player, ammoItem);
gatheredAmmo += ammoInClip;

if (gatheredAmmo >= neededAmmo) {
return gatheredAmmo;
}
}
}

return gatheredAmmo;
}

private void takeAmmoSingle(Player player, ItemStack ammoItem) {
if (player.getInventory().getItemInOffHand().equals(ammoItem)) {
ItemStack offHand = player.getInventory().getItemInOffHand();
if (offHand.getAmount() > 1) {
offHand.setAmount(offHand.getAmount() - 1);
} else {
player.getInventory().setItemInOffHand(new ItemStack(Material.AIR));
}
} else {
if (ammoItem.getAmount() > 1) {
ammoItem.setAmount(ammoItem.getAmount() - 1);
} else {
player.getInventory().removeItem(ammoItem);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void onGetWeapon(CommandInvocation commandInvocation) {
}
}

@Subcommand(name = "getammo", usage = "<ammoType> [player]", permission = true,
@Subcommand(name = "getammo", usage = "<ammoType> [player|ammo]", permission = true,
aliases = "ammo", description = "Get ammo from the config files.")
public void onGetAmmo(CommandInvocation commandInvocation) {
String[] args = commandInvocation.getArguments();
Expand All @@ -152,7 +152,11 @@ public void onGetAmmo(CommandInvocation commandInvocation) {

if (args.length > 2) {
Player target = Bukkit.getPlayer(args[2]);
if (target == null) {
if (target == null && PluginUtils.getInstance().isValidInteger(args[2])) {
((Player) sender).getInventory().addItem(ammunition.getItem(Integer.parseInt(args[2])).toItemStack());
MessagesConfig.SUCCESSFULLY_ADDED_AMMO_TO_INVENTORY.get(sender);
return;
} else if (target == null) {
MessagesConfig.ERROR_PLAYER_NOT_FOUND.get(sender);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public enum DefaultConfig {
HITBOX_INCREASE("hitbox-increase", 0.2),
PROTECTION_DAMAGE_REDUCTION_ENABLED("protection-damage-reduction.enabled", true),
PROTECTION_DAMAGE_REDUCTION_AMOUNT("protection-damage-reduction.amount", 5),
RAYTRACE_PARTICLE_ENABLED("raytrace-particle.enabled", false);
RAYTRACE_PARTICLE_ENABLED("raytrace-particle.enabled", false),
SMART_AMMO_LOADING("smart-ammo-loading", false),
PARTICLE_TRAIL("particle-trail", "REDSTONE");

private final @Getter String path;
private final @Getter Object value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ public class FireablePreFireListener implements Listener {
private final String DURABILITY_KEY = "gunshell_weapon_durability";

private final String AMMUNITION_KEY = "gunshell_ammunition_key";
private final String AMMUNITION_AMMO_KEY = "gunshell_ammunition_ammo";

@EventHandler
public void onFireablePreFire(FireablePreFireEvent event) {
if (event.isCancelled()) return;

Player player = event.getPlayer();
GunshellFireable fireable = event.getFireable();
ItemStack itemStack = player.getInventory().getItemInMainHand();
Expand All @@ -69,53 +70,7 @@ public void onFireablePreFire(FireablePreFireEvent event) {
}

if (ammo <= 0 && PluginUtils.getInstance().getItemWithNBTTags(player, AMMUNITION_KEY, ammunitionKeys).isPresent()) {
ItemStack ammoItem = PluginUtils.getInstance().getItemWithNBTTags(player, AMMUNITION_KEY, ammunitionKeys).get();
int ammoAmount = NBTEditor.getInt(ammoItem, AMMUNITION_AMMO_KEY);
GunshellPlugin.getInstance().getReloadingSet().add(player.getUniqueId());

for (Player target : player.getLocation().getWorld().getPlayers()) {
if (target.getLocation().distance(player.getLocation()) <= fireable.getSoundRange()) {
target.playSound(player.getLocation(), fireable.getReloadSound(), fireable.getSoundVolume(), 1F);
}
}

MessagesConfig.RELOADING_START.get(player,
new PlaceHolder("Durability", String.valueOf(durability)),
new PlaceHolder("Ammo", String.valueOf(ammoAmount > fireable.getMaxAmmo() ? fireable.getMaxAmmo() : ammoAmount)),
new PlaceHolder("MaxAmmo", String.valueOf(fireable.getMaxAmmo())));

if (player.getInventory().getItemInOffHand().equals(ammoItem)) {
ItemStack offHand = player.getInventory().getItemInOffHand();
if (offHand.getAmount() > 1) {
offHand.setAmount(offHand.getAmount() - 1);
} else {
player.getInventory().setItemInOffHand(new ItemStack(Material.AIR));
}
} else {
if (ammoItem.getAmount() > 1) {
ammoItem.setAmount(ammoItem.getAmount() - 1);
} else {
player.getInventory().removeItem(ammoItem);
}
}

Bukkit.getScheduler().runTaskLater(GunshellPlugin.getInstance(), () -> {
int finalAmmoAmount = ammoAmount > fireable.getMaxAmmo() ? fireable.getMaxAmmo() : ammoAmount;
PluginUtils.getInstance().applyNBTTag(itemStack, GUN_AMMO_KEY, finalAmmoAmount);
PluginUtils.getInstance().applyNBTTag(itemStack, GUN_AMMOTYPE_KEY, NBTEditor.getString(ammoItem, AMMUNITION_KEY));
fireable.updateItemMeta(itemStack, finalAmmoAmount);

MessagesConfig.SHOW_AMMO_DURABILITY.get(player,
new PlaceHolder("Durability", String.valueOf(durability)),
new PlaceHolder("Ammo", String.valueOf(finalAmmoAmount)),
new PlaceHolder("MaxAmmo", String.valueOf(fireable.getMaxAmmo())));

GunshellPlugin.getInstance().getReloadingSet().remove(player.getUniqueId());
MessagesConfig.RELOADING_FINISHED.get(player,
new PlaceHolder("Durability", String.valueOf(durability)),
new PlaceHolder("Ammo", String.valueOf(finalAmmoAmount)),
new PlaceHolder("MaxAmmo", String.valueOf(fireable.getMaxAmmo())));
}, fireable.getReloadTime());
fireable.reload(player, itemStack, durability);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,53 +55,7 @@ public void onPlayerSwapHand(PlayerSwapHandItemsEvent event) {
}

if (ammo <= 0 && PluginUtils.getInstance().getItemWithNBTTags(player, AMMUNITION_KEY, ammunitionKeys).isPresent()) {
ItemStack ammoItem = PluginUtils.getInstance().getItemWithNBTTags(player, AMMUNITION_KEY, ammunitionKeys).get();
int ammoAmount = NBTEditor.getInt(ammoItem, AMMUNITION_AMMO_KEY);
GunshellPlugin.getInstance().getReloadingSet().add(player.getUniqueId());

for (Player target : player.getLocation().getWorld().getPlayers()) {
if (target.getLocation().distance(player.getLocation()) <= fireable.getSoundRange()) {
target.playSound(player.getLocation(), fireable.getReloadSound(), fireable.getSoundVolume(), 1F);
}
}

MessagesConfig.RELOADING_START.get(player,
new PlaceHolder("Durability", String.valueOf(durability)),
new PlaceHolder("Ammo", String.valueOf(ammoAmount > fireable.getMaxAmmo() ? fireable.getMaxAmmo() : ammoAmount)),
new PlaceHolder("MaxAmmo", String.valueOf(fireable.getMaxAmmo())));

if (player.getInventory().getItemInOffHand().equals(ammoItem)) {
ItemStack offHand = player.getInventory().getItemInOffHand();
if (offHand.getAmount() > 1) {
offHand.setAmount(offHand.getAmount() - 1);
} else {
player.getInventory().setItemInOffHand(new ItemStack(Material.AIR));
}
} else {
if (ammoItem.getAmount() > 1) {
ammoItem.setAmount(ammoItem.getAmount() - 1);
} else {
player.getInventory().removeItem(ammoItem);
}
}

Bukkit.getScheduler().runTaskLater(GunshellPlugin.getInstance(), () -> {
int finalAmmoAmount = ammoAmount > fireable.getMaxAmmo() ? fireable.getMaxAmmo() : ammoAmount;
PluginUtils.getInstance().applyNBTTag(itemStack, GUN_AMMO_KEY, finalAmmoAmount);
PluginUtils.getInstance().applyNBTTag(itemStack, GUN_AMMOTYPE_KEY, NBTEditor.getString(ammoItem, AMMUNITION_KEY));
fireable.updateItemMeta(itemStack, finalAmmoAmount);

MessagesConfig.SHOW_AMMO_DURABILITY.get(player,
new PlaceHolder("Durability", String.valueOf(durability)),
new PlaceHolder("Ammo", String.valueOf(finalAmmoAmount)),
new PlaceHolder("MaxAmmo", String.valueOf(fireable.getMaxAmmo())));

GunshellPlugin.getInstance().getReloadingSet().remove(player.getUniqueId());
MessagesConfig.RELOADING_FINISHED.get(player,
new PlaceHolder("Durability", String.valueOf(durability)),
new PlaceHolder("Ammo", String.valueOf(finalAmmoAmount)),
new PlaceHolder("MaxAmmo", String.valueOf(fireable.getMaxAmmo())));;
}, fireable.getReloadTime());
fireable.reload(player, itemStack, durability);
} else {
GunshellAmmunition newAmmunition = GunshellPlugin.getInstance().getWeaponRegistry().getAmmunition()
.get(NBTEditor.getString(itemStack, GUN_AMMOTYPE_KEY));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package com.jazzkuh.gunshell.compatibility;

import com.cryptomorin.xseries.particles.XParticle;
import com.jazzkuh.gunshell.GunshellPlugin;
import com.jazzkuh.gunshell.api.objects.GunshellRayTraceResult;
import com.jazzkuh.gunshell.common.configuration.DefaultConfig;
import com.jazzkuh.gunshell.utils.PluginUtils;
import de.slikey.effectlib.effect.ParticleEffect;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.block.Block;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;

import java.util.Optional;

public interface CompatibilityLayer {
GunshellRayTraceResult performRayTrace(LivingEntity player, double range);
String getRayTraceResult(Player player, int range);
Expand All @@ -31,13 +34,16 @@ default void spawnParticleLine(Vector vector, LivingEntity player, int range) {
hitLocation = vector.toLocation(player.getWorld());
}

Optional<XParticle> particle = XParticle.of(DefaultConfig.PARTICLE_TRAIL.asString());
if (particle.isEmpty()) return;

Location playerLocation = PluginUtils.getInstance().getRightHandLocation((Player) player);

Vector directionVector = hitLocation.toVector().subtract(playerLocation.toVector()).normalize();
for (double i = 0; i < playerLocation.distance(hitLocation); i += 0.2) {
Location particleLocation = playerLocation.clone().add(directionVector.clone().multiply(i));
ParticleEffect particleEffect = new ParticleEffect(GunshellPlugin.getInstance().getEffectManager());
particleEffect.particle = Particle.REDSTONE;
particleEffect.particle = particle.get().get();
particleEffect.color = Color.WHITE;
particleEffect.particleSize = 1;
particleEffect.particleCount = 1;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/jazzkuh/gunshell/utils/PluginUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.util.Vector;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -62,6 +63,18 @@ public Optional<ItemStack> getItemWithNBTTags(Player player, String tag, List<St
return Optional.empty();
}

public Optional<List<ItemStack>> getItemsWithNBTTags(Player player, String tag, List<String> values) {
List<ItemStack> items = new ArrayList<>();
for (ItemStack item : player.getInventory().getContents()) {
if (item == null || !NBTEditor.contains(item, tag)) continue;
if (values.stream().anyMatch(value -> NBTEditor.getString(item, tag).equals(value))) {
items.add(item);
}
}

return items.isEmpty() ? Optional.empty() : Optional.of(items);
}

public void applyNBTTag(ItemStack itemStack, String key, Object value) {
ItemStack newItemStack = NBTEditor.set(itemStack, value, key);
ItemMeta itemMeta = newItemStack.getItemMeta();
Expand Down

0 comments on commit dda5f3f

Please sign in to comment.