Skip to content

Commit

Permalink
Concurrent
Browse files Browse the repository at this point in the history
  • Loading branch information
PetteriM1 committed Jul 15, 2024
1 parent 0a485ff commit b953395
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 45 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>cn.yescallop.essentialsnk</groupId>
<artifactId>essentialsnk</artifactId>
<version>1.9.2-IGN.20</version>
<version>1.9.2-IGN.21</version>

<name>EssentialsNK</name>
<description>Essential commands for your server</description>
Expand Down
54 changes: 26 additions & 28 deletions src/main/java/cn/yescallop/essentialsnk/EssentialsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import cn.nukkit.nbt.tag.DoubleTag;
import cn.nukkit.nbt.tag.FloatTag;
import cn.nukkit.nbt.tag.ListTag;
import cn.nukkit.permission.PermissionAttachmentInfo;
import cn.nukkit.plugin.PluginLogger;
import cn.nukkit.utils.Config;
import cn.nukkit.utils.ConfigSection;
Expand All @@ -31,29 +30,26 @@
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.concurrent.ConcurrentHashMap;

public class EssentialsAPI {

private static final int/*long*/ TP_EXPIRATION = 1200;//TimeUnit.MINUTES.toMillis(1);
private static final Pattern COOLDOWN_PATTERN = Pattern.compile("^essentialsnk\\.cooldown\\.([0-9]+)$", Pattern.CASE_INSENSITIVE);
private static final Pattern TP_COOLDOWN_PATTERN = Pattern.compile("^essentialsnk\\.tp\\.cooldown\\.([0-9]+)$", Pattern.CASE_INSENSITIVE);
private static final Pattern HOMES_PERMISSION_PATTERN = Pattern.compile("^essentialsnk\\.homes\\.([0-9]+)$", Pattern.CASE_INSENSITIVE);
private static final int/*long*/ TP_EXPIRATION = 1200;//TimeUnit.MINUTES.toMillis(1); -- ticks
//private static final Pattern COOLDOWN_PATTERN = Pattern.compile("^essentialsnk\\.cooldown\\.([0-9]+)$", Pattern.CASE_INSENSITIVE);
//private static final Pattern TP_COOLDOWN_PATTERN = Pattern.compile("^essentialsnk\\.tp\\.cooldown\\.([0-9]+)$", Pattern.CASE_INSENSITIVE);
//private static final Pattern HOMES_PERMISSION_PATTERN = Pattern.compile("^essentialsnk\\.homes\\.([0-9]+)$", Pattern.CASE_INSENSITIVE);
public static final Integer[] NON_SOLID_BLOCKS = new Integer[]{Block.AIR, Block.SAPLING, Block.WATER, Block.STILL_WATER, Block.LAVA, Block.STILL_LAVA, Block.COBWEB, Block.TALL_GRASS, Block.BUSH, Block.DANDELION,
Block.POPPY, Block.BROWN_MUSHROOM, Block.RED_MUSHROOM, Block.TORCH, Block.FIRE, Block.WHEAT_BLOCK, Block.SIGN_POST, Block.WALL_SIGN, Block.SUGARCANE_BLOCK,
Block.PUMPKIN_STEM, Block.MELON_STEM, Block.VINE, Block.CARROT_BLOCK, Block.POTATO_BLOCK, Block.DOUBLE_PLANT};
private static EssentialsAPI instance = null;
private static EssentialsAPI instance;
private static final Duration THIRTY_DAYS = Duration.ZERO.plusDays(30);
private final Vector3 temporalVector = new Vector3();
private final EssentialsNK plugin;
private final Map<CommandSender, Long> cooldown = new IdentityHashMap<>();
//private final Map<CommandSender, Long> cooldown = new IdentityHashMap<>();
private final List<TPCooldown> tpCooldowns = new ArrayList<>();
private final Map<Player, Location> playerLastLocation = new HashMap<>();
private final Map<Integer, TPRequest> tpRequests = new /*Concurrent*/HashMap<>();
private final Set<String> vanishedPlayers = new HashSet<>();
private final Map<String, String> lastMessagedPlayers = new HashMap<>();
private final Map<Player, Location> playerLastLocation = new ConcurrentHashMap<>();
private final Map<Integer, TPRequest> tpRequests = new ConcurrentHashMap<>();
private final Set<String> vanishedPlayers = ConcurrentHashMap.newKeySet();
private final Map<String, String> lastMessagedPlayers = new ConcurrentHashMap<>();

private final ConfigType homeConfig = null;
private final ConfigType warpConfig;
Expand Down Expand Up @@ -115,7 +111,7 @@ public Location getLastLocation(Player player) {
}

public boolean hasCooldown(CommandSender sender) {
long cooldown = Long.MAX_VALUE;
/*long cooldown = Long.MAX_VALUE;
for (PermissionAttachmentInfo info : sender.getEffectivePermissions().values()) {
Matcher matcher = COOLDOWN_PATTERN.matcher(info.getPermission());
if (matcher.find()) {
Expand All @@ -137,11 +133,11 @@ public boolean hasCooldown(CommandSender sender) {
sender.sendMessage(Language.translate("commands.generic.cooldown", timeLeft));
return true;
}
}
}*/
return false;
}

private OptionalInt hasTPCooldown(Player player) {
/*private OptionalInt hasTPCooldown(Player player) {
int cooldown = Integer.MAX_VALUE;
for (PermissionAttachmentInfo info : player.getEffectivePermissions().values()) {
Matcher matcher = TP_COOLDOWN_PATTERN.matcher(info.getPermission());
Expand All @@ -157,10 +153,10 @@ private OptionalInt hasTPCooldown(Player player) {
return OptionalInt.of(cooldown);
}
return OptionalInt.empty();
}
}*/

public OptionalInt getAllowedHomes(Player player) {
int homes = 0;
/*int homes = 0;
for (PermissionAttachmentInfo info : player.getEffectivePermissions().values()) {
Matcher matcher = HOMES_PERMISSION_PATTERN.matcher(info.getPermission());
if (matcher.find()) {
Expand All @@ -173,7 +169,7 @@ public OptionalInt getAllowedHomes(Player player) {
if (!player.isOp() && homes > 0) {
return OptionalInt.of(homes);
}
}*/
return OptionalInt.empty();
}

Expand All @@ -182,16 +178,16 @@ public void onTP(Player player, Position position, String message) {
}

public void onTP(Player player, Location location, String message) {
OptionalInt cooldown = hasTPCooldown(player);
/*OptionalInt cooldown = hasTPCooldown(player);
if (cooldown.isPresent()) {
player.sendMessage(Language.translate("commands.generic.teleporation.cooldown", cooldown.getAsInt()));
tpCooldowns.add(new TPCooldown(player, location,
System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(cooldown.getAsInt()), message));
} else {
} else {*/
player.level.threadedExecutor.execute(() -> player.teleport(location));
player.sendMessage(message);
}
//}
}

public List<TPCooldown> getTpCooldowns() {
Expand Down Expand Up @@ -476,9 +472,10 @@ public Position getStandablePositionAt(Position pos) {
int x = pos.getFloorX();
int y = pos.getFloorY() + 1;
int z = pos.getFloorZ();
Vector3 temporalVector = new Vector3();
for (; y <= 128; y++) {
if (!pos.level.getBlock(this.temporalVector.setComponents(x, y, z)).isSolid() && !pos.level.getBlock(this.temporalVector.setComponents(x, y + 1, z)).isSolid()) {
return new Position(x + 0.5, pos.level.getBlock(this.temporalVector.setComponents(x, y - 1, z)).getBoundingBox().getMaxY(), z + 0.5, pos.level);
if (!pos.level.getBlock(temporalVector.setComponents(x, y, z)).isSolid() && !pos.level.getBlock(temporalVector.setComponents(x, y + 1, z)).isSolid()) {
return new Position(x + 0.5, pos.level.getBlock(temporalVector.setComponents(x, y - 1, z)).getBoundingBox().getMaxY(), z + 0.5, pos.level);
}
}
return null;
Expand All @@ -487,9 +484,10 @@ public Position getStandablePositionAt(Position pos) {
public Position getHighestStandablePositionAt(Position pos) {
int x = pos.getFloorX();
int z = pos.getFloorZ();
Vector3 temporalVector = new Vector3();
for (int y = 127; y >= 0; y--) {
if (pos.level.getBlock(this.temporalVector.setComponents(x, y, z)).isSolid()) {
return new Position(x + 0.5, pos.level.getBlock(this.temporalVector.setComponents(x, y, z)).getBoundingBox().getMaxY(), z + 0.5, pos.level);
if (pos.level.getBlock(temporalVector.setComponents(x, y, z)).isSolid()) {
return new Position(x + 0.5, pos.level.getBlock(temporalVector.setComponents(x, y, z)).getBoundingBox().getMaxY(), z + 0.5, pos.level);
}
}
return null;
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/cn/yescallop/essentialsnk/EssentialsNK.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package cn.yescallop.essentialsnk;

import cn.nukkit.plugin.PluginBase;
import cn.nukkit.scheduler.TaskHandler;
import cn.yescallop.essentialsnk.command.CommandManager;
import cn.yescallop.essentialsnk.task.TeleportationTask;

public class EssentialsNK extends PluginBase {

private EssentialsAPI api;
private TaskHandler task;
//private TaskHandler task;
public static EssentialsNK instance;

@Override
Expand All @@ -19,11 +17,11 @@ public void onEnable() {
this.api = new EssentialsAPI(this);
CommandManager.registerAll(this.api);
this.getServer().getPluginManager().registerEvents(new EventListener(this.api), this);
task = this.getServer().getScheduler().scheduleRepeatingTask(this, new TeleportationTask(api), 1);
//task = this.getServer().getScheduler().scheduleRepeatingTask(this, new TeleportationTask(api), 1);
}

@Override
/*@Override
public void onDisable() {
task.cancel();
}
}*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class GamemodeCommand extends CommandBase {

public GamemodeCommand(EssentialsAPI api) {
super("gamemode", api);
this.setAliases(new String[]{"gm", "gma", "gmc", "gms", "gmsp", "gmt", "adventure", "creative", "survival", "spectator", "viewer"});
this.setAliases(new String[]{"gm", "gma", "gmc", "gms", "gmsp", "gmt"/*, "adventure", "creative", "survival", "spectator", "viewer"*/});

// command parameters
commandParameters.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MessageCommand extends CommandBase {

public MessageCommand(EssentialsAPI api) {
super("message", api);
this.setAliases(new String[]{"m", "msg", "w", "whisper", "tell", "privatemessage", "pm"});
this.setAliases(new String[]{"m", "msg", "w", "whisper", "tell"/*, "privatemessage", "pm"*/});

// command parameters
this.commandParameters.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import cn.nukkit.scheduler.Task;
import cn.yescallop.essentialsnk.EssentialsAPI;
import cn.yescallop.essentialsnk.TPCooldown;

import java.util.Iterator;

public class TeleportationTask extends Task {
private final EssentialsAPI api;
Expand All @@ -15,7 +12,7 @@ public TeleportationTask(EssentialsAPI api) {

@Override
public void onRun(int i) {
Iterator<TPCooldown> iter = api.getTpCooldowns().iterator();
/*Iterator<TPCooldown> iter = api.getTpCooldowns().iterator();
long time = System.currentTimeMillis();
while (iter.hasNext()) {
Expand All @@ -24,6 +21,6 @@ public void onRun(int i) {
cooldown.execute();
iter.remove();
}
}
}*/
}
}
6 changes: 3 additions & 3 deletions src/main/resources/lang/eng.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ commands.burn.description=Set a player on fire
commands.burn.usage=/burn <player> <seconds>
commands.burn.success={%0} is now on fire!
commands.clearinventory.description=Clear your or other's inventory
commands.clearinventory.description=Remove all items from inventory
commands.clearinventory.usage=/clearinventory [player]
commands.clearinventory.success=Your inventory has been cleared
commands.clearinventory.success.other={%0}'s inventory has been cleared
Expand Down Expand Up @@ -100,7 +100,7 @@ commands.home.list=Available homes:
commands.home.notexists=Cannot find home {%0}
commands.home.success=Teleporting to home {%0}...
commands.ignore.description=Ignore all messages from a specified player
commands.ignore.description=Ignore messages from a specified player
commands.ignore.usage=/ignore <player>
commands.ignore.self=You cannot ignore yourself!
Expand All @@ -123,7 +123,7 @@ commands.lightning.description=Strike a lightning!
commands.lightning.usage=/lightning [player]
commands.lightning.success=Lightning summoned!

commands.message.description=Send a private message to the specified player
commands.message.description=Sends a private message to the given player
commands.message.usage=/msg <player> <message>
commands.message.toformat=[me -> {%0}] {%1}
commands.message.fromformat=[{%0} -> me] {%1}
Expand Down

0 comments on commit b953395

Please sign in to comment.