cfi packet listener

This commit is contained in:
Jesse Boyd
2019-10-30 12:58:21 +01:00
parent 72951cdf23
commit c383fab2c2
8 changed files with 153 additions and 114 deletions

View File

@ -141,15 +141,15 @@ public class FaweBukkit implements IFawe, Listener {
PluginManager manager = Bukkit.getPluginManager();
if (manager.getPlugin("PacketListenerApi") == null) {
File output = new File(plugin.getDataFolder().getParentFile(), "PacketListenerAPI_v3.6.0-SNAPSHOT.jar");
byte[] jarData = Jars.PL_v3_6_0.download();
File output = new File(plugin.getDataFolder().getParentFile(), "PacketListenerAPI_v3.7.3-SNAPSHOT.jar");
byte[] jarData = Jars.PL_v3_7_3.download();
try (FileOutputStream fos = new FileOutputStream(output)) {
fos.write(jarData);
}
}
if (manager.getPlugin("MapManager") == null) {
File output = new File(plugin.getDataFolder().getParentFile(), "MapManager_v1.4.0-SNAPSHOT.jar");
byte[] jarData = Jars.MM_v1_4_0.download();
File output = new File(plugin.getDataFolder().getParentFile(), "MapManager_v1.7.3-SNAPSHOT.jar");
byte[] jarData = Jars.MM_v1_7_3.download();
try (FileOutputStream fos = new FileOutputStream(output)) {
fos.write(jarData);
}

View File

@ -20,6 +20,7 @@
package com.boydti.fawe.bukkit.adapter.mc1_14;
import com.boydti.fawe.Fawe;
import com.boydti.fawe.FaweCache;
import com.boydti.fawe.beta.implementation.ChunkPacket;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
@ -679,7 +680,12 @@ public final class Spigot_v1_14_R4 extends CachedBukkitAdapter implements Bukkit
nmsPacket = MapChunkUtil_1_14.create(this, packet);
packet.setNativePacket(nmsPacket);
}
entityPlayer.playerConnection.sendPacket(nmsPacket);
try {
FaweCache.IMP.CHUNK_FLAG.get().set(true);
entityPlayer.playerConnection.sendPacket(nmsPacket);
} finally {
FaweCache.IMP.CHUNK_FLAG.get().set(false);
}
}
});
}

View File

@ -1,5 +1,6 @@
package com.boydti.fawe.bukkit.listener;
import com.boydti.fawe.FaweCache;
import com.boydti.fawe.command.CFICommands;
import com.boydti.fawe.object.RunnableVal3;
import com.boydti.fawe.object.brush.visualization.VirtualWorld;
@ -15,20 +16,32 @@ import com.comphenix.protocol.wrappers.BlockPosition;
import com.comphenix.protocol.wrappers.ChunkCoordIntPair;
import com.comphenix.protocol.wrappers.EnumWrappers;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.BukkitPlayer;
import com.sk89q.worldedit.event.platform.BlockInteractEvent;
import com.sk89q.worldedit.event.platform.Interaction;
import com.sk89q.worldedit.extension.platform.PlatformManager;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.world.block.BlockState;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.List;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockTypes;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
/**
* The CFIPacketListener handles packets for editing the VirtualWorld
@ -45,99 +58,90 @@ public class CFIPacketListener implements Listener {
this.plugin = plugin;
this.protocolmanager = ProtocolLibrary.getProtocolManager();
// TODO NOT IMPLEMENTED
// // Direct digging to the virtual world
// registerBlockEvent(PacketType.Play.Client.BLOCK_DIG, false, new RunnableVal3<PacketEvent, VirtualWorld, BlockVector3>() {
// @Override
// public void run(Builder event, URI gen, String pt) {
// try {
// Player plr = event.getPlayer();
// BlockVector3 realPos = pt.add(gen.getOrigin().toBlockPoint());
// if (!sendBlockChange(plr, gen, pt, Interaction.HIT)) {
// gen.setBlock(pt, BlockTypes.AIR.getDefaultState());
// }
// } catch (WorldEditException e) {
// e.printStackTrace();
// }
// }
// });
//
// // Direct placing to the virtual world
// RunnableVal3<PacketEvent, VirtualWorld, BlockVector3> placeTask = new RunnableVal3<PacketEvent, VirtualWorld, BlockVector3>() {
// @Override
// public void run(Builder event, URI gen, String pt) {
// try {
// Player plr = event.getPlayer();
// List<EnumWrappers.Hand> hands = event.getPacket().getHands().getValues();
//
// EnumWrappers.Hand enumHand = hands.isEmpty() ? EnumWrappers.Hand.MAIN_HAND : hands.get(0);
// PlayerInventory inv = plr.getInventory();
// ItemStack hand = enumHand == EnumWrappers.Hand.MAIN_HAND ? inv.getItemInMainHand() : inv.getItemInOffHand();
// if (hand.getType().isBlock()) {
// Material type = hand.getType();
// switch (type) {
// case AIR:
// case CAVE_AIR:
// case VOID_AIR:
// break;
// default: {
// BlockStateHolder block = BukkitAdapter.asBlockState(hand);
// if (block != null) {
// gen.setBlock(pt, block);
// return;
// }
// }
// }
// }
// pt = getRelPos(event, gen);
// sendBlockChange(plr, gen, pt, Interaction.OPEN);
// } catch (WorldEditException e) {
// e.printStackTrace();
// }
// }
// };
// registerBlockEvent(PacketType.Play.Client.BLOCK_PLACE, true, placeTask);
// registerBlockEvent(PacketType.Play.Client.USE_ITEM, true, placeTask);
//
// // Cancel block change packets where the real world overlaps with the virtual one
// registerBlockEvent(PacketType.Play.Server.BLOCK_CHANGE, false, new RunnableVal3<PacketEvent, VirtualWorld, BlockVector3>() {
// @Override
// public void run(Builder event, URI gen, String pt) {
// // Do nothing
// }
// });
//
// // Modify chunk packets where the real world overlaps with the virtual one
// protocolmanager.addPacketListener(new PacketAdapter(plugin, ListenerPriority.NORMAL, PacketType.Play.Server.MAP_CHUNK) {
// @Override
// public void onPacketSending(PacketEvent event) {
// if (!event.isServerPacket()) return;
//
// VirtualWorld gen = getGenerator(event);
// if (gen != null) {
// BlockVector3 origin = gen.getOrigin().toBlockPoint();
// PacketContainer packet = event.getPacket();
// StructureModifier<Integer> ints = packet.getIntegers();
// int cx = ints.read(0);
// int cz = ints.read(1);
//
// int ocx = origin.getBlockX() >> 4;
// int ocz = origin.getBlockZ() >> 4;
//
// if (gen.contains(BlockVector3.at((cx - ocx) << 4, 0, (cz - ocz) << 4))) {
// event.setCancelled(true);
//
// Player plr = event.getPlayer();
//
// FaweQueue queue = SetQueue.IMP.getNewQueue(plr.getWorld().getName(), true, false);
//
// FaweChunk toSend = gen.getSnapshot(cx - ocx, cz - ocz);
// toSend.setLoc(gen, cx, cz);
// queue.sendChunkUpdate(toSend, FawePlayer.wrap(plr));
// }
// }
// }
// });
// Direct digging to the virtual world
registerBlockEvent(PacketType.Play.Client.BLOCK_DIG, false, new RunnableVal3<PacketEvent, VirtualWorld, BlockVector3>() {
@Override
public void run(PacketEvent event, VirtualWorld gen, BlockVector3 pt) {
try {
Player plr = event.getPlayer();
BlockVector3 realPos = pt.add(gen.getOrigin().toBlockPoint());
if (!sendBlockChange(plr, gen, pt, Interaction.HIT)) {
gen.setBlock(pt, BlockTypes.AIR.getDefaultState());
}
} catch (WorldEditException e) {
e.printStackTrace();
}
}
});
// Direct placing to the virtual world
RunnableVal3<PacketEvent, VirtualWorld, BlockVector3> placeTask = new RunnableVal3<PacketEvent, VirtualWorld, BlockVector3>() {
@Override
public void run(PacketEvent event, VirtualWorld gen, BlockVector3 pt) {
try {
Player plr = event.getPlayer();
List<EnumWrappers.Hand> hands = event.getPacket().getHands().getValues();
EnumWrappers.Hand enumHand = hands.isEmpty() ? EnumWrappers.Hand.MAIN_HAND : hands.get(0);
PlayerInventory inv = plr.getInventory();
ItemStack hand = enumHand == EnumWrappers.Hand.MAIN_HAND ? inv.getItemInMainHand() : inv.getItemInOffHand();
if (hand.getType().isBlock()) {
Material type = hand.getType();
switch (type) {
case AIR:
case CAVE_AIR:
case VOID_AIR:
break;
default: {
BlockStateHolder block = BukkitAdapter.asBlockState(hand);
if (block != null) {
gen.setBlock(pt, block);
return;
}
}
}
}
pt = getRelPos(event, gen);
sendBlockChange(plr, gen, pt, Interaction.OPEN);
} catch (WorldEditException e) {
e.printStackTrace();
}
}
};
registerBlockEvent(PacketType.Play.Client.BLOCK_PLACE, true, placeTask);
registerBlockEvent(PacketType.Play.Client.USE_ITEM, true, placeTask);
// Cancel block change packets where the real world overlaps with the virtual one
registerBlockEvent(PacketType.Play.Server.BLOCK_CHANGE, false, new RunnableVal3<PacketEvent, VirtualWorld, BlockVector3>() {
@Override
public void run(PacketEvent event, VirtualWorld gen, BlockVector3 pt) {
// Do nothing
}
});
// Modify chunk packets where the real world overlaps with the virtual one
protocolmanager.addPacketListener(new PacketAdapter(plugin, ListenerPriority.NORMAL, PacketType.Play.Server.MAP_CHUNK) {
@Override
public void onPacketSending(PacketEvent event) {
if (!event.isServerPacket() || FaweCache.IMP.CHUNK_FLAG.get().get()) return;
VirtualWorld gen = getGenerator(event);
if (gen != null) {
BlockVector3 origin = gen.getOrigin().toBlockPoint();
PacketContainer packet = event.getPacket();
StructureModifier<Integer> ints = packet.getIntegers();
int cx = ints.read(0);
int cz = ints.read(1);
int ocx = origin.getBlockX() >> 4;
int ocz = origin.getBlockZ() >> 4;
if (gen.contains(BlockVector3.at((cx - ocx) << 4, 0, (cz - ocz) << 4))) {
event.setCancelled(true);
gen.refreshChunk(cx - ocx, cz - ocz);
}
}
}
});
// The following few listeners are to ignore block collisions where the virtual and real world overlap