Files
src
build.gradle
settings.gradle
Code
7 379
src/main/java/com/kodari/rapidcrossbow/RapidCrossbowPlugin.java
package com.kodari.rapidcrossbow;
import com.kodari.rapidcrossbow.command.GiveCrossbowCommand;
import com.kodari.rapidcrossbow.listener.CrossbowListener;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.command.PluginCommand;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
public class RapidCrossbowPlugin extends JavaPlugin {
private NamespacedKey crossbowKey;
private NamespacedKey maxArrowsKey;
private NamespacedKey spreadKey;
private NamespacedKey reloadSecondsKey;
private NamespacedKey lastEmptyKey;
private NamespacedKey volleyArrowKey;
private NamespacedKey collectibleArrowKey;
@Override
public void onEnable() {
saveDefaultConfig();
crossbowKey = new NamespacedKey(this, "rapid_crossbow");
maxArrowsKey = new NamespacedKey(this, "max_arrows");
spreadKey = new NamespacedKey(this, "spread");
reloadSecondsKey = new NamespacedKey(this, "reload_seconds");
lastEmptyKey = new NamespacedKey(this, "last_empty");
volleyArrowKey = new NamespacedKey(this, "volley_arrow");
collectibleArrowKey = new NamespacedKey(this, "collectible_arrow");
PluginCommand giveCrossbowCommand = getCommand("givec");
if (giveCrossbowCommand != null) {
GiveCrossbowCommand giveCrossbowCommandExecutor = new GiveCrossbowCommand(this);
giveCrossbowCommand.setExecutor(giveCrossbowCommandExecutor);
giveCrossbowCommand.setTabCompleter(giveCrossbowCommandExecutor);
}
getServer().getPluginManager().registerEvents(new CrossbowListener(this), this);
}
public ItemStack createCrossbow(int maxArrows, double spread, double reloadSeconds) {
Material crossbowMaterial = Material.matchMaterial("CROSSBOW");
if (crossbowMaterial == null) {
return null;
}
ItemStack crossbow = new ItemStack(crossbowMaterial);
ItemMeta meta = crossbow.getItemMeta();
if (meta == null) {
return null;
}
PersistentDataContainer data = meta.getPersistentDataContainer();
data.set(crossbowKey, PersistentDataType.BYTE, (byte) 1);
data.set(maxArrowsKey, PersistentDataType.INTEGER, maxArrows);
data.set(spreadKey, PersistentDataType.DOUBLE, spread);
data.set(reloadSecondsKey, PersistentDataType.DOUBLE, reloadSeconds);
crossbow.setItemMeta(meta);
return crossbow;
}
public boolean isRapidCrossbow(ItemStack item) {
if (item == null || !item.hasItemMeta()) {
return false;
}
return item.getItemMeta().getPersistentDataContainer().has(crossbowKey, PersistentDataType.BYTE);
}
public int getMaxArrows(ItemStack item) {
Integer maxArrows = item.getItemMeta().getPersistentDataContainer().get(maxArrowsKey, PersistentDataType.INTEGER);
return maxArrows == null ? 1 : Math.max(1, maxArrows);
}
public double getSpread(ItemStack item) {
Double spread = item.getItemMeta().getPersistentDataContainer().get(spreadKey, PersistentDataType.DOUBLE);
return spread == null ? 0.0 : Math.max(0.0, spread);
}
public double getReloadSeconds(ItemStack item) {
Double reloadSeconds = item.getItemMeta().getPersistentDataContainer().get(reloadSecondsKey, PersistentDataType.DOUBLE);
return reloadSeconds == null ? 1.0 : Math.max(0.1, reloadSeconds);
}
public long getLastEmptyTime(ItemStack item) {
Long lastEmptyTime = item.getItemMeta().getPersistentDataContainer().get(lastEmptyKey, PersistentDataType.LONG);
return lastEmptyTime == null ? -1L : lastEmptyTime;
}
public void markMagazineEmpty(ItemStack item, long time) {
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return;
}
meta.getPersistentDataContainer().set(lastEmptyKey, PersistentDataType.LONG, time);
item.setItemMeta(meta);
}
public int getConfiguredMaxArrows() {
return Math.max(1, getConfig().getInt("max-arrows-limit", 200));
}
public double getConfiguredMaxReloadSeconds() {
return Math.max(0.1, getConfig().getDouble("max-reload-debuff-seconds", 60.0));
}
public double getConfiguredMaxSpread() {
return Math.max(0.0, getConfig().getDouble("max-spread-degrees", 90.0));
}
public double getArrowSpeed() {
return Math.max(0.1, getConfig().getDouble("arrow-speed", 3.0));
}
public double getSpawnOffset() {
return Math.max(0.0, getConfig().getDouble("spawn-offset", 0.5));
}
public NamespacedKey getVolleyArrowKey() {
return volleyArrowKey;
}
public NamespacedKey getCollectibleArrowKey() {
return collectibleArrowKey;
}
}