Skip to content

Commit

Permalink
Allow plugin specific KitAbilities
Browse files Browse the repository at this point in the history
Added KitAbilityManager
Added ArmorHelper.getArmorTypes
  • Loading branch information
Tigerpanzer02 committed Oct 25, 2023
1 parent b53831d commit ef79004
Show file tree
Hide file tree
Showing 8 changed files with 288 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,17 @@
import plugily.projects.minigamesbox.classic.handlers.setup.SetupInventory;
import plugily.projects.minigamesbox.classic.handlers.setup.categories.PluginSetupCategoryManager;
import plugily.projects.minigamesbox.classic.handlers.sign.SignManager;
import plugily.projects.minigamesbox.classic.kits.KitAbilityHandler;
import plugily.projects.minigamesbox.classic.kits.ability.KitAbilityHandler;
import plugily.projects.minigamesbox.classic.kits.KitMenuHandler;
import plugily.projects.minigamesbox.classic.kits.KitRegistry;
import plugily.projects.minigamesbox.classic.kits.ability.KitAbilityManager;
import plugily.projects.minigamesbox.classic.preferences.ConfigPreferences;
import plugily.projects.minigamesbox.classic.user.User;
import plugily.projects.minigamesbox.classic.user.UserManager;
import plugily.projects.minigamesbox.classic.utils.actionbar.ActionBarManager;
import plugily.projects.minigamesbox.classic.utils.configuration.ConfigUtils;
import plugily.projects.minigamesbox.classic.utils.dimensional.CuboidSelector;
import plugily.projects.minigamesbox.classic.utils.helper.ArmorHelper;
import plugily.projects.minigamesbox.classic.utils.helper.BukkitHelper;
import plugily.projects.minigamesbox.classic.utils.hologram.HologramManager;
import plugily.projects.minigamesbox.classic.utils.items.ItemManager;
Expand Down Expand Up @@ -131,6 +133,7 @@ public class PluginMain extends JavaPlugin {
private KitRegistry kitRegistry;
private MessageManager messageManager;
private LanguageManager languageManager;
private KitAbilityManager kitAbilityManager;
private PluginArgumentsRegistry argumentsRegistry;
private PluginArenaManager arenaManager;
private Metrics metrics;
Expand Down Expand Up @@ -221,6 +224,7 @@ public void onEnable() {
public void initializeDefaultClasses() {
messageManager = new MessageManager(this);
languageManager = new LanguageManager(this);
kitAbilityManager = new KitAbilityManager(this);
MessageBuilder.init(this);
TitleBuilder.init(this);
languageConfig = ConfigUtils.getConfig(this, "language");
Expand Down Expand Up @@ -569,4 +573,8 @@ public ActionBarManager getActionBarManager() {
public Random getRandom() {
return random;
}

public KitAbilityManager getKitAbilityManager() {
return kitAbilityManager;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* MiniGamesBox - Library box with massive content that could be seen as minigames core.
* Copyright (C) 2023 Plugily Projects - maintained by Tigerpanzer_02 and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package plugily.projects.minigamesbox.classic.kits.ability;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import plugily.projects.minigamesbox.classic.PluginMain;
import plugily.projects.minigamesbox.classic.handlers.language.MessageBuilder;
import plugily.projects.minigamesbox.classic.user.User;
import plugily.projects.minigamesbox.classic.utils.helper.ArmorHelper;
import plugily.projects.minigamesbox.classic.utils.version.events.api.PlugilyPlayerInteractEvent;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

public class KitAbility {
private static final PluginMain plugin = JavaPlugin.getPlugin(PluginMain.class);
private static final Map<String, KitAbility> kitAbilities = new HashMap<>();

static {
kitAbilities.put("NO_ARMOUR", new KitAbility("NO_ARMOUR", inventoryClickEvent -> {
if(!(inventoryClickEvent.getInventory().getType().equals(InventoryType.PLAYER) || inventoryClickEvent.getInventory().getType().equals(InventoryType.CRAFTING))) {
return;
}
User user = plugin.getUserManager().getUser((Player) inventoryClickEvent.getWhoClicked());
Bukkit.getScheduler().runTaskLater(plugin, () -> {
for(ItemStack stack : inventoryClickEvent.getWhoClicked().getInventory().getArmorContents()) {
if(stack == null || !ArmorHelper.getArmorTypes().contains(stack.getType())) {
continue;
}
//we cannot cancel event using scheduler, we must remove all armor contents from inventory manually
new MessageBuilder("KIT_CANNOT_WEAR_ARMOR").asKey().send(user.getPlayer());
inventoryClickEvent.getWhoClicked().getInventory().setHelmet(new ItemStack(Material.AIR, 1));
inventoryClickEvent.getWhoClicked().getInventory().setChestplate(new ItemStack(Material.AIR, 1));
inventoryClickEvent.getWhoClicked().getInventory().setLeggings(new ItemStack(Material.AIR, 1));
inventoryClickEvent.getWhoClicked().getInventory().setBoots(new ItemStack(Material.AIR, 1));
return;
}
}, 1);
}, playerInteractHandler -> {
if(ArmorHelper.getArmorTypes().contains(playerInteractHandler.getItem().getType())) {
playerInteractHandler.setCancelled(true);
new MessageBuilder("KIT_CANNOT_WEAR_ARMOR").asKey().player(playerInteractHandler.getPlayer()).sendPlayer();
}
}));
}

private final String name;
private final Consumer<InventoryClickEvent> clickConsumer;
private final Consumer<PlugilyPlayerInteractEvent> interactConsumer;

public KitAbility(String name, Consumer<InventoryClickEvent> inventoryClickHandler, Consumer<PlugilyPlayerInteractEvent> playerInteractHandler) {
this.name = name;
this.clickConsumer = inventoryClickHandler;
this.interactConsumer = playerInteractHandler;
}

public String getName() {
return name;
}

public Consumer<InventoryClickEvent> getClickConsumer() {
return clickConsumer;
}

public Consumer<PlugilyPlayerInteractEvent> getInteractConsumer() {
return interactConsumer;
}

public static Map<String, KitAbility> getKitAbilities() {
return Collections.unmodifiableMap(kitAbilities);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* MiniGamesBox - Library box with massive content that could be seen as minigames core.
* Copyright (C) 2023 Plugily Projects - maintained by Tigerpanzer_02 and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package plugily.projects.minigamesbox.classic.kits.ability;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import plugily.projects.minigamesbox.classic.PluginMain;
import plugily.projects.minigamesbox.classic.user.User;
import plugily.projects.minigamesbox.classic.utils.version.events.api.PlugilyPlayerInteractEvent;

public class KitAbilityHandler implements Listener {

private final PluginMain plugin;

public KitAbilityHandler(PluginMain plugin) {
this.plugin = plugin;
if(!plugin.getConfigPreferences().getOption("KITS")) {
return;
}
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

@EventHandler
public void onKitInventoryClick(InventoryClickEvent event) {
if(!(event.getWhoClicked() instanceof Player)) {
return;
}
User user = plugin.getUserManager().getUser((Player) event.getWhoClicked());
if(!plugin.getArenaRegistry().isInArena((Player) event.getWhoClicked())) {
return;
}
for(KitAbility kitAbility : plugin.getKitAbilityManager().getKitAbilities().values()) {
if(user.getKit().hasAbility(kitAbility)) {
kitAbility.getClickConsumer().accept(event);
}
}
}

@EventHandler
public void onKitInteractClick(PlugilyPlayerInteractEvent event) {
if(!plugin.getArenaRegistry().isInArena(event.getPlayer())) {
return;
}
if(!event.hasItem()) {
return;
}
for(KitAbility kitAbility : plugin.getKitAbilityManager().getKitAbilities().values()) {
if(plugin.getUserManager().getUser(event.getPlayer()).getKit().hasAbility(kitAbility)) {
kitAbility.getInteractConsumer().accept(event);
}
}

}

}
Loading

0 comments on commit ef79004

Please sign in to comment.