Skip to content
This repository has been archived by the owner on Mar 18, 2022. It is now read-only.

Commit

Permalink
Some tests, clean up, and mobs config value
Browse files Browse the repository at this point in the history
  • Loading branch information
ohowe1 committed Jun 1, 2021
1 parent 48dbc6b commit 9547d40
Show file tree
Hide file tree
Showing 24 changed files with 313 additions and 380 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ out/
## Eclipse
##############################
.settings/
bin/
tmp/
.metadata
.classpath
Expand All @@ -65,7 +64,6 @@ local.properties
## NetBeans
##############################
nbproject/private/
build/
nbbuild/
dist/
nbdist/
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/me/ohowe12/spectatormode/SpectatorMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@
import dev.jorel.commandapi.arguments.IntegerArgument;
import dev.jorel.commandapi.arguments.PlayerArgument;
import me.ohowe12.spectatormode.context.SpectatorContextCalculator;
import me.ohowe12.spectatormode.listener.*;
import me.ohowe12.spectatormode.util.*;
import me.ohowe12.spectatormode.listener.OnCommandPreprocessListener;
import me.ohowe12.spectatormode.listener.OnLogOnListener;
import me.ohowe12.spectatormode.listener.OnMoveListener;
import me.ohowe12.spectatormode.util.ConfigManager;
import me.ohowe12.spectatormode.util.Logger;
import me.ohowe12.spectatormode.util.Messenger;
import me.ohowe12.spectatormode.util.UpdateChecker;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile;
Expand Down Expand Up @@ -104,7 +107,7 @@ private void initializeLuckPermsContext() {
pluginLogger.debugLog("LuckPerms not enabled");
return;
}
SpectatorContextCalculator.initalizeSpectatorContext(this);
SpectatorContextCalculator.initializeSpectatorContext(this);
}

private void addMetrics() {
Expand Down Expand Up @@ -170,8 +173,9 @@ public void registerCommands() {
spectatorManager.togglePlayer((Player) args[0], true);
});

CommandAPICommand mainCommand = new CommandAPICommand("s").withAliases("smps").withPermission("smpspectator.use").executesPlayer((player,
args) -> {
CommandAPICommand mainCommand = new CommandAPICommand("s").withAliases("smps").withPermission("smpspectator" +
".use").executesPlayer((player,
args) -> {
spectatorManager.togglePlayer(player);
}).withSubcommand(enableCommand).withSubcommand(disableCommand).withSubcommand(reloadCommand).withSubcommand(effectCommand).withSubcommand(forceCommand);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,32 @@

package me.ohowe12.spectatormode.context;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;

import me.ohowe12.spectatormode.SpectatorMode;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.context.ContextCalculator;
import net.luckperms.api.context.ContextConsumer;
import net.luckperms.api.context.ContextSet;
import net.luckperms.api.context.ImmutableContextSet;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;

public class SpectatorContextCalculator implements ContextCalculator<Player> {

private final SpectatorMode plugin;

public static void initalizeSpectatorContext(SpectatorMode plugin) {
public SpectatorContextCalculator(SpectatorMode plugin) {
this.plugin = plugin;
}

public static void initializeSpectatorContext(SpectatorMode plugin) {
RegisteredServiceProvider<LuckPerms> provider = Bukkit.getServicesManager().getRegistration(LuckPerms.class);
if (provider != null) {
LuckPerms api = provider.getProvider();
api.getContextManager().registerCalculator(new SpectatorContextCalculator(plugin));
}
}

public SpectatorContextCalculator(SpectatorMode plugin) {
this.plugin = plugin;
}

@Override
public void calculate(Player target, ContextConsumer contextConsumer) {
contextConsumer.accept("SMP Spectator",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

package me.ohowe12.spectatormode.listener;

import me.ohowe12.spectatormode.util.Messenger;
import me.ohowe12.spectatormode.SpectatorMode;
import me.ohowe12.spectatormode.util.Messenger;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -50,13 +50,14 @@ public void onCommandEvent(final PlayerCommandPreprocessEvent e) {
return;
}

String rawCommand = e.getMessage().substring(1).split(" ")[0]; // /back -> back || /essentials:back -> essentials:back
String rawCommand = e.getMessage().substring(1).split(" ")[0]; // /back -> back || /essentials:back ->
// essentials:back
String[] splited = rawCommand.split(":");

rawCommand = splited[splited.length - 1];
if (plugin.getConfigManager().getList("bad-commands")
.contains(rawCommand)) {
Messenger.send(player,"bad-command-message");
.contains(rawCommand)) {
Messenger.send(player, "bad-command-message");
e.setCancelled(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@

import me.ohowe12.spectatormode.SpectatorMode;
import me.ohowe12.spectatormode.util.Messenger;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand Down Expand Up @@ -76,6 +74,9 @@ private boolean shouldCancelMoveEvent(PlayerMoveEvent e) {

private boolean outsideWorldBorder(PlayerMoveEvent e) {
Location location = e.getTo();
if (plugin.isUnitTest()) {
return false;
}
return !location.getWorld().getWorldBorder().isInside(location);
}

Expand Down
43 changes: 25 additions & 18 deletions src/main/java/me/ohowe12/spectatormode/state/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package me.ohowe12.spectatormode.state;

import me.ohowe12.spectatormode.SpectatorMode;
import me.ohowe12.spectatormode.util.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
Expand All @@ -43,8 +44,8 @@
@SuppressWarnings("unchecked")
public class State {

private Player player;
private final SpectatorMode plugin;
private Player player;
private Location playerLocation;
private int fireTicks;
private ArrayList<PotionEffect> potionEffects;
Expand All @@ -59,8 +60,8 @@ public State(@NotNull Player player, @NotNull SpectatorMode plugin) {
fireTicks = player.getFireTicks();
potionEffects = new ArrayList<>(player.getActivePotionEffects());
waterBubbles = player.getRemainingAir();
if (!plugin.isUnitTest())
prepareMobs();

prepareMobs();
}

public State(@NotNull Map<String, Object> serialized, @NotNull SpectatorMode plugin) {
Expand Down Expand Up @@ -91,27 +92,33 @@ public Map<String, Boolean> getMobIds() {
public Player getPlayer() {
return player;
}
public UUID getPlayerUUID() {
return player.getUniqueId();
}

public void resetPlayer(Player player) {
player.teleport(getPlayerLocation());
player.setFireTicks(getFireTicks());
player.setFireTicks(getFireTicks());
player.addPotionEffects(getPotionEffects());
player.setRemainingAir(getWaterBubbles());
}

private void deserialize(@NotNull Map<String, Object> serialized) {
playerLocation = (Location) serialized.get("Location");
fireTicks = (int) serialized.get("Fire ticks");
potionEffects = (ArrayList<PotionEffect>) serialized.get("Potions");
waterBubbles = (int) serialized.get("Water bubbles");
mobIds = (Map<String, Boolean>) serialized.get("Mobs");

try {
playerLocation = (Location) serialized.get("Location");
fireTicks = (int) serialized.get("Fire ticks");
potionEffects = (ArrayList<PotionEffect>) serialized.get("Potions");
waterBubbles = (int) serialized.get("Water bubbles");
mobIds = (Map<String, Boolean>) serialized.get("Mobs");
} catch (ClassCastException exception) {
plugin.getPluginLogger().log(Logger.ANSI_RED + "There has been an error with your data.yml file!\nYou can" +
" either fix this your self my removing the file and letting it regenerate itself, manually " +
"fixing everything inside of it, or join the discord server for help. Please provide this error message with it:");
throw exception;
}
}

private void prepareMobs() {
if (!plugin.getConfigManager().getBoolean("mobs") || plugin.isUnitTest()) {
return;
}
World world = player.getWorld();
Chunk defaultChunk = world.getChunkAt(getPlayerLocation());
for (int x = 0; x <= 4; x++) {
Expand All @@ -127,11 +134,11 @@ private void processMobChunk(Chunk chunk) {
}
}

private void checkAndAddEntity(Entity e) {
if (e instanceof LivingEntity) {
private void checkAndAddEntity(Entity entity) {
if (entity instanceof LivingEntity) {
@NotNull
LivingEntity living = (LivingEntity) e;
if (e instanceof Player) {
LivingEntity living = (LivingEntity) entity;
if (entity instanceof Player) {
return;
}
if (living.getRemoveWhenFarAway()) {
Expand All @@ -154,7 +161,7 @@ private void addLivingEntity(LivingEntity living) {
}

public void unPrepareMobs() {
if (plugin.isUnitTest()) {
if (plugin.getConfigManager().getBoolean("mobs") || plugin.isUnitTest()) {
return;
}
@NotNull
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/me/ohowe12/spectatormode/state/StateHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public void removePlayer(Player player) {
public Set<String> allPlayersInState() {
return stateMap.keySet();
}

public Collection<State> allStates() {
return stateMap.values();
}
Expand All @@ -90,8 +91,7 @@ public void save() {
}
});
}
for (@NotNull
final Map.Entry<String, State> entry : stateMap.entrySet()) {
for (@NotNull final Map.Entry<String, State> entry : stateMap.entrySet()) {
dataFile.set("data." + entry.getKey(), entry.getValue().serialize());
}
try {
Expand All @@ -109,17 +109,18 @@ public void load() {
Objects.requireNonNull(dataFile.getConfigurationSection("data")).getKeys(false).forEach(key -> {
final Map<String, Object> value = new HashMap<>();

@SuppressWarnings("unchecked")
final ArrayList<PotionEffect> potions = (ArrayList<PotionEffect>) dataFile
@SuppressWarnings("unchecked") final ArrayList<PotionEffect> potions =
(ArrayList<PotionEffect>) dataFile
.getList("data." + key + ".Potions");
value.put("Potions", potions);

final int waterBubbles = dataFile.getInt("data." + key + ".Water bubbles");
value.put("Water bubbles", waterBubbles);

final Map<String, Boolean> mobs = new HashMap<>();
Objects.requireNonNull(dataFile.getConfigurationSection("data." + key + ".Mobs")).getKeys(false)
.forEach(mobKey -> mobs.put(mobKey, dataFile.getBoolean("data." + key + ".Mobs" + mobKey)));
for (String mobKey : dataFile.getConfigurationSection("data." + key + ".Mobs").getKeys(false)) {
mobs.put(mobKey, dataFile.getBoolean("data." + key + ".Mobs" + mobKey));
}
value.put("Mobs", mobs);

final int fireTicks = dataFile.getInt("data." + key + ".Fire ticks");
Expand All @@ -128,12 +129,6 @@ public void load() {
final Location location = dataFile.getLocation("data." + key + ".Location");
value.put("Location", location);

String placeHolder = dataFile.getString("data." + key + ".PlaceholderUUID");
value.put("PlaceholderUUID", placeHolder);

boolean needsMob = dataFile.getBoolean("data." + key + "NeedsMob");
value.put("NeedsMob", needsMob);

stateMap.put(key, new State(value, plugin));
});
} catch (final NullPointerException ignored) {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/me/ohowe12/spectatormode/util/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@

package me.ohowe12.spectatormode.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import me.ohowe12.spectatormode.SpectatorMode;
import org.bukkit.ChatColor;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class ConfigManager {

private final FileConfiguration config;
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/me/ohowe12/spectatormode/util/Messenger.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@

public abstract class Messenger {

private static SpectatorMode plugin;

private Messenger() {

}

private static SpectatorMode plugin;

public static void init(@NotNull SpectatorMode plugin) {
Messenger.plugin = plugin;
}
Expand All @@ -56,7 +56,8 @@ public static void send(@NotNull CommandSender sender, @NotNull String msgkey, @
send(sender, sender, msgkey, extra);
}

public static void send(@NotNull CommandSender sender, @NotNull CommandSender target, @NotNull String msgkey, @NotNull String extra) {
public static void send(@NotNull CommandSender sender, @NotNull CommandSender target, @NotNull String msgkey,
@NotNull String extra) {
String cfgmsg = Objects.requireNonNull(plugin.getConfigManager(), "Messenger not initialized")
.getColorizedString(msgkey)
.replace("/target/", target.getName());
Expand All @@ -66,6 +67,6 @@ public static void send(@NotNull CommandSender sender, @NotNull CommandSender ta
if (!(sender instanceof Player))
sender.sendMessage(cfgmsg);
else
((Player)sender).spigot().sendMessage(type, new TextComponent(cfgmsg));
((Player) sender).spigot().sendMessage(type, new TextComponent(cfgmsg));
}
}
15 changes: 8 additions & 7 deletions src/main/java/me/ohowe12/spectatormode/util/UpdateChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,28 @@

package me.ohowe12.spectatormode.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.Consumer;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;

public class UpdateChecker {

private UpdateChecker() {

}


public static void getVersion(final @NotNull Consumer<String> consumer, Plugin plugin) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try (InputStream inputStream = new URL(
"https://api.spigotmc.org/legacy/update.php?resource=77267")
.openStream(); Scanner scanner = new Scanner(inputStream)) {
"https://api.spigotmc.org/legacy/update.php?resource=77267")
.openStream(); Scanner scanner = new Scanner(inputStream)) {
if (scanner.hasNext()) {
consumer.accept(scanner.next());
}
Expand Down
Loading

0 comments on commit 9547d40

Please sign in to comment.