From c784c7fc40774ecdc3c787bcd16854c9c5dd877f Mon Sep 17 00:00:00 2001 From: alazeprt Date: Sat, 24 Aug 2024 08:18:01 +0800 Subject: [PATCH 1/4] Add rtp event --- .../event/BukkitEventDispatcher.java | 6 +- .../huskhomes/event/RandomTeleportEvent.java | 31 ++++++++++ .../huskhomes/command/RTPCommand.java | 1 + .../huskhomes/event/IRandomTeleportEvent.java | 11 ++++ .../huskhomes/teleport/Teleport.java | 3 +- .../event/RandomTeleportCallback.java | 61 +++++++++++++++++++ 6 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 bukkit/src/main/java/net/william278/huskhomes/event/RandomTeleportEvent.java create mode 100644 common/src/main/java/net/william278/huskhomes/event/IRandomTeleportEvent.java create mode 100644 fabric/src/main/java/net/william278/huskhomes/event/RandomTeleportCallback.java diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java b/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java index 1afc7808..ef87a8b8 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java +++ b/bukkit/src/main/java/net/william278/huskhomes/event/BukkitEventDispatcher.java @@ -46,7 +46,11 @@ default boolean fireIsCancelled(@NotNull T event) { @Override @NotNull default ITeleportEvent getTeleportEvent(@NotNull Teleport teleport) { - return teleport.getType() == Teleport.Type.BACK ? new TeleportBackEvent(teleport) : new TeleportEvent(teleport); + return switch (teleport.getType()) { + case BACK -> new TeleportBackEvent(teleport); + case RANDOM_TELEPORT -> new RandomTeleportEvent(teleport); + default -> new TeleportEvent(teleport); + }; } @Override diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/RandomTeleportEvent.java b/bukkit/src/main/java/net/william278/huskhomes/event/RandomTeleportEvent.java new file mode 100644 index 00000000..e5e56797 --- /dev/null +++ b/bukkit/src/main/java/net/william278/huskhomes/event/RandomTeleportEvent.java @@ -0,0 +1,31 @@ +package net.william278.huskhomes.event; + +import net.william278.huskhomes.position.Position; +import net.william278.huskhomes.teleport.Teleport; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class RandomTeleportEvent extends TeleportEvent implements IRandomTeleportEvent, Cancellable { + private static final HandlerList HANDLER_LIST = new HandlerList(); + + public RandomTeleportEvent(@NotNull Teleport teleport) { + super(teleport); + } + + @Override + @NotNull + public Position getPosition() { + return (Position) getTeleport().getTarget(); + } + + @NotNull + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + @SuppressWarnings("unused") + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } +} diff --git a/common/src/main/java/net/william278/huskhomes/command/RTPCommand.java b/common/src/main/java/net/william278/huskhomes/command/RTPCommand.java index cea3ed74..862f68dd 100644 --- a/common/src/main/java/net/william278/huskhomes/command/RTPCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/RTPCommand.java @@ -189,6 +189,7 @@ private void performLocalRTP(@NotNull OnlineUser teleporter, @NotNull CommandUse // Build and execute the teleport final TeleportBuilder builder = Teleport.builder(plugin) .teleporter(teleporter) + .type(Teleport.Type.RANDOM_TELEPORT) .actions(TransactionResolver.Action.RANDOM_TELEPORT) .target(position.get()); builder.buildAndComplete(executor.equals(teleporter), args); diff --git a/common/src/main/java/net/william278/huskhomes/event/IRandomTeleportEvent.java b/common/src/main/java/net/william278/huskhomes/event/IRandomTeleportEvent.java new file mode 100644 index 00000000..5b0dbbc8 --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/event/IRandomTeleportEvent.java @@ -0,0 +1,11 @@ +package net.william278.huskhomes.event; + +import net.william278.huskhomes.position.Position; +import org.jetbrains.annotations.NotNull; + +public interface IRandomTeleportEvent extends ITeleportEvent { + + @NotNull + Position getPosition(); + +} \ No newline at end of file diff --git a/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java b/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java index 9f383baf..bd669e77 100644 --- a/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java +++ b/common/src/main/java/net/william278/huskhomes/teleport/Teleport.java @@ -203,7 +203,8 @@ private void performTransactions() { public enum Type { TELEPORT(0), RESPAWN(1), - BACK(2); + BACK(2), + RANDOM_TELEPORT(3); private final int typeId; diff --git a/fabric/src/main/java/net/william278/huskhomes/event/RandomTeleportCallback.java b/fabric/src/main/java/net/william278/huskhomes/event/RandomTeleportCallback.java new file mode 100644 index 00000000..083a1d52 --- /dev/null +++ b/fabric/src/main/java/net/william278/huskhomes/event/RandomTeleportCallback.java @@ -0,0 +1,61 @@ +package net.william278.huskhomes.event; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.util.ActionResult; +import net.william278.huskhomes.position.Position; +import net.william278.huskhomes.teleport.Teleport; +import org.jetbrains.annotations.NotNull; + +import java.util.function.Function; + +public interface RandomTeleportCallback extends FabricEventCallback { + @NotNull + net.fabricmc.fabric.api.event.Event EVENT = EventFactory.createArrayBacked(RandomTeleportCallback.class, + (listeners) -> (event) -> { + for (RandomTeleportCallback listener : listeners) { + final ActionResult result = listener.invoke(event); + if (event.isCancelled()) { + return ActionResult.FAIL; + } else if (result == ActionResult.FAIL) { + event.setCancelled(true); + return result; + } + } + + return ActionResult.PASS; + }); + + @NotNull + Function SUPPLIER = (teleport) -> + new IRandomTeleportEvent() { + private boolean cancelled = false; + + @Override + @NotNull + public Position getPosition() { + return (Position) getTeleport().getTarget(); + } + + @Override + @NotNull + public Teleport getTeleport() { + return teleport; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + public @NotNull Event getEvent() { + return EVENT; + } + + }; +} From 95b3b95e49ec3fa7084ee1b32542cee46c6127a5 Mon Sep 17 00:00:00 2001 From: alazeprt Date: Sat, 24 Aug 2024 09:24:25 +0800 Subject: [PATCH 2/4] Change the style of code --- .../huskhomes/event/RandomTeleportEvent.java | 19 +++++++++++++++++ .../huskhomes/event/IRandomTeleportEvent.java | 19 +++++++++++++++++ .../event/RandomTeleportCallback.java | 21 ++++++++++++++++++- 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/bukkit/src/main/java/net/william278/huskhomes/event/RandomTeleportEvent.java b/bukkit/src/main/java/net/william278/huskhomes/event/RandomTeleportEvent.java index e5e56797..861a06a3 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/event/RandomTeleportEvent.java +++ b/bukkit/src/main/java/net/william278/huskhomes/event/RandomTeleportEvent.java @@ -1,3 +1,22 @@ +/* + * This file is part of HuskHomes, licensed under the Apache License 2.0. + * + * Copyright (c) William278 + * Copyright (c) contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package net.william278.huskhomes.event; import net.william278.huskhomes.position.Position; diff --git a/common/src/main/java/net/william278/huskhomes/event/IRandomTeleportEvent.java b/common/src/main/java/net/william278/huskhomes/event/IRandomTeleportEvent.java index 5b0dbbc8..2ce679a0 100644 --- a/common/src/main/java/net/william278/huskhomes/event/IRandomTeleportEvent.java +++ b/common/src/main/java/net/william278/huskhomes/event/IRandomTeleportEvent.java @@ -1,3 +1,22 @@ +/* + * This file is part of HuskHomes, licensed under the Apache License 2.0. + * + * Copyright (c) William278 + * Copyright (c) contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package net.william278.huskhomes.event; import net.william278.huskhomes.position.Position; diff --git a/fabric/src/main/java/net/william278/huskhomes/event/RandomTeleportCallback.java b/fabric/src/main/java/net/william278/huskhomes/event/RandomTeleportCallback.java index 083a1d52..6c873587 100644 --- a/fabric/src/main/java/net/william278/huskhomes/event/RandomTeleportCallback.java +++ b/fabric/src/main/java/net/william278/huskhomes/event/RandomTeleportCallback.java @@ -1,3 +1,22 @@ +/* + * This file is part of HuskHomes, licensed under the Apache License 2.0. + * + * Copyright (c) William278 + * Copyright (c) contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package net.william278.huskhomes.event; import net.fabricmc.fabric.api.event.Event; @@ -11,7 +30,7 @@ public interface RandomTeleportCallback extends FabricEventCallback { @NotNull - net.fabricmc.fabric.api.event.Event EVENT = EventFactory.createArrayBacked(RandomTeleportCallback.class, + Event EVENT = EventFactory.createArrayBacked(RandomTeleportCallback.class, (listeners) -> (event) -> { for (RandomTeleportCallback listener : listeners) { final ActionResult result = listener.invoke(event); From 3d52086c140976eb102ed8fa58035512af537b83 Mon Sep 17 00:00:00 2001 From: alazeprt Date: Mon, 16 Sep 2024 20:03:19 +0800 Subject: [PATCH 3/4] Add sponge event --- .../event/SpongeEventDispatcher.java | 3 +- .../event/SpongeRandomTeleportEvent.java | 60 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 sponge/src/main/java/net/william278/huskhomes/event/SpongeRandomTeleportEvent.java diff --git a/sponge/src/main/java/net/william278/huskhomes/event/SpongeEventDispatcher.java b/sponge/src/main/java/net/william278/huskhomes/event/SpongeEventDispatcher.java index 4e190f64..d3b41708 100644 --- a/sponge/src/main/java/net/william278/huskhomes/event/SpongeEventDispatcher.java +++ b/sponge/src/main/java/net/william278/huskhomes/event/SpongeEventDispatcher.java @@ -46,7 +46,8 @@ default boolean fireIsCancelled @Override @NotNull default ITeleportEvent getTeleportEvent(@NotNull Teleport teleport) { - return new SpongeTeleportEvent(teleport); + return teleport.getType() == Teleport.Type.RANDOM_TELEPORT ? new SpongeRandomTeleportEvent(teleport) : + new SpongeTeleportEvent(teleport); } @Override diff --git a/sponge/src/main/java/net/william278/huskhomes/event/SpongeRandomTeleportEvent.java b/sponge/src/main/java/net/william278/huskhomes/event/SpongeRandomTeleportEvent.java new file mode 100644 index 00000000..b0ec5d27 --- /dev/null +++ b/sponge/src/main/java/net/william278/huskhomes/event/SpongeRandomTeleportEvent.java @@ -0,0 +1,60 @@ +/* + * This file is part of HuskHomes, licensed under the Apache License 2.0. + * + * Copyright (c) William278 + * Copyright (c) contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.william278.huskhomes.event; + +import net.william278.huskhomes.teleport.Teleport; +import org.jetbrains.annotations.NotNull; +import org.spongepowered.api.event.Cancellable; +import org.spongepowered.api.event.Cause; +import org.spongepowered.api.event.Event; + +public class SpongeRandomTeleportEvent implements IRandomTeleportEvent, Event, Cancellable { + + private boolean cancelled = false; + private final Teleport teleport; + + public SpongeRandomTeleportEvent(@NotNull Teleport teleport) { + this.teleport = teleport; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @NotNull + @Override + public Teleport getTeleport() { + return teleport; + } + + @Override + public Cause cause() { + return Cause.builder() + .append(teleport.getTeleporter()) + .build(); + } + +} From 1daaf7b1f2e42b144622a85df7c01fb4a1a38e0f Mon Sep 17 00:00:00 2001 From: alazeprt Date: Mon, 16 Sep 2024 20:09:20 +0800 Subject: [PATCH 4/4] Fix bugs --- .../huskhomes/event/SpongeRandomTeleportEvent.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sponge/src/main/java/net/william278/huskhomes/event/SpongeRandomTeleportEvent.java b/sponge/src/main/java/net/william278/huskhomes/event/SpongeRandomTeleportEvent.java index b0ec5d27..7772f55e 100644 --- a/sponge/src/main/java/net/william278/huskhomes/event/SpongeRandomTeleportEvent.java +++ b/sponge/src/main/java/net/william278/huskhomes/event/SpongeRandomTeleportEvent.java @@ -19,6 +19,7 @@ package net.william278.huskhomes.event; +import net.william278.huskhomes.position.Position; import net.william278.huskhomes.teleport.Teleport; import org.jetbrains.annotations.NotNull; import org.spongepowered.api.event.Cancellable; @@ -57,4 +58,8 @@ public Cause cause() { .build(); } + @Override + public @NotNull Position getPosition() { + return (Position) teleport.getTarget(); + } }