Add Sensitivity and Recovery ranged permission

This commit is contained in:
Sn0wStorm 2021-04-12 20:20:52 +02:00
parent 7f0c4ad3c2
commit 3166f8c81b
5 changed files with 133 additions and 13 deletions

25
pom.xml
View File

@ -76,7 +76,7 @@
<version>3.2.0</version>
<executions>
<execution>
<phase>install</phase>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
@ -109,6 +109,7 @@
<url>http://nexus.hc.to/content/repositories/pub_releases</url>
</repository>
<repository>
<!-- WorldEdit, WorldGuard -->
<id>sk89q-repo</id>
<url>http://maven.sk89q.com/repo/</url>
</repository>
@ -165,6 +166,12 @@
<artifactId>VaultAPI</artifactId>
<version>1.6</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.sk89q</groupId>
@ -181,7 +188,7 @@
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>7.2.0-SNAPSHOT</version>
<version>7.2.4</version>
<scope>provided</scope>
<exclusions>
<exclusion>
@ -193,7 +200,7 @@
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-core</artifactId>
<version>7.2.0-SNAPSHOT</version>
<version>7.2.3</version>
<scope>provided</scope>
<exclusions>
<exclusion>
@ -219,11 +226,17 @@
<artifactId>LWCX</artifactId>
<version>2.2.6</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.TechFortress</groupId>
<artifactId>GriefPrevention</artifactId>
<version>16.16.0</version>
<version>16.17.1</version>
<scope>provided</scope>
</dependency>
<dependency>
@ -241,7 +254,7 @@
<dependency>
<groupId>com.github.TheBusyBiscuit</groupId>
<artifactId>Slimefun4</artifactId>
<version>RC-17</version>
<version>RC-21</version>
<scope>provided</scope>
</dependency>
<!--<dependency>
@ -278,7 +291,7 @@
<dependency>
<groupId>nl.rutgerkok</groupId>
<artifactId>blocklocker</artifactId>
<version>1.9</version>
<version>1.9.2</version>
<scope>provided</scope>
</dependency>
<dependency>

View File

@ -129,3 +129,11 @@ permissions:
description: Will despite config-setting not be kicked on overdrink
brewery.bypass.teleport:
description: Will despite config-setting not be teleported on login
# -- Modifiers --
brewery.sensitive.100:
description: How sensitive to alcohol the player is in percent
brewery.recovery.2:
description: How quickly the player will naturally reduce his drunkeness per minute

View File

@ -8,6 +8,7 @@ import com.dre.brewery.filedata.BConfig;
import com.dre.brewery.lore.BrewLore;
import com.dre.brewery.recipe.BEffect;
import com.dre.brewery.utility.BUtil;
import com.dre.brewery.utility.PermissionUtil;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.apache.commons.lang.mutable.MutableInt;
@ -44,6 +45,7 @@ public class BPlayer {
private int quality = 0;// = quality of drunkeness * drunkeness
private int drunkeness = 0;// = amount of drunkeness
private int offlineDrunk = 0;// drunkeness when gone offline
private int alcRecovery = -1; // Drunkeness reduce per minute
private Vector push = new Vector(0, 0, 0);
private int time = 20;
@ -160,6 +162,7 @@ public class BPlayer {
if (bPlayer == null) {
bPlayer = addPlayer(player);
}
// In this event the added alcohol amount is calculated, based on the sensitivity permission
BrewDrinkEvent drinkEvent = new BrewDrinkEvent(brew, meta, player, bPlayer);
if (meta != null) {
P.p.getServer().getPluginManager().callEvent(drinkEvent);
@ -348,7 +351,7 @@ public class BPlayer {
}
// drain the drunkeness by amount, returns true when player has to be removed
public boolean drain(Player player, int amount) {
public boolean drain(@Nullable Player player, int amount) {
if (drunkeness > 0) {
quality -= getQuality() * amount;
}
@ -528,6 +531,16 @@ public class BPlayer {
}
}
public void recalculateAlcRecovery(@Nullable Player player) {
setAlcRecovery(2);
if (player != null) {
int rec = PermissionUtil.getAlcRecovery(player);
if (rec > -1) {
setAlcRecovery(rec);
}
}
}
// #### Puking ####
@ -806,17 +819,18 @@ public class BPlayer {
// decreasing drunkeness over time
public static void onUpdate() {
if (!players.isEmpty()) {
int soberPerMin = 2;
Iterator<Map.Entry<String, BPlayer>> iter = players.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, BPlayer> entry = iter.next();
String uuid = entry.getKey();
BPlayer bplayer = entry.getValue();
if (bplayer.drunkeness == soberPerMin) {
// Prevent 0 drunkeness
soberPerMin++;
Player playerIfOnline = BUtil.getPlayerfromString(uuid);
if (bplayer.getAlcRecovery() == -1) {
bplayer.recalculateAlcRecovery(playerIfOnline);
}
if (bplayer.drain(BUtil.getPlayerfromString(uuid), soberPerMin)) {
if (bplayer.drain(playerIfOnline, bplayer.getAlcRecovery())) {
iter.remove();
if (BConfig.sqlDrunkSync && BConfig.sqlSync != null) {
BConfig.sqlSync.removePlayer(UUID.fromString(uuid));
@ -902,4 +916,11 @@ public class BPlayer {
return offlineDrunk;
}
public int getAlcRecovery() {
return alcRecovery;
}
public void setAlcRecovery(int alcRecovery) {
this.alcRecovery = alcRecovery;
}
}

View File

@ -2,10 +2,12 @@ package com.dre.brewery.api.events.brew;
import com.dre.brewery.BPlayer;
import com.dre.brewery.Brew;
import com.dre.brewery.utility.PermissionUtil;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
/**
@ -25,10 +27,30 @@ public class BrewDrinkEvent extends BrewEvent implements Cancellable {
super(brew, meta);
this.player = player;
this.bPlayer = bPlayer;
alc = brew.getOrCalcAlc();
alc = calcAlcWSensitivity(brew.getOrCalcAlc());
quality = brew.getQuality();
}
/**
* Calculate the Alcohol to add to the player using his sensitivity permission (if existing)
*
* <p>If the player has been given the brewery.sensitive.xx permission, will factor in the sensitivity to the given alcohol amount.
* <p>Will return the calculated value without changing the event
*
* @param alc The base amount of alcohol
* @return The amount of alcohol given the players alcohol-sensitivity
*/
@Contract(pure = true)
public int calcAlcWSensitivity(int alc) {
int sensitive = PermissionUtil.getDrinkSensitive(player);
if (sensitive == 0) {
alc = 0;
} else if (sensitive > 0) {
alc *= ((float) sensitive) / 100f;
}
return alc;
}
public Player getPlayer() {
return player;
}

View File

@ -1,9 +1,13 @@
package com.dre.brewery.utility;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permissible;
import org.bukkit.permissions.PermissionAttachmentInfo;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class PermissionUtil {
@ -76,6 +80,58 @@ public class PermissionUtil {
return sender.hasPermission(bPerm.permission);
}
/**
* Returns the Sensitivity of the Player towards Alcohol in percent.
* <p>Sensitivity describes how much of the alcohol gets transferred to the players drunkeness
*
* <p>100 means normal alcohol sensitivity
* <p>less than 100 means less alcohol gets added.
* <p>more than 100 means more alcohol gets added.
* <p>0 means no alcohol gets added.
*
* @param player The player of whom to get the sensitivity of
* @return The Players alcohol sensitivity
*/
public static int getDrinkSensitive(Permissible player) {
return getRangedPermission(player, "brewery.sensitive.");
}
/**
* Returns the Alcohol recovery rate of the player in drunkeness per minute.
* <p>The default is 2 drunkeness per minute
*
*
* @param player The player of whom to get the recovery rate of
* @return The Players alcohol recovery rate
*/
public static int getAlcRecovery(Permissible player) {
return getRangedPermission(player, "brewery.recovery.");
}
/**
* Get the number behind the given permission
* <p>i.e. for brewery.sensitive.100 it returns 100
*
* @param player The player to get the ranged permission of
* @param subPermission The permission string before the number
* @return The permission number as int
*/
public static int getRangedPermission(Permissible player, String subPermission) {
Optional<PermissionAttachmentInfo> found = player.getEffectivePermissions().stream().
filter(x -> x.getPermission().startsWith(subPermission)).
findFirst();
if (found.isPresent()) {
String permission = found.get().getPermission();
int lastDot = permission.lastIndexOf('.');
int value = NumberUtils.toInt(permission.substring(lastDot + 1), -1);
if (value >= 0) {
return value;
}
}
return -1;
}
/**
* Brewery Permissions of _only_ the Commands