/*
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 .
*/
package de.steamwar.bausystem;
import com.sk89q.worldedit.command.WorldEditCommands;
import de.steamwar.bausystem.commands.*;
import de.steamwar.bausystem.commands.worldedit.*;
import de.steamwar.bausystem.tracer.ShowManager;
import de.steamwar.bausystem.tracer.TNTTracer;
import de.steamwar.bausystem.tracer.TraceListener;
import de.steamwar.bausystem.world.*;
import de.steamwar.core.CommandRemover;
import de.steamwar.core.Core;
import de.steamwar.scoreboard.SWScoreboard;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit;
import org.bukkit.GameRule;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
public class BauSystem extends JavaPlugin implements Listener {
private static BauSystem plugin;
private static UUID owner;
private static List sections;
public static final String PREFIX = "§eBauSystem§8» §7";
public static final String SECTION_PATH = "/home/minecraft/backbone/server/UserBau/";
private BukkitTask autoShutdown;
@Override
public void onEnable() {
plugin = this;
String worldName = Bukkit.getWorlds().get(0).getName();
try{
owner = UUID.fromString(worldName);
sections = ArenaSection.loadFromFile(new File(Bukkit.getWorldContainer().getPath() + '/' + getOwner().toString() + "/sections.yml"));
}catch(IllegalArgumentException e){
try{
owner = null;
sections = ArenaSection.loadFromFile(new File(Bukkit.getWorldContainer().getPath() + '/' + worldName + "/sections.yml"));
}catch(IOException | InvalidConfigurationException ex){
getLogger().log(Level.SEVERE, "owner is no UUID / failed to load sections.yml", e);
Bukkit.shutdown();
return;
}
} catch (InvalidConfigurationException | IOException e) {
getLogger().log(Level.SEVERE, "Failed to load sections.yml", e);
Bukkit.shutdown();
return;
}
try {
CommandRemover.removeAll("tp", "gamemode", "time", "clear");
CommandInjector.injectCommand(new CommandTeleport());
CommandInjector.injectCommand(new CommandGamemode());
CommandInjector.injectCommand(new CommandTime());
CommandInjector.injectCommand(new CommandClear());
} catch (Exception e) {
getLogger().log(Level.SEVERE, "Failed to replace commands", e);
Bukkit.shutdown();
return;
}
getCommand("trace").setExecutor(new CommandTrace());
getCommand("trace").setTabCompleter(new CommandTraceTabCompleter());
getCommand("tpslimit").setExecutor(new CommandTPSLimiter());
getCommand("tpslimit").setTabCompleter(new CommandTPSLimiterTabComplete());
getCommand("nightvision").setExecutor(new CommandNV());
getCommand("reset").setExecutor(new CommandReset());
getCommand("speed").setExecutor(new CommandSpeed());
getCommand("tnt").setExecutor(new CommandTNT());
getCommand("fire").setExecutor(new CommandFire());
getCommand("freeze").setExecutor(new CommandFreeze());
getCommand("testblock").setExecutor(new CommandTestblock());
getCommand("bau").setExecutor(new CommandBau());
getCommand("bauinfo").setExecutor(new CommandInfo());
getCommand("protect").setExecutor(new CommandProtect());
getCommand("skull").setExecutor(new CommandSkull());
getCommand("loader").setExecutor(new CommandLoader());
getCommand("lockschem").setExecutor(new CommandLockschem());
getCommand("debugstick").setExecutor(new CommandDebugStick());
getCommand("watervision").setExecutor(new CommandGills());
getCommand("detonator").setExecutor(new CommandDetonator());
getCommand("detonator").setTabCompleter(new CommandDetonatorTabCompleter());
getCommand("/1").setExecutor(new WECommandPos1());
getCommand("/2").setExecutor(new WECommandPos2());
getCommand("/90").setExecutor(new WECommandRotate90());
getCommand("/-90").setExecutor(new WECommandRotateN90());
getCommand("/180").setExecutor(new WECommandRotate180());
getCommand("/c").setExecutor(new WECommandCopy());
getCommand("/p").setExecutor(new WECommandPaste());
getCommand("/flopy").setExecutor(new WECommandFlopy());
getCommand("/flopyp").setExecutor(new WECommandFlopyp());
Bukkit.getPluginManager().registerEvents(this, this);
Bukkit.getPluginManager().registerEvents(new RegionListener(), this);
Bukkit.getPluginManager().registerEvents(new BauScoreboard(), this);
Bukkit.getPluginManager().registerEvents(new TraceListener(), this);
Bukkit.getPluginManager().registerEvents(new ClipboardListener(), this);
new AFKStopper();
TNTTracer.init();
autoShutdown = Bukkit.getScheduler().runTaskLater(this, Bukkit::shutdown, 1200);
}
public static BauSystem getPlugin(){
return plugin;
}
public static UUID getOwner(){
//Lazy loading to improve startup time of the server in 1.15
if(owner == null){
try{
owner = SteamwarUser.get(Integer.parseInt(Bukkit.getWorlds().get(0).getName())).getUUID();
}catch(NumberFormatException e){
Bukkit.shutdown();
throw new SecurityException("owner is not a UserID", e);
}
}
return owner;
}
public static List getSections(){
return sections;
}
public static int getOwnerID(){
return SteamwarUser.get(getOwner()).getId();
}
@EventHandler
public void onDeath(PlayerDeathEvent e) {
e.setDeathMessage(null);
}
@EventHandler
public void onJoin(PlayerLoginEvent e) {
if(autoShutdown != null){
autoShutdown.cancel();
autoShutdown = null;
}
Player p = e.getPlayer();
p.setOp(true);
ShowManager.add(p);
if (Core.getVersion() == 15)
Bukkit.getWorlds().get(0).setGameRule(GameRule.REDUCED_DEBUG_INFO, false);
}
@EventHandler
public void onLeave(PlayerQuitEvent e) {
Player p = e.getPlayer();
SWScoreboard.removeScoreboard(p);
if (Bukkit.getOnlinePlayers().isEmpty() || (Bukkit.getOnlinePlayers().size() == 1 && Bukkit.getOnlinePlayers().contains(p))) {
Bukkit.shutdown();
}
}
@EventHandler
public void onInventoryClick(InventoryClickEvent e) {
ItemStack stack = e.getCursor();
if (stack == null || !stack.hasItemMeta())
return;
assert stack.getItemMeta() != null;
if (stack.getItemMeta().hasEnchants()) {
for (Enchantment en : Enchantment.values()) {
if (stack.getEnchantmentLevel(en) > en.getMaxLevel())
stack.removeEnchantment(en);
}
}
Material material = stack.getType();
if (material == Material.POTION || material == Material.SPLASH_POTION || material == Material.LINGERING_POTION) {
stack.setType(Material.MILK_BUCKET);
}
if (Core.getVersion() < 14) {
e.setCurrentItem(stack);
return;
}
if (stack.getItemMeta().hasAttributeModifiers()) {
ItemMeta meta = stack.getItemMeta();
for (Attribute a : Attribute.values()) {
meta.removeAttributeModifier(a);
}
stack.setItemMeta(meta);
}
e.setCurrentItem(stack);
}
}