This commit is contained in:
Marco-Byte-1 2021-03-06 11:42:04 +05:30
parent ee971706d3
commit 1bc6d53739
12 changed files with 187 additions and 142 deletions

View File

@ -3,7 +3,6 @@ package io.github.simplexdev.strike;
import io.github.simplexdev.strike.api.ConfigUser;
import java.util.Arrays;
import io.github.simplexdev.strike.api.Spawn;
import io.github.simplexdev.strike.api.utils.InventoryEditConfigManager;
import io.github.simplexdev.strike.listeners.InventoryEditGUI;
import io.github.simplexdev.strike.listeners.SpawnController;
@ -44,7 +43,7 @@ public class StrikeCommand implements CommandExecutor {
else if ("get".equalsIgnoreCase(args[0]) && sender instanceof Player) {
Player player = (Player) sender;
player.getInventory().setContents(new InventoryEditConfigManager(plugin).getInventory(player));
player.getInventory().setContents(new InventoryEditConfigManager(plugin).getInventoryItems(player));
}
else if ("set-spawn".equalsIgnoreCase(args[0]) && sender instanceof Player) {

View File

@ -7,8 +7,6 @@ import org.bukkit.plugin.java.JavaPlugin;
public final class StrikePlugin extends JavaPlugin {
//TODO
// Fall Damage Disable
// Bullet through Grass, Glass, Fence, Gates, Bars
// NPC Edit
// LaunchPad

View File

@ -5,9 +5,6 @@ import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
public class Spawn {
private static World world = null;
private static Location spawn = null;
@ -24,22 +21,17 @@ public class Spawn {
config.set("spawn.coords.x", spawn.getX());
config.set("spawn.coords.y", spawn.getY());
config.set("spawn.coords.z", spawn.getZ());
try {
config.save(new File(config.getCurrentPath()));
} catch (IOException e) {
e.printStackTrace();
}
plugin.saveConfig();
Spawn.spawn = spawn;
}
public static void setWorld(World world, JavaPlugin plugin) {
plugin.getConfig().set("spawn.world", world.getName());
try {
plugin.getConfig().save(new File(plugin.getConfig().getCurrentPath()));
} catch (IOException e) {
e.printStackTrace();
}
plugin.saveConfig();
Spawn.world = world;
}

View File

@ -3,43 +3,41 @@ package io.github.simplexdev.strike.api.utils;
import io.github.simplexdev.strike.listeners.Grenade;
import io.github.simplexdev.strike.listeners.Gun;
import io.github.simplexdev.strike.listeners.HealthPackage;
import io.github.simplexdev.strike.listeners.ItemManager;
import org.bukkit.Material;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
public class InventoryEditConfigManager {
private final JavaPlugin plugin;
private final FileConfiguration dataConfig;
private final File dataFile;
private FileConfiguration dataConfig;
public InventoryEditConfigManager(JavaPlugin plugin) {
this.plugin = plugin;
dataFile = new File("plugins\\Strike\\inventories.yml");
if (!dataFile.isFile()) {
try {
dataFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
dataConfig = new YamlConfiguration();
try {
if (!dataFile.isFile()) {
dataFile.createNewFile();
}
dataConfig = new YamlConfiguration();
dataConfig.load(dataFile);
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidConfigurationException e) {
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
}
@ -57,17 +55,23 @@ public class InventoryEditConfigManager {
}
public ItemStack[] getInventory(Player player) {
public ItemStack[] getInventoryItems(Player player) {
ItemStack[] items = new ItemStack[9];
ItemStack[] defaultItems = new ItemStack[9];
ItemMeta meta;
defaultItems[0] = new ItemStack(Material.WOODEN_SWORD);
meta = defaultItems[0].getItemMeta();
meta.setUnbreakable(true);
defaultItems[0].setItemMeta(meta);
defaultItems[1] = new Gun(plugin).createItem();
defaultItems[2] = new Grenade(plugin).createItem();
defaultItems[8] = new HealthPackage(plugin).createItem();
String uuid = player.getUniqueId().toString();
if (dataConfig.get(uuid) == null) {
ItemStack[] defaultItems = new ItemStack[9];
defaultItems[0] = new ItemStack(Material.WOODEN_SWORD);
defaultItems[1] = new Gun(plugin).createItem();
defaultItems[2] = new Grenade(plugin).createItem();
defaultItems[8] = new HealthPackage(plugin).createItem();
try {
setInventory(player, defaultItems);
@ -83,13 +87,23 @@ public class InventoryEditConfigManager {
for (String string : setString) {
if (string.startsWith(uuid) && !string.equalsIgnoreCase(uuid)) {
Integer integer = Integer.parseInt(string.replace(uuid + ".", "").trim());
int integer = Integer.parseInt(string.replace(uuid + ".", "").trim());
items[integer] = dataConfig.getItemStack(uuid + "." + integer);
}
}
if (!Arrays.asList(defaultItems).containsAll(Arrays.asList(items))) {
try {
setInventory(player, defaultItems);
} catch (IOException e) {
e.printStackTrace();
}
return defaultItems;
}
return items;
@ -97,15 +111,13 @@ public class InventoryEditConfigManager {
public int getItemSlot(ItemStack itemStack, Player player) {
ItemStack[] itemStacks = getInventory(player);
ItemStack[] itemStacks = getInventoryItems(player);
for (int i = 0; i < itemStacks.length; i++)
if (itemStacks[i].isSimilar(itemStack))
if (itemStacks[i] != null && itemStacks[i].isSimilar(itemStack))
return i;
return 0;
}
;
}

View File

@ -39,12 +39,10 @@ public class Grenade implements ConfigUser {
ItemStack stack = new ItemStack(Material.MAGMA_CREAM, 1);
ItemMeta meta = this.plugin.getServer().getItemFactory().getItemMeta(Material.MAGMA_CREAM);
meta.setDisplayName(ChatColor.RED + "Grenade");
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("grenade.name")));
stack.setItemMeta(meta);
this.plugin.getServer().getOnlinePlayers().forEach(player -> player.getInventory().addItem(new ItemStack[]{stack}));
return stack.clone();
}
@ -73,12 +71,12 @@ public class Grenade implements ConfigUser {
public void run() {
if (item != null) {
item.getLocation().createExplosion(4.0F, false, false);
Collection<LivingEntity> entities = item.getLocation().getNearbyLivingEntities(4.0D);
Collection<LivingEntity> entities = item.getLocation().getNearbyLivingEntities(10.0D);
List<Player> players = new ArrayList<>();
entities.forEach(livingEntity -> {
if (livingEntity instanceof Player) {
if (livingEntity instanceof Player && !player.equals(livingEntity)) {
players.add((Player) livingEntity);
}
});
@ -96,10 +94,11 @@ public class Grenade implements ConfigUser {
@EventHandler
private void onPlayerDamage(EntityDamageEvent e) {
if (!e.getEntity().getWorld().equals(Spawn.getWorld()) || !(e.getEntity() instanceof Player))
if (e.getCause() != EntityDamageEvent.DamageCause.BLOCK_EXPLOSION || !e.getEntity().getWorld().equals(Spawn.getWorld()) || !(e.getEntity() instanceof Player))
return;
Player player = (Player) e.getEntity();
Player player = (Player) e.getEntity();;
Player killer = null;
for (Map.Entry<Player, List<Player>> entry : map.entrySet()) {

View File

@ -65,6 +65,12 @@ public class Gun implements ConfigUser {
blocks.add(Material.WATER);
blocks.add(Material.LAVA);
blocks.add(Material.AIR);
blocks.add(Material.COBWEB);
blocks.add(Material.ACACIA_LEAVES);
blocks.add(Material.BIRCH_LEAVES);
blocks.add(Material.OAK_LEAVES);
blocks.add(Material.DARK_OAK_LEAVES);
blocks.add(Material.SPRUCE_LEAVES);
return blocks;
}
@ -85,8 +91,6 @@ public class Gun implements ConfigUser {
stack.setItemMeta(meta);
this.plugin.getServer().getOnlinePlayers().forEach(player -> player.getInventory().addItem(new ItemStack[]{stack}));
return stack.clone();
}
@ -155,8 +159,6 @@ public class Gun implements ConfigUser {
Entity entity = getEntity(player, player.getEyeLocation().clone(), 0.0D);
System.out.println(entity);
if (!(entity instanceof LivingEntity)) {
return;
}

View File

@ -1,73 +1,74 @@
package io.github.simplexdev.strike.listeners;
package io.github.simplexdev.strike.listeners;
import io.github.simplexdev.strike.api.ConfigUser;
import io.github.simplexdev.strike.api.Spawn;
import io.github.simplexdev.strike.events.GrenadeKillEvent;
import io.github.simplexdev.strike.events.GunKillEvent;
import io.github.simplexdev.strike.api.utils.SkullCreator;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import io.github.simplexdev.strike.api.ConfigUser;
import io.github.simplexdev.strike.api.Spawn;
import io.github.simplexdev.strike.api.utils.SkullCreator;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Skull;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
public class HealthPackage implements ConfigUser {
private final JavaPlugin plugin;
private ItemStack healthPackage;
private String usedMessage;
private int regainHealth;
import java.util.Locale;
public HealthPackage(JavaPlugin plugin) {
this.plugin = plugin;
refresh();
}
public class HealthPackage implements ConfigUser {
private final JavaPlugin plugin;
private ItemStack healthPackage;
private String usedMessage;
private int regainHealth;
@EventHandler
public void onInteract(PlayerInteractEvent e) {
Player player = e.getPlayer();
ItemStack item = player.getInventory().getItemInMainHand();
public HealthPackage(JavaPlugin plugin) {
this.plugin = plugin;
refresh();
}
if (!player.getWorld().equals(Spawn.getWorld())) {
return;
}
if (player.getInventory().getItemInMainHand().isSimilar(this.healthPackage)) {
if (e.getAction().equals(Action.LEFT_CLICK_BLOCK) || e.getAction().equals(Action.LEFT_CLICK_AIR)) {
return;
}
player.setHealth(this.regainHealth);
player.sendMessage(ChatColor.translateAlternateColorCodes('&', usedMessage));
@EventHandler
public void onInteract(PlayerInteractEvent e) {
Player player = e.getPlayer();
ItemStack item = player.getInventory().getItemInMainHand();
if (item.getAmount() == 1) {
player.getInventory().setItemInMainHand(null);
} else {
if (!player.getWorld().equals(Spawn.getWorld())) {
return;
}
if (player.getInventory().getItemInMainHand().isSimilar(this.healthPackage)) {
if (e.getAction().equals(Action.LEFT_CLICK_BLOCK) || e.getAction().equals(Action.LEFT_CLICK_AIR)) {
return;
}
player.setHealth(this.regainHealth);
player.sendMessage(ChatColor.translateAlternateColorCodes('&', usedMessage));
item.setAmount(item.getAmount() - 1);
}
e.setCancelled(true);
}
}
if (item.getAmount() == 1) {
player.getInventory().setItemInMainHand(null);
} else {
public ItemStack createItem() {
String base64 = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNGExNTU4YTgzZjQwMjZkYjIzMmY4MGJjOTYxNWNjM2JhNDE1ZGM0MDk0MGE1YTEzYWUyYThjOTBiMTVjM2MzZSJ9fX0=";
item.setAmount(item.getAmount() - 1);
}
e.setCancelled(true);
}
}
ItemStack healthPackage = SkullCreator.itemWithUrl(SkullCreator.createSkull(), base64);
ItemMeta meta = plugin.getServer().getItemFactory().getItemMeta(Material.PLAYER_HEAD);
public ItemStack createItem() {
String base64 = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNGExNTU4YTgzZjQwMjZkYjIzMmY4MGJjOTYxNWNjM2JhNDE1ZGM0MDk0MGE1YTEzYWUyYThjOTBiMTVjM2MzZSJ9fX0=";
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', this.plugin.getConfig().getString("health-package.name")));
ItemStack healthPackage = SkullCreator.createSkull();
ItemMeta meta = healthPackage.getItemMeta();
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("health-package.name")));
healthPackage.setItemMeta(meta);
healthPackage.setItemMeta(meta);
return healthPackage.clone();
}
healthPackage = SkullCreator.itemWithBase64(healthPackage, base64);
public void refresh() {
this.usedMessage = plugin.getConfig().getString("health-package.restore-health-message", "You have restored your health");
this.regainHealth = plugin.getConfig().getInt("health-package.restore-health");
this.healthPackage = createItem();
}
}
return healthPackage.clone();
}
public void refresh() {
this.usedMessage = plugin.getConfig().getString("health-package.restore-health-message", "You have restored your health");
this.regainHealth = plugin.getConfig().getInt("health-package.restore-health");
this.healthPackage = createItem();
}
}

View File

@ -30,7 +30,7 @@ public class InventoryEditGUI implements Listener {
Inventory inventory = Bukkit.createInventory(null, 9);
inventories.add(inventory);
inventory.setContents(configManager.getInventory(player));
inventory.setContents(configManager.getInventoryItems(player));
player.openInventory(inventory);
}
@ -52,7 +52,7 @@ public class InventoryEditGUI implements Listener {
Player player = (Player) e.getWhoClicked();
Inventory openedInventory = player.getOpenInventory().getTopInventory();
if (inventories.contains(openedInventory) && !e.getClickedInventory().equals(openedInventory))
if (inventories.contains(openedInventory) && (e.getClickedInventory() == null || e.isShiftClick() || !e.getClickedInventory().equals(openedInventory)))
e.setCancelled(true);
}

View File

@ -10,6 +10,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.plugin.java.JavaPlugin;
public class ItemManager implements ConfigUser {
@ -22,15 +23,14 @@ public class ItemManager implements ConfigUser {
this.plugin = plugin;
refresh();
}
@EventHandler
public void onDeath(PlayerDeathEvent e) {
private void onDeath(PlayerDeathEvent e) {
Player player = e.getEntity();
if (player.getWorld().equals(Spawn.getWorld())) {
addItem(player);
if (player.getKiller() != null);
if (player.getKiller() != null)
addItem(player.getKiller());
}
}
@ -45,22 +45,35 @@ public class ItemManager implements ConfigUser {
addItem(e.getKiller());
}
private void addItem(Player player) {
public void addItem(Player player) {
InventoryEditConfigManager configManager = new InventoryEditConfigManager(plugin);
if (!hasItem(player.getInventory(), grenade))
player.getInventory().setItem(configManager.getItemSlot(grenade, player), grenade);
setItem(player, grenade, configManager);
setItem(player, healthPackage, configManager);
if (!hasItem(player.getInventory(), healthPackage))
player.getInventory().setItem(configManager.getItemSlot(healthPackage, player), healthPackage);
player.updateInventory();
}
private void setItem(Player player, ItemStack itemStack, InventoryEditConfigManager configManager) {
PlayerInventory playerInventory = player.getInventory();
if (!hasItem(playerInventory, itemStack)) {
int slot = configManager.getItemSlot(itemStack, player);
if (playerInventory.getItem(slot) != null)
playerInventory.addItem(itemStack);
else
playerInventory.setItem(slot, itemStack);
}
}
private boolean hasItem(Inventory inventory, ItemStack itemStack) {
for (int i = 0; i < inventory.getSize(); i++)
if (inventory.getItem(0).isSimilar(itemStack))
for (int i = 0; i < inventory.getSize(); i++) {
if (inventory.getItem(i) != null && inventory.getItem(i).isSimilar(itemStack))
return true;
}
return false;
}

View File

@ -2,10 +2,13 @@ package io.github.simplexdev.strike.listeners;
import com.destroystokyo.paper.event.player.PlayerJumpEvent;
import io.github.simplexdev.strike.api.ConfigUser;
import io.github.simplexdev.strike.api.Spawn;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerToggleFlightEvent;
import org.bukkit.plugin.Plugin;
@ -26,22 +29,30 @@ public class Jumper implements ConfigUser {
this.coolDownTime = Integer.valueOf(plugin.getConfig().getInt("double-jump.cooldown"));
}
@EventHandler
private void onDamage(EntityDamageEvent e) {
if (!(e.getEntity() instanceof Player))
return;
Player player = (Player) e.getEntity();
if (!player.getWorld().equals(Spawn.getWorld()) || e.getCause() != EntityDamageEvent.DamageCause.FALL)
return;
e.setCancelled(true);
}
@EventHandler
private void onPlayerMove(PlayerMoveEvent e) {
private void onPlayerJump(PlayerJumpEvent e) {
Player player = e.getPlayer();
GameMode mode = player.getGameMode();
if (mode == GameMode.CREATIVE || mode == GameMode.SPECTATOR) {
if (!player.getWorld().equals(Spawn.getWorld()) || mode == GameMode.CREATIVE || mode == GameMode.SPECTATOR)
return;
if (playersOnCoolDown.containsKey(player.getUniqueId()))
return;
}
player.setAllowFlight(true);
if (playersOnCoolDown.containsKey(player.getUniqueId())) {
return;
}
}
@ -50,7 +61,7 @@ public class Jumper implements ConfigUser {
final Player player = e.getPlayer();
GameMode mode = player.getGameMode();
if (mode == GameMode.CREATIVE || mode == GameMode.SPECTATOR) {
if (!player.getWorld().equals(Spawn.getWorld()) || mode == GameMode.CREATIVE || mode == GameMode.SPECTATOR) {
return;
}
player.setAllowFlight(false);
@ -63,7 +74,9 @@ public class Jumper implements ConfigUser {
(new BukkitRunnable() {
public void run() {
Jumper.playersOnCoolDown.remove(player.getPlayer().getUniqueId());
player.sendMessage(ChatColor.translateAlternateColorCodes('&', Jumper.this.plugin.getConfig().getString("double-jump.cooldown-finish-message")));
if (plugin.getConfig().getBoolean("double-jump.message-enabled"))
player.sendMessage(ChatColor.translateAlternateColorCodes('&', Jumper.this.plugin.getConfig().getString("double-jump.cooldown-finish-message")));
}
}).runTaskLater((Plugin) this.plugin, 20L * this.coolDownTime.intValue());
}

View File

@ -3,15 +3,15 @@ package io.github.simplexdev.strike.listeners;
import io.github.simplexdev.strike.api.ConfigUser;
import io.github.simplexdev.strike.api.Spawn;
import io.github.simplexdev.strike.api.utils.InventoryEditConfigManager;
import io.github.simplexdev.strike.events.GrenadeKillEvent;
import io.github.simplexdev.strike.events.GunKillEvent;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
@ -24,6 +24,19 @@ public class SpawnController implements ConfigUser {
}
@EventHandler
private void onPlayerWorldChange(PlayerChangedWorldEvent e) {
if (e.getPlayer().getWorld().equals(Spawn.getWorld()))
new ItemManager(plugin).addItem(e.getPlayer());
}
@EventHandler
private void onMove(PlayerMoveEvent e) {
Player player = e.getPlayer();
if (player.getWorld().equals(Spawn.getWorld()) && player.getFoodLevel() < 20)
player.setFoodLevel(20);
}
@EventHandler
private void onDeath(PlayerDeathEvent e) {
Player player = e.getEntity();
@ -34,9 +47,11 @@ public class SpawnController implements ConfigUser {
@EventHandler(priority = EventPriority.HIGH)
private void onRespawn(PlayerRespawnEvent e) {
if (e.getPlayer().getWorld().equals(Spawn.getWorld())) {
Player player = e.getPlayer();
if (player.getWorld().equals(Spawn.getWorld())) {
e.setRespawnLocation(Spawn.getSpawn());
e.getPlayer().getInventory().setContents(new InventoryEditConfigManager(plugin).getInventory(e.getPlayer()));
player.getInventory().setContents(new InventoryEditConfigManager(plugin).getInventoryItems(e.getPlayer()));
player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("spawn.respawn-message")));
}
}

View File

@ -1,6 +1,7 @@
double-jump:
cooldown: 10 #Time in Seconds
cooldown-finish-message: "Done"
message-enabled: true
gun:
ammo: 30