Skip to content

Commit

Permalink
Add sleep mechanics, Entities translate
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbit4 committed Jan 13, 2025
1 parent 6044cd5 commit 13530f0
Show file tree
Hide file tree
Showing 14 changed files with 436 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = 'pl.norbit'
version = 'v1.5.0'
version = 'v1.5.1'

repositories {
mavenCentral()
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/pl/norbit/survivaltweaks/SurvivalTweaks.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.plugin.java.JavaPlugin;
import pl.norbit.survivaltweaks.commands.MainCommand;
import pl.norbit.survivaltweaks.commands.TrackCommand;
import pl.norbit.survivaltweaks.mechanics.SleepMechanic;
import pl.norbit.survivaltweaks.mechanics.listeners.*;
import pl.norbit.survivaltweaks.mechanics.MechanicsLoader;
import pl.norbit.survivaltweaks.utils.GlowUtils;
Expand All @@ -28,6 +29,7 @@ public void onEnable() {

GlowUtils.init();
PlaceholderUtils.load();
SleepMechanic.startTask();

pluginManager.registerEvents(new PlayerEatListener(), this);
pluginManager.registerEvents(new PlayerSpyGlassListener(), this);
Expand All @@ -39,6 +41,7 @@ public void onEnable() {
pluginManager.registerEvents(new AmethystBreakListener(), this);
pluginManager.registerEvents(new BlazeWaterDeathListener(), this);
pluginManager.registerEvents(new VillagerChangeListener(), this);
pluginManager.registerEvents(new BedListener(), this);

getCommand("survivaltweaks").setExecutor(new MainCommand());
getCommand("track").setExecutor(new TrackCommand());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package pl.norbit.survivaltweaks.mechanics;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.*;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -64,8 +63,11 @@ protected static void check(Player p, ItemStack itemInMainHand, ItemStack itemIn
}
}

//translate entity name
final String mobName = Config.getMobNameOrDefault(targetEntity.getType(), targetEntity.getName());

String message = Config.getEntityHpDisplay()
.replace("{ENTITY}", targetEntity.getName())
.replace("{ENTITY}", mobName)
.replace("{HEALTH}", String.valueOf((int) livingEntity.getHealth()));

PlayerUtils.sendActionBar(p, message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static boolean isEnabled(Mechanic mechanic) {
case ENTITY_HP -> Config.isEntityHpEnabled();
case BLAZE_DROP -> Config.isBlazeWaterDropEnabled();
case VIILAGER_PROFESSION_CHANGE -> Config.isVillagerProfessionCooldownEnabled();
case SLEEP -> Config.isSleepMechanicEnabled();
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package pl.norbit.survivaltweaks.mechanics;

import org.bukkit.entity.Player;
import pl.norbit.survivaltweaks.mechanics.model.Mechanic;
import pl.norbit.survivaltweaks.settings.Config;
import pl.norbit.survivaltweaks.utils.ChatUtils;
import pl.norbit.survivaltweaks.utils.PlayerUtils;
import pl.norbit.survivaltweaks.utils.TaskUtils;
import pl.norbit.survivaltweaks.utils.WorldUtils;

import java.util.List;

public class SleepMechanic {
private SleepMechanic() {
throw new IllegalStateException("Utility class");
}

public static void startTask() {
TaskUtils.asyncTimer(SleepMechanic::checkSleepingPlayers, 60, 30);
}

private static void checkSleepingPlayers() {
if(MechanicsLoader.isDisabled(Mechanic.SLEEP)){
return;
}

List<Player> sleeping = PlayerUtils.getSleepingPlayers();

int sleepingPlayersCount = sleeping.size();

if(sleepingPlayersCount == 0){
return;
}

int needSleepingPlayersCount = PlayerUtils.getNeedSleepingPlayersCount();

String subtitle = Config.getSleepMechanicSubtitleMessage()
.replace("{SLEEP}", String.valueOf(sleepingPlayersCount))
.replace("{NEED}", String.valueOf(needSleepingPlayersCount));

sleeping.forEach(p -> p.sendTitle(ChatUtils.format(Config.getSleepMechanicTitleMessage()), ChatUtils.format(subtitle), 0, 32, 0));

if (sleepingPlayersCount >= needSleepingPlayersCount) {
sleeping.forEach(p -> p.sendTitle(ChatUtils.format(Config.getSleepMechanicTitleMessage()), ChatUtils.format(Config.getSleepMechanicSubtitleSuccessMessage()), 0, 32, 0));
TaskUtils.sync(() -> WorldUtils.getWorlds().forEach(world -> world.setTime(0)));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package pl.norbit.survivaltweaks.mechanics.listeners;

import io.papermc.paper.event.player.PlayerBedFailEnterEvent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.event.world.TimeSkipEvent;
import pl.norbit.survivaltweaks.mechanics.MechanicsLoader;
import pl.norbit.survivaltweaks.mechanics.model.Mechanic;
import pl.norbit.survivaltweaks.settings.Config;
import pl.norbit.survivaltweaks.utils.ChatUtils;
import pl.norbit.survivaltweaks.utils.PlayerUtils;

import java.util.List;

public class BedListener implements Listener {

@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerSleep(PlayerBedEnterEvent e) {
if(MechanicsLoader.isDisabled(Mechanic.SLEEP)){
return;
}

PlayerBedEnterEvent.BedEnterResult bedEnterResult = e.getBedEnterResult();

if (bedEnterResult != PlayerBedEnterEvent.BedEnterResult.OK) {
return;
}

String message = Config.getSleepMechanicActionBarMessage()
.replace("{SLEEP}", String.valueOf(PlayerUtils.getSleepingPlayersCount()))
.replace("{NEED}", String.valueOf(PlayerUtils.getNeedSleepingPlayersCount()));

List<Player> onlinePlayers = PlayerUtils.getOnlinePlayers();
onlinePlayers.forEach(player -> player.sendActionBar(ChatUtils.format(message)));
}

@EventHandler
public void onTimeSkip(TimeSkipEvent e) {
if(MechanicsLoader.isDisabled(Mechanic.SLEEP)){
return;
}

if (e.getSkipReason() == TimeSkipEvent.SkipReason.NIGHT_SKIP) {
e.setCancelled(true);
}
}

@EventHandler
public void onPlayerSleep(PlayerBedFailEnterEvent e) {
if(MechanicsLoader.isDisabled(Mechanic.SLEEP)){
return;
}

TextComponent text = Component.text(ChatUtils.format(Config.getSleepMechanicDenyMessage()));
e.setMessage(text);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pl.norbit.survivaltweaks.mechanics.listeners;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Fireball;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
Expand Down Expand Up @@ -40,6 +41,17 @@ public void onPlayerInteract(PlayerInteractEvent e) {
}

if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) {

Block clickedBlock = e.getClickedBlock();

if(clickedBlock != null){
Material mat = clickedBlock.getType();

if(Config.getFireballBlockedList().contains(mat)){
return;
}
}

Fireball fireball = p.launchProjectile(Fireball.class);
fireball.setYield(Config.getFireballYield());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ public void onPlayerInteract(PlayerInteractEvent e) {

int distance = (int) p.getLocation().distance(targetEntity.getLocation());

//translate entity name
final String mobName = Config.getMobNameOrDefault(targetEntity.getType(), targetEntity.getName());

String message = Config.getSpyglass()
.replace("{ENTITY}", targetEntity.getName())
.replace("{ENTITY}", mobName)
.replace("{HEALTH}", String.valueOf((int) livingEntity.getHealth()))
.replace("{DISTANCE}", String.valueOf(distance));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public enum Mechanic {
AMETHYST,
ENTITY_HP,
BLAZE_DROP,
VIILAGER_PROFESSION_CHANGE
VIILAGER_PROFESSION_CHANGE,
SLEEP
}
61 changes: 61 additions & 0 deletions src/main/java/pl/norbit/survivaltweaks/settings/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
import lombok.Getter;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.EntityType;
import pl.norbit.survivaltweaks.SurvivalTweaks;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class Config {
@Getter
private static String reloadMessage;
Expand Down Expand Up @@ -83,6 +89,9 @@ public class Config {
@Getter
private static int fireballYield;

@Getter
private static List<Material> fireballBlockedList;

@Getter
private static boolean playerHeadEnabled;

Expand Down Expand Up @@ -185,9 +194,36 @@ public class Config {
@Getter
private static String villagerProfessionCooldownTimeSeconds;

@Getter
private static boolean sleepMechanicEnabled;

@Getter
private static double sleepMechanicPercentage;

@Getter
private static String sleepMechanicActionBarMessage;

@Getter
private static String sleepMechanicTitleMessage;

@Getter
private static String sleepMechanicSubtitleMessage;

@Getter
private static String sleepMechanicSubtitleSuccessMessage;

@Getter
private static String sleepMechanicDenyMessage;

@Getter
private static String entityHpDisplay;

private static Map<String, String> mobNames;

public static String getMobNameOrDefault(EntityType mobType, String defaultName) {
return mobNames.getOrDefault(mobType.name().toUpperCase(), defaultName);
}

private Config() {
throw new IllegalStateException("Utility class");
}
Expand Down Expand Up @@ -261,6 +297,12 @@ public static void load(boolean reload) {
fireballCooldown = config.getInt("mechanics.fireball.cooldown");
fireballYield = config.getInt("mechanics.fireball.yield");

fireballBlockedList = config.getStringList("mechanics.fireball.blocked-blocks")
.stream()
.map(Material::getMaterial)
.filter(Objects::nonNull)
.toList();

//playerHead
playerHeadEnabled = config.getBoolean("mechanics.player-head.enabled");
playerHeadDropChance = config.getDouble("mechanics.player-head.chance");
Expand Down Expand Up @@ -291,6 +333,15 @@ public static void load(boolean reload) {
villagerProfessionCooldownTimeMinutes = config.getString("mechanics.villager-profession-cooldown.time.minutes");
villagerProfessionCooldownTimeSeconds = config.getString("mechanics.villager-profession-cooldown.time.seconds");

//sleep
sleepMechanicEnabled = config.getBoolean("mechanics.sleep.enabled");
sleepMechanicPercentage = config.getDouble("mechanics.sleep.percentage");
sleepMechanicActionBarMessage = config.getString("mechanics.sleep.messages.action-bar");
sleepMechanicTitleMessage = config.getString("mechanics.sleep.messages.title");
sleepMechanicSubtitleMessage = config.getString("mechanics.sleep.messages.subtitle");
sleepMechanicDenyMessage = config.getString("mechanics.sleep.messages.deny");
sleepMechanicSubtitleSuccessMessage = config.getString("mechanics.sleep.messages.subtitle-skip");

//custom death message
customDeathMessageEnabled = config.getBoolean("mechanics.dead-messages.enabled");
deathMessagePrefix = config.getString("mechanics.dead-messages.prefix");
Expand All @@ -313,5 +364,15 @@ public static void load(boolean reload) {
deathMessageDragon = config.getString("mechanics.dead-messages.messages.dragon");
deathMessageHotFloor = config.getString("mechanics.dead-messages.messages.hot-floor");
deathMessageOther = config.getString("mechanics.dead-messages.messages.other");

mobNames = new HashMap<>();

for (String mob : config.getConfigurationSection("mob-names").getKeys(false)) {
String name = config.getString("mob-names." + mob);
if (name == null) {
continue;
}
mobNames.put(mob.toUpperCase(), name);
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/pl/norbit/survivaltweaks/utils/ChatUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public static String format(String text, Player p) {
return PlaceholderUtils.setPlaceholders(translateColorCodes(text), p);
}

public static String format(String text) {
return PlaceholderUtils.setPlaceholders(translateColorCodes(text), null);
}

/**
* @param text The string of text to apply color/effects to
* @return Returns a string of text with color/effects applied
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/pl/norbit/survivaltweaks/utils/PlayerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.bukkit.util.RayTraceResult;
import org.bukkit.util.Vector;
import pl.norbit.survivaltweaks.SurvivalTweaks;
import pl.norbit.survivaltweaks.settings.Config;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -42,6 +43,34 @@ public static ItemStack getCustomSkull(OfflinePlayer offlinePlayer) {
return head;
}

public static int getNeedSleepingPlayersCount() {
int needSleepingPlayersCount = getNeedSleepingPlayersCount(Config.getSleepMechanicPercentage());
return needSleepingPlayersCount == 0 ? 1 : needSleepingPlayersCount;
}

private static int getNeedSleepingPlayersCount(double percent) {
long count = getOnlinePlayers()
.stream()
.filter(player -> !player.isOp())
.filter(player -> !player.hasPermission("survivaltweaks.bypass"))
.count();
return (int) Math.ceil(count * percent);
}

public static int getSleepingPlayersCount() {
int count = (int) getOnlinePlayers()
.stream()
.filter(Player::isSleeping)
.count();
return count == 0 ? 1 : count;
}
public static List<Player> getSleepingPlayers() {
return getOnlinePlayers()
.stream()
.filter(Player::isSleeping)
.toList();
}

public static List<Player> getOnlinePlayers() {
return new ArrayList<>(SurvivalTweaks.getInstance().getServer().getOnlinePlayers());
}
Expand Down
Loading

0 comments on commit 13530f0

Please sign in to comment.