wip modding (8): more events

This commit is contained in:
Bixilon 2020-10-02 19:33:24 +02:00
parent daf4af24fe
commit 3071fa7499
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
16 changed files with 386 additions and 26 deletions

View File

@ -13,8 +13,7 @@
package de.bixilon.minosoft.modding.event;
import de.bixilon.minosoft.modding.event.events.ChatMessageReceivingEvent;
import de.bixilon.minosoft.modding.event.events.ChatMessageSendingEvent;
import de.bixilon.minosoft.modding.event.events.*;
public class EventListener {
public void onChatMessageReceiving(ChatMessageReceivingEvent event) {
@ -23,4 +22,21 @@ public class EventListener {
public void onChatMessageSending(ChatMessageSendingEvent event) {
}
public void onLoginDisconnect(LoginDisconnectEvent event) {
}
public void onDisconnect(DisconnectEvent event) {
}
public void onResourcePackChange(ResourcePackChangeEvent event) {
}
public void onBlockBreakAnimation(BlockBreakAnimationEvent event) {
}
public void onHealthChange(UpdateHealthEvent event) {
}
public void onOpenSignEditor(OpenSignEditorEvent event) {
}
}

View File

@ -0,0 +1,58 @@
/*
* Codename 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.game.datatypes.world.BlockPosition;
import de.bixilon.minosoft.modding.event.EventListener;
import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.packets.clientbound.play.PacketBlockBreakAnimation;
@MinimumProtocolVersion(protocolId = 32)
public class BlockBreakAnimationEvent extends CancelableEvent {
private final int entityId;
private final BlockPosition position;
private final byte stage;
public BlockBreakAnimationEvent(Connection connection, int entityId, BlockPosition position, byte stage) {
super(connection);
this.entityId = entityId;
this.position = position;
this.stage = stage;
}
public BlockBreakAnimationEvent(Connection connection, PacketBlockBreakAnimation pkg) {
super(connection);
this.entityId = pkg.getEntityId();
this.position = pkg.getPosition();
this.stage = pkg.getStage();
}
public int getEntityId() {
return entityId;
}
public BlockPosition getPosition() {
return position;
}
public byte getStage() {
return stage;
}
@Override
public void handle(EventListener listener) {
listener.onBlockBreakAnimation(this);
}
}

View File

@ -0,0 +1,32 @@
/*
* Codename 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;
public abstract class CancelableEvent extends Event {
private boolean cancelled = false;
protected CancelableEvent(Connection connection) {
super(connection);
}
public boolean isCancelled() {
return cancelled;
}
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}

View File

@ -21,7 +21,7 @@ import de.bixilon.minosoft.protocol.packets.clientbound.play.PacketChatMessageRe
import java.util.UUID;
public class ChatMessageReceivingEvent extends Event {
public class ChatMessageReceivingEvent extends CancelableEvent {
private final TextComponent message;
private final ChatTextPositions position;
private final UUID sender;

View File

@ -16,8 +16,8 @@ package de.bixilon.minosoft.modding.event.events;
import de.bixilon.minosoft.modding.event.EventListener;
import de.bixilon.minosoft.protocol.network.Connection;
public class ChatMessageSendingEvent extends Event {
private final String message;
public class ChatMessageSendingEvent extends CancelableEvent {
private String message;
public ChatMessageSendingEvent(Connection connection, String message) {
super(connection);
@ -28,6 +28,10 @@ public class ChatMessageSendingEvent extends Event {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public void handle(EventListener listener) {
listener.onChatMessageSending(this);

View File

@ -0,0 +1,42 @@
/*
* Codename 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.game.datatypes.TextComponent;
import de.bixilon.minosoft.modding.event.EventListener;
import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.packets.clientbound.login.PacketLoginDisconnect;
public class DisconnectEvent extends Event {
private final TextComponent reason;
public DisconnectEvent(Connection connection, TextComponent reason) {
super(connection);
this.reason = reason;
}
public DisconnectEvent(Connection connection, PacketLoginDisconnect pkg) {
super(connection);
this.reason = pkg.getReason();
}
public TextComponent getReason() {
return reason;
}
@Override
public void handle(EventListener listener) {
listener.onDisconnect(this);
}
}

View File

@ -20,7 +20,6 @@ import javax.annotation.Nullable;
public abstract class Event {
private final Connection connection;
private boolean cancelled = false;
protected Event(Connection connection) {
this.connection = connection;
@ -31,13 +30,5 @@ public abstract class Event {
return connection;
}
public boolean isCancelled() {
return cancelled;
}
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
public abstract void handle(EventListener listener);
}

View File

@ -0,0 +1,42 @@
/*
* Codename 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.game.datatypes.TextComponent;
import de.bixilon.minosoft.modding.event.EventListener;
import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.packets.clientbound.play.PacketDisconnect;
public class LoginDisconnectEvent extends Event {
private final TextComponent reason;
public LoginDisconnectEvent(Connection connection, TextComponent reason) {
super(connection);
this.reason = reason;
}
public LoginDisconnectEvent(Connection connection, PacketDisconnect pkg) {
super(connection);
this.reason = pkg.getReason();
}
public TextComponent getReason() {
return reason;
}
@Override
public void handle(EventListener listener) {
listener.onLoginDisconnect(this);
}
}

View File

@ -0,0 +1,42 @@
/*
* Codename 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.game.datatypes.world.BlockPosition;
import de.bixilon.minosoft.modding.event.EventListener;
import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.packets.clientbound.play.PacketOpenSignEditor;
public class OpenSignEditorEvent extends CancelableEvent {
private final BlockPosition position;
public OpenSignEditorEvent(Connection connection, BlockPosition position) {
super(connection);
this.position = position;
}
public OpenSignEditorEvent(Connection connection, PacketOpenSignEditor pkg) {
super(connection);
this.position = pkg.getPosition();
}
public BlockPosition getPosition() {
return position;
}
@Override
public void handle(EventListener listener) {
listener.onOpenSignEditor(this);
}
}

View File

@ -0,0 +1,59 @@
/*
* Codename 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.EventListener;
import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.packets.clientbound.play.PacketResourcePackSend;
@MinimumProtocolVersion(protocolId = 32)
public class ResourcePackChangeEvent extends CancelableEvent {
private String url;
private String hash;
public ResourcePackChangeEvent(Connection connection, String url, String hash) {
super(connection);
this.url = url;
this.hash = hash;
}
public ResourcePackChangeEvent(Connection connection, PacketResourcePackSend pkg) {
super(connection);
this.url = pkg.getUrl();
this.hash = pkg.getHash();
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@MaximumProtocolVersion(protocolId = 204)
public String getHash() {
return hash;
}
@MaximumProtocolVersion(protocolId = 204)
public void setHash(String hash) {
this.hash = hash;
}
@Override
public void handle(EventListener listener) {
listener.onResourcePackChange(this);
}
}

View File

@ -0,0 +1,55 @@
/*
* Codename 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.EventListener;
import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.packets.clientbound.play.PacketUpdateHealth;
public class UpdateHealthEvent extends Event {
private final float health;
private final int food;
private final float saturation;
public UpdateHealthEvent(Connection connection, float health, int food, float saturation) {
super(connection);
this.health = health;
this.food = food;
this.saturation = saturation;
}
public UpdateHealthEvent(Connection connection, PacketUpdateHealth pkg) {
super(connection);
this.health = pkg.getHealth();
this.food = pkg.getFood();
this.saturation = pkg.getSaturation();
}
public float getHealth() {
return health;
}
public int getFood() {
return food;
}
public float getSaturation() {
return saturation;
}
@Override
public void handle(EventListener listener) {
listener.onHealthChange(this);
}
}

View File

@ -27,6 +27,7 @@ import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.logging.LogLevels;
import de.bixilon.minosoft.modding.event.EventListener;
import de.bixilon.minosoft.modding.event.EventManager;
import de.bixilon.minosoft.modding.event.events.CancelableEvent;
import de.bixilon.minosoft.modding.event.events.Event;
import de.bixilon.minosoft.ping.ServerListPing;
import de.bixilon.minosoft.protocol.modding.channels.DefaultPluginChannels;
@ -433,6 +434,9 @@ public class Connection {
public boolean fireEvent(Event event) {
Minosoft.eventManagers.forEach((eventManager -> eventManager.getGlobalEventListeners().forEach(event::handle)));
eventListeners.forEach(event::handle);
return event.isCancelled();
if (event instanceof CancelableEvent) {
return ((CancelableEvent) event).isCancelled();
}
return false;
}
}

View File

@ -18,7 +18,7 @@ import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
public class PackerResourcePackSend implements ClientboundPacket {
public class PacketResourcePackSend implements ClientboundPacket {
String url;
String hash;

View File

@ -29,7 +29,7 @@ import de.bixilon.minosoft.game.datatypes.scoreboard.Team;
import de.bixilon.minosoft.game.datatypes.world.BlockPosition;
import de.bixilon.minosoft.game.datatypes.world.Chunk;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.modding.event.events.ChatMessageReceivingEvent;
import de.bixilon.minosoft.modding.event.events.*;
import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.packets.clientbound.login.*;
import de.bixilon.minosoft.protocol.packets.clientbound.play.*;
@ -37,7 +37,6 @@ import de.bixilon.minosoft.protocol.packets.clientbound.status.PacketStatusPong;
import de.bixilon.minosoft.protocol.packets.clientbound.status.PacketStatusResponse;
import de.bixilon.minosoft.protocol.packets.serverbound.login.PacketEncryptionResponse;
import de.bixilon.minosoft.protocol.packets.serverbound.play.PacketKeepAliveResponse;
import de.bixilon.minosoft.protocol.packets.serverbound.play.PacketResourcePackStatus;
import de.bixilon.minosoft.util.nbt.tag.CompoundTag;
import de.bixilon.minosoft.util.nbt.tag.StringTag;
@ -113,6 +112,7 @@ public class PacketHandler {
}
public void handle(PacketLoginDisconnect pkg) {
connection.fireEvent(new LoginDisconnectEvent(connection, pkg.getReason()));
Log.info(String.format("Disconnecting from server (reason=%s)", pkg.getReason().getColoredMessage()));
connection.disconnect();
}
@ -167,6 +167,8 @@ public class PacketHandler {
}
public void handle(PacketUpdateHealth pkg) {
connection.fireEvent(new UpdateHealthEvent(connection, pkg));
connection.getPlayer().setFood(pkg.getFood());
connection.getPlayer().setHealth(pkg.getHealth());
connection.getPlayer().setSaturation(pkg.getSaturation());
@ -193,6 +195,7 @@ public class PacketHandler {
}
public void handle(PacketDisconnect pkg) {
connection.fireEvent(new LoginDisconnectEvent(connection, pkg));
// got kicked
connection.disconnect();
}
@ -306,7 +309,10 @@ public class PacketHandler {
}
public void handle(PacketOpenSignEditor pkg) {
// ToDo
OpenSignEditorEvent event = new OpenSignEditorEvent(connection, pkg);
if (connection.fireEvent(event)) {
return;
}
}
public void handle(PacketSpawnObject pkg) {
@ -381,7 +387,6 @@ public class PacketHandler {
}
public void handle(PacketUseBed pkg) {
// ToDo
}
public void handle(PacketBlockEntityMetadata pkg) {
@ -389,11 +394,13 @@ public class PacketHandler {
}
public void handle(PacketBlockBreakAnimation pkg) {
// ToDo
BlockBreakAnimationEvent event = new BlockBreakAnimationEvent(connection, pkg);
if (connection.fireEvent(event)) {
return;
}
}
public void handle(PacketBlockAction pkg) {
// ToDo
}
public void handle(PacketExplosion pkg) {
@ -509,9 +516,12 @@ public class PacketHandler {
connection.getPlayer().setTabFooter(pkg.getFooter());
}
public void handle(PackerResourcePackSend pkg) {
public void handle(PacketResourcePackSend pkg) {
ResourcePackChangeEvent event = new ResourcePackChangeEvent(connection, pkg);
if (connection.fireEvent(event)) {
return;
}
// ToDo ask user, download pack. for now just send an okay
connection.sendPacket(new PacketResourcePackStatus(pkg.getHash(), PacketResourcePackStatus.ResourcePackStates.SUCCESSFULLY));
}
public void handle(PacketEntityProperties pkg) {

View File

@ -14,6 +14,7 @@
package de.bixilon.minosoft.protocol.protocol;
import de.bixilon.minosoft.game.datatypes.player.Hands;
import de.bixilon.minosoft.modding.event.events.ChatMessageSendingEvent;
import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.packets.serverbound.play.*;
@ -31,7 +32,11 @@ public class PacketSender {
}
public void sendChatMessage(String message) {
connection.sendPacket(new PacketChatMessageSending(message));
ChatMessageSendingEvent event = new ChatMessageSendingEvent(connection, message);
if (connection.fireEvent(event)) {
return;
}
connection.sendPacket(new PacketChatMessageSending(event.getMessage()));
}
public void spectateEntity(UUID entityUUID) {

View File

@ -156,7 +156,7 @@ public class Packets {
PLAY_UNLOCK_RECIPES(PacketUnlockRecipes.class),
PLAY_DESTROY_ENTITIES(PacketDestroyEntity.class),
PLAY_REMOVE_ENTITY_EFFECT(PacketRemoveEntityEffect.class),
PLAY_RESOURCE_PACK_SEND(PackerResourcePackSend.class),
PLAY_RESOURCE_PACK_SEND(PacketResourcePackSend.class),
PLAY_RESPAWN(PacketRespawn.class),
PLAY_ENTITY_HEAD_ROTATION(PacketEntityHeadRotation.class),
PLAY_SELECT_ADVANCEMENT_TAB(PacketSelectAdvancementTab.class),