Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK-4957 reworked storage of last location game mode and visibility … #11

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<properties>
<!-- project version -->
<revision>0.7.5</revision>
<revision>0.8.0</revision>
<changelist>-SNAPSHOT</changelist>
</properties>

Expand Down
48 changes: 16 additions & 32 deletions src/main/java/nl/svenar/powercamera/CameraHandler.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package nl.svenar.powercamera;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import nl.svenar.powercamera.data.CameraMode;
import nl.svenar.powercamera.data.PlayerCameraData;
Expand All @@ -16,14 +12,16 @@
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
@SuppressWarnings({"PMD.AvoidCatchingGenericException", "PMD.AvoidDuplicateLiterals", "PMD.AvoidLiteralsInIfCondition", "PMD.AvoidReassigningParameters", "PMD.CallSuperInConstructor", "PMD.CognitiveComplexity", "PMD.CommentRequired", "PMD.CyclomaticComplexity", "PMD.GodClass", "PMD.LiteralsFirstInComparisons", "PMD.LocalVariableCouldBeFinal", "PMD.LooseCoupling", "PMD.MethodArgumentCouldBeFinal", "PMD.NPathComplexity", "PMD.RedundantFieldInitializer", "PMD.UnnecessaryCast", "PMD.UseDiamondOperator", "PMD.UselessParentheses"})
public class CameraHandler extends BukkitRunnable {

private static final int SINGLE_FRAME_DURATION_MS = 50;

private int ticks = 0;

private final PowerCamera plugin;

private final Player player;
Expand All @@ -34,11 +32,7 @@ public class CameraHandler extends BukkitRunnable {

private final HashMap<Integer, ArrayList<String>> cameraPathCommands = new HashMap<>();

private GameMode previousGamemode;

private Location previousPlayerLocation;

private boolean previousInvisible;
private int ticks = 0;

public CameraHandler(PowerCamera plugin, Player player, String cameraName) {
this.plugin = plugin;
Expand Down Expand Up @@ -153,10 +147,7 @@ private double lerpYaw(double start, double end, float progress) { // Linear int
}

public CameraHandler start() {
this.previousGamemode = this.player.getGameMode();
this.previousPlayerLocation = this.player.getLocation();
this.previousInvisible = Util.isPlayerInvisible(this.player);

plugin.getConfigActiveCameras().setCameraActive(cameraName, player);
if (this.plugin.getConfigPlugin().getConfig().getBoolean("camera-effects.spectator-mode")) {
player.setGameMode(GameMode.SPECTATOR);
}
Expand All @@ -173,6 +164,7 @@ public CameraHandler start() {
if (!this.player.hasPermission("powercamera.hidestartmessages")) {
this.player.sendMessage(this.plugin.getPluginChatPrefix() + ChatColor.GREEN + "Viewing the path of camera '" + this.cameraName + "'!");
}

return this;
}

Expand All @@ -184,13 +176,9 @@ public CameraHandler stop() {
// ignored
}

player.teleport(previousPlayerLocation);
if (this.plugin.getConfigPlugin().getConfig().getBoolean("camera-effects.spectator-mode")) {
player.setGameMode(previousGamemode);
}
if (this.plugin.getConfigPlugin().getConfig().getBoolean("camera-effects.invisible")) {
player.setInvisible(previousInvisible);
}
final boolean resetGameMode = this.plugin.getConfigPlugin().getConfig().getBoolean("camera-effects.spectator-mode");
final boolean resetVisible = this.plugin.getConfigPlugin().getConfig().getBoolean("camera-effects.invisible");
plugin.getConfigActiveCameras().setCameraInactive(player, true, resetGameMode, resetVisible);

if (!this.player.hasPermission("powercamera.hidestartmessages")) {
player.sendMessage(plugin.getPluginChatPrefix() + ChatColor.GREEN + "The path of camera '" + cameraName + "' has ended!");
Expand Down Expand Up @@ -230,13 +218,11 @@ public void run() {
if (getCameraData().getCameraMode() == CameraMode.NONE) {
return;
}
player.teleport(previousPlayerLocation);
if (plugin.getConfigPlugin().getConfig().getBoolean("camera-effects.spectator-mode")) {
player.setGameMode(previousGamemode);
}
if (plugin.getConfigPlugin().getConfig().getBoolean("camera-effects.invisible")) {
player.setInvisible(previousInvisible);
}

final boolean resetGameMode = this.plugin.getConfigPlugin().getConfig().getBoolean("camera-effects.spectator-mode");
final boolean resetVisible = this.plugin.getConfigPlugin().getConfig().getBoolean("camera-effects.invisible");
plugin.getConfigActiveCameras().setCameraInactive(player, true, resetGameMode, resetVisible);

getCameraData().setCameraMode(CameraMode.NONE);
player.sendMessage(plugin.getPluginChatPrefix() + ChatColor.GREEN + "Preview ended!");

Expand Down Expand Up @@ -264,10 +250,8 @@ public CameraHandler preview(Player player, int num, int previewTime) {
player.sendMessage(plugin.getPluginChatPrefix() + ChatColor.GREEN + "Preview started of point " + (num + 1) + "!");
player.sendMessage(plugin.getPluginChatPrefix() + ChatColor.GREEN + "Ending in " + previewTime + " seconds.");

previousGamemode = player.getGameMode();
previousPlayerLocation = player.getLocation();
plugin.getConfigActiveCameras().setCameraActive(cameraName, player);
Location point = Util.deserializeLocation(cameraPoints.get(num).split(":", 3)[2]);
previousInvisible = player.isInvisible();

getCameraData().setCameraMode(CameraMode.PREVIEW);
if (this.plugin.getConfigPlugin().getConfig().getBoolean("camera-effects.spectator-mode")) {
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/nl/svenar/powercamera/PowerCamera.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package nl.svenar.powercamera;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import nl.svenar.powercamera.commands.MainCommand;
import nl.svenar.powercamera.config.ActiveCameraStorage;
import nl.svenar.powercamera.config.CameraStorage;
import nl.svenar.powercamera.config.PluginConfig;
import nl.svenar.powercamera.events.ChatTabExecutor;
Expand All @@ -19,6 +15,11 @@
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@SuppressFBWarnings({"DLS_DEAD_LOCAL_STORE", "MS_MUTABLE_COLLECTION_PKGPROTECT", "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"})
@SuppressWarnings({"PMD.AtLeastOneConstructor", "PMD.AvoidDuplicateLiterals", "PMD.AvoidReassigningLoopVariables", "PMD.CognitiveComplexity", "PMD.CommentRequired", "PMD.CyclomaticComplexity", "PMD.LocalVariableCouldBeFinal", "PMD.LooseCoupling", "PMD.UnnecessaryImport", "PMD.UseDiamondOperator"})
public class PowerCamera extends JavaPlugin {
Expand All @@ -27,13 +28,13 @@ public class PowerCamera extends JavaPlugin {

public static final List<String> DONATION_URLS = Arrays.asList("https://ko-fi.com/svenar", "https://patreon.com/svenar");

private PlayerCameraDataTracker playerCameraDataTracker;

// public Map<UUID, String> playerSelectedCamera = new HashMap<>(); // Selected camera name
// public Map<UUID, CameraMode> playerCameraMode = new HashMap<>(); // When the player is viewing the camera (/pc start & /pc preview)
// public Map<UUID, CameraHandler> playerCameraHandler = new HashMap<>(); // When the player is viewing the camera (/pc start & /pc preview)
public Instant powercameraStartTime = Instant.now();

private PlayerCameraDataTracker playerCameraDataTracker;

private PluginDescriptionFile pdf;

private String pluginChatPrefix = ChatColor.BLACK + "[" + ChatColor.AQUA + "%pluginName%" + ChatColor.BLACK + "] ";
Expand All @@ -42,6 +43,8 @@ public class PowerCamera extends JavaPlugin {

private CameraStorage configCameras;

private ActiveCameraStorage configActiveCameras;

private MainCommand mainCommand;

@Override
Expand Down Expand Up @@ -93,6 +96,7 @@ public String getPluginChatPrefix() {
private void setupConfig() {
configPlugin = new PluginConfig(this, "config.yml");
configCameras = new CameraStorage(this);
configActiveCameras = new ActiveCameraStorage(this);

configPlugin.getConfig().set("version", null);
configCameras.getConfig().set("version", null);
Expand Down Expand Up @@ -150,6 +154,10 @@ public CameraStorage getConfigCameras() {
return configCameras;
}

public ActiveCameraStorage getConfigActiveCameras() {
return configActiveCameras;
}

public MainCommand getMainCommand() {
return mainCommand;
}
Expand Down
24 changes: 7 additions & 17 deletions src/main/java/nl/svenar/powercamera/Util.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package nl.svenar.powercamera;

import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffectType;

import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
@SuppressWarnings({"PMD.CommentRequired", "PMD.FieldDeclarationsShouldBeAtStartOfClass", "PMD.LocalVariableCouldBeFinal", "PMD.MethodArgumentCouldBeFinal", "PMD.ShortClassName", "PMD.ShortVariable"})
public final class Util {

private Util() {
}

private static final Pattern REGEX_INT = Pattern.compile("^\\d+[^a-zA-Z]?$");

private static final Pattern REGEX_SECONDS = Pattern.compile("\\d+[sS]");
Expand All @@ -26,6 +21,9 @@ private Util() {

private static final Pattern REGEX_HOURS = Pattern.compile("\\d+[hH]");

private Util() {
}

public static String serializeLocation(Location loc) {
return loc.getWorld().getUID() + ";" + loc.getX() + ";" + loc.getY() + ";" + loc.getZ() + ";" + loc.getYaw() + ";" + loc.getPitch();
}
Expand Down Expand Up @@ -77,12 +75,4 @@ public static int timeStringToSecondsConverter(String timeInput) {

return seconds;
}

public static boolean isPlayerInvisible(Player player) {
try {
return player.isInvisible();
} catch (NoSuchMethodError e) {
return player.hasPotionEffect(PotionEffectType.INVISIBILITY);
}
}
}
Loading
Loading