From 57c108dcc7531dc8f8e8aa993cb5ffe43256ad27 Mon Sep 17 00:00:00 2001 From: William278 Date: Mon, 27 Mar 2023 12:43:59 +0100 Subject: [PATCH] Update for compatibility with HuskHomes v4 --- bukkit/build.gradle | 2 +- .../husktowns/hook/HuskHomesHook.java | 72 ++++++++++++------- 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/bukkit/build.gradle b/bukkit/build.gradle index 99667c73..55081b40 100644 --- a/bukkit/build.gradle +++ b/bukkit/build.gradle @@ -13,7 +13,7 @@ dependencies { compileOnly 'commons-io:commons-io:2.11.0' compileOnly 'net.william278:Annotaml:2.0.1' compileOnly 'net.william278:DesertWell:1.1.1' - compileOnly 'net.william278:HuskHomes2:3.2.1' + compileOnly 'net.william278:HuskHomes2:4.0.2' compileOnly 'com.github.MilkBowl:VaultAPI:1.7.1' compileOnly 'com.github.Emibergo02:RedisEconomy:4.0-SNAPSHOT' compileOnly 'net.luckperms:api:5.4' diff --git a/bukkit/src/main/java/net/william278/husktowns/hook/HuskHomesHook.java b/bukkit/src/main/java/net/william278/husktowns/hook/HuskHomesHook.java index 211262ce..b3d4955b 100644 --- a/bukkit/src/main/java/net/william278/husktowns/hook/HuskHomesHook.java +++ b/bukkit/src/main/java/net/william278/husktowns/hook/HuskHomesHook.java @@ -1,10 +1,10 @@ package net.william278.husktowns.hook; import net.william278.huskhomes.api.HuskHomesAPI; -import net.william278.huskhomes.event.HomeSaveEvent; -import net.william278.huskhomes.position.Server; -import net.william278.huskhomes.position.World.Environment; -import net.william278.huskhomes.teleport.TimedTeleport; +import net.william278.huskhomes.event.HomeCreateEvent; +import net.william278.huskhomes.event.HomeEditEvent; +import net.william278.huskhomes.teleport.TeleportationException; +import net.william278.huskhomes.user.CommandUser; import net.william278.husktowns.BukkitHuskTowns; import net.william278.husktowns.HuskTowns; import net.william278.husktowns.claim.Position; @@ -38,37 +38,55 @@ public void onEnable() { @Override public void teleport(@NotNull OnlineUser user, @NotNull Position position, @NotNull String server) { - getHuskHomes().ifPresent(api -> api.teleportBuilder(api.adaptUser(((BukkitUser) user).getPlayer())) - .setTarget(new net.william278.huskhomes.position.Position( - position.getX(), - position.getY(), - position.getZ(), - position.getYaw(), - position.getPitch(), - new net.william278.huskhomes.position.World(position.getWorld().getName(), position.getWorld().getUuid()), - new Server(server) - )) - .toTimedTeleport() - .thenAccept(TimedTeleport::execute)); + try { + getHuskHomes().ifPresent(api -> api.teleportBuilder(api.adaptUser(((BukkitUser) user).getPlayer())) + .target(net.william278.huskhomes.position.Position.at( + position.getX(), + position.getY(), + position.getZ(), + position.getYaw(), + position.getPitch(), + net.william278.huskhomes.position.World.from(position.getWorld().getName(), position.getWorld().getUuid()), + server + )) + .toTimedTeleport().execute()); + } catch (TeleportationException e) { + plugin.getLocales().getLocale("error_town_spawn_not_set") + .ifPresent(user::sendMessage); + } } @EventHandler(ignoreCancelled = true) - public void onPlayerSetHome(@NotNull HomeSaveEvent event) { - final Optional user = plugin.getOnlineUsers().stream() - .filter(online -> online.getUuid().equals(event.getHome().owner.uuid)).findFirst(); - if (user.isEmpty()) { - return; + public void onPlayerSetHome(@NotNull HomeCreateEvent event) { + if (this.cancelEvent(event.getCreator(), event.getPosition())) { + event.setCancelled(true); } - final Environment environment = event.getHome().world.environment == null ? Environment.CUSTOM : event.getHome().world.environment; - final Position position = Position.at(event.getHome().x, event.getHome().y, event.getHome().z, - World.of(event.getHome().world.uuid, event.getHome().world.name, - environment.name().toLowerCase())); - if (plugin.getOperationHandler().cancelOperation(Operation - .of(user.get(), Operation.Type.BLOCK_INTERACT, position))) { + } + + @EventHandler(ignoreCancelled = true) + public void onPlayerRelocateHome(@NotNull HomeEditEvent event) { + if (this.cancelEvent(event.getEditor(), event.getHome())) { event.setCancelled(true); } } + private boolean cancelEvent(@NotNull CommandUser creator, @NotNull net.william278.huskhomes.position.Position home) { + final Optional user = creator instanceof net.william278.huskhomes.user.OnlineUser online ? + plugin.getOnlineUsers().stream().filter(u -> u.getUuid().equals(online.getUuid())).findFirst() : + Optional.empty(); + if (user.isEmpty()) { + return false; + } + + final net.william278.huskhomes.position.World world = home.getWorld(); + final Position position = Position.at(home.getX(), home.getY(), home.getZ(), + World.of(world.getUuid(), world.getName(), world.getEnvironment().name()), + home.getYaw(), home.getPitch()); + + return plugin.getOperationHandler().cancelOperation(Operation + .of(user.get(), Operation.Type.BLOCK_INTERACT, position)); + } + private Optional getHuskHomes() { return Optional.ofNullable(api); }