SteamWar/FightSystem
Archiviert
13
1

Improve PersonalKitCreator #179

Manuell gemergt
Lixfel hat 1 Commits von personalKitCreatorRework nach master 2020-08-22 08:36:24 +02:00 zusammengeführt
7 geänderte Dateien mit 256 neuen und 180 gelöschten Zeilen
Nur Änderungen aus Commit ce67d0e4f1 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -74,6 +74,7 @@ public class FightSystem extends JavaPlugin {
new TestListener();
new NormalJoinListener();
new RankedJoinListener();
new PersonalKitCreator();
if(Core.getVersion() > 8)
new ExtendedInventoryListener();
@ -154,7 +155,6 @@ public class FightSystem extends JavaPlugin {
throw new SecurityException(fightState.name());
setFightState(FightState.PRE_RUNNING);
PersonalKitCreator.closeAll();
Fight.getBlueTeam().loadKits();
Fight.getRedTeam().loadKits();
setAllPlayersGM(GameMode.SURVIVAL);

Datei anzeigen

@ -1,14 +1,14 @@
package de.steamwar.fightsystem.commands;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.FightSystem;
import de.steamwar.fightsystem.fight.Fight;
import de.steamwar.fightsystem.fight.FightPlayer;
import de.steamwar.fightsystem.fight.FightTeam;
import de.steamwar.fightsystem.kit.Kit;
import de.steamwar.fightsystem.kit.KitManager;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.listener.PersonalKitCreator;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.utils.PersonalKitCreator;
import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem;
import de.steamwar.inventory.SWListInv;
@ -78,7 +78,7 @@ public class GUI {
return;
if(Config.PersonalKits){
new PersonalKitCreator(p);
PersonalKitCreator.openKitCreator(p);
return;
}

Datei anzeigen

@ -5,13 +5,12 @@ import de.steamwar.fightsystem.commands.GUI;
import de.steamwar.inventory.SWInventory;
import de.steamwar.inventory.SWItem;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;
import java.util.*;
public class Kit {
@ -74,20 +73,41 @@ public class Kit {
return enterStage;
}
public boolean isStackInKit(ItemStack stack){
public boolean isEnchantmentInKit(ItemStack stack){
for(ItemStack is : inventory){
if(stack.equals(is))
if(similar(stack, is))
return true;
}
if(armor != null){
for(ItemStack is : armor){
if(stack.equals(is))
if(similar(stack, is))
return true;
}
}
return false;
}
private boolean similar(ItemStack stack, ItemStack stack2){
if(stack == null || stack2 == null)
return false;
if(stack.getType() != stack2.getType())
return false;
if(stack.hasItemMeta() != stack2.hasItemMeta())
return false;
if(stack.getItemMeta() == null || stack2.getItemMeta() == null)
return true;
//Enchantment Map comparison used for default similarity check does not work
Map<Enchantment, Integer> en = stack.getItemMeta().getEnchants();
Map<Enchantment, Integer> en2 = new HashMap<>(stack.getItemMeta().getEnchants());
for(Map.Entry<Enchantment, Integer> e : en.entrySet()){
if(!en2.remove(e.getKey(), e.getValue()))
return false;
}
return en2.isEmpty();
}
public void loadToPlayer(Player player) {
player.getInventory().setContents(inventory);
if(armor != null)

Datei anzeigen

@ -2,7 +2,6 @@ package de.steamwar.fightsystem.listener;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.utils.PersonalKitCreator;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;

Datei anzeigen

@ -22,7 +22,12 @@ public class HotbarGUIListener extends BasicListener {
public void handlePlayerInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
if(event.getItem() != null) {
if(!PersonalKitCreator.notInKitCreator(player))
return;
if(event.getItem() == null)
return;
FightTeam fightTeam = Fight.getPlayerTeam(player);
if(fightTeam == null)
return;
@ -59,7 +64,7 @@ public class HotbarGUIListener extends BasicListener {
case "§eRespawn":
player.teleport(fightTeam.getSpawn());
break;
}
}
}
}

Datei anzeigen

@ -0,0 +1,187 @@
package de.steamwar.fightsystem.listener;
import de.steamwar.core.Core;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.FightSystem;
import de.steamwar.fightsystem.fight.Fight;
import de.steamwar.fightsystem.fight.FightPlayer;
import de.steamwar.fightsystem.kit.Kit;
import de.steamwar.fightsystem.kit.KitManager;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.sql.PersonalKit;
import de.steamwar.sql.SteamwarUser;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
public class PersonalKitCreator extends BasicListener {
private static final Map<HumanEntity, InventoryBackup> openKitCreators = new HashMap<>();
private static final EnumSet<FightState> enabled = Config.PersonalKits ? EnumSet.of(FightState.PRE_LEADER_SETUP, FightState.PRE_SCHEM_SETUP, FightState.POST_SCHEM_SETUP) : EnumSet.noneOf(FightState.class);
public PersonalKitCreator(){
super(enabled);
}
public static void openKitCreator(Player player){
if(!enabled.contains(FightSystem.getFightState()))
return;
new InventoryBackup(player);
FightPlayer fightPlayer = Fight.getFightPlayer(player);
assert fightPlayer != null;
PersonalKit kit = PersonalKit.get(SteamwarUser.get(player.getUniqueId()).getId(), Config.SchematicType.toDB());
if(kit == null){
fightPlayer.getKit().loadToPlayer(player);
}else{
player.getInventory().setContents(kit.getInventory());
player.getInventory().setArmorContents(kit.getArmor());
}
player.updateInventory();
player.setGameMode(GameMode.CREATIVE);
}
static boolean notInKitCreator(HumanEntity player){
return !openKitCreators.containsKey(player);
}
@Override
public void disable(){
while(!openKitCreators.isEmpty()){
openKitCreators.values().iterator().next().player.closeInventory();
}
super.disable();
}
@EventHandler
public void onInventoryClick(InventoryClickEvent e){
if(!openKitCreators.containsKey(e.getWhoClicked()))
return;
Player player = (Player) e.getWhoClicked();
//Deny bad items
if(isBadItem(e.getCursor(), player))
e.setCancelled(true);
checkForClear(e, player);
}
@EventHandler
public void onMoveEvent(PlayerMoveEvent e){
if(!openKitCreators.containsKey(e.getPlayer()))
return;
e.setCancelled(true);
toActionbar(e.getPlayer(), TextComponent.fromLegacyText("§eInventar zum Anpassen des Kits öffnen§8!"));
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent e) {
InventoryBackup backup = openKitCreators.get(e.getPlayer());
if(backup == null)
return;
backup.close();
}
@EventHandler
public void onPlayerExit(PlayerQuitEvent e){
InventoryBackup backup = openKitCreators.get(e.getPlayer());
if(backup == null)
return;
backup.close();
}
private void checkForClear(InventoryClickEvent e, Player player){
if(e.getAction() != InventoryAction.PLACE_ALL)
return;
ItemStack[] items = e.getWhoClicked().getInventory().getContents();
for(int i = 0; i < items.length; i++){
ItemStack stack = items[i];
if(stack != null && i != e.getSlot())
return;
}
FightPlayer fightPlayer = Fight.getFightPlayer(player);
assert fightPlayer != null;
Bukkit.getScheduler().runTaskLater(FightSystem.getPlugin(), () -> fightPlayer.getKit().loadToPlayer(player), 1);
}
private static boolean isBadItem(ItemStack stack, Player player){
if(stack == null)
return false;
//Check for forbidden item
if(Config.ForbiddenItems.contains(stack.getType().name()))
return true;
//Check for attribute modifiers
if(Core.getVersion() >= 14 && stack.hasItemMeta() && Objects.requireNonNull(stack.getItemMeta()).hasAttributeModifiers()){
Bukkit.getLogger().log(Level.SEVERE, "Spieler " + player.getName() + " hat versucht ein Item mit einem Attribute-Modifier zu bekommen.");
return true;
}
Kit normal = KitManager.getKitByName(Config.MemberDefault);
assert normal != null;
return !normal.isEnchantmentInKit(stack) && !stack.getEnchantments().isEmpty();
}
private static class InventoryBackup{
private final Player player;
private final ItemStack[] contents;
private final ItemStack[] armor;
private InventoryBackup(Player player){
openKitCreators.put(player, this);
this.player = player;
this.contents = player.getInventory().getContents();
this.armor = player.getInventory().getArmorContents();
}
private void loadBackup(){
PlayerInventory inventory = player.getInventory();
inventory.setContents(contents);
inventory.setArmorContents(armor);
player.updateInventory();
}
private void close(){
openKitCreators.remove(player);
PersonalKit.save(SteamwarUser.get(player.getUniqueId()).getId(), Config.SchematicType.toDB(), removeBadItems(player.getInventory().getContents()), removeBadItems(player.getInventory().getArmorContents()));
loadBackup();
player.setGameMode(GameMode.SURVIVAL);
}
private ItemStack[] removeBadItems(ItemStack[] inventory){
Kit normal = KitManager.getKitByName(Config.MemberDefault);
assert normal != null;
for(int i = 0; i < inventory.length; i++){
if(isBadItem(inventory[i], player))
inventory[i] = null;
}
return inventory;
}
}
}

Datei anzeigen

@ -1,135 +0,0 @@
package de.steamwar.fightsystem.utils;
import de.steamwar.core.Core;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.FightSystem;
import de.steamwar.fightsystem.fight.Fight;
import de.steamwar.fightsystem.fight.FightPlayer;
import de.steamwar.fightsystem.kit.Kit;
import de.steamwar.fightsystem.kit.KitManager;
import de.steamwar.sql.PersonalKit;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
public class PersonalKitCreator implements Listener {
private static final Map<Player, PersonalKitCreator> openKitCreators = new HashMap<>();
private final Player player;
private final SteamwarUser user;
private final ItemStack[] contents;
private final ItemStack[] armor;
public PersonalKitCreator(Player player){
this.player = player;
contents = player.getInventory().getContents();
armor = player.getInventory().getArmorContents();
user = SteamwarUser.get(player.getUniqueId());
PersonalKit kit = PersonalKit.get(user.getId(), Config.SchematicType.toDB());
if(kit == null){
FightPlayer fightPlayer = Fight.getFightPlayer(player);
assert fightPlayer != null;
fightPlayer.getKit().loadToPlayer(player);
}else{
player.getInventory().setContents(kit.getInventory());
player.getInventory().setArmorContents(kit.getArmor());
}
player.setGameMode(GameMode.CREATIVE);
Bukkit.getPluginManager().registerEvents(this, FightSystem.getPlugin());
openKitCreators.put(player, this);
}
public static void closeAll(){
while(!openKitCreators.isEmpty()){
openKitCreators.values().iterator().next().player.closeInventory();
}
}
public static boolean notInKitCreator(HumanEntity player){
return !openKitCreators.containsKey(player);
}
@EventHandler
public void onInventoryClick(InventoryClickEvent e){
if(!player.equals(e.getWhoClicked()))
return;
//Deny bad items
if(e.getCursor() != null){
ItemStack stack = e.getCursor();
if(Config.ForbiddenItems.contains(stack.getType().name())){
e.setCancelled(true);
return;
}
Kit normal = KitManager.getKitByName(Config.MemberDefault);
if(!normal.isStackInKit(stack)){
if(stack.getEnchantments().size() > 0){
e.setCancelled(true);
return;
}
if(Core.getVersion() >= 14 && stack.hasItemMeta() && Objects.requireNonNull(stack.getItemMeta()).hasAttributeModifiers()){
e.setCancelled(true);
Bukkit.getLogger().log(Level.SEVERE, "Spieler " + player.getName() + " hat versucht ein Item mit einem Attribute-Modifier zu bekommen.");
return;
}
}
}
//Check for clear
if(e.getAction() != InventoryAction.PLACE_ALL)
return;
ItemStack[] items = e.getWhoClicked().getInventory().getContents();
for(int i = 0; i < items.length; i++){
ItemStack stack = items[i];
if(stack != null && i != e.getSlot())
return;
}
FightPlayer fightPlayer = Fight.getFightPlayer(player);
assert fightPlayer != null;
Bukkit.getScheduler().runTaskLater(FightSystem.getPlugin(), () -> fightPlayer.getKit().loadToPlayer(player), 1);
}
@EventHandler
public void onMoveEvent(PlayerMoveEvent e){
if(!player.equals(e.getPlayer()))
return;
e.setCancelled(true);
}
@EventHandler
public void onInventoryClose(InventoryCloseEvent e) {
if(!player.equals(e.getPlayer()))
return;
close();
}
private void close(){
PersonalKit.save(user.getId(), Config.SchematicType.toDB(), player.getInventory().getContents(), player.getInventory().getArmorContents());
player.getInventory().setContents(contents);
player.getInventory().setArmorContents(armor);
player.setGameMode(GameMode.SURVIVAL);
HandlerList.unregisterAll(this);
openKitCreators.remove(player);
}
}