Skip to content

Commit

Permalink
Update for compatibility with HuskHomes v4
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Mar 27, 2023
1 parent c2cfe63 commit 57c108d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
2 changes: 1 addition & 1 deletion bukkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<? extends OnlineUser> 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<? extends OnlineUser> 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<HuskHomesAPI> getHuskHomes() {
return Optional.ofNullable(api);
}
Expand Down

0 comments on commit 57c108d

Please sign in to comment.