Skip to content

Commit

Permalink
Made quick implementation of a cooldown
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-265 committed Feb 20, 2024
1 parent 528581a commit ddff869
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 11 deletions.
13 changes: 12 additions & 1 deletion src/main/java/at/alex/falexhomes/FalexHomes.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,31 @@
import at.alex.falexhomes.commands.*;
import at.alex.falexhomes.tabcompleter.delHomeCompleter;
import at.alex.falexhomes.tabcompleter.homeCompleter;
import at.alex.falexhomes.utils.PlayerTime;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public final class FalexHomes extends JavaPlugin {
public final class FalexHomes extends JavaPlugin implements Listener {
private File customConfigFile;
private FileConfiguration customConfig;
public static List<PlayerTime> homeCooldown = new ArrayList<>();

@Override
public void onEnable() {
Bukkit.getPluginManager().registerEvents(this, (Plugin)this);
createCustomConfig();
getConfig().options().copyDefaults(true);
saveConfig();
Expand Down
25 changes: 24 additions & 1 deletion src/main/java/at/alex/falexhomes/commands/home.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package at.alex.falexhomes.commands;

import at.alex.falexhomes.FalexHomes;
import at.alex.falexhomes.utils.Chatter;
import at.alex.falexhomes.utils.FileHandler;
import at.alex.falexhomes.utils.PlayerTime;
import at.alex.falexhomes.utils.TimeUtils;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.command.Command;
Expand All @@ -13,6 +16,8 @@
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.NotNull;

import java.security.KeyStore;

public class home implements CommandExecutor {
private String homeName;
Chatter chatter = new Chatter();
Expand Down Expand Up @@ -41,11 +46,29 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}
homeName = args[0];
}
chatter.DebugLogger("CURRENT TIME: " + String.valueOf(TimeUtils.getCurrentTime()));
boolean playerExistsInCooldown = false;
int indexPlayer = -1;
for (PlayerTime i : FalexHomes.homeCooldown) {
if (i.getPlayerUUID().equals(player.getUniqueId().toString())) {
indexPlayer = FalexHomes.homeCooldown.indexOf(i);
}
}
if (indexPlayer != -1) {
PlayerTime timePlayer = FalexHomes.homeCooldown.get(indexPlayer);
int timeSinceLast = TimeUtils.getCurrentTime() - timePlayer.getTime();
if (timeSinceLast < fileHandler.teleportCooldown) {
sender.sendMessage(chatter.getMessageString("Cooldown").replace("%time%", String.valueOf(20 - timeSinceLast)));
return true;
}
FalexHomes.homeCooldown.remove(indexPlayer);
}


PotionEffectType effectType = PotionEffectType.getByName(fileHandler.TeleportEffect.toUpperCase());
PotionEffect teleportationEffect = new PotionEffect(effectType, fileHandler.TeleportEffectDuartion, fileHandler.TelpeortEffectAmplifier, false,false);
player.addPotionEffect(teleportationEffect);

FalexHomes.homeCooldown.add(new PlayerTime(player.getUniqueId().toString(), TimeUtils.getCurrentTime()));


chatter.DebugLogger(homeName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand All @@ -20,15 +21,20 @@ public class delHomeCompleter implements TabCompleter {
FileHandler fileHandler = new FileHandler();
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
//chatter.DebugLogger(Arrays.stream(args).toList().toString());
//chatter.DebugLogger(String.valueOf(Arrays.stream(args).count()));
List<String> homes;
Player player = (Player) sender;
if (fileHandler.GetHomesFromPlayer(player) == null) {
chatter.DebugLogger("Player has no Homes");
return null;
//chatter.DebugLogger("Player has no Homes");
return new ArrayList<>();
}
if (Arrays.stream(args).count() > 1) {
return new ArrayList<>();
}
homes = new ArrayList<>(fileHandler.GetHomesFromPlayer(player));
homes.remove("default");
chatter.DebugLogger(homes.toString());
//chatter.DebugLogger(homes.toString());
String input = args[0].toLowerCase();

List<String> completions = null;
Expand All @@ -43,6 +49,9 @@ public class delHomeCompleter implements TabCompleter {
}
if (completions != null)
Collections.sort(completions);
else {
return new ArrayList<>();
}
return completions;
}
}
15 changes: 9 additions & 6 deletions src/main/java/at/alex/falexhomes/tabcompleter/homeCompleter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ public class homeCompleter implements TabCompleter {

@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
chatter.DebugLogger(Arrays.stream(args).toList().toString());
chatter.DebugLogger(String.valueOf(Arrays.stream(args).count()));
// chatter.DebugLogger(Arrays.stream(args).toList().toString());
// chatter.DebugLogger(String.valueOf(Arrays.stream(args).count()));
List<String> homes;
Player player = (Player) sender;
if (fileHandler.GetHomesFromPlayer(player) == null) {
chatter.DebugLogger("Player has no Homes");
return null;
// chatter.DebugLogger("Player has no Homes");
return new ArrayList<>();
}
if (Arrays.stream(args).count() > 1) {
return null;
return new ArrayList<>();
}
homes = new ArrayList<>(fileHandler.GetHomesFromPlayer(player));
homes.remove("default");
chatter.DebugLogger(homes.toString());
// chatter.DebugLogger(homes.toString());
String input = args[0].toLowerCase();

List<String> completions = null;
Expand All @@ -46,6 +46,9 @@ public class homeCompleter implements TabCompleter {
}
if (completions != null)
Collections.sort(completions);
else {
return new ArrayList<>();
}
return completions;
}
}
1 change: 1 addition & 0 deletions src/main/java/at/alex/falexhomes/utils/FileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public int GetTotalHomeCount(Player player) {
public int TelpeortEffectAmplifier = plugin.getConfig().getInt("Teleportation-Effect.amplifier");

public String TeleportSound = plugin.getConfig().getString("Teleportation-Sound.type");
public int teleportCooldown = plugin.getConfig().getInt("Teleportation.cooldown");


}
28 changes: 28 additions & 0 deletions src/main/java/at/alex/falexhomes/utils/PlayerTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package at.alex.falexhomes.utils;


public class PlayerTime {
private String playerUUID;
private int gameTime;

public PlayerTime(String playerName, int gameTime) {
this.playerUUID = playerName;
this.gameTime = gameTime;
}

public String getPlayerUUID() {
return playerUUID;
}

public int getTime() {
return gameTime;
}

public void setPlayerUUID(String playerName) {
this.playerUUID = playerName;
}

public void setTime(int gameTime) {
this.gameTime = gameTime;
}
}
12 changes: 12 additions & 0 deletions src/main/java/at/alex/falexhomes/utils/TimeUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package at.alex.falexhomes.utils;

import java.util.Date;

public class TimeUtils {
public static int getCurrentTime() {
// Ab 2038 gibt es ein problem
Date date = new Date();
Long longTime = new Long(date.getTime()/1000);
return longTime.intValue();
}
}
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ Language:
HomeDeleted: Home %HomeName% was deleted!
HomeListStatistic: Used %Statistic%
HomeListMessage: Your Homes %listhomes%
Cooldown: You have to wait %time% seconds

General:
MaxHomes: 5

Teleportation:
cooldown: 20
Teleportation-Effect:
type: blindness
duaration: 60
Expand Down

0 comments on commit ddff869

Please sign in to comment.