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
+27 -6
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;
}
}
@@ -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;
}
@@ -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