SteamWar/FightSystem
Archiviert
13
1
Dieses Repository wurde am 2024-08-05 archiviert. Du kannst Dateien ansehen und es klonen, aber nicht pushen oder Issues/Pull-Requests öffnen.
FightSystem/FightSystem_Main/src/de/steamwar/fightsystem/fight/Fight.java
Lixfel 59a000d3f2 Fix closed channel packets
Signed-off-by: Lixfel <agga-games@gmx.de>
2020-11-15 11:24:57 +01:00

201 Zeilen
8.0 KiB
Java

/*
This file is a part of the SteamWar software.
Copyright (C) 2020 SteamWar.de-Serverteam
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.fightsystem.fight;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.EnumWrappers;
import com.comphenix.protocol.wrappers.PlayerInfoData;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.record.RecordSystem;
import de.steamwar.fightsystem.FightSystem;
import de.steamwar.sql.Schematic;
import org.bukkit.*;
import org.bukkit.entity.Player;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
public class Fight {
private Fight(){}
public static final FightTeam redTeam = new FightTeam(Config.TeamRedName, Config.TeamRedPrefix, Config.TeamRedSpawn, Config.TeamRedCornerX, Config.TeamRedCornerY, Config.TeamRedCornerZ, Config.TeamRedRotate, false, Config.RedLeader);
public static final FightTeam blueTeam = new FightTeam(Config.TeamBlueName, Config.TeamBluePrefix, Config.TeamBlueSpawn, Config.TeamBlueCornerX, Config.TeamBlueCornerY, Config.TeamBlueCornerZ, Config.TeamBlueRotate, true, Config.BlueLeader);
private static int schemRank;
public static void init(){
IFight.init(redTeam, blueTeam);
}
public static FightTeam getPlayerTeam(Player player) {
if(redTeam.isPlayerInTeam(player))
return redTeam;
if(blueTeam.isPlayerInTeam(player))
return blueTeam;
return null;
}
public static FightTeam getOpposite(FightTeam fightTeam) {
if(fightTeam == null){
throw new IllegalArgumentException();
}
if(fightTeam == redTeam)
return blueTeam;
else
return redTeam;
}
public static FightTeam getInvitedTeam(Player player){
if(redTeam.getInvited().contains(player))
return redTeam;
else if(blueTeam.getInvited().contains(player))
return blueTeam;
return null;
}
public static FightPlayer getFightPlayer(Player player) {
if(redTeam.isPlayerInTeam(player))
return redTeam.getFightPlayer(player);
if(blueTeam.isPlayerInTeam(player))
return blueTeam.getFightPlayer(player);
return null;
}
public static FightTeam getRedTeam() {
return redTeam;
}
public static FightTeam getBlueTeam() {
return blueTeam;
}
public static void playSound(Sound sound, float volume, float pitch) {
if(Config.recording())
RecordSystem.soundAtPlayer(sound.name(), volume, pitch);
//volume: max. 100, pitch: max. 2
Bukkit.getServer().getOnlinePlayers().forEach(player -> player.playSound(player.getLocation(), sound, volume, pitch));
}
public static void setLevel(int level) {
Bukkit.getServer().getOnlinePlayers().forEach(player -> player.setLevel(level));
}
public static FightTeam getTeamByName(String name) {
if(redTeam.getName().equalsIgnoreCase(name))
return redTeam;
if(blueTeam.getName().equalsIgnoreCase(name))
return blueTeam;
return null;
}
public static void replaceSync() {
Fight.getRedTeam().replaceSync(Config.ReplaceObsidian, Material.OBSIDIAN, Material.TNT);
Fight.getBlueTeam().replaceSync(Config.ReplaceObsidian, Material.OBSIDIAN, Material.TNT);
Fight.getRedTeam().replaceSync(Config.ReplaceBedrock, Material.BEDROCK, Material.SLIME_BLOCK);
Fight.getBlueTeam().replaceSync(Config.ReplaceBedrock, Material.BEDROCK, Material.SLIME_BLOCK);
}
public static void setPlayerGamemode(Player player, GameMode gameMode) {
player.setGameMode(gameMode);
if(gameMode == GameMode.SPECTATOR) {
for(Player currentPlayer : Bukkit.getServer().getOnlinePlayers()) {
if(currentPlayer.getUniqueId() != player.getUniqueId() && currentPlayer.getGameMode() == GameMode.SPECTATOR) {
currentPlayer.hidePlayer(FightSystem.getPlugin(), player);
player.hidePlayer(FightSystem.getPlugin(), currentPlayer);
}
}
if(FightSystem.getEventLeiter() == player || Config.test())
return;
Bukkit.getScheduler().runTaskLater(FightSystem.getPlugin(), () -> {
if(!player.isOnline())
return;
PacketContainer gm1packet = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.PLAYER_INFO);
gm1packet.getPlayerInfoAction().write(0, EnumWrappers.PlayerInfoAction.UPDATE_GAME_MODE);
List<PlayerInfoData> playerInfoActions = new ArrayList<>();
playerInfoActions.add(new PlayerInfoData(WrappedGameProfile.fromPlayer(player), 1, EnumWrappers.NativeGameMode.CREATIVE, WrappedChatComponent.fromText(player.getDisplayName())));
gm1packet.getPlayerInfoDataLists().write(0, playerInfoActions);
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(player, gm1packet);
} catch (InvocationTargetException e) {
Bukkit.getLogger().log(Level.SEVERE, "Invocation target exception", e);
}
}, 2);
}else if(gameMode == GameMode.SURVIVAL) {
for(Player currentPlayer : Bukkit.getServer().getOnlinePlayers()) {
if(currentPlayer.getUniqueId() != player.getUniqueId() && currentPlayer.getGameMode() == GameMode.SPECTATOR) {
currentPlayer.showPlayer(FightSystem.getPlugin(), player);
player.showPlayer(FightSystem.getPlugin(), currentPlayer);
}
}
}
}
public static int getMaxRank(){
/* MaxRank of 0 is Pubonly*/
return schemRank;
}
public static void calcAvailibleSchemTypes() {
if(Config.OnlyPublicSchematics){
schemRank = 0;
return;
}
if(Config.IgnorePublicOnly || Config.event() || Config.Ranked){
schemRank = 1000;
return;
}
if(redTeam.getLeader() == null || redTeam.getLeader().getPlayer() == null ||
blueTeam.getLeader() == null || blueTeam.getLeader().getPlayer() == null){
schemRank = 1000;
return;
}
if(Config.RanksEnabled)
schemRank = Math.min(schemRank(redTeam.getLeader()), schemRank(blueTeam.getLeader()));
else if(Schematic.getSchemsOfType(redTeam.getLeader().getPlayer().getUniqueId(), Config.SchematicType).isEmpty() ||
Schematic.getSchemsOfType(blueTeam.getLeader().getPlayer().getUniqueId(), Config.SchematicType).isEmpty())
schemRank = 0;
else
schemRank = 1;
}
private static int schemRank(FightPlayer fightPlayer){
int rank = 1;
List<Schematic> schematics = Schematic.getSchemsOfType(fightPlayer.getPlayer().getUniqueId(), Config.SchematicType);
for(Schematic schem : schematics){
if(schem.getRank() > rank)
rank = schem.getRank();
}
return rank;
}
}