improve CancelableEvents

This commit is contained in:
Bixilon 2021-06-20 13:34:19 +02:00
parent 8907adb032
commit 694e5a9489
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
21 changed files with 75 additions and 210 deletions

View File

@ -16,8 +16,9 @@ package de.bixilon.minosoft.gui.rendering.modding.events
import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.Rendering
import de.bixilon.minosoft.gui.rendering.system.window.BaseWindow
import de.bixilon.minosoft.modding.event.events.CancelableEvent
class WindowCloseEvent(
renderWindow: RenderWindow = Rendering.currentContext!!,
val window: BaseWindow,
) : RenderEvent(renderWindow)
) : RenderEvent(renderWindow), CancelableEvent

View File

@ -32,7 +32,7 @@ class EventInvokerMethod(
if (!method.parameters[0].type.isAssignableFrom(event.javaClass)) {
return
}
if (!this.isIgnoreCancelled && event is CancelableEvent && event.isCancelled) {
if (!this.isIgnoreCancelled && event is CancelableEvent && event.cancelled) {
return
}
method(listener, event)

View File

@ -1,50 +0,0 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.modding.event.events;
import de.bixilon.minosoft.protocol.network.connection.PlayConnection;
import de.bixilon.minosoft.protocol.packets.s2c.play.BlockBreakAnimationS2CP;
import glm_.vec3.Vec3i;
public class BlockBreakAnimationEvent extends CancelableEvent {
private final int entityId;
private final Vec3i position;
private final int stage;
public BlockBreakAnimationEvent(PlayConnection connection, int entityId, Vec3i position, int stage) {
super(connection);
this.entityId = entityId;
this.position = position;
this.stage = stage;
}
public BlockBreakAnimationEvent(PlayConnection connection, BlockBreakAnimationS2CP pkg) {
super(connection);
this.entityId = pkg.getAnimationId();
this.position = pkg.getBlockPosition();
this.stage = pkg.getStage();
}
public int getEntityId() {
return this.entityId;
}
public Vec3i getPosition() {
return this.position;
}
public int getStage() {
return this.stage;
}
}

View File

@ -0,0 +1,29 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.modding.event.events
import de.bixilon.minosoft.modding.event.EventInitiators
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
import de.bixilon.minosoft.protocol.packets.s2c.play.BlockBreakAnimationS2CP
import glm_.vec3.Vec3i
class BlockBreakAnimationEvent(
connection: PlayConnection,
initiator: EventInitiators,
val animationId: Int,
val blockPosition: Vec3i,
val stage: Int,
) : PlayConnectionEvent(connection, initiator), CancelableEvent {
constructor(connection: PlayConnection, packet: BlockBreakAnimationS2CP) : this(connection, EventInitiators.SERVER, packet.animationId, packet.blockPosition, packet.stage)
}

View File

@ -12,12 +12,20 @@
*/
package de.bixilon.minosoft.modding.event.events
import de.bixilon.minosoft.modding.event.EventInitiators
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
import de.bixilon.minosoft.util.KUtil.synchronizedSetOf
abstract class CancelableEvent @JvmOverloads constructor(
connection: PlayConnection,
initiator: EventInitiators = EventInitiators.DEFAULT,
) : PlayConnectionEvent(connection, initiator) {
var isCancelled = false
interface CancelableEvent {
var cancelled: Boolean
get() = CANCELLED_EVENTS.contains(this)
set(value) {
if (value) {
CANCELLED_EVENTS -= this
} else {
CANCELLED_EVENTS += this
}
}
private companion object {
val CANCELLED_EVENTS: MutableSet<CancelableEvent> = synchronizedSetOf()
}
}

View File

@ -25,7 +25,7 @@ class ChatMessageReceiveEvent(
val message: ChatComponent,
val position: ChatTextPositions,
val sender: UUID?,
) : CancelableEvent(connection, initiator) {
) : PlayConnectionEvent(connection, initiator), CancelableEvent {
constructor(connection: PlayConnection, packet: ChatMessageS2CP) : this(connection, EventInitiators.SERVER, packet.message, packet.position, packet.sender)

View File

@ -15,4 +15,4 @@ package de.bixilon.minosoft.modding.event.events
import de.bixilon.minosoft.modding.event.EventInitiators
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
class ChatMessageSendEvent(connection: PlayConnection, val message: String) : CancelableEvent(connection, EventInitiators.CLIENT)
class ChatMessageSendEvent(connection: PlayConnection, val message: String) : PlayConnectionEvent(connection, EventInitiators.CLIENT), CancelableEvent

View File

@ -25,7 +25,7 @@ class CollectItemAnimationEvent(
val collectedEntity: Entity,
val collector: LivingEntity,
val count: Int,
) : CancelableEvent(connection, initiator) {
) : PlayConnectionEvent(connection, initiator), CancelableEvent {
constructor(connection: PlayConnection, packet: ItemCollectAnimationS2CP) : this(connection, EventInitiators.SERVER, connection.world.entities[packet.itemEntityId]!!.unsafeCast<Entity>(), connection.world.entities[packet.collectorEntityId]!!.unsafeCast<LivingEntity>(), packet.count)
}

View File

@ -24,7 +24,7 @@ class ContainerCloseEvent(
connection: PlayConnection,
initiator: EventInitiators,
val containerId: Int,
) : CancelableEvent(connection, initiator) {
) : PlayConnectionEvent(connection, initiator), CancelableEvent {
constructor(connection: PlayConnection, packet: ContainerCloseS2CP) : this(connection, EventInitiators.SERVER, packet.containerId)

View File

@ -22,7 +22,7 @@ class ExperienceChangeEvent(
val bar: Float,
val level: Int,
val total: Int,
) : CancelableEvent(connection, initiator) {
) : PlayConnectionEvent(connection, initiator), CancelableEvent {
constructor(connection: PlayConnection, packet: ExperienceSetS2CP) : this(connection, EventInitiators.SERVER, packet.bar, packet.level, packet.total)
}

View File

@ -1,110 +0,0 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.modding.event.events;
import com.google.common.collect.HashBiMap;
import de.bixilon.minosoft.data.Difficulties;
import de.bixilon.minosoft.data.abilities.Gamemodes;
import de.bixilon.minosoft.data.registries.Dimension;
import de.bixilon.minosoft.data.registries.ResourceLocation;
import de.bixilon.minosoft.protocol.network.connection.PlayConnection;
import de.bixilon.minosoft.protocol.packets.s2c.play.JoinGameS2CP;
public class JoinGameEvent extends CancelableEvent {
private final int entityId;
private final boolean hardcore;
private final Gamemodes gamemode;
private final Dimension dimension;
private final Difficulties difficulty;
private final int viewDistance;
private final int maxPlayers;
private final boolean reducedDebugScreen;
private final boolean enableRespawnScreen;
private final long hashedSeed;
private final HashBiMap<ResourceLocation, Dimension> dimensions;
public JoinGameEvent(PlayConnection connection, int entityId, boolean hardcore, Gamemodes gamemode, Dimension dimension, Difficulties difficulty, int viewDistance, int maxPlayers, boolean reducedDebugScreen, boolean enableRespawnScreen, long hashedSeed, HashBiMap<ResourceLocation, Dimension> dimensions) {
super(connection);
this.entityId = entityId;
this.hardcore = hardcore;
this.gamemode = gamemode;
this.dimension = dimension;
this.difficulty = difficulty;
this.viewDistance = viewDistance;
this.maxPlayers = maxPlayers;
this.reducedDebugScreen = reducedDebugScreen;
this.enableRespawnScreen = enableRespawnScreen;
this.hashedSeed = hashedSeed;
this.dimensions = dimensions;
}
public JoinGameEvent(PlayConnection connection, JoinGameS2CP pkg) {
super(connection);
this.entityId = pkg.getEntityId();
this.hardcore = pkg.isHardcore();
this.gamemode = pkg.getGamemode();
this.dimension = pkg.getDimension();
this.difficulty = pkg.getDifficulty();
this.viewDistance = pkg.getViewDistance();
this.maxPlayers = pkg.getMaxPlayers();
this.reducedDebugScreen = pkg.isReducedDebugScreen();
this.enableRespawnScreen = pkg.isEnableRespawnScreen();
this.hashedSeed = pkg.getHashedSeed();
this.dimensions = pkg.getDimensions();
}
public int getEntityId() {
return this.entityId;
}
public boolean isHardcore() {
return this.hardcore;
}
public Gamemodes getGamemode() {
return this.gamemode;
}
public Dimension getDimension() {
return this.dimension;
}
public Difficulties getDifficulty() {
return this.difficulty;
}
public int getViewDistance() {
return this.viewDistance;
}
public int getMaxPlayers() {
return this.maxPlayers;
}
public boolean isReducedDebugScreen() {
return this.reducedDebugScreen;
}
public boolean isEnableRespawnScreen() {
return this.enableRespawnScreen;
}
public long getHashedSeed() {
return this.hashedSeed;
}
public HashBiMap<ResourceLocation, Dimension> getDimensions() {
return this.dimensions;
}
}

View File

@ -21,7 +21,7 @@ class OpenSignEditorEvent(
connection: PlayConnection,
initiator: EventInitiators,
val blockPosition: Vec3i,
) : CancelableEvent(connection, initiator) {
) : PlayConnectionEvent(connection, initiator), CancelableEvent {
constructor(connection: PlayConnection, packet: SignEditorOpenS2CP) : this(connection, EventInitiators.SERVER, packet.blockPosition)
}

View File

@ -27,7 +27,7 @@ class ParticleSpawnEvent(
val speed: Float,
val count: Int,
val data: ParticleData,
) : CancelableEvent(connection, initiator) {
) : PlayConnectionEvent(connection, initiator), CancelableEvent {
val position: Vec3d = position
get() = Vec3d(field)
val offset: Vec3 = offset

View File

@ -29,7 +29,7 @@ class PlaySoundEvent(
val soundEvent: SoundEvent,
val volume: Float,
val pitch: Float,
) : CancelableEvent(connection, initiator) {
) : PlayConnectionEvent(connection, initiator), CancelableEvent {
val position: Vec3 = position
get() = Vec3(field)

View File

@ -22,7 +22,7 @@ class PlayerListInfoChangeEvent(
initiator: EventInitiators,
val header: ChatComponent,
val footer: ChatComponent,
) : CancelableEvent(connection, initiator) {
) : PlayConnectionEvent(connection, initiator), CancelableEvent {
constructor(connection: PlayConnection, packet: TabListTextSetS2CP) : this(connection, EventInitiators.SERVER, packet.header, packet.footer)
}

View File

@ -10,30 +10,19 @@
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.modding.event.events
package de.bixilon.minosoft.modding.event.events;
import de.bixilon.minosoft.data.player.tab.TabListItemData
import de.bixilon.minosoft.modding.event.EventInitiators
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
import de.bixilon.minosoft.protocol.packets.s2c.play.TabListDataS2CP
import java.util.*
import de.bixilon.minosoft.data.player.tab.TabListItemData;
import de.bixilon.minosoft.protocol.network.connection.PlayConnection;
import de.bixilon.minosoft.protocol.packets.s2c.play.TabListDataS2CP;
class PlayerListItemChangeEvent(
connection: PlayConnection,
initiator: EventInitiators,
val items: Map<UUID, TabListItemData>,
) : PlayConnectionEvent(connection, initiator), CancelableEvent {
import java.util.Map;
import java.util.UUID;
public class PlayerListItemChangeEvent extends CancelableEvent {
private final Map<UUID, TabListItemData> items;
public PlayerListItemChangeEvent(PlayConnection connection, Map<UUID, TabListItemData> items) {
super(connection);
this.items = items;
}
public PlayerListItemChangeEvent(PlayConnection connection, TabListDataS2CP pkg) {
super(connection);
this.items = pkg.getItems();
}
public Map<UUID, TabListItemData> getItems() {
return this.items;
}
constructor(connection: PlayConnection, packet: TabListDataS2CP) : this(connection, EventInitiators.SERVER, packet.items)
}

View File

@ -20,10 +20,10 @@ import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer
class PluginMessageReceiveEvent(
connection: PlayConnection,
initiators: EventInitiators,
initiator: EventInitiators,
val channel: ResourceLocation,
data: PlayInByteBuffer,
) : CancelableEvent(connection, initiators) {
) : PlayConnectionEvent(connection, initiator), CancelableEvent {
val data: PlayInByteBuffer = data
get() = PlayInByteBuffer(field)

View File

@ -23,7 +23,7 @@ class ResourcePackRequestEvent(
val url: String,
val hash: String,
val promptText: ChatComponent?,
) : CancelableEvent(connection, initiator) {
) : PlayConnectionEvent(connection, initiator), CancelableEvent {
constructor(connection: PlayConnection, packet: ResourcepackRequestS2CP) : this(connection, EventInitiators.SERVER, packet.url, packet.hash, packet.promptText)

View File

@ -21,7 +21,7 @@ class TimeChangeEvent(
initiator: EventInitiators,
val age: Long,
val time: Long,
) : CancelableEvent(connection, initiator) {
) : PlayConnectionEvent(connection, initiator), CancelableEvent {
constructor(connection: PlayConnection, packet: WorldTimeSetS2CP) : this(connection, EventInitiators.SERVER, packet.age, packet.time)

View File

@ -65,7 +65,9 @@ abstract class Connection : EventMaster {
eventInvoker(event)
}
if (event is CancelableEvent) {
return event.isCancelled
val cancelled = event.cancelled
event.cancelled = false // Cleanup memory
return cancelled
}
return false
}

View File

@ -21,7 +21,6 @@ import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.data.world.biome.accessor.BlockBiomeAccessor
import de.bixilon.minosoft.data.world.biome.accessor.NoiseBiomeAccessor
import de.bixilon.minosoft.modding.channels.DefaultPluginChannels
import de.bixilon.minosoft.modding.event.events.JoinGameEvent
import de.bixilon.minosoft.protocol.ErrorHandler
import de.bixilon.minosoft.protocol.network.connection.Connection
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
@ -140,9 +139,6 @@ class JoinGameS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() {
}
override fun handle(connection: PlayConnection) {
if (connection.fireEvent(JoinGameEvent(connection, this))) {
return
}
val playerEntity = connection.player
playerEntity.tabListItem.gamemode = gamemode