Skip to content

Commit

Permalink
refactors and basic api
Browse files Browse the repository at this point in the history
  • Loading branch information
YouHaveTrouble committed Feb 19, 2021
1 parent 9058610 commit 8626d79
Show file tree
Hide file tree
Showing 32 changed files with 189 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

import eu.endermite.togglepvp.commands.MainCommand;
import eu.endermite.togglepvp.config.ConfigCache;
import eu.endermite.togglepvp.listeners.player.*;
import eu.endermite.togglepvp.listeners.player.WolfAttackPlayerListener;
import eu.endermite.togglepvp.listeners.wolf.WolfTargettingPlayerListener;
import eu.endermite.togglepvp.listeners.unspecific.*;
import eu.endermite.togglepvp.listeners.wolf.*;
import eu.endermite.togglepvp.players.PlayerManager;
import eu.endermite.togglepvp.players.SmartCache;
import eu.endermite.togglepvp.util.Listener;
Expand All @@ -15,16 +10,14 @@
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import org.reflections.Reflections;
import org.reflections.scanners.TypeAnnotationsScanner;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;
import java.util.Set;

public final class TogglePvP extends JavaPlugin {
public final class TogglePvp extends JavaPlugin {

@Getter private static TogglePvP plugin;
@Getter private static TogglePvp plugin;
private ConfigCache configCache;
private PlayerManager playerManager;
private DatabaseSQLite sqLite;
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/eu/endermite/togglepvp/TogglePvpAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package eu.endermite.togglepvp;

import eu.endermite.togglepvp.players.SmartCache;
import java.util.UUID;

public class TogglePvpAPI {

/**
* Sets player PvP state. This will always save to database.
* @param uuid uuid of player to set
* @param newState State to set
*/
public static void setPvpEnabled(UUID uuid, boolean newState) {
SmartCache.setPlayerPvpState(uuid, newState);
}

/**
* Gets player's PvP state. If player is not cached this will query the database.
* @param uuid uuid of player to get data from.
* @return true if enabled, false if disabled
*/
public static boolean getPvpEnabled(UUID uuid) {
return SmartCache.getPlayerData(uuid).isPvpEnabled();
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.endermite.togglepvp.commands;

import eu.endermite.togglepvp.TogglePvP;
import eu.endermite.togglepvp.TogglePvp;
import eu.endermite.togglepvp.util.PluginMessages;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
Expand All @@ -11,7 +11,7 @@
public class HelpCommand {

public static void help(CommandSender sender, String[] args) {
Bukkit.getScheduler().runTaskAsynchronously(TogglePvP.getPlugin(), () -> {
Bukkit.getScheduler().runTaskAsynchronously(TogglePvp.getPlugin(), () -> {

List<String> helpPage = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.endermite.togglepvp.commands;

import eu.endermite.togglepvp.TogglePvP;
import eu.endermite.togglepvp.TogglePvp;
import eu.endermite.togglepvp.util.PluginMessages;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -33,14 +33,14 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
ReloadCommand.reload(sender);
break;
default:
sender.sendMessage(PluginMessages.parseMessage(TogglePvP.getPlugin().getConfigCache().getNo_such_command()));
sender.sendMessage(PluginMessages.parseMessage(TogglePvp.getPlugin().getConfigCache().getNo_such_command()));
break;
}
} else {
HelpCommand.help(sender, args);
}
} else {
sender.sendMessage(PluginMessages.parseMessage(TogglePvP.getPlugin().getConfigCache().getNo_permission()));
sender.sendMessage(PluginMessages.parseMessage(TogglePvp.getPlugin().getConfigCache().getNo_permission()));
}
return true;
}
Expand Down
56 changes: 28 additions & 28 deletions src/main/java/eu/endermite/togglepvp/commands/PvpToggleCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.endermite.togglepvp.commands;

import eu.endermite.togglepvp.TogglePvP;
import eu.endermite.togglepvp.TogglePvp;
import eu.endermite.togglepvp.util.CombatTimer;
import eu.endermite.togglepvp.util.PluginMessages;
import net.md_5.bungee.api.ChatMessageType;
Expand All @@ -14,10 +14,10 @@ public class PvpToggleCommand {

public static void toggle(CommandSender sender, String[] args) {

Bukkit.getScheduler().runTaskAsynchronously(TogglePvP.getPlugin(), () -> {
Bukkit.getScheduler().runTaskAsynchronously(TogglePvp.getPlugin(), () -> {

if (!sender.hasPermission("togglepvp.command.toggle")) {
String message = TogglePvP.getPlugin().getConfigCache().getNo_permission();
String message = TogglePvp.getPlugin().getConfigCache().getNo_permission();
BaseComponent[] component = TextComponent.fromLegacyText(message);
sender.spigot().sendMessage(component);
return;
Expand All @@ -28,17 +28,17 @@ public static void toggle(CommandSender sender, String[] args) {
Player player = (Player) sender;

if (CombatTimer.isInCombat(player.getUniqueId())) {
sender.sendMessage(PluginMessages.parseMessage(TogglePvP.getPlugin().getConfigCache().getCant_do_that_during_combat()));
sender.sendMessage(PluginMessages.parseMessage(TogglePvp.getPlugin().getConfigCache().getCant_do_that_during_combat()));
return;
}

boolean currentState = TogglePvP.getPlugin().getPlayerManager().togglePlayerPvpState(player.getUniqueId());
boolean currentState = TogglePvp.getPlugin().getPlayerManager().togglePlayerPvpState(player.getUniqueId());

String message = "";
if (currentState) {
message = PluginMessages.parseMessage(TogglePvP.getPlugin().getConfigCache().getPvp_enabled());
message = PluginMessages.parseMessage(TogglePvp.getPlugin().getConfigCache().getPvp_enabled());
} else {
message = PluginMessages.parseMessage(TogglePvP.getPlugin().getConfigCache().getPvp_disabled());
message = PluginMessages.parseMessage(TogglePvp.getPlugin().getConfigCache().getPvp_disabled());
}
BaseComponent[] component = TextComponent.fromLegacyText(message);
player.spigot().sendMessage(ChatMessageType.CHAT, component);
Expand All @@ -47,7 +47,7 @@ public static void toggle(CommandSender sender, String[] args) {
}
} else if (args.length == 2) {
if (!sender.hasPermission("togglepvp.command.toggle.others")) {
String message = TogglePvP.getPlugin().getConfigCache().getNo_permission();
String message = TogglePvp.getPlugin().getConfigCache().getNo_permission();
BaseComponent[] component = TextComponent.fromLegacyText(message);
sender.spigot().sendMessage(component);
return;
Expand All @@ -56,18 +56,18 @@ public static void toggle(CommandSender sender, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if (CombatTimer.isInCombat(player.getUniqueId())) {
sender.sendMessage(PluginMessages.parseMessage(TogglePvP.getPlugin().getConfigCache().getCant_do_that_during_combat()));
sender.sendMessage(PluginMessages.parseMessage(TogglePvp.getPlugin().getConfigCache().getCant_do_that_during_combat()));
return;
}
}
try {
Player player = Bukkit.getPlayer(args[1]);
boolean currentState = TogglePvP.getPlugin().getPlayerManager().togglePlayerPvpState(player.getUniqueId());
boolean currentState = TogglePvp.getPlugin().getPlayerManager().togglePlayerPvpState(player.getUniqueId());
String message;
if (currentState) {
message = TogglePvP.getPlugin().getConfigCache().getPvp_enabled_other();
message = TogglePvp.getPlugin().getConfigCache().getPvp_enabled_other();
} else {
message = TogglePvP.getPlugin().getConfigCache().getPvp_disabled_other();
message = TogglePvp.getPlugin().getConfigCache().getPvp_disabled_other();
}
sender.sendMessage(PluginMessages.parsePlayerName(player, message));

Expand All @@ -86,7 +86,7 @@ public static void toggle(CommandSender sender, String[] args) {

public static void enable(CommandSender sender, String[] args) {
if (!sender.hasPermission("togglepvp.command.toggle")) {
String message = TogglePvP.getPlugin().getConfigCache().getNo_permission();
String message = TogglePvp.getPlugin().getConfigCache().getNo_permission();
BaseComponent[] component = TextComponent.fromLegacyText(message);
sender.spigot().sendMessage(component);
return;
Expand All @@ -95,35 +95,35 @@ public static void enable(CommandSender sender, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if (CombatTimer.isInCombat(player.getUniqueId())) {
sender.sendMessage(PluginMessages.parseMessage(TogglePvP.getPlugin().getConfigCache().getCant_do_that_during_combat()));
sender.sendMessage(PluginMessages.parseMessage(TogglePvp.getPlugin().getConfigCache().getCant_do_that_during_combat()));
return;
}
TogglePvP.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), true);
String message = PluginMessages.parseMessage(TogglePvP.getPlugin().getConfigCache().getPvp_enabled());
TogglePvp.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), true);
String message = PluginMessages.parseMessage(TogglePvp.getPlugin().getConfigCache().getPvp_enabled());
BaseComponent[] component = TextComponent.fromLegacyText(message);
player.spigot().sendMessage(ChatMessageType.CHAT, component);
} else {
sender.sendMessage("Try /pvp enable <player>");
}
} else if (args.length == 2) {
if (!sender.hasPermission("togglepvp.command.toggle.others")) {
String message = TogglePvP.getPlugin().getConfigCache().getNo_permission();
String message = TogglePvp.getPlugin().getConfigCache().getNo_permission();
BaseComponent[] component = TextComponent.fromLegacyText(message);
sender.spigot().sendMessage(component);
return;
}
if (sender instanceof Player) {
Player player = (Player) sender;
if (CombatTimer.isInCombat(player.getUniqueId())) {
sender.sendMessage(PluginMessages.parseMessage(TogglePvP.getPlugin().getConfigCache().getCant_do_that_during_combat()));
sender.sendMessage(PluginMessages.parseMessage(TogglePvp.getPlugin().getConfigCache().getCant_do_that_during_combat()));
return;
}
}
try {
Player player = Bukkit.getPlayer(args[1]);
String message = TogglePvP.getPlugin().getConfigCache().getPvp_enabled_other();
String message = TogglePvp.getPlugin().getConfigCache().getPvp_enabled_other();
sender.sendMessage(PluginMessages.parsePlayerName(player, message));
TogglePvP.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), true);
TogglePvp.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), true);
} catch (NullPointerException e) {
sender.sendMessage(PluginMessages.parseMessage("&cPlayer offline."));
}
Expand All @@ -138,7 +138,7 @@ public static void enable(CommandSender sender, String[] args) {

public static void disable(CommandSender sender, String[] args) {
if (!sender.hasPermission("togglepvp.command.toggle")) {
String message = TogglePvP.getPlugin().getConfigCache().getNo_permission();
String message = TogglePvp.getPlugin().getConfigCache().getNo_permission();
BaseComponent[] component = TextComponent.fromLegacyText(message);
sender.spigot().sendMessage(component);
return;
Expand All @@ -147,35 +147,35 @@ public static void disable(CommandSender sender, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
if (CombatTimer.isInCombat(player.getUniqueId())) {
sender.sendMessage(PluginMessages.parseMessage(TogglePvP.getPlugin().getConfigCache().getCant_do_that_during_combat()));
sender.sendMessage(PluginMessages.parseMessage(TogglePvp.getPlugin().getConfigCache().getCant_do_that_during_combat()));
return;
}
TogglePvP.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), false);
String message = PluginMessages.parseMessage(TogglePvP.getPlugin().getConfigCache().getPvp_disabled());
TogglePvp.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), false);
String message = PluginMessages.parseMessage(TogglePvp.getPlugin().getConfigCache().getPvp_disabled());
BaseComponent[] component = TextComponent.fromLegacyText(message);
player.spigot().sendMessage(ChatMessageType.CHAT, component);
} else {
sender.sendMessage("Try /pvp disable <player>");
}
} else if (args.length == 2) {
if (!sender.hasPermission("togglepvp.command.toggle.others")) {
String message = TogglePvP.getPlugin().getConfigCache().getNo_permission();
String message = TogglePvp.getPlugin().getConfigCache().getNo_permission();
BaseComponent[] component = TextComponent.fromLegacyText(message);
sender.spigot().sendMessage(component);
return;
}
if (sender instanceof Player) {
Player player = (Player) sender;
if (CombatTimer.isInCombat(player.getUniqueId())) {
sender.sendMessage(PluginMessages.parseMessage(TogglePvP.getPlugin().getConfigCache().getCant_do_that_during_combat()));
sender.sendMessage(PluginMessages.parseMessage(TogglePvp.getPlugin().getConfigCache().getCant_do_that_during_combat()));
return;
}
}
try {
Player player = Bukkit.getPlayer(args[1]);
String message = TogglePvP.getPlugin().getConfigCache().getPvp_disabled_other();
String message = TogglePvp.getPlugin().getConfigCache().getPvp_disabled_other();
sender.sendMessage(PluginMessages.parsePlayerName(player, message));
TogglePvP.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), true);
TogglePvp.getPlugin().getPlayerManager().setPlayerPvpState(player.getUniqueId(), true);
} catch (NullPointerException e) {
sender.sendMessage(PluginMessages.parseMessage("&cPlayer offline."));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.endermite.togglepvp.commands;

import eu.endermite.togglepvp.TogglePvP;
import eu.endermite.togglepvp.TogglePvp;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
Expand All @@ -10,14 +10,14 @@ public class ReloadCommand {

public static void reload(CommandSender sender) {

Bukkit.getScheduler().runTaskAsynchronously(TogglePvP.getPlugin(), () -> {
Bukkit.getScheduler().runTaskAsynchronously(TogglePvp.getPlugin(), () -> {
if (!sender.hasPermission("togglepvp.command.reload")) {
String message = TogglePvP.getPlugin().getConfigCache().getNo_permission();
String message = TogglePvp.getPlugin().getConfigCache().getNo_permission();
BaseComponent[] component = TextComponent.fromLegacyText(message);
sender.spigot().sendMessage(component);
return;
}
TogglePvP.getPlugin().reloadPluginConfig(sender);
TogglePvp.getPlugin().reloadPluginConfig(sender);
});

}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/eu/endermite/togglepvp/config/ConfigCache.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.endermite.togglepvp.config;

import eu.endermite.togglepvp.TogglePvP;
import eu.endermite.togglepvp.TogglePvp;
import lombok.Getter;
import org.bukkit.configuration.Configuration;

Expand Down Expand Up @@ -31,7 +31,7 @@ public class ConfigCache {

public ConfigCache() {

Configuration config = TogglePvP.getPlugin().getConfig();
Configuration config = TogglePvp.getPlugin().getConfig();

// Settings
this.pvp_enabled_by_default = config.getBoolean("settings.pvp_enabled_by_default", false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.endermite.togglepvp.listeners.player;

import eu.endermite.togglepvp.TogglePvP;
import eu.endermite.togglepvp.TogglePvp;
import eu.endermite.togglepvp.config.ConfigCache;
import eu.endermite.togglepvp.util.CombatTimer;
import eu.endermite.togglepvp.util.PluginMessages;
Expand All @@ -25,10 +25,10 @@ public void onPlayerAttack(org.bukkit.event.entity.EntityDamageByEntityEvent eve
Player damager = (Player) damagerEntity;
Player victim = (Player) victimEntity;

ConfigCache config = TogglePvP.getPlugin().getConfigCache();
ConfigCache config = TogglePvp.getPlugin().getConfigCache();

boolean damagerPvpEnabled = TogglePvP.getPlugin().getPlayerManager().getPlayerPvPState(damager.getUniqueId());
boolean victimPvpEnabled = TogglePvP.getPlugin().getPlayerManager().getPlayerPvPState(victim.getUniqueId());
boolean damagerPvpEnabled = TogglePvp.getPlugin().getPlayerManager().getPlayerPvPState(damager.getUniqueId());
boolean victimPvpEnabled = TogglePvp.getPlugin().getPlayerManager().getPlayerPvPState(victim.getUniqueId());

if (!damagerPvpEnabled) {
event.setCancelled(true);
Expand Down
Loading

0 comments on commit 8626d79

Please sign in to comment.