more events

This commit is contained in:
Bixilon 2020-10-09 19:55:17 +02:00
parent 53e5dce736
commit 1a24dc2bb5
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
9 changed files with 283 additions and 13 deletions

View File

@ -84,4 +84,16 @@ public class EventListener {
public void onLightningBoltSpawn(LightningBoltSpawnEvent event) { public void onLightningBoltSpawn(LightningBoltSpawnEvent event) {
} }
public void onMultiBlockChange(MultiBlockChangeEvent event) {
}
public void onBlockEntityMetaDataChange(BlockEntityMetaDataChangeEvent event) {
}
public void onChunkDataChange(ChunkDataChangeEvent event) {
}
public void onEffect(EffectEvent event) {
}
} }

View File

@ -0,0 +1,62 @@
/*
* 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.PacketBlockEntityMetadata;
import de.bixilon.minosoft.util.nbt.tag.CompoundTag;
import javax.annotation.Nullable;
public class BlockEntityMetaDataChangeEvent extends Event {
final BlockPosition position;
final PacketBlockEntityMetadata.BlockEntityActions action;
final CompoundTag nbt;
public BlockEntityMetaDataChangeEvent(Connection connection, BlockPosition position, PacketBlockEntityMetadata.BlockEntityActions action, CompoundTag nbt) {
super(connection);
this.position = position;
this.action = action;
this.nbt = nbt;
this.nbt.setFinal();
}
public BlockEntityMetaDataChangeEvent(Connection connection, PacketBlockEntityMetadata pkg) {
super(connection);
this.position = pkg.getPosition();
this.action = pkg.getAction();
this.nbt = pkg.getNbt();
this.nbt.setFinal();
}
public BlockPosition getPosition() {
return position;
}
@Nullable
public PacketBlockEntityMetadata.BlockEntityActions getAction() {
return action;
}
public CompoundTag getNbt() {
return nbt;
}
@Override
public void handle(EventListener listener) {
listener.onBlockEntityMetaDataChange(this);
}
}

View File

@ -0,0 +1,68 @@
/*
* 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.Chunk;
import de.bixilon.minosoft.game.datatypes.world.ChunkLocation;
import de.bixilon.minosoft.modding.event.EventListener;
import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.packets.clientbound.play.PacketChunkData;
import de.bixilon.minosoft.util.nbt.tag.CompoundTag;
/**
* Fired when a new chunk is received or a full chunk changes
*/
public class ChunkDataChangeEvent extends Event {
private final ChunkLocation location;
private final Chunk chunk;
private final CompoundTag heightMap;
public ChunkDataChangeEvent(Connection connection, ChunkLocation location, Chunk chunk, CompoundTag heightMap) {
super(connection);
this.location = location;
this.chunk = chunk;
this.heightMap = heightMap;
}
public ChunkDataChangeEvent(Connection connection, ChunkLocation location, Chunk chunk) {
super(connection);
this.location = location;
this.chunk = chunk;
this.heightMap = new CompoundTag();
}
public ChunkDataChangeEvent(Connection connection, PacketChunkData pkg) {
super(connection);
this.location = pkg.getLocation();
this.chunk = pkg.getChunk();
this.heightMap = pkg.getHeightMap();
}
public ChunkLocation getLocation() {
return location;
}
public Chunk getChunk() {
return chunk;
}
public CompoundTag getHeightMap() {
return heightMap;
}
@Override
public void handle(EventListener listener) {
listener.onChunkDataChange(this);
}
}

View File

@ -0,0 +1,63 @@
/*
* 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.PacketEffect;
public class EffectEvent extends CancelableEvent {
final PacketEffect.EffectEffects effect;
final BlockPosition position;
final int data;
final boolean disableRelativeVolume;
public EffectEvent(Connection connection, PacketEffect.EffectEffects effect, BlockPosition position, int data, boolean disableRelativeVolume) {
super(connection);
this.effect = effect;
this.position = position;
this.data = data;
this.disableRelativeVolume = disableRelativeVolume;
}
public EffectEvent(Connection connection, PacketEffect pkg) {
super(connection);
this.effect = pkg.getEffect();
this.position = pkg.getPosition();
this.data = pkg.getData();
this.disableRelativeVolume = pkg.isDisableRelativeVolume();
}
public PacketEffect.EffectEffects getEffect() {
return effect;
}
public BlockPosition getPosition() {
return position;
}
public int getData() {
return data;
}
public boolean isDisableRelativeVolume() {
return disableRelativeVolume;
}
@Override
public void handle(EventListener listener) {
listener.onEffect(this);
}
}

View File

@ -0,0 +1,56 @@
/*
* 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.objectLoader.blocks.Block;
import de.bixilon.minosoft.game.datatypes.world.ChunkLocation;
import de.bixilon.minosoft.game.datatypes.world.InChunkLocation;
import de.bixilon.minosoft.modding.event.EventListener;
import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.packets.clientbound.play.PacketMultiBlockChange;
import java.util.HashMap;
/**
* Fired when at least block is changed
*/
public class MultiBlockChangeEvent extends Event {
private final HashMap<InChunkLocation, Block> blocks;
private final ChunkLocation location;
public MultiBlockChangeEvent(Connection connection, HashMap<InChunkLocation, Block> blocks, ChunkLocation location) {
super(connection);
this.blocks = blocks;
this.location = location;
}
public MultiBlockChangeEvent(Connection connection, PacketMultiBlockChange pkg) {
super(connection);
this.blocks = pkg.getBlocks();
this.location = pkg.getLocation();
}
public HashMap<InChunkLocation, Block> getBlocks() {
return blocks;
}
public ChunkLocation getLocation() {
return location;
}
@Override
public void handle(EventListener listener) {
listener.onMultiBlockChange(this);
}
}

View File

@ -29,7 +29,6 @@ public class PacketBlockEntityMetadata implements ClientboundPacket {
@Override @Override
public boolean read(InByteBuffer buffer) { public boolean read(InByteBuffer buffer) {
if (buffer.getProtocolId() < 6) { if (buffer.getProtocolId() < 6) {
position = buffer.readBlockPositionShort(); position = buffer.readBlockPositionShort();
action = BlockEntityActions.byId(buffer.readByte(), buffer.getProtocolId()); action = BlockEntityActions.byId(buffer.readByte(), buffer.getProtocolId());
@ -56,6 +55,10 @@ public class PacketBlockEntityMetadata implements ClientboundPacket {
return position; return position;
} }
public BlockEntityActions getAction() {
return action;
}
public CompoundTag getNbt() { public CompoundTag getNbt() {
return nbt; return nbt;
} }

View File

@ -25,7 +25,7 @@ import de.bixilon.minosoft.util.Util;
import java.util.HashMap; import java.util.HashMap;
public class PacketChunkBulk implements ClientboundPacket { public class PacketChunkBulk implements ClientboundPacket {
final HashMap<ChunkLocation, Chunk> chunkMap = new HashMap<>(); final HashMap<ChunkLocation, Chunk> chunks = new HashMap<>();
@Override @Override
public boolean read(InByteBuffer buffer) { public boolean read(InByteBuffer buffer) {
@ -49,7 +49,7 @@ public class PacketChunkBulk implements ClientboundPacket {
short sectionBitMask = buffer.readShort(); short sectionBitMask = buffer.readShort();
short addBitMask = buffer.readShort(); short addBitMask = buffer.readShort();
chunkMap.put(new ChunkLocation(x, z), ChunkUtil.readChunkPacket(decompressed, sectionBitMask, addBitMask, true, containsSkyLight)); chunks.put(new ChunkLocation(x, z), ChunkUtil.readChunkPacket(decompressed, sectionBitMask, addBitMask, true, containsSkyLight));
} }
return true; return true;
} }
@ -67,7 +67,7 @@ public class PacketChunkBulk implements ClientboundPacket {
sectionBitMask[i] = buffer.readShort(); sectionBitMask[i] = buffer.readShort();
} }
for (int i = 0; i < chunkCount; i++) { for (int i = 0; i < chunkCount; i++) {
chunkMap.put(new ChunkLocation(x[i], z[i]), ChunkUtil.readChunkPacket(buffer, sectionBitMask[i], (short) 0, true, containsSkyLight)); chunks.put(new ChunkLocation(x[i], z[i]), ChunkUtil.readChunkPacket(buffer, sectionBitMask[i], (short) 0, true, containsSkyLight));
} }
return true; return true;
} }
@ -79,10 +79,10 @@ public class PacketChunkBulk implements ClientboundPacket {
@Override @Override
public void log() { public void log() {
Log.protocol(String.format("Chunk bulk packet received (chunks=%s)", chunkMap.size())); Log.protocol(String.format("Chunk bulk packet received (chunks=%s)", chunks.size()));
} }
public HashMap<ChunkLocation, Chunk> getChunkMap() { public HashMap<ChunkLocation, Chunk> getChunks() {
return chunkMap; return chunks;
} }
} }

View File

@ -22,11 +22,10 @@ import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.protocol.protocol.PacketHandler; import de.bixilon.minosoft.protocol.protocol.PacketHandler;
public class PacketEffect implements ClientboundPacket { public class PacketEffect implements ClientboundPacket {
// is this class used??? What about PacketParticle or PacketSoundEffect?
EffectEffects effect; EffectEffects effect;
BlockPosition position; BlockPosition position;
int data; int data;
boolean disableRelativeVolume; // normally only at MOB_ENDERDRAGON_END and MOB_WITHER_SPAWN, but we allow this everywhere boolean disableRelativeVolume;
@Override @Override
public boolean read(InByteBuffer buffer) { public boolean read(InByteBuffer buffer) {

View File

@ -167,7 +167,9 @@ public class PacketHandler {
} }
public void handle(PacketChunkBulk pkg) { public void handle(PacketChunkBulk pkg) {
connection.getPlayer().getWorld().setChunks(pkg.getChunkMap()); pkg.getChunks().forEach(((location, chunk) -> connection.fireEvent(new ChunkDataChangeEvent(connection, location, chunk))));
connection.getPlayer().getWorld().setChunks(pkg.getChunks());
} }
public void handle(PacketUpdateHealth pkg) { public void handle(PacketUpdateHealth pkg) {
@ -311,6 +313,7 @@ public class PacketHandler {
Log.warn(String.format("Server tried to change blocks in unloaded chunks! (location=%s)", pkg.getLocation())); Log.warn(String.format("Server tried to change blocks in unloaded chunks! (location=%s)", pkg.getLocation()));
return; return;
} }
connection.fireEvent(new MultiBlockChangeEvent(connection, pkg));
chunk.setBlocks(pkg.getBlocks()); chunk.setBlocks(pkg.getBlocks());
} }
@ -343,6 +346,9 @@ public class PacketHandler {
} }
public void handle(PacketChunkData pkg) { public void handle(PacketChunkData pkg) {
pkg.getBlockEntities().forEach(((position, compoundTag) -> connection.fireEvent(new BlockEntityMetaDataChangeEvent(connection, position, null, compoundTag))));
connection.fireEvent(new ChunkDataChangeEvent(connection, pkg));
connection.getPlayer().getWorld().setChunk(pkg.getLocation(), pkg.getChunk()); connection.getPlayer().getWorld().setChunk(pkg.getLocation(), pkg.getChunk());
connection.getPlayer().getWorld().setBlockEntityData(pkg.getBlockEntities()); connection.getPlayer().getWorld().setBlockEntityData(pkg.getBlockEntities());
} }
@ -404,6 +410,7 @@ public class PacketHandler {
} }
public void handle(PacketBlockEntityMetadata pkg) { public void handle(PacketBlockEntityMetadata pkg) {
connection.fireEvent(new BlockEntityMetaDataChangeEvent(connection, pkg));
connection.getPlayer().getWorld().setBlockEntityData(pkg.getPosition(), pkg.getNbt()); connection.getPlayer().getWorld().setBlockEntityData(pkg.getPosition(), pkg.getNbt());
} }
@ -487,7 +494,9 @@ public class PacketHandler {
} }
public void handle(PacketEffect pkg) { public void handle(PacketEffect pkg) {
// ToDo if (connection.fireEvent(new EffectEvent(connection, pkg))) {
return;
}
} }
public void handle(PacketScoreboardObjective pkg) { public void handle(PacketScoreboardObjective pkg) {
@ -499,7 +508,6 @@ public class PacketHandler {
} }
public void handle(PacketScoreboardUpdateScore pkg) { public void handle(PacketScoreboardUpdateScore pkg) {
// ToDo handle correctly
switch (pkg.getAction()) { switch (pkg.getAction()) {
case CREATE_UPDATE -> connection.getPlayer().getScoreboardManager().getObjective(pkg.getScoreName()).addScore(new ScoreboardScore(pkg.getItemName(), pkg.getScoreName(), pkg.getScoreValue())); case CREATE_UPDATE -> connection.getPlayer().getScoreboardManager().getObjective(pkg.getScoreName()).addScore(new ScoreboardScore(pkg.getItemName(), pkg.getScoreName(), pkg.getScoreValue()));
case REMOVE -> { case REMOVE -> {
@ -547,7 +555,6 @@ public class PacketHandler {
if (connection.fireEvent(event)) { if (connection.fireEvent(event)) {
return; return;
} }
// ToDo ask user, download pack. for now just send an okay
} }
public void handle(PacketEntityProperties pkg) { public void handle(PacketEntityProperties pkg) {