Skip to content

Commit

Permalink
Updated to 6f7f6c6
Browse files Browse the repository at this point in the history
  • Loading branch information
StarWishsama committed Feb 3, 2020
1 parent 59127df commit ae1c34c
Show file tree
Hide file tree
Showing 42 changed files with 4,227 additions and 2,785 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>me.mrCookieSlime</groupId>
<artifactId>Slimefun</artifactId>
<version>4.2-UNOFFCIAL-20200202</version>
<version>4.2-UNOFFCIAL-20200203</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down Expand Up @@ -127,7 +127,7 @@
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>7.0.1</version>
<version>7.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
11 changes: 0 additions & 11 deletions renovate.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package io.github.thebusybiscuit.slimefun4.api.player;

import java.util.Optional;
import java.util.OptionalInt;
import java.util.concurrent.TimeUnit;

import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;

import io.github.thebusybiscuit.cscorelib2.data.PersistentDataAPI;

/**
* A very simple API that is meant for adding/getting/clearing custom status effects
* to/from players.
*
* The effects are stored via {@link PersistentDataAPI} and use NBT data that is
* saved across server restarts.
*
* You can specify a level for your status effect too.
*
* @author TheBusyBiscuit
*
*/
public class StatusEffect implements Keyed {

private final NamespacedKey key;

public StatusEffect(NamespacedKey key) {
this.key = key;
}

@Override
public NamespacedKey getKey() {
return key;
}

/**
* This applies this {@link StatusEffect} to the given {@link Player}.
* You can specify a duration, this will reference
* {@link StatusEffect#add(Player, int, int, TimeUnit)} with a level of 1.
*
* @param p The {@link Player} whom to apply the effect to
* @param duration The duration of how long that status effect shall last
* @param unit The {@link TimeUnit} for the given duration
*/
public void add(Player p, int duration, TimeUnit unit) {
add(p, 1, duration, unit);
}

/**
* This applies this {@link StatusEffect} to the given {@link Player}.
*
* @param p The {@link Player} whom to apply the effect to
* @param level The level of this effect
* @param duration The duration of how long that status effect shall last
* @param unit The {@link TimeUnit} for the given duration
*/
public void add(Player p, int level, int duration, TimeUnit unit) {
PersistentDataAPI.setString(p, getKey(), level + ";" + System.currentTimeMillis() + unit.toMillis(duration));
}

/**
* This applies this {@link StatusEffect} to the given {@link Player}.
* This will apply it permanently, there is no duration.
*
* @param p The {@link Player} whom to apply the effect to
* @param level The level of this effect
*/
public void addPermanent(Player p, int level) {
PersistentDataAPI.setString(p, getKey(), level + ";0");
}

/**
* This will check whether this {@link StatusEffect} is currently applied
* to that {@link Player}.
* If the effect has expired, it will automatically remove all associated
* NBT data of this effect.
*
* @param p The {@link Player} to check for
* @return Whether this {@link StatusEffect} is currently applied
*/
public boolean isPresent(Player p) {
Optional<String> optional = PersistentDataAPI.getOptionalString(p, getKey());

if (optional.isPresent()) {
String[] data = optional.get().split(";");
long timestamp = Long.parseLong(data[1]);

if (timestamp == 0 || timestamp >= System.currentTimeMillis()) {
return true;
}
else {
clear(p);
return false;
}
}
else return false;
}

/**
* This method returns an {@link OptionalInt} describing the level of this status
* effect on that player.
*
* @param p The {@link Player} to check for
* @return An {@link OptionalInt} that describes the result
*/
public OptionalInt getLevel(Player p) {
Optional<String> optional = PersistentDataAPI.getOptionalString(p, getKey());

if (optional.isPresent()) {
String[] data = optional.get().split(";");
return OptionalInt.of(Integer.parseInt(data[0]));

}
else return OptionalInt.empty();
}

/**
* This will remove this {@link StatusEffect} from the given {@link Player}.
*
* @param p The {@link Player} to clear it from
*/
public void clear(Player p) {
PersistentDataAPI.remove(p, getKey());
}

}
Loading

0 comments on commit ae1c34c

Please sign in to comment.