Alpha 1.0

This commit is contained in:
Paldiu 2022-03-27 19:21:35 -05:00
parent 6d211490c0
commit 73affcf4c4
6 changed files with 108 additions and 27 deletions

View File

@ -6,7 +6,10 @@ import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.util.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public final class FeelingLucky extends JavaPlugin {
private static final Map<UUID, PlayerConfig> configMap = new HashMap<>();

View File

@ -1,6 +1,9 @@
package io.github.simplex.luck;
import io.github.simplex.luck.player.Luck;
import org.bukkit.Bukkit;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
public class SneakyWorker {
public static void sneakyTry(SneakyTry sneakyTry) {
@ -15,10 +18,18 @@ public class SneakyWorker {
}
}
public static void silentTry(SneakyTry sneakyTry) {
public static void quietTry(SneakyTry sneakyTry) {
try {
sneakyTry.tryThis();
} catch (Exception ignored) {}
} catch (Exception ignored) {
}
}
public static void move(Item item) {
ItemStack stack = item.getItemStack();
int rng = (Luck.RNG().nextInt(2, 5)) + stack.getAmount();
stack.setAmount(rng);
item.setItemStack(stack);
}
public interface SneakyTry {

View File

@ -3,27 +3,37 @@ package io.github.simplex.luck.listener;
import io.github.simplex.lib.PotionEffectBuilder;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.ListBox;
import io.github.simplex.luck.SneakyWorker;
import io.github.simplex.luck.player.Luck;
import io.github.simplex.luck.player.PlayerHandler;
import net.kyori.adventure.text.Component;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Witch;
import org.bukkit.World;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockDropItemEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public record PlayerListener(FeelingLucky plugin) implements Listener {
private static final Map<UUID, Player> entityPlayerMap = new HashMap<>();
@EventHandler
public void takeDamage(EntityDamageEvent event) {
Entity entity = event.getEntity();
@ -70,11 +80,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
percentage = Math.abs(percentage);
if (luck.quickRNG(percentage)) {
event.setCancelled(true);
player.addPotionEffect(PotionEffectBuilder.newEffect()
.type(ListBox.potionEffects.get(Luck.RNG().nextInt(ListBox.potionEffects.size() - 1)))
.amplifier(Luck.RNG().nextInt(1, 5))
.duration(Luck.RNG().nextInt(1, 120))
.create());
player.addPotionEffect(PotionEffectBuilder.newEffect().type(ListBox.potionEffects.get(Luck.RNG().nextInt(ListBox.potionEffects.size() - 1))).amplifier(Luck.RNG().nextInt(1, 5)).duration(Luck.RNG().nextInt(1, 120)).create());
}
return;
}
@ -89,16 +95,66 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
}
}
@EventHandler
public void extraBlockDrops(BlockDropItemEvent event) {
Player player = event.getPlayer();
Luck luck = PlayerHandler.getLuckContainer(player);
List<Item> items = event.getItems();
if (luck.quickRNG(luck.getPercentage())) {
items.forEach(SneakyWorker::move);
}
}
@EventHandler
public void checkForPreItemDrop(EntityDamageByEntityEvent event) {
if (!(event.getEntity() instanceof LivingEntity entity)) {
return;
}
if (!(event.getDamager() instanceof Player player)) {
return;
}
if (!(entity.getHealth() <= 0.0)) {
return;
}
if (entity instanceof Witch witch) {
if (Luck.quickRNG2(33.0)) {
Location location = witch.getLocation();
World world = location.getWorld();
Item item = world.dropItemNaturally(location, new ItemStack(Material.RABBIT_FOOT, 1));
new EntityDropItemEvent(witch, item).callEvent();
}
}
entityPlayerMap.put(entity.getUniqueId(), player);
}
@EventHandler
public void itemDrops(EntityDropItemEvent event) {
Entity entity = event.getEntity();
if (entityPlayerMap.get(entity.getUniqueId()) == null) return;
Player player = entityPlayerMap.get(entity.getUniqueId());
Luck luck = PlayerHandler.getLuckContainer(player);
Item item = event.getItemDrop();
ItemStack stack = item.getItemStack();
int amount = stack.getAmount();
if (luck.quickRNG(luck.getPercentage())) {
int rng = Luck.RNG().nextInt(2, 5);
amount += rng;
stack.setAmount(amount);
event.getItemDrop().setItemStack(stack);
}
}
@EventHandler
public void restoreHunger(PlayerItemConsumeEvent event) {
ItemStack item = event.getItem();
Luck luck = PlayerHandler.getLuckContainer(event.getPlayer());
PotionEffect effect = PotionEffectBuilder.newEffect()
.type(PotionEffectType.SATURATION)
.amplifier(2)
.duration(10)
.particles(false)
.create();
PotionEffect effect = PotionEffectBuilder.newEffect().type(PotionEffectType.SATURATION).amplifier(2).duration(10).particles(false).create();
if (luck.notDefault()) {
double percentage = luck.getPercentage();
ListBox.foods.forEach(food -> {
@ -149,5 +205,4 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
}
}
}
}

View File

@ -33,6 +33,17 @@ public class Luck implements LuckContainer {
return new SplittableRandom();
}
public static boolean quickRNG2(double percentage) {
double rng;
if (percentage >= 100.0) {
rng = 100.0; // 100% chance to trigger, obviously;
} else {
rng = RNG().nextDouble(0.0, 99.0);
}
return (percentage >= rng);
}
@Override
public Attribute asAttribute() {
return Attribute.GENERIC_LUCK;
@ -65,13 +76,13 @@ public class Luck implements LuckContainer {
public boolean quickRNG(double percentage) {
double rng;
if (percentage > 99.0) {
rng = RNG().nextDouble(100.0, 199.0);
if (percentage >= 100.0) {
rng = 100.0; // 100% chance to trigger, obviously;
} else {
rng = RNG().nextDouble(0.0, 99.0);
}
if (multiplier() > 0.0) {
if (multiplier() > 1.0) {
return ((percentage * multiplier()) >= rng);
}
@ -87,7 +98,7 @@ public class Luck implements LuckContainer {
return player.getAttribute(Attribute.GENERIC_LUCK).getDefaultValue();
}
public void setValue(double value) {
protected void setValue(double value) {
player.getAttribute(Attribute.GENERIC_LUCK).setBaseValue(value);
Bukkit.getPluginManager().callEvent(event);
}

View File

@ -6,7 +6,9 @@ import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Contract;
import java.io.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.nio.charset.StandardCharsets;
public class PlayerConfig extends YamlConfiguration {
@ -38,7 +40,9 @@ public class PlayerConfig extends YamlConfiguration {
configFile = file;
config = loadConfiguration(configFile);
if (config.getString("username").equalsIgnoreCase("replace")) {
String tempUsername = config.getString("username");
if (tempUsername != null && tempUsername.equalsIgnoreCase("replace")) {
config.set("username", player.getName());
config.set("luck", PlayerHandler.getLuckContainer(player).defaultValue());
config.set("multiplier", "1.0");

View File

@ -1,3 +0,0 @@
username: replace
luck: 0.0
multiplier: 1.0