Skip to content

Commit

Permalink
Merge pull request #109 from mrgeneralq/kick-from-bed
Browse files Browse the repository at this point in the history
Insomnia update
  • Loading branch information
mrgeneralq committed Apr 3, 2022
2 parents 1e2b29b + 1d961d5 commit 8e613e6
Show file tree
Hide file tree
Showing 23 changed files with 563 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>me.mrgeneralq</groupId>
<artifactId>sleep-most</artifactId>
<version>4.14.0</version>
<version>4.15.0</version>
<name>SleepMost</name>

<properties>
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/me/mrgeneralq/sleepmost/Sleepmost.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import me.mrgeneralq.sleepmost.eventlisteners.*;
import me.mrgeneralq.sleepmost.interfaces.IBossBarService;
import me.mrgeneralq.sleepmost.interfaces.ISleepService;
import me.mrgeneralq.sleepmost.interfaces.IWorldPropertyService;
import me.mrgeneralq.sleepmost.runnables.Heartbeat;
import me.mrgeneralq.sleepmost.services.WorldPropertyService;
import me.mrgeneralq.sleepmost.statics.ServerVersion;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -37,11 +39,11 @@ public void onEnable() {
this.registerBossBars();

//init commands
SleepmostCommand sleepmostCommand = new SleepmostCommand(bootstrapper.getSleepService(), bootstrapper.getMessageService(), bootstrapper.getUpdateService(), bootstrapper.getFlagService(), bootstrapper.getFlagsRepository(), bootstrapper.getConfigRepository(), bootstrapper.getCooldownService(), bootstrapper.getBossBarService());
SleepmostCommand sleepmostCommand = new SleepmostCommand(bootstrapper.getSleepService(), bootstrapper.getMessageService(), bootstrapper.getUpdateService(), bootstrapper.getFlagService(), bootstrapper.getFlagsRepository(), bootstrapper.getConfigRepository(), bootstrapper.getCooldownService(), bootstrapper.getBossBarService(), bootstrapper.getWorldPropertyService());
getCommand("sleepmost").setExecutor(sleepmostCommand);

PluginManager pm = Bukkit.getPluginManager();
pm.registerEvents(new PlayerBedEnterEventListener(bootstrapper.getSleepService(), bootstrapper.getMessageService(), bootstrapper.getCooldownService(), bootstrapper.getFlagsRepository(), bootstrapper.getBossBarService()), this);
pm.registerEvents(new PlayerBedEnterEventListener(bootstrapper.getSleepService(), bootstrapper.getMessageService(), bootstrapper.getCooldownService(), bootstrapper.getFlagsRepository(), bootstrapper.getBossBarService(), bootstrapper.getWorldPropertyService()), this);
pm.registerEvents(new PlayerQuitEventListener(bootstrapper.getCooldownService(), bootstrapper.getSleepService(), bootstrapper.getBossBarService()), this);

if(ServerVersion.CURRENT_VERSION.hasTimeSkipEvent())
Expand All @@ -56,19 +58,27 @@ public void onEnable() {
pm.registerEvents(new PlayerBedLeaveEventListener(bootstrapper.getSleepService()), this);
pm.registerEvents(new WorldLoadEventListener(bootstrapper.getBossBarService()),this);
pm.registerEvents(new PlayerSleepStateChangeEventListener(this, bootstrapper.getSleepService(), bootstrapper.getFlagsRepository(), bootstrapper.getBossBarService(), bootstrapper.getMessageService(), bootstrapper.getCooldownService()), this);
pm.registerEvents(new TimeCycleChangeEventListener(bootstrapper.getSleepService(), bootstrapper.getWorldPropertyService(), bootstrapper.getFlagsRepository()),this );

Bukkit.getScheduler().runTaskAsynchronously(this, () -> notifyIfNewUpdateExists(bootstrapper.getUpdateService()));
runTimers(bootstrapper.getSleepService());

runPreTimerTasks();
runTimers(bootstrapper.getSleepService(), bootstrapper.getWorldPropertyService());
}

private void runTimers(ISleepService sleepService){
new Heartbeat(sleepService).runTaskTimer(this, 20,20);
private void runPreTimerTasks(){
for(World world: Bukkit.getWorlds())
this.bootstrapper.getWorldPropertyService().createNewWorldProperty(world);
}

private void runTimers(ISleepService sleepService, IWorldPropertyService worldPropertyService){

new Heartbeat(sleepService, worldPropertyService).runTaskTimer(this, 20,20);
}

private void notifyIfNewUpdateExists(IUpdateService updateService)
{
//testa

if(updateService.hasUpdate())
getLogger().info("UPDATE FOUND: A newer version of sleep-most is available to download!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public class SleepmostCommand implements CommandExecutor, TabCompleter {
private final IConfigRepository configRepository;
private final ICooldownService cooldownService;
private final IBossBarService bossBarService;
private final IWorldPropertyService worldPropertyService;

public SleepmostCommand(ISleepService sleepService, IMessageService messageService, IUpdateService updateService, IFlagService flagService, IFlagsRepository flagsRepository, IConfigRepository configRepository, ICooldownService cooldownService, IBossBarService bossBarService){
public SleepmostCommand(ISleepService sleepService, IMessageService messageService, IUpdateService updateService, IFlagService flagService, IFlagsRepository flagsRepository, IConfigRepository configRepository, ICooldownService cooldownService, IBossBarService bossBarService, IWorldPropertyService worldPropertyService){
this.sleepService = sleepService;
this.messageService = messageService;
this.updateService = updateService;
Expand All @@ -35,6 +36,7 @@ public SleepmostCommand(ISleepService sleepService, IMessageService messageServi
this.configRepository = configRepository;
this.cooldownService = cooldownService;
this.bossBarService = bossBarService;
this.worldPropertyService = worldPropertyService;
this.registerSubCommands();
}

Expand All @@ -48,7 +50,9 @@ private void registerSubCommands(){
subCommands.put("reset", new ResetSubCommand(this.messageService, this.flagService));
subCommands.put("setops", new SetOnePlayerSleepCommand(this.sleepService, this.messageService,this.flagService, this.flagsRepository));
subCommands.put("bed", new BedSubCommand(this.sleepService,this.messageService));
subCommands.put("sleep", new SleepSubCommand(this.sleepService,this.flagsRepository,this.messageService,this.cooldownService, this.bossBarService));
subCommands.put("sleep", new SleepSubCommand(this.sleepService,this.flagsRepository,this.messageService,this.cooldownService, this.bossBarService, this.worldPropertyService));
subCommands.put("kick", new KickSubCommand(this.sleepService,this.messageService));
subCommands.put("insomnia", new InsomniaSubCommand(this.sleepService, this.flagsRepository, this.messageService, this.worldPropertyService));

//enable when debugging
//subCommands.put("test", new TestCommand(this.messageService, this.flagsRepository, this.configRepository));
Expand Down Expand Up @@ -78,6 +82,8 @@ public boolean onCommand(CommandSender sender, Command command, String commandLa
sender.sendMessage(colorize("&e/sm bed &fteleport to your current bed spawn location"));
sender.sendMessage(colorize("&e/sm reload &freload the config file"));
sender.sendMessage(colorize("&e/sm sleep &fput yourself in sleep status"));
sender.sendMessage(colorize("&e/sm kick &fkick a player from the bed"));
sender.sendMessage(colorize("&e/sm insomnia &fBlock sleeping for the current night"));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package me.mrgeneralq.sleepmost.commands.subcommands;

import me.mrgeneralq.sleepmost.enums.ConfigMessage;
import me.mrgeneralq.sleepmost.interfaces.*;
import me.mrgeneralq.sleepmost.messages.MessageTemplate;
import me.mrgeneralq.sleepmost.models.WorldProperty;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import java.util.List;

public class InsomniaSubCommand implements ISubCommand {

private final ISleepService sleepService;
private final IFlagsRepository flagsRepository;
private final IMessageService messageService;
private final IWorldPropertyService worldPropertyService;

public InsomniaSubCommand(ISleepService sleepService, IFlagsRepository flagsRepository, IMessageService messageService, IWorldPropertyService worldPropertyService) {
this.sleepService = sleepService;
this.flagsRepository = flagsRepository;
this.messageService = messageService;
this.worldPropertyService = worldPropertyService;
}


@Override
public boolean executeCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {

Player player = (Player) sender;
World world = player.getWorld();

if(!sleepService.isEnabledAt(world)){
this.messageService.sendMessage(player, messageService.fromTemplate(MessageTemplate.NOT_ENABLED_FOR_WORLD));
return true;
}

if (!sleepService.isNight(world)) {
String notNightMessage = this.messageService.fromTemplate(MessageTemplate.CMD_ONLY_DURING_NIGHT);
player.sendMessage(notNightMessage);
return true;
}

if (this.worldPropertyService.getWorldProperties(world).isInsomniaEnabled()) {
String insomniaMessage = this.messageService.fromTemplate(MessageTemplate.INSOMNIA_ALREADY_ENABLED);
player.sendMessage(insomniaMessage);
return true;
}

WorldProperty property = this.worldPropertyService.getWorldProperties(world);
property.setInsomniaEnabled(true);

this.worldPropertyService.setWorldProperty(world, property);

String insomniaMessage = this.messageService.fromTemplate(MessageTemplate.INSOMNIA_ENABLED);
List<Player> sleepingPlayers = this.sleepService.getSleepers(world);

for(Player p: sleepingPlayers){
p.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 60,1));
p.teleport(p.getLocation());
String targetInsomniaMessage = this.messageService.fromTemplate(MessageTemplate.INSOMNIA_NOT_SLEEPY);
p.sendMessage(targetInsomniaMessage);
}

player.sendMessage(insomniaMessage);
return true;

}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package me.mrgeneralq.sleepmost.commands.subcommands;

import me.mrgeneralq.sleepmost.interfaces.IMessageService;
import me.mrgeneralq.sleepmost.interfaces.ISleepService;
import me.mrgeneralq.sleepmost.interfaces.ISubCommand;
import me.mrgeneralq.sleepmost.messages.MessageTemplate;
import me.mrgeneralq.sleepmost.statics.CommandSenderUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class KickSubCommand implements ISubCommand {

private final ISleepService sleepService;
private final IMessageService messageService;

public KickSubCommand(ISleepService sleepService, IMessageService messageService) {
this.sleepService = sleepService;
this.messageService = messageService;
}

@Override
public boolean executeCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {

if(!CommandSenderUtils.hasWorld(sender)){
this.messageService.sendMessage(sender, messageService.fromTemplate(MessageTemplate.NO_CONSOLE_COMMAND));
return true;
}

Player player = (Player) sender;

World world = CommandSenderUtils.getWorldOf(sender);

if(args.length < 2){
this.messageService.sendMessage(sender, messageService.fromTemplate(MessageTemplate.SPECIFY_PLAYER));
return true;
}

String targetPlayerName = args[1];

if(Bukkit.getPlayer(targetPlayerName) == null){
this.messageService.sendMessage(sender, messageService.fromTemplate(MessageTemplate.TARGET_NOT_ONLINE));
return true;
}

Player targetPlayer = Bukkit.getPlayer(targetPlayerName);

if(!this.sleepService.isPlayerAsleep(targetPlayer)){
this.messageService.sendMessage(sender, messageService.fromTemplate(MessageTemplate.TARGET_NOT_SLEEPING));
return true;
}

targetPlayer.teleport(targetPlayer.getLocation());
this.messageService.sendMessage(sender,"Player has been kicked from the bed");
this.sleepService.setSleeping(targetPlayer, false);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ public class SleepSubCommand implements ISubCommand {
private final ISleepService sleepService;
private final IFlagsRepository flagsRepository;
private final IMessageService messageService;
private final IWorldPropertyService worldPropertyService;

public SleepSubCommand(ISleepService sleepService, IFlagsRepository flagsRepository, IMessageService messageService, ICooldownService cooldownService, IBossBarService bossBarService) {
public SleepSubCommand(ISleepService sleepService, IFlagsRepository flagsRepository, IMessageService messageService, ICooldownService cooldownService, IBossBarService bossBarService, IWorldPropertyService worldPropertyService) {
this.sleepService = sleepService;
this.flagsRepository = flagsRepository;
this.messageService = messageService;
this.worldPropertyService = worldPropertyService;
}


Expand Down Expand Up @@ -54,6 +56,14 @@ public boolean executeCommand(CommandSender sender, Command cmd, String commandL
return true;
}


if(this.worldPropertyService.getWorldProperties(world).isInsomniaEnabled()){
String insomniaMessage = this.messageService.fromTemplate(MessageTemplate.INSOMNIA_NOT_SLEEPY);
player.sendMessage(insomniaMessage);
return true;
}


boolean updatedSleepStatus = !this.sleepService.isPlayerAsleep(player);

this.messageService.sendMessage(player, this.messageService.fromTemplate(getStatusTemplate(updatedSleepStatus)));
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/me/mrgeneralq/sleepmost/enums/TimeCycle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.mrgeneralq.sleepmost.enums;

public enum TimeCycle {
DAY,
NIGHT,
UNKNOWN
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.mrgeneralq.sleepmost.enums.SleepSkipCause;
import me.mrgeneralq.sleepmost.interfaces.*;
import me.mrgeneralq.sleepmost.messages.MessageBuilder;
import me.mrgeneralq.sleepmost.messages.MessageTemplate;
import me.mrgeneralq.sleepmost.statics.DataContainer;
import me.mrgeneralq.sleepmost.statics.ServerVersion;
import org.bukkit.Bukkit;
Expand All @@ -24,18 +25,21 @@ public class PlayerBedEnterEventListener implements Listener {
private final DataContainer dataContainer;
private final IFlagsRepository flagsRepository;
private final IBossBarService bossBarService;
private final IWorldPropertyService worldPropertyService;

public PlayerBedEnterEventListener(ISleepService sleepService,
IMessageService messageService,
ICooldownService cooldownService,
IFlagsRepository flagsRepository,
IBossBarService bossBarService
IBossBarService bossBarService,
IWorldPropertyService worldPropertyService
) {
this.sleepService = sleepService;
this.messageService = messageService;
this.cooldownService = cooldownService;
this.flagsRepository = flagsRepository;
this.bossBarService = bossBarService;
this.worldPropertyService = worldPropertyService;
this.dataContainer = DataContainer.getContainer();
}

Expand Down Expand Up @@ -74,8 +78,18 @@ public void onPlayerBedEnter(PlayerBedEnterEvent e) {
return;
}


if(this.worldPropertyService.getWorldProperties(world).isInsomniaEnabled()){
String insomniaMessage = this.messageService.fromTemplate(MessageTemplate.INSOMNIA_NOT_SLEEPY);
player.sendMessage(insomniaMessage);
e.setCancelled(true);
return;
}


if(!this.sleepService.isPlayerAsleep(player))
this.sleepService.setSleeping(player , true);

}

}
Loading

0 comments on commit 8e613e6

Please sign in to comment.