mirror of
https://github.com/SimplexDevelopment/FeelingLucky.git
synced 2024-11-12 21:46:06 +00:00
Updates
This commit is contained in:
parent
dcd7f7401c
commit
831913c668
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -1,41 +0,0 @@
|
||||
package io.github.simplex.crumb;
|
||||
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Config extends YamlConfiguration {
|
||||
private final String CONFIG_NAME = "config.yml";
|
||||
|
||||
private final File dataFolder;
|
||||
private final Crumb plugin;
|
||||
private final File configFile;
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
public Config(Crumb plugin, boolean copyResource) {
|
||||
this.plugin = plugin;
|
||||
this.dataFolder = plugin.getDataFolder();
|
||||
if (!dataFolder.exists()) dataFolder.mkdirs();
|
||||
File file = new File(dataFolder, CONFIG_NAME);
|
||||
if (!file.exists()) {
|
||||
SneakyWorker.sneakyTry(() -> {
|
||||
file.createNewFile();
|
||||
plugin.saveResource(CONFIG_NAME, true);
|
||||
});
|
||||
}
|
||||
if (copyResource) plugin.saveResource(CONFIG_NAME, true);
|
||||
configFile = file;
|
||||
loadConfiguration(configFile);
|
||||
}
|
||||
|
||||
public void save() {
|
||||
SneakyWorker.sneakyTry(() -> super.save(configFile));
|
||||
}
|
||||
|
||||
public void load() {
|
||||
SneakyWorker.sneakyTry(() -> {
|
||||
super.load(configFile);
|
||||
});
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package io.github.simplex.crumb;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public final class Crumb extends JavaPlugin {
|
||||
public Config config;
|
||||
public PlayerHandler handler;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
config = new Config(this, false);
|
||||
handler = new PlayerHandler(this);
|
||||
this.getServer().getPluginManager().registerEvents(handler, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
config.save();
|
||||
// Plugin shutdown logic
|
||||
}
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
package io.github.simplex.crumb;
|
||||
|
||||
import org.bukkit.attribute.Attribute;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerLoginEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class PlayerHandler implements Listener {
|
||||
private final Map<Player, Luck> playerLuckMap = new HashMap<>();
|
||||
private final List<Player> markedPlayers = new ArrayList<>();
|
||||
private final Crumb plugin;
|
||||
|
||||
public PlayerHandler(Crumb plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public Luck getLuckContainer(Player player) {
|
||||
return playerLuckMap.get(player);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void initializePlayer(PlayerLoginEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
String uuid = player.getUniqueId().toString();
|
||||
ConfigurationSection identifier = plugin.config.getConfigurationSection(uuid);
|
||||
Luck luck = new Luck(player);
|
||||
if (identifier == null) {
|
||||
identifier = plugin.config.createSection(player.getUniqueId().toString());
|
||||
identifier.set("luck", luck.defaultValue());
|
||||
plugin.config.save();
|
||||
}
|
||||
luck.setValue(identifier.getDouble("luck"));
|
||||
playerLuckMap.put(player, luck);
|
||||
}
|
||||
|
||||
public void updatePlayer(Player player, Luck luck) {
|
||||
playerLuckMap.replace(player, luck);
|
||||
}
|
||||
|
||||
public void markPlayer(Player player) {
|
||||
markedPlayers.add(player);
|
||||
}
|
||||
|
||||
public void unmarkPlayer(Player player) {
|
||||
markedPlayers.remove(player);
|
||||
}
|
||||
|
||||
public boolean isMarked(Player player) {
|
||||
return markedPlayers.contains(player);
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package io.github.simplex.crumb;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
public class SneakyWorker {
|
||||
public static void sneakyTry(SneakyTry sneakyTry) {
|
||||
try {
|
||||
sneakyTry.tryThis();
|
||||
} catch (Exception ex) {
|
||||
Bukkit.getLogger().severe(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
interface SneakyTry {
|
||||
void tryThis() throws Exception;
|
||||
}
|
||||
}
|
29
src/main/java/io/github/simplex/luck/FeelingLucky.java
Normal file
29
src/main/java/io/github/simplex/luck/FeelingLucky.java
Normal file
@ -0,0 +1,29 @@
|
||||
package io.github.simplex.luck;
|
||||
|
||||
import io.github.simplex.luck.player.PlayerConfig;
|
||||
import io.github.simplex.luck.player.PlayerHandler;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class FeelingLucky extends JavaPlugin {
|
||||
public PlayerHandler handler;
|
||||
private static final List<PlayerConfig> configList = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
handler = new PlayerHandler(this);
|
||||
configList.forEach(PlayerConfig::load);
|
||||
this.getServer().getPluginManager().registerEvents(handler, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
configList.forEach(PlayerConfig::save);
|
||||
}
|
||||
|
||||
public static List<PlayerConfig> getConfigList() {
|
||||
return configList;
|
||||
}
|
||||
}
|
21
src/main/java/io/github/simplex/luck/SneakyWorker.java
Normal file
21
src/main/java/io/github/simplex/luck/SneakyWorker.java
Normal file
@ -0,0 +1,21 @@
|
||||
package io.github.simplex.luck;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
public class SneakyWorker {
|
||||
public static void sneakyTry(SneakyTry sneakyTry) {
|
||||
try {
|
||||
sneakyTry.tryThis();
|
||||
} catch (Exception ex) {
|
||||
String sb = "An error of type: "
|
||||
+ ex.getClass().getSimpleName()
|
||||
+ " has occurred. A cause will be printed. \n\n"
|
||||
+ ex.getCause();
|
||||
Bukkit.getLogger().severe(sb);
|
||||
}
|
||||
}
|
||||
|
||||
public interface SneakyTry {
|
||||
void tryThis() throws Exception;
|
||||
}
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
package io.github.simplex.crumb.listener;
|
||||
package io.github.simplex.luck.listener;
|
||||
|
||||
import io.github.simplex.crumb.Crumb;
|
||||
import io.github.simplex.crumb.Luck;
|
||||
import io.github.simplex.luck.FeelingLucky;
|
||||
import io.github.simplex.luck.player.Luck;
|
||||
import io.github.simplex.luck.player.PlayerHandler;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.Witch;
|
||||
@ -23,9 +23,9 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PlayerListener implements Listener {
|
||||
private final Crumb plugin;
|
||||
private final FeelingLucky plugin;
|
||||
|
||||
public PlayerListener(Crumb plugin) {
|
||||
public PlayerListener(FeelingLucky plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@ -36,9 +36,9 @@ public class PlayerListener implements Listener {
|
||||
return;
|
||||
}
|
||||
Player player = (Player) event.getEntity();
|
||||
Luck luck = plugin.handler.getLuckContainer(player);
|
||||
Luck luck = PlayerHandler.getLuckContainer(player);
|
||||
if (ListBox.acceptedCauses.contains(event.getCause())) {
|
||||
if (!luck.isDefault()) {
|
||||
if (luck.notDefault()) {
|
||||
double percentage = luck.getPercentage();
|
||||
if (luck.quickRNG(percentage)) {
|
||||
event.setCancelled(true);
|
||||
@ -49,7 +49,7 @@ public class PlayerListener implements Listener {
|
||||
}
|
||||
|
||||
if (ListBox.sideCauses.contains(event.getCause())) {
|
||||
if (!luck.isDefault()) {
|
||||
if (luck.notDefault()) {
|
||||
double percentage = luck.getPercentage();
|
||||
if (luck.quickRNG(percentage)) {
|
||||
event.setCancelled(true);
|
||||
@ -64,8 +64,8 @@ public class PlayerListener implements Listener {
|
||||
@EventHandler
|
||||
public void restoreHunger(PlayerItemConsumeEvent event) {
|
||||
ItemStack item = event.getItem();
|
||||
Luck luck = plugin.handler.getLuckContainer(event.getPlayer());
|
||||
if (!luck.isDefault()) {
|
||||
Luck luck = PlayerHandler.getLuckContainer(event.getPlayer());
|
||||
if (luck.notDefault()) {
|
||||
double percentage = luck.getPercentage();
|
||||
ListBox.foods.forEach(food -> {
|
||||
if (item.isSimilar(food)) {
|
||||
@ -82,7 +82,7 @@ public class PlayerListener implements Listener {
|
||||
Action action = event.getAction();
|
||||
ItemStack foot = new ItemStack(Material.RABBIT_FOOT);
|
||||
Player player = event.getPlayer();
|
||||
Luck luck = plugin.handler.getLuckContainer(player);
|
||||
Luck luck = PlayerHandler.getLuckContainer(player);
|
||||
if (action.isRightClick() && player.getInventory().getItemInMainHand().isSimilar(foot)) {
|
||||
double rng = luck.RNG().nextDouble(2.0, 5.0);
|
||||
player.getInventory().remove(player.getInventory().getItemInMainHand());
|
||||
@ -106,7 +106,7 @@ public class PlayerListener implements Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
Luck luck = plugin.handler.getLuckContainer(player);
|
||||
Luck luck = PlayerHandler.getLuckContainer(player);
|
||||
if (cause.equals(DamageCause.MAGIC) || cause.equals(DamageCause.POISON)) {
|
||||
if (luck.quickRNG(33.0)) {
|
||||
luck.takeFrom(5.0);
|
@ -1,6 +1,7 @@
|
||||
package io.github.simplex.crumb;
|
||||
package io.github.simplex.luck.player;
|
||||
|
||||
import io.github.simplex.api.LuckContainer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.attribute.Attribute;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@ -10,17 +11,17 @@ public class Luck implements LuckContainer {
|
||||
private final Player player;
|
||||
private final double multiplier;
|
||||
private final double BASE_VALUE;
|
||||
private final PlayerLuckChangeEvent event;
|
||||
|
||||
public Luck(Player player) {
|
||||
this.player = player;
|
||||
multiplier = 1.0;
|
||||
BASE_VALUE = player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue();
|
||||
this(player, 1.0);
|
||||
}
|
||||
|
||||
public Luck(Player player, double multiplier) {
|
||||
this.player = player;
|
||||
this.multiplier = multiplier;
|
||||
BASE_VALUE = player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue();
|
||||
event = new PlayerLuckChangeEvent(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -66,7 +67,7 @@ public class Luck implements LuckContainer {
|
||||
}
|
||||
|
||||
if (multiplier() > 0.0) {
|
||||
return (percentage >= (rng * multiplier()));
|
||||
return ((percentage * multiplier()) >= rng);
|
||||
}
|
||||
|
||||
return (percentage >= rng);
|
||||
@ -83,22 +84,33 @@ public class Luck implements LuckContainer {
|
||||
|
||||
public void setValue(double value) {
|
||||
player.getAttribute(Attribute.GENERIC_LUCK).setBaseValue(value);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
}
|
||||
|
||||
public void addTo(double value) {
|
||||
setValue(baseValue() + value);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
}
|
||||
|
||||
public void takeFrom(double value) {
|
||||
setValue(baseValue() - value);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
}
|
||||
|
||||
public double getPercentage() {
|
||||
return baseValue() - defaultValue();
|
||||
}
|
||||
|
||||
public boolean isDefault() {
|
||||
return baseValue() == defaultValue();
|
||||
public boolean notDefault() {
|
||||
return baseValue() != defaultValue();
|
||||
}
|
||||
|
||||
public boolean lessThan(double value) {
|
||||
return baseValue() < value;
|
||||
}
|
||||
|
||||
public boolean greaterThan (double value) {
|
||||
return baseValue() > value;
|
||||
}
|
||||
|
||||
@Override
|
@ -0,0 +1,51 @@
|
||||
package io.github.simplex.luck.player;
|
||||
|
||||
import io.github.simplex.luck.FeelingLucky;
|
||||
import io.github.simplex.luck.SneakyWorker;
|
||||
import org.bukkit.attribute.Attribute;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public class PlayerConfig extends YamlConfiguration {
|
||||
private final File configFile;
|
||||
private volatile YamlConfiguration config;
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
public PlayerConfig(FeelingLucky plugin, Player player) {
|
||||
File dataFolder = new File(plugin.getDataFolder(), "players");
|
||||
if (!dataFolder.exists()) dataFolder.mkdirs();
|
||||
File file = new File(dataFolder, player.getUniqueId() + ".yml");
|
||||
if (!file.exists()) {
|
||||
SneakyWorker.sneakyTry(() -> {
|
||||
file.createNewFile();
|
||||
InputStreamReader reader = new InputStreamReader(plugin.getResource("default_player.yml"));
|
||||
loadConfiguration(reader).save(file);
|
||||
});
|
||||
}
|
||||
configFile = file;
|
||||
config = loadConfiguration(configFile);
|
||||
|
||||
if (config.getString("username").equalsIgnoreCase("replace")) {
|
||||
config.set("username", player.getName());
|
||||
config.set("luck", new Luck(player).defaultValue());
|
||||
config.set("multiplier", "1.0");
|
||||
save();
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
SneakyWorker.sneakyTry(() -> config.save(configFile));
|
||||
}
|
||||
|
||||
public void load() {
|
||||
SneakyWorker.sneakyTry(() -> config = loadConfiguration(configFile));
|
||||
}
|
||||
|
||||
public YamlConfiguration getConfig() {
|
||||
return config;
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package io.github.simplex.luck.player;
|
||||
|
||||
import io.github.simplex.luck.FeelingLucky;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerLoginEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public record PlayerHandler(FeelingLucky plugin) implements Listener {
|
||||
private static final Map<Player, Luck> playerLuckMap = new HashMap<>();
|
||||
private static final List<Player> markedPlayers = new ArrayList<>();
|
||||
|
||||
public static Luck getLuckContainer(Player player) {
|
||||
return playerLuckMap.get(player);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void initializePlayer(PlayerLoginEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
PlayerConfig config = new PlayerConfig(plugin, player);
|
||||
|
||||
String username = config.getString("username");
|
||||
double luckstat = config.getDouble("luck");
|
||||
double multiplier = config.getDouble("multiplier");
|
||||
|
||||
if (!player.getName().equalsIgnoreCase(username)) {
|
||||
config.set("username", player.getName());
|
||||
config.save();
|
||||
config.load();
|
||||
}
|
||||
|
||||
Luck luck = new Luck(player, multiplier);
|
||||
luck.setValue(luckstat);
|
||||
|
||||
FeelingLucky.getConfigList().add(config);
|
||||
playerLuckMap.put(player, luck);
|
||||
}
|
||||
|
||||
public void updatePlayer(Player player, Luck luck) {
|
||||
playerLuckMap.replace(player, luck);
|
||||
}
|
||||
|
||||
public static void markPlayer(Player player) {
|
||||
markedPlayers.add(player);
|
||||
}
|
||||
|
||||
public static void unmarkPlayer(Player player) {
|
||||
markedPlayers.remove(player);
|
||||
}
|
||||
|
||||
public static boolean isMarked(Player player) {
|
||||
return markedPlayers.contains(player);
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package io.github.simplex.luck.player;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.player.PlayerEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class PlayerLuckChangeEvent extends PlayerEvent {
|
||||
public final HandlerList handlerList = new HandlerList();
|
||||
|
||||
public PlayerLuckChangeEvent(@NotNull Player who) {
|
||||
super(who);
|
||||
Luck luck = PlayerHandler.getLuckContainer(who);
|
||||
if (luck.lessThan(0.0) && !PlayerHandler.isMarked(who)) {
|
||||
PlayerHandler.markPlayer(who);
|
||||
return;
|
||||
}
|
||||
|
||||
if (luck.greaterThan(0.0) && PlayerHandler.isMarked(who)) {
|
||||
PlayerHandler.unmarkPlayer(who);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull HandlerList getHandlers() {
|
||||
return handlerList;
|
||||
}
|
||||
}
|
3
src/main/resources/default_player.yml
Normal file
3
src/main/resources/default_player.yml
Normal file
@ -0,0 +1,3 @@
|
||||
username: replace
|
||||
luck: 0.0
|
||||
multiplier: 1.0
|
@ -1,4 +1,5 @@
|
||||
name: Crumb
|
||||
name: FeelingLucky
|
||||
version: '${version}'
|
||||
main: io.github.simplex.crumb.Crumb
|
||||
author: SimplexDevelopment
|
||||
main: io.github.simplex.luck.FeelingLucky
|
||||
api-version: 1.18
|
||||
|
Loading…
Reference in New Issue
Block a user