Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 9a38498

Browse files
committed
Continue work on new core
1 parent af2daed commit 9a38498

13 files changed

+384
-39
lines changed

build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ dependencies {
4141

4242
// litecommands
4343
implementation("dev.rollczi.litecommands:bukkit-adventure:2.8.7")
44+
45+
// cdn configs
46+
implementation("net.dzikoysk:cdn:1.14.4")
4447
}
4548

4649
java {
@@ -84,6 +87,7 @@ tasks {
8487
"dev.rollczi.litecommands",
8588
"panda.std",
8689
"net.kyori",
90+
"net.dzikoysk",
8791
).forEach { relocate(it, prefix) }
8892
}
8993
}

src/main/java/com/eternalcode/randomteleport/RandomTeleport.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.eternalcode.randomteleport;
22

3+
import com.eternalcode.randomteleport.config.ConfigManager;
4+
import com.eternalcode.randomteleport.config.impl.ButtonDataConfig;
5+
import com.eternalcode.randomteleport.teleport.TeleportButtonService;
6+
import com.eternalcode.randomteleport.teleport.TeleportController;
37
import com.eternalcode.randomteleport.teleport.TeleportService;
48
import dev.rollczi.litecommands.LiteCommands;
59
import dev.rollczi.litecommands.bukkit.adventure.platform.LiteBukkitAdventurePlatformFactory;
@@ -23,15 +27,21 @@ public void onEnable() {
2327

2428
this.audienceProvider = BukkitAudiences.create(this);
2529

30+
ConfigManager configManager = new ConfigManager(this.getDataFolder());
31+
ButtonDataConfig buttonDataConfig = configManager.load(new ButtonDataConfig());
32+
2633
TeleportService teleportService = new TeleportService();
34+
TeleportButtonService teleportButtonService = new TeleportButtonService();
2735

2836
this.liteCommands = LiteBukkitAdventurePlatformFactory.builder(server, "randomteleport", this.audienceProvider)
2937
.argument(Player.class, new BukkitPlayerArgument<>(this.getServer(), "cant find player"))
3038
.contextualBind(Player.class, new BukkitOnlyPlayerContextual<>("only for console"))
3139

32-
.commandInstance(new RandomTeleportCommand(teleportService))
40+
.commandInstance(new RandomTeleportCommand(teleportService, teleportButtonService, buttonDataConfig, configManager))
3341

3442
.register();
43+
44+
server.getPluginManager().registerEvents(new TeleportController(), this);
3545
}
3646

3747
@Override
Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,48 @@
11
package com.eternalcode.randomteleport;
22

3+
import com.eternalcode.randomteleport.config.ConfigManager;
4+
import com.eternalcode.randomteleport.config.impl.ButtonDataConfig;
5+
import com.eternalcode.randomteleport.shared.Position;
6+
import com.eternalcode.randomteleport.shared.PositionAdapter;
7+
import com.eternalcode.randomteleport.teleport.TeleportButtonPlace;
8+
import com.eternalcode.randomteleport.teleport.TeleportButtonService;
9+
import com.eternalcode.randomteleport.teleport.TeleportButtonType;
310
import com.eternalcode.randomteleport.teleport.TeleportService;
411
import dev.rollczi.litecommands.argument.Arg;
512
import dev.rollczi.litecommands.command.execute.Execute;
613
import dev.rollczi.litecommands.command.route.Route;
14+
import org.bukkit.Location;
715
import org.bukkit.entity.Player;
816

917
@Route(name = "randomteleport", aliases = { "rt", "rtp" })
1018
public class RandomTeleportCommand {
1119

1220
private final TeleportService teleportService;
21+
private final TeleportButtonService teleportButtonService;
1322

14-
public RandomTeleportCommand(TeleportService teleportService) {
23+
private final ButtonDataConfig buttonDataConfig;
24+
private final ConfigManager configManager;
25+
26+
public RandomTeleportCommand(TeleportService teleportService, TeleportButtonService teleportButtonService, ButtonDataConfig buttonDataConfig, ConfigManager configManager) {
1527
this.teleportService = teleportService;
28+
this.teleportButtonService = teleportButtonService;
29+
this.buttonDataConfig = buttonDataConfig;
30+
this.configManager = configManager;
1631
}
1732

1833
@Execute
1934
void randomTeleport(Player player, @Arg int radius) {
2035
this.teleportService.randomTeleportPlayer(player, radius);
2136
}
37+
38+
@Execute(route = "setbutton")
39+
void button(Player player, @Arg String name, @Arg TeleportButtonType type) {
40+
Location buttonLocation = this.teleportButtonService.setButtonsAround(player);
41+
Position convertButtonLocation = PositionAdapter.convert(buttonLocation);
42+
43+
TeleportButtonPlace buttonPlace = new TeleportButtonPlace(name, convertButtonLocation, type);
44+
this.buttonDataConfig.buttons.put(name, buttonPlace);
45+
this.configManager.save(this.buttonDataConfig);
46+
}
2247
}
48+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.eternalcode.randomteleport.config;
2+
3+
import net.dzikoysk.cdn.Cdn;
4+
import net.dzikoysk.cdn.CdnFactory;
5+
6+
import java.io.File;
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
10+
public class ConfigManager {
11+
12+
private final Cdn cdn = CdnFactory
13+
.createYamlLike()
14+
.getSettings()
15+
.build();
16+
17+
private final Set<ReloadableConfig> configs = new HashSet<>();
18+
private final File dataFolder;
19+
20+
public ConfigManager(File dataFolder) {
21+
this.dataFolder = dataFolder;
22+
}
23+
24+
public <T extends ReloadableConfig> T load(T config) {
25+
this.cdn.load(config.resource(this.dataFolder), config)
26+
.orThrow(RuntimeException::new);
27+
28+
this.cdn.render(config, config.resource(this.dataFolder))
29+
.orThrow(RuntimeException::new);
30+
31+
this.configs.add(config);
32+
33+
return config;
34+
}
35+
36+
public <T extends ReloadableConfig> void save(T config) {
37+
this.cdn.render(config, config.resource(this.dataFolder))
38+
.orThrow(RuntimeException::new);
39+
}
40+
41+
public void reload() {
42+
for (ReloadableConfig config : this.configs) {
43+
this.load(config);
44+
}
45+
}
46+
47+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.eternalcode.randomteleport.config;
2+
3+
import net.dzikoysk.cdn.source.Resource;
4+
5+
import java.io.File;
6+
7+
public interface ReloadableConfig {
8+
9+
Resource resource(File folder);
10+
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.eternalcode.randomteleport.config.impl;
2+
3+
import com.eternalcode.randomteleport.config.ReloadableConfig;
4+
import com.eternalcode.randomteleport.teleport.TeleportButtonPlace;
5+
import net.dzikoysk.cdn.source.Resource;
6+
import net.dzikoysk.cdn.source.Source;
7+
8+
import java.io.File;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
public class ButtonDataConfig implements ReloadableConfig {
13+
14+
public Map<String, TeleportButtonPlace> buttons = new HashMap<>();
15+
16+
@Override
17+
public Resource resource(File folder) {
18+
return Source.of(folder, "buttons.yml");
19+
}
20+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.eternalcode.randomteleport.shared;
2+
3+
import java.util.Objects;
4+
import java.util.regex.Matcher;
5+
import java.util.regex.Pattern;
6+
7+
/**
8+
* Disclaimer - Bukkit {@link org.bukkit.Location} storage may cause a memory leak, because it is a wrapper for
9+
* coordinates and {@link org.bukkit.World} reference. If you need to store location use {@link Position} and
10+
* {@link PositionAdapter}.
11+
* */
12+
public final class Position {
13+
14+
public final static String NONE_WORLD = "__NONE__";
15+
16+
private final static Pattern PARSE_FORMAT = Pattern.compile("Position\\{x=(?<x>-?[\\d.]+), y=(?<y>-?[\\d.]+), z=(?<z>-?[\\d.]+), yaw=(?<yaw>-?[\\d.]+), pitch=(?<pitch>-?[\\d.]+), world='(?<world>.+)'}");
17+
18+
private final double x;
19+
private final double y;
20+
private final double z;
21+
private final float yaw;
22+
private final float pitch;
23+
private final String world;
24+
25+
26+
public Position(double x, double y, double z, float yaw, float pitch, String world) {
27+
this.x = x;
28+
this.y = y;
29+
this.z = z;
30+
this.yaw = yaw;
31+
this.pitch = pitch;
32+
this.world = world;
33+
}
34+
35+
public String getWorld() {
36+
return this.world;
37+
}
38+
39+
public double getX() {
40+
return this.x;
41+
}
42+
43+
public double getY() {
44+
return this.y;
45+
}
46+
47+
public double getZ() {
48+
return this.z;
49+
}
50+
51+
public float getYaw() {
52+
return this.yaw;
53+
}
54+
55+
public float getPitch() {
56+
return this.pitch;
57+
}
58+
59+
public boolean isNoneWorld() {
60+
return this.world.equals(NONE_WORLD);
61+
}
62+
63+
@Override
64+
public boolean equals(Object o) {
65+
if (this == o) {
66+
return true;
67+
}
68+
69+
if (o == null || getClass() != o.getClass()) {
70+
return false;
71+
}
72+
73+
Position position = (Position) o;
74+
75+
return Double.compare(position.x, this.x) == 0
76+
&& Double.compare(position.y, this.y) == 0
77+
&& Double.compare(position.z, this.z) == 0
78+
&& Float.compare(position.yaw, this.yaw) == 0
79+
&& Float.compare(position.pitch, this.pitch) == 0
80+
&& this.world.equals(position.world);
81+
}
82+
83+
@Override
84+
public int hashCode() {
85+
return Objects.hash(this.x, this.y, this.z, this.yaw, this.pitch, this.world);
86+
}
87+
88+
@Override
89+
public String toString() {
90+
return "Position{" +
91+
"x=" + this.x +
92+
", y=" + this.y +
93+
", z=" + this.z +
94+
", yaw=" + this.yaw +
95+
", pitch=" + this.pitch +
96+
", world='" + this.world + '\'' +
97+
'}';
98+
}
99+
100+
public static Position parse(String parse) {
101+
Matcher matcher = PARSE_FORMAT.matcher(parse);
102+
103+
if (!matcher.find()) {
104+
throw new IllegalArgumentException("Invalid position format: " + parse);
105+
}
106+
107+
return new Position(
108+
Double.parseDouble(matcher.group("x")),
109+
Double.parseDouble(matcher.group("y")),
110+
Double.parseDouble(matcher.group("z")),
111+
Float.parseFloat(matcher.group("yaw")),
112+
Float.parseFloat(matcher.group("pitch")),
113+
matcher.group("world")
114+
);
115+
}
116+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.eternalcode.randomteleport.shared;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.Location;
5+
import org.bukkit.World;
6+
7+
public final class PositionAdapter {
8+
9+
private PositionAdapter() {}
10+
11+
public static Position convert(Location location) {
12+
if (location.getWorld() == null) {
13+
throw new IllegalStateException("World is not defined");
14+
}
15+
16+
return new Position(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch(), location.getWorld().getName());
17+
}
18+
19+
public static Location convert(Position position) {
20+
World world = Bukkit.getWorld(position.getWorld());
21+
22+
if (world == null) {
23+
throw new IllegalStateException("World is not defined");
24+
}
25+
26+
return new Location(world, position.getX(), position.getY(), position.getZ(), position.getYaw(), position.getPitch());
27+
}
28+
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.eternalcode.randomteleport.teleport;
2+
3+
import com.eternalcode.randomteleport.shared.Position;
4+
import net.dzikoysk.cdn.entity.Contextual;
5+
6+
@Contextual
7+
public final class TeleportButtonPlace {
8+
9+
public String name;
10+
public Position position;
11+
public TeleportButtonType type;
12+
13+
public TeleportButtonPlace(String name, Position position, TeleportButtonType type) {
14+
this.name = name;
15+
this.position = position;
16+
this.type = type;
17+
}
18+
19+
}

0 commit comments

Comments
 (0)