Skip to content

Commit

Permalink
GH-4 Add couting time as game time (#8)
Browse files Browse the repository at this point in the history
* Added game time
  • Loading branch information
P1otrulla authored Nov 19, 2024
1 parent 88224fa commit 3b30951
Show file tree
Hide file tree
Showing 17 changed files with 303 additions and 108 deletions.
17 changes: 8 additions & 9 deletions newbieprotection-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
id("xyz.jpenilla.run-paper") version "2.3.1"
}

group = "dev.piotrulla.hypemc.signgui"
group = "dev.piotrulla.newbieprotection"
version = "1.0.0"

repositories {
Expand Down Expand Up @@ -50,7 +50,7 @@ dependencies {
implementation("com.eternalcode:multification-bukkit:$eternalcodeVersion")

// packetevents
val scoreboardLibraryVersion = "2.2.0"
val scoreboardLibraryVersion = "2.2.1"
implementation("net.megavex:scoreboard-library-api:$scoreboardLibraryVersion")
runtimeOnly("net.megavex:scoreboard-library-implementation:$scoreboardLibraryVersion")
runtimeOnly("net.megavex:scoreboard-library-packetevents:$scoreboardLibraryVersion")
Expand Down Expand Up @@ -79,11 +79,13 @@ tasks.withType<JavaCompile> {

bukkit {
main = "dev.piotrulla.newbieprotection.NewbieProtectionPlugin"
apiVersion = "1.13"
website = "https://github.com/P1otrulla/ptrlNewbieProtection"
description = "Secure your newbie!"
prefix = "ptrlNewbieProtection"
author = "Piotrulla"
name = "ptrlNewbieProtection"
version = "${project.version}"
name = "ptrlNewbieProtection"
author = "Piotrulla"
apiVersion = "1.13"
softDepend = listOf("PacketEvents")
}

Expand All @@ -94,7 +96,7 @@ tasks {
}

tasks.withType<ShadowJar> {
archiveFileName.set("ptrlNewbieProtection -${project.version}.jar")
archiveFileName.set("ptrlNewbieProtection - ${project.version}.jar")

exclude(
"org/intellij/lang/annotations/**",
Expand All @@ -107,13 +109,10 @@ tasks.withType<ShadowJar> {
val prefix = "dev.piotrulla.newbieprotection.shared"

listOf(
"com.github.retrooper",
"com.eternalcode",
"net.megavex",
"dev.rollczi",
"eu.okaeri",
"org.bstats",
"net.kyori",
"org.yaml",
).forEach { pack ->
relocate(pack, "$prefix.$pack")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@
@Permission("ptrlNewbieProtection.admin")
public class NewbieProtectionAdminCommand {

private final NewbieProtectionNameTagServiceImpl newbieProtectionNameTagServiceImpl;
private final NewbieProtectionService newbieProtectionService;
private final NewbieProtectionMultification multification;
private final NewbieProtectionMetrics metrics;
private final NewbieProtectionNameTagServiceImpl newbieProtectionNameTagServiceImpl;

public NewbieProtectionAdminCommand(NewbieProtectionService newbieProtectionService, NewbieProtectionMultification multification, NewbieProtectionMetrics metrics, NewbieProtectionNameTagServiceImpl newbieProtectionNameTagServiceImpl) {
public NewbieProtectionAdminCommand(
NewbieProtectionNameTagServiceImpl newbieProtectionNameTagServiceImpl, NewbieProtectionService newbieProtectionService,
NewbieProtectionMultification multification, NewbieProtectionMetrics metrics
) {
this.newbieProtectionNameTagServiceImpl = newbieProtectionNameTagServiceImpl;
this.newbieProtectionService = newbieProtectionService;
this.multification = multification;
this.metrics = metrics;
this.newbieProtectionNameTagServiceImpl = newbieProtectionNameTagServiceImpl;
}

@Execute(name = "add")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
@Command(name = "newbieprotection")
public class NewbieProtectionCommand {

private final NewbieProtectionNameTagServiceImpl newbieProtectionNameTagServiceImpl;
private final NewbieProtectionService newbieProtectionService;
private final NewbieProtectionMultification multification;
private final NewbieProtectionMetrics metrics;
private final NewbieProtectionNameTagServiceImpl newbieProtectionNameTagServiceImpl;

public NewbieProtectionCommand(NewbieProtectionService newbieProtectionService, NewbieProtectionMultification multification, NewbieProtectionMetrics metrics, NewbieProtectionNameTagServiceImpl newbieProtectionNameTagServiceImpl) {
public NewbieProtectionCommand(
NewbieProtectionNameTagServiceImpl newbieProtectionNameTagServiceImpl, NewbieProtectionService newbieProtectionService,
NewbieProtectionMultification multification, NewbieProtectionMetrics metrics
) {
this.newbieProtectionNameTagServiceImpl = newbieProtectionNameTagServiceImpl;
this.newbieProtectionService = newbieProtectionService;
this.multification = multification;
this.metrics = metrics;
this.newbieProtectionNameTagServiceImpl = newbieProtectionNameTagServiceImpl;
}

@Execute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import dev.piotrulla.newbieprotection.configuration.implementation.NewbieConfiguration;
import dev.piotrulla.newbieprotection.metrics.NewbieProtectionMetrics;
import dev.piotrulla.newbieprotection.util.DurationUtil;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
Expand All @@ -14,20 +13,58 @@
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.projectiles.ProjectileSource;

import java.time.Duration;
import java.time.Instant;

public class NewbieProtectionController implements Listener {

private final NewbieProtectionNameTagServiceImpl nameTagService;
private final NewbieProtectionDataRepository dataRepository;
private final NewbieProtectionMultification multification;
private final NewbieProtectionService protectionService;
private final NewbieConfiguration configuration;
private final NewbieProtectionMetrics metrics;

public NewbieProtectionController(NewbieProtectionMultification multification, NewbieProtectionService protectionService, NewbieConfiguration configuration, NewbieProtectionMetrics metrics, NewbieProtectionNameTagServiceImpl nameTagService) {
public NewbieProtectionController(NewbieProtectionMultification multification, NewbieProtectionService protectionService, NewbieConfiguration configuration, NewbieProtectionMetrics metrics, NewbieProtectionNameTagServiceImpl nameTagService, NewbieProtectionDataRepository dataRepository) {
this.multification = multification;
this.protectionService = protectionService;
this.configuration = configuration;
this.metrics = metrics;
this.nameTagService = nameTagService;
this.dataRepository = dataRepository;
}

@EventHandler
public void onNameTag(PlayerJoinEvent event) {
Player player = event.getPlayer();

if (!this.dataRepository.isExists(player.getUniqueId())) {
return;
}

Newbie load = this.dataRepository.load(player.getUniqueId());

if (load == null && this.configuration.protectionTimeType == NewbieProtectionTime.REAL_TIME) {
return;
}

if (this.configuration.protectionTimeType == NewbieProtectionTime.REAL_TIME) {
Instant loadedAt = load.issuedAt();
Instant end = loadedAt.plus(this.configuration.protectionTime);

if (end.isBefore(Instant.now())) {
return;
}

this.protectionService.startProtection(player, Duration.between(Instant.now(), end));
}
else {
this.protectionService.startProtection(player, load.protectionTime());
}

if (this.configuration.nameTag.enabled) {
this.nameTagService.applyNameTag(player);
}
}

@EventHandler
Expand All @@ -43,7 +80,7 @@ public void onJoin(PlayerJoinEvent event) {
this.metrics.addProtectedPlayer();

if (this.configuration.nameTag.enabled) {
Bukkit.getScheduler().runTaskLater(NewbieProtectionPlugin.getProvidingPlugin(NewbieProtectionPlugin.class), () -> this.nameTagService.applyNameTag(player), 40L);
this.nameTagService.applyNameTag(player);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package dev.piotrulla.newbieprotection;

import eu.okaeri.configs.OkaeriConfig;

import java.time.Duration;
import java.time.Instant;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class NewbieProtectionDataInMemory extends OkaeriConfig implements NewbieProtectionDataRepository {

private Set<Entry> entries = new HashSet<>();

@Override
public boolean isExists(UUID uniqueId) {
return this.entries.stream().anyMatch(entry -> entry.uniqueId().equals(uniqueId));
}

@Override
public void save(Newbie newbie) {
this.entries.add(new Entry(newbie.uniqueId(), newbie.issuedAt(), newbie.protectionTime()));
this.save();
}

@Override
public void remove(Newbie newbie) {
this.entries.removeIf(entry -> entry.uniqueId().equals(newbie.uniqueId()));
this.save();
}

@Override
public void update(Newbie newbie) {
this.entries.removeIf(entry -> entry.uniqueId().equals(newbie.uniqueId()));
this.save(newbie);
}

@Override
public Newbie load(UUID uniqueId) {
return this.entries.stream()
.filter(entry -> entry.uniqueId().equals(uniqueId))
.findFirst()
.map(entry -> new NewbieProtectionUser(uniqueId, entry.issuedAt(), entry.protectionTime()))
.orElse(null);
}

public static class Entry extends OkaeriConfig {

private UUID uniqueId;
private Instant issuedAt;
private Duration protectionTime;

public Entry(UUID uniqueId, Instant issuedAt, Duration protectionTime) {
this.uniqueId = uniqueId;
this.issuedAt = issuedAt;
this.protectionTime = protectionTime;
}

UUID uniqueId() {
return this.uniqueId;
}

Instant issuedAt() {
return this.issuedAt;
}

Duration protectionTime() {
return this.protectionTime;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.piotrulla.newbieprotection;

import java.util.UUID;

public interface NewbieProtectionDataRepository {

boolean isExists(UUID uniqueId);

void save(Newbie newbie);

void remove(Newbie newbie);

void update(Newbie newbie);

Newbie load(UUID uniqueId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public class NewbieProtectionMultification extends BukkitMultification<MessagesC
private final MessagesConfiguration messagesCfg;
private final MiniMessage miniMessage;

public NewbieProtectionMultification(AudienceProvider audienceProvider, MessagesConfiguration messagesCfg, MiniMessage miniMessage) {
public NewbieProtectionMultification(
AudienceProvider audienceProvider, MessagesConfiguration messagesCfg,
MiniMessage miniMessage
) {
this.audienceProvider = audienceProvider;
this.messagesCfg = messagesCfg;
this.miniMessage = miniMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,29 @@
import net.megavex.scoreboardlibrary.api.team.ScoreboardTeam;
import net.megavex.scoreboardlibrary.api.team.TeamDisplay;
import net.megavex.scoreboardlibrary.api.team.TeamManager;
import org.bukkit.Server;
import org.bukkit.entity.Player;

import java.util.logging.Logger;

public class NewbieProtectionNameTagServiceImpl implements NewbieProtectionNameTagService {

private final NewbieConfiguration newbieConfiguration;
private final ScoreboardLibrary scoreboardLibrary;
private final MiniMessage miniMessage;
private final Server server;
private final Logger logger;
private TeamManager teamManager;

public NewbieProtectionNameTagServiceImpl(NewbieConfiguration newbieConfiguration, ScoreboardLibrary scoreboardLibrary, MiniMessage miniMessage) {

public NewbieProtectionNameTagServiceImpl(
NewbieConfiguration newbieConfiguration, ScoreboardLibrary scoreboardLibrary,
MiniMessage miniMessage, Server server, Logger logger
) {
this.newbieConfiguration = newbieConfiguration;
this.scoreboardLibrary = scoreboardLibrary;
this.miniMessage = miniMessage;
this.server = server;
this.logger = logger;
}

@Override
Expand All @@ -35,12 +45,24 @@ public void applyNameTag(Player player) {
switch (this.newbieConfiguration.nameTag.type) {
case PREFIX -> display.prefix(this.miniMessage.deserialize(this.newbieConfiguration.nameTag.content));
case SUFFIX -> display.suffix(this.miniMessage.deserialize(this.newbieConfiguration.nameTag.content));
default -> throw new IllegalStateException("Unexpected value: " + this.newbieConfiguration.nameTag.type + " in NameTagType");
default -> {
display.prefix(this.miniMessage.deserialize(this.newbieConfiguration.nameTag.content));

this.logger.warning("Unexpected value: " + this.newbieConfiguration.nameTag.type + " in configuration file in section NameTagType, using prefix as default");
}
}
display.addEntry(player.getName());
this.teamManager.addPlayer(player);

team.display(player, display);

for (Player onlinePlayer : this.server.getOnlinePlayers()) {
if (onlinePlayer.equals(player)) {
continue;
}

this.teamManager.addPlayer(onlinePlayer);
}
}

@Override
Expand Down
Loading

0 comments on commit 3b30951

Please sign in to comment.