Skip to content

Commit

Permalink
1.0.12 Bugfixes (#33)
Browse files Browse the repository at this point in the history
* Fix randomjoin not being random

* Fix lobby world not loading non-default worlds

This commit will address the issue sending a player to a world that was
not loaded/created before.

* Append -bugfixes suffix to version

* Fix empty messages being shown

* Replace outdated links with new ones

* Add commit patch to version

* Fix improper island positioning

Due to island position being inaccurate, it caused some islands to clash together.

This commit solves #31 issue.

* Add debug commands for islands
  • Loading branch information
Tofpu authored Aug 23, 2023
1 parent 03bab9d commit 56546db
Show file tree
Hide file tree
Showing 18 changed files with 386 additions and 27 deletions.
20 changes: 19 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ allprojects {
}

group = "io.tofpu.speedbridge2"
version = "1.1.0"
version = "1.1.0-" + "git rev-parse --short=8 HEAD".runCommand(rootDir)

repositories {
mavenLocal()
Expand Down Expand Up @@ -72,3 +72,21 @@ allprojects {
}
}
}

fun String.runCommand(
workingDir: File = File("."),
timeoutAmount: Long = 60,
timeoutUnit: TimeUnit = TimeUnit.SECONDS
): String = ProcessBuilder(split("\\s(?=(?:[^'\"`]*(['\"`])[^'\"`]*\\1)*[^'\"`]*$)".toRegex()))
.directory(workingDir)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
.apply { waitFor(timeoutAmount, timeoutUnit) }
.run {
val error = errorStream.bufferedReader().readText().trim()
if (error.isNotEmpty()) {
throw Exception(error)
}
inputStream.bufferedReader().readText().trim()
}
2 changes: 1 addition & 1 deletion speedbridge2-spigot-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id("xyz.jpenilla.run-paper") version "1.0.6"
id("xyz.jpenilla.run-paper") version "2.0.1"
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.bukkit.plugin.java.JavaPlugin;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;

public final class SpeedBridge {
private static BukkitAudiences adventure;
Expand Down Expand Up @@ -71,13 +73,18 @@ public void enable() {
arenaManager, leaderboard);
DynamicClass.alternativeScan(getClass().getClassLoader(), "io.tofpu" +
".speedbridge2");
} catch (final IOException | NoClassDefFoundError e) {

Field objectMap = DynamicClass.class.getDeclaredField("OBJECT_MAP");
objectMap.setAccessible(true);
Map<Class<?>, Object> o = (Map<Class<?>, Object>) objectMap.get(null);
BridgeUtil.debug("key set of dynamic class: " + o.keySet());
} catch (final IOException | NoClassDefFoundError | NoSuchFieldException | IllegalAccessException e) {
throw new IllegalStateException(e);
}

if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
ExpansionHandler.INSTANCE.load();

log("Hooking into PlaceholderAPI...");
new PluginExpansion(javaPlugin, playerService);
}
Expand All @@ -88,7 +95,7 @@ public void enable() {
IslandSetupHandler.INSTANCE.load();

log("Registering the commands...");
CommandManager.load(javaPlugin, playerService, islandService);
CommandManager.load(javaPlugin, playerService, islandService, arenaManager);

log("Loading the Block Menu GUI.");
BlockMenuManager.INSTANCE.load();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import io.tofpu.speedbridge2.command.parser.LampParseRegistry;
import io.tofpu.speedbridge2.command.subcommand.CommandCompletion;
import io.tofpu.speedbridge2.command.subcommand.SpeedBridgeCommand;
import io.tofpu.speedbridge2.command.subcommand.debug.SpeedBridgeDebugCommand;
import io.tofpu.speedbridge2.model.common.util.BridgeUtil;
import io.tofpu.speedbridge2.model.island.IslandService;
import io.tofpu.speedbridge2.model.island.arena.ArenaManager;
import io.tofpu.speedbridge2.model.island.object.Island;
import io.tofpu.speedbridge2.model.player.PlayerFactory;
import io.tofpu.speedbridge2.model.player.PlayerService;
Expand All @@ -31,8 +33,8 @@ public final class CommandManager {
private static BukkitCommandHandler commandHandler;

public static void load(final @NotNull Plugin plugin,
final @NotNull PlayerService playerService,
final @NotNull IslandService islandService) {
final @NotNull PlayerService playerService,
final @NotNull IslandService islandService, ArenaManager arenaManager) {
commandHandler = BukkitCommandHandler.create(plugin);

commandHandler.registerResponseHandler(String.class, (response, actor, command) -> {
Expand Down Expand Up @@ -83,6 +85,7 @@ public boolean isCustomType(final Class<?> type) {
constructCommandConditions();

commandHandler.register(new SpeedBridgeCommand(playerService, islandService));
commandHandler.register(new SpeedBridgeDebugCommand(arenaManager));
}

private static void constructTabCompleter(final @NotNull IslandService islandService) {
Expand All @@ -96,8 +99,10 @@ private static void constructTabCompleter(final @NotNull IslandService islandSer
private static void constructContext() {
final AbstractLampRegistry<?, AbstractLampContext<?>> registry =
DynamicClass.getInstance(LampContextRegistry.class);

if (registry == null) {
return;
BridgeUtil.debug("Unable to find LampContextRegistry instance... shutting down!");
throw new RuntimeException("Unable to find LampContextRegistry instance... shutting down now!");
}

BridgeUtil.debug("Constructing contexts...");
Expand All @@ -113,7 +118,8 @@ private static void constructContext() {
private static void constructParsers() {
final AbstractLampRegistry<?, AbstractLampParser<?>> lampParseRegistry = DynamicClass.getInstance(LampParseRegistry.class);
if (lampParseRegistry == null) {
return;
BridgeUtil.debug("Unable to find LampParseRegistry instance... shutting down!");
throw new RuntimeException("Unable to find LampParseRegistry instance... shutting down now!");
}

BridgeUtil.debug("Constructing parsers...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class HelpCommandGenerator {
private static final String KEY_STYLE = "<yellow>%s";
private static final String VALUE_STYLE = "<white>%s";
private static final String COMMAND_STYLE = "<yellow>/sb %s %s<dark_gray>- <white>%s";
private static final String DISCORD_LINK = "https://discord.gg/cDQjsHugPw";

private static Component helpMessageComponent = null;

Expand Down Expand Up @@ -65,8 +66,7 @@ public static void generateHelpCommand(final @NotNull Plugin plugin) {

builder.title(String.format(TITLE, "Support"))
.pair(String.format(KEY_STYLE, "Discord"), String.format(VALUE_STYLE,
"<click:OPEN_URL:https://tofpu" + ".me/discord>tofpu" +
".me/discord"));
"<click:OPEN_URL:" + DISCORD_LINK + ">" + DISCORD_LINK));

return builder.build();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import revxrsal.commands.annotation.AutoComplete;
import revxrsal.commands.annotation.Command;
import revxrsal.commands.annotation.Default;
Expand All @@ -39,7 +40,9 @@
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import static io.tofpu.speedbridge2.model.common.Message.INSTANCE;
import static io.tofpu.speedbridge2.model.common.util.MessageUtil.Symbols.ARROW_RIGHT;
Expand Down Expand Up @@ -77,9 +80,7 @@ public void onLobbySet(final BridgePlayer bridgePlayer) {
ConfigurationManager.INSTANCE.getLobbyCategory()
.setLobbyLocation(bridgePlayer.getPlayer()
.getLocation())
.whenComplete((unused, throwable) -> {
BridgeUtil.sendMessage(bridgePlayer, INSTANCE.lobbySetLocation);
});
.thenRun(() -> BridgeUtil.sendMessage(bridgePlayer, INSTANCE.lobbySetLocation));
}

@Subcommand("create")
Expand Down Expand Up @@ -346,11 +347,7 @@ public String onRandomJoin(final BridgePlayer bridgePlayer) {
return INSTANCE.alreadyInAIsland;
}

final Optional<Island> optionalIsland = islandService.getAllIslands()
.stream()
.parallel()
.filter(Island::isReady)
.findAny();
final Optional<Island> optionalIsland = getRandomIsland();

if (!optionalIsland.isPresent()) {
return INSTANCE.noAvailableIsland;
Expand All @@ -362,6 +359,17 @@ public String onRandomJoin(final BridgePlayer bridgePlayer) {
return String.format(INSTANCE.joinedAnIsland, island.getSlot() + "");
}

@NotNull
private Optional<Island> getRandomIsland() {
final List<Island> filteredIslands = islandService.getAllIslands()
.stream()
.parallel()
.filter(Island::isReady)
.collect(Collectors.toList());

return Optional.ofNullable(filteredIslands.get(ThreadLocalRandom.current().nextInt(filteredIslands.size())));
}

@Subcommand("setup")
@Description("Create an island setup")
@CommandPermission("speedbridge.setup.admin")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.tofpu.speedbridge2.command.subcommand.debug;

import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.function.operation.ForwardExtentCopy;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.Operations;
import io.tofpu.multiworldedit.EditSessionWrapper;
import io.tofpu.multiworldedit.MultiWorldEditAPI;
import io.tofpu.speedbridge2.model.common.util.BridgeUtil;
import io.tofpu.speedbridge2.model.island.object.land.IslandLand;

import java.io.IOException;

public class DestroyableLand extends IslandLand {
private EditSession editSession;

public DestroyableLand(IslandLand islandLand) {
super(islandLand.getIsland(), islandLand.getWorld(), islandLand.getPositions());
}

@Override
public void generatePlot() throws WorldEditException {
// TODO: Make this generation operation async
BridgeUtil.debug("Generating plot at " + getPlotLocation().toString() + " for " +
"island " + getIsland().getSlot());

final BukkitWorld bukkitWorld = new BukkitWorld(getWorld());

try (final EditSessionWrapper editSessionWrapper = MultiWorldEditAPI.getMultiWorldEdit()
.create(bukkitWorld, -1)) {
final Clipboard schematicClipboard = getIsland().getSchematicClipboard();
this.editSession = editSessionWrapper.to();

final Operation operation = MultiWorldEditAPI.getMultiWorldEdit()
.create(schematicClipboard, editSession, bukkitWorld)
.to((int) getX(), (int) getY(), (int) getZ())
.ignoreAirBlocks(true)
.build();

Operations.completeLegacy(operation);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}

public void destroy() {
// TODO: Make this generation operation async
BridgeUtil.debug("Generating plot at " + getPlotLocation().toString() + " for " +
"island " + getIsland().getSlot());

final BukkitWorld bukkitWorld = new BukkitWorld(getWorld());

try (final EditSessionWrapper editSessionWrapper = MultiWorldEditAPI.getMultiWorldEdit()
.create(bukkitWorld, -1)) {
EditSession session = editSessionWrapper.to();
this.editSession.undo(session);
// session.undo(this.editSession);
} catch (IOException e) {
throw new IllegalStateException(e);
}
this.free();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.tofpu.speedbridge2.command.subcommand.debug;

import io.tofpu.speedbridge2.model.player.object.BridgePlayer;

import java.util.UUID;

public class EmptyBridgePlayer extends BridgePlayer {
protected EmptyBridgePlayer(UUID id) {
super(null, null, id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.tofpu.speedbridge2.command.subcommand.debug;

import io.tofpu.speedbridge2.model.island.arena.ArenaManager;
import io.tofpu.speedbridge2.model.island.object.GameIsland;
import io.tofpu.speedbridge2.model.island.object.land.IslandLand;

public class GameIsland2 extends GameIsland {
private IslandLand islandLand;

public GameIsland2(final ArenaManager arenaManager, GameIsland gameIsland) {
super(arenaManager, gameIsland.getIsland(), gameIsland.getGamePlayer());
}

public void setIslandPlot(final IslandLand islandLand) {
this.islandLand = islandLand;
}

@Override
public IslandLand getIslandPlot() {
return this.islandLand;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package io.tofpu.speedbridge2.command.subcommand.debug;

import com.sk89q.worldedit.WorldEditException;
import io.tofpu.speedbridge2.model.island.IslandFactory;
import io.tofpu.speedbridge2.model.island.arena.ArenaManager;
import io.tofpu.speedbridge2.model.island.object.GameIsland;
import io.tofpu.speedbridge2.model.island.object.Island;
import io.tofpu.speedbridge2.model.island.object.land.IslandLand;
import io.tofpu.speedbridge2.model.player.object.GamePlayer;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import revxrsal.commands.annotation.Command;
import revxrsal.commands.annotation.Default;
import revxrsal.commands.annotation.Subcommand;
import revxrsal.commands.annotation.Usage;
import revxrsal.commands.bukkit.annotation.CommandPermission;

import java.util.*;

@Command("speedbridge debug")
@CommandPermission("speedbridge.debug")
public class SpeedBridgeDebugCommand {
private final Map<Island, List<GameIsland>> generatedGames = new HashMap<>();
private final ArenaManager arenaManager;

public SpeedBridgeDebugCommand(ArenaManager arenaManager) {
this.arenaManager = arenaManager;
}

@Subcommand("arena teleport")
public String islandTeleport(final Player player) {
double[] positions = arenaManager.getPositions();
player.teleport(new Location(this.arenaManager.getWorld(), positions[0], positions[1], positions[2]));
return String.format("Teleported you to world %s with %s coordinates", arenaManager.getWorld().getName(), positions);
}

@Subcommand("island generate")
@Usage("island generate <island> [amount]")
public String generateGame(final Island island, final @Default("1") int amount) {
for (int i = 0; i < amount; i++) {
generateGame(island);
}
return String.format("Generated %s games of island type %s", amount, island.getSlot());
}

@Subcommand("island destroy")
@Usage("island destroy")
public String islandClear(final Island island) {
int gameAmountDestroyed = destroyGames(island);
int islandType = island.getSlot();
return String.format("Destroyed %s games of island type %s!", gameAmountDestroyed, islandType);
}

private int destroyGames(final Island island) {
List<GameIsland> games = this.generatedGames.get(island);
games.forEach(game -> {
DestroyableLand islandPlot = (DestroyableLand) game.getIslandPlot();
islandPlot.destroy();
});
return games.size();
}

public void generateGame(final Island island) {
final GamePlayer gamePlayer = GamePlayer.of(new EmptyBridgePlayer(UUID.randomUUID()));
final GameIsland2 gameIsland = new GameIsland2(arenaManager, IslandFactory.createGame(island, gamePlayer));

DestroyableLand islandLand = new DestroyableLand(arenaManager.justReservePlot(gameIsland));
try {
islandLand.generatePlot();
} catch (WorldEditException e) {
throw new RuntimeException(e);
}

gameIsland.setIslandPlot(islandLand);

this.generatedGames.compute(island, (island1, gameIslands) -> {
if (gameIslands == null) {
gameIslands = new ArrayList<>();
}
gameIslands.add(gameIsland);
return gameIslands;
});
}
}
Loading

0 comments on commit 56546db

Please sign in to comment.