Skip to content

Commit

Permalink
Merge pull request #271 from Nuytemans-Dieter/v4.0.1
Browse files Browse the repository at this point in the history
V4.0.1
  • Loading branch information
Nuytemans-Dieter authored Jan 8, 2022
2 parents bc9b23f + 633349a commit 3b07fbe
Show file tree
Hide file tree
Showing 21 changed files with 156 additions and 69 deletions.
18 changes: 1 addition & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
<version>5.0.1</version>
</dependency>


<!-- SPIGOT -->
<dependency>
<groupId>org.spigotmc</groupId>
Expand All @@ -85,22 +84,6 @@
<version>1.1.0</version>
</dependency>

<!-- BUNGEECORD -->
<!-- <dependency>-->
<!-- <groupId>net.md-5</groupId>-->
<!-- <artifactId>bungeecord-api</artifactId>-->
<!-- <version>1.14-SNAPSHOT</version>-->
<!-- <type>jar</type>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>net.md-5</groupId>-->
<!-- <artifactId>bungeecord-api</artifactId>-->
<!-- <version>1.14-SNAPSHOT</version>-->
<!-- <type>javadoc</type>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->

<!-- ESSENTIALS -->
<dependency>
<groupId>net.ess3</groupId>
Expand Down Expand Up @@ -190,6 +173,7 @@
<include>org.bstats:*</include>
<include>com.github.BetterPluginsSpigot:*</include>
<include>com.google.inject:*</include>
<include>javax.inject:*</include>
<include>aopalliance:aopalliance:*</include>
</includes>
</artifactSet>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
import org.bukkit.ChatColor;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.logging.Level;

public class BetterSleeping extends JavaPlugin implements IReloadable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package be.betterplugins.bettersleeping.commands;

import be.betterplugins.bettersleeping.listeners.BuffsHandler;
import be.betterplugins.bettersleeping.model.BypassChecker;
import be.betterplugins.bettersleeping.model.permissions.BypassChecker;
import be.betterplugins.bettersleeping.util.Theme;
import be.betterplugins.core.commands.shortcuts.PlayerBPCommand;
import be.betterplugins.core.messaging.messenger.Messenger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import be.betterplugins.bettersleeping.listeners.BuffsHandler;
import be.betterplugins.bettersleeping.messaging.ScreenMessenger;
import be.betterplugins.bettersleeping.model.sleeping.SleepWorldManager;
import be.betterplugins.bettersleeping.model.BypassChecker;
import be.betterplugins.bettersleeping.model.permissions.BypassChecker;
import be.betterplugins.bettersleeping.util.FileLogger;
import be.betterplugins.bettersleeping.util.Theme;
import be.betterplugins.core.CoreFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import be.betterplugins.bettersleeping.animation.ZZZAnimation;
import be.betterplugins.bettersleeping.animation.location.PlayerSleepLocation;
import be.betterplugins.bettersleeping.api.BecomeDayEvent;
import be.betterplugins.bettersleeping.model.ConfigContainer;
import be.betterplugins.core.interfaces.IReloadable;
import be.betterplugins.core.messaging.logging.BPLogger;
import com.google.inject.Inject;
Expand All @@ -24,22 +25,33 @@
public class AnimationHandler implements Listener, IReloadable
{

private final boolean isEnabled;

private final Map<UUID, ZZZAnimation> sleepingAnimations;
private final JavaPlugin plugin;

private final BPLogger logger;

@Inject
public AnimationHandler(JavaPlugin plugin, BPLogger logger)
public AnimationHandler(JavaPlugin plugin, ConfigContainer configContainer, BPLogger logger)
{
this.plugin = plugin;
this.logger = logger;

this.isEnabled = configContainer.getConfig().getBoolean("enable_animations");

this.logger.log(Level.FINE, "Are animations enabled? " + this.isEnabled);

this.sleepingAnimations = new HashMap<>();
}

public void startSleepingAnimation(Player player)
{
if (!isEnabled) {
this.logger.log(Level.FINEST,"Attempted to start a sleeping animation but they were disabled");
return;
}

this.logger.log(Level.FINEST, "Starting animation for player " + player.getName());

// Start animations
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package be.betterplugins.bettersleeping.listeners;

import be.betterplugins.bettersleeping.model.BypassChecker;
import be.betterplugins.bettersleeping.model.permissions.BypassChecker;
import be.betterplugins.bettersleeping.model.ConfigContainer;
import be.betterplugins.bettersleeping.model.SleepStatus;
import be.betterplugins.bettersleeping.model.sleeping.SleepWorldManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import be.betterplugins.bettersleeping.api.BecomeDayEvent;
import be.betterplugins.bettersleeping.model.ConfigContainer;
import be.betterplugins.bettersleeping.model.BypassChecker;
import be.betterplugins.bettersleeping.model.permissions.BypassChecker;
import be.betterplugins.core.messaging.logging.BPLogger;
import be.betterplugins.core.messaging.messenger.Messenger;
import be.betterplugins.core.messaging.messenger.MsgEntry;
Expand Down Expand Up @@ -130,7 +130,7 @@ private void giveEffects(List<Player> players, Set<PotionEffect> effects, List<S
*/
private Set<PotionEffect> readPotions(FileConfiguration config, String section)
{
Set<PotionEffect> potions = new HashSet<PotionEffect>();
Set<PotionEffect> potions = new HashSet<>();

// Prevent reading faulty config
if ( ! config.isConfigurationSection(section) )
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package be.betterplugins.bettersleeping.messaging;

import be.betterplugins.bettersleeping.model.BypassChecker;
import be.betterplugins.bettersleeping.util.Theme;
import be.betterplugins.core.messaging.logging.BPLogger;
import be.betterplugins.core.messaging.messenger.Messenger;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -29,10 +27,7 @@ public ScreenMessenger(JavaPlugin plugin, Map<String, String> messages, String p
// Register creation and deletion of messengers for every player
plugin.getServer().getPluginManager().registerEvents(new PlayerQueueEventListener(), plugin);
// Create messenger for every player online (e.g. in case of reload)
plugin.getServer().getOnlinePlayers().forEach(player ->
{
playerMessengerMap.put(player, new ScreenMessageSender(plugin, player));
});
plugin.getServer().getOnlinePlayers().forEach(player -> playerMessengerMap.put(player, new ScreenMessageSender(plugin, player)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package be.betterplugins.bettersleeping.model;
package be.betterplugins.bettersleeping.model.permissions;

import be.betterplugins.bettersleeping.hooks.EssentialsHook;
import be.betterplugins.bettersleeping.model.ConfigContainer;
import be.betterplugins.core.messaging.logging.BPLogger;
import com.google.inject.Inject;
import com.google.inject.Singleton;
Expand All @@ -18,11 +19,13 @@ public class BypassChecker
{

private final EssentialsHook essentialsHook;
private final PermissionsCache permissionsCache;
private final Set<GameMode> bypassedGameModes;

@Inject
public BypassChecker(ConfigContainer config, EssentialsHook essentialsHook, BPLogger logger)
public BypassChecker(ConfigContainer config, PermissionsCache permissionsCache, EssentialsHook essentialsHook, BPLogger logger)
{
this.permissionsCache = permissionsCache;
YamlConfiguration bypassConfig = config.getBypassing();

// Get all bypassed gamemodes from the config file
Expand Down Expand Up @@ -64,7 +67,7 @@ public Set<GameMode> getBypassedGameModes()
public boolean isPlayerBypassed(Player player)
{
// Permission based bypassing
boolean hasBSBypass = player.hasPermission("bettersleeping.bypass");
boolean hasBSBypass = permissionsCache.hasPermission(player, "bettersleeping.bypass");

// Gamemode based bypassing
boolean gamemodeBypass = bypassedGameModes.contains( player.getGameMode() );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package be.betterplugins.bettersleeping.model.permissions;

import be.betterplugins.bettersleeping.messaging.ScreenMessenger;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Singleton
public class PermissionsCache implements Listener
{

Map<UUID, Map<String, Boolean>> permissionsCacheMap;

@Inject
public PermissionsCache(JavaPlugin plugin)
{
this.permissionsCacheMap = new HashMap<>();

// Argument may be null in unit tests
if (plugin != null)
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

public boolean hasPermission(Player player, String permission)
{
Map<String, Boolean> permissionMap = permissionsCacheMap.get(player.getUniqueId());

if (permissionMap == null)
{
permissionMap = new HashMap<>();
}

if (!permissionMap.containsKey(permission))
{
permissionMap.put(permission, player.hasPermission(permission));
}

permissionsCacheMap.put(player.getUniqueId(), permissionMap);

return permissionMap.get(permission);
}

@EventHandler
public void onPlayerLeave (PlayerQuitEvent playerQuitEvent)
{
this.permissionsCacheMap.remove(playerQuitEvent.getPlayer().getUniqueId());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package be.betterplugins.bettersleeping.model.sleeping;

import be.betterplugins.bettersleeping.model.BypassChecker;
import be.betterplugins.bettersleeping.model.permissions.BypassChecker;
import be.betterplugins.bettersleeping.sleepersneeded.AbsoluteNeeded;
import be.betterplugins.core.messaging.logging.BPLogger;
import be.betterplugins.bettersleeping.sleepersneeded.ISleepersCalculator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package be.betterplugins.bettersleeping.model.sleeping;

import be.betterplugins.bettersleeping.listeners.AnimationHandler;
import be.betterplugins.bettersleeping.model.BypassChecker;
import be.betterplugins.bettersleeping.model.permissions.BypassChecker;
import be.betterplugins.bettersleeping.model.ConfigContainer;
import be.betterplugins.bettersleeping.model.SleepStatus;
import be.betterplugins.bettersleeping.runnables.SleepRunnable;
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/templates/buffs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

# To reset a BetterSleeping config file, you can simply delete it and perform /bs reload or restart the server

# Configuration guide and default values: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/v3.0.0/src/main/resources/buffs.yml
# All default config files can be found here: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/v3.0.0/src/main/resources
# Default values: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/BetterSleeping-v4/src/main/resources/buffs.yml
# Config template: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/BetterSleeping-v4/src/main/resources/templates/buffs.yml
# All default config files can be found here: https://github.com/Nuytemans-Dieter/BetterSleeping/tree/BetterSleeping-v4/src/main/resources

# These are effects that will be applied after sleeping, only on the players that have slept
# The idea is to give a well rested bonus so sleeping is encouraged and more people will try to sleep
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/templates/bypassing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

# To reset a BetterSleeping config file, you can simply delete it and perform /bs reload or restart the server

# Configuration guide and default values: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/v3.0.0/src/main/resources/bypassing.yml
# All default config files can be found here: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/v3.0.0/src/main/resources
# Default values: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/BetterSleeping-v4/src/main/resources/bypassing.yml
# Config template: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/BetterSleeping-v4/src/main/resources/templates/bypassing.yml
# All default config files can be found here: https://github.com/Nuytemans-Dieter/BetterSleeping/tree/BetterSleeping-v4/src/main/resources

# Players with bypass permissions WILL be counted towards the sleeping count
# They can get buffs if they sleep but not sleeping won't give them debuffs
Expand Down
7 changes: 4 additions & 3 deletions src/main/resources/templates/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
# The progress of the next release can be found here: https://github.com/Nuytemans-Dieter/BetterSleeping/milestones
# Considered buying the premium version? (Spigot account needed)https://www.spigotmc.org/resources/bettersleeping-premium-1-13-1-16.78951/

# Configuration guide and default values: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/v3.0.0/src/main/resources/config.yml
# All default config files can be found here: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/v3.0.0/src/main/resources
# Default values: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/BetterSleeping-v4/src/main/resources/config.yml
# Config template: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/BetterSleeping-v4/src/main/resources/templates/config.yml
# All default config files can be found here: https://github.com/Nuytemans-Dieter/BetterSleeping/tree/BetterSleeping-v4/src/main/resources

# After changing this option, please delete 'lang.yml' and do /bs reload or restart your server
# The default language file to be used
# Options: en-US (English), nl-BE (Dutch), es-ES (Spanish), pt-PT (Portuguese), de-DE.yml (German), fr-FR (French), Chinese (zh-ZH)
# Want to disable all messages? Set this to: "silent". It will load an empty config files and will disable most messages
# Up-to-date list here: https://github.com/Nuytemans-Dieter/BetterSleeping/tree/v3.0.0/src/main/resources/lang
# Up-to-date list here: https://github.com/Nuytemans-Dieter/BetterSleeping/tree/BetterSleeping-v4/src/main/resources/lang
language: {language}

# Choose where messages show up
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/templates/hooks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

# To reset a BetterSleeping config file, you can simply delete it and perform /bs reload or restart the server

# Default config values: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/v3.0.0/src/main/resources/hooks.yml
# All default config values: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/v3.0.0/src/main/resources
# Default values: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/BetterSleeping-v4/src/main/resources/hooks.yml
# Config template: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/BetterSleeping-v4/src/main/resources/templates/hooks.yml
# All default config files can be found here: https://github.com/Nuytemans-Dieter/BetterSleeping/tree/BetterSleeping-v4/src/main/resources


# Whether or not essentials afk players will be ignored by the required sleeping count
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/lang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


# Want to add new translations or fix existing ones? Please let us know on Discord (https://discord.gg/AS46VGT) or GitHub!
# All supported languages: https://github.com/Nuytemans-Dieter/BetterSleeping/tree/v3.0.0/src/main/resources/lang
# All supported languages: https://github.com/Nuytemans-Dieter/BetterSleeping/tree/BetterSleeping-v4/src/main/resources/lang

# CONFIGURATION GUIDE:

Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/templates/sleeping_settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

# To reset a BetterSleeping config file, you can simply delete it and perform /bs reload or restart the server

# Configuration guide and default values: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/v3.0.0/src/main/resources/sleeping_settings.yml
# All default config files can be found here: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/v3.0.0/src/main/resources
# Default values: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/BetterSleeping-v4/src/main/resources/sleeping_settings.yml
# Config template: https://github.com/Nuytemans-Dieter/BetterSleeping/blob/BetterSleeping-v4/src/main/resources/templates/sleeping_settings.yml
# All default config files can be found here: https://github.com/Nuytemans-Dieter/BetterSleeping/tree/BetterSleeping-v4/src/main/resources

# Choose an option from below (percentage or absolute)
# With percentage, a relative amount of players needs to sleep (eg. needed: 30 -> 13 online players, 30% of 13 is 3,9. Will be rounded to the nearest number -> 4 sleeping players needed)
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/mock/MockPermissionsCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package mock;

import be.betterplugins.bettersleeping.model.permissions.PermissionsCache;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashSet;

public class MockPermissionsCache extends PermissionsCache
{

HashSet<Player> playersWithBypassPermission;

public MockPermissionsCache(JavaPlugin plugin, HashSet<Player> playersWithBypassPermission)
{
super(plugin);
this.playersWithBypassPermission = playersWithBypassPermission;
}

@Override
public boolean hasPermission(Player player, String permission)
{
return playersWithBypassPermission.contains(player);
}
}
Loading

0 comments on commit 3b07fbe

Please sign in to comment.