Skip to content

Commit

Permalink
フライキックされる問題を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
bea4dev committed May 8, 2021
1 parent 61a7ed5 commit 1da9d1d
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ parallelWorld.setBlock(block, Material.STONE);
<dependency>
<groupId>com.github.Be4rJP</groupId>
<artifactId>Parallel</artifactId>
<version>v1.0.0</version>
<version>1.0.2</version>
</dependency>
```
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>be4rjp</groupId>
<artifactId>Parallel</artifactId>
<version>v1.0.1</version>
<version>1.0.2</version>
<packaging>jar</packaging>

<name>Parallel</name>
Expand Down
44 changes: 42 additions & 2 deletions src/main/java/be4rjp/parallel/nms/NMSUtil.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package be4rjp.parallel.nms;

import be4rjp.parallel.util.BlockPosition3i;
import com.mojang.datafixers.util.Pair;
import io.netty.channel.Channel;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -231,4 +229,46 @@ public static void sendEntityTeleportPacket(Player player, Object entity)
Method sendPacket = getNMSClass("PlayerConnection").getMethod("sendPacket", getNMSClass("Packet"));
sendPacket.invoke(getConnection(player), packet);
}


public static int getCombinedId(Object iBlockData)
throws ClassNotFoundException, SecurityException, NoSuchMethodException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {

Class<?> Block = getNMSClass("Block");
Class<?> IBlockData = getNMSClass("IBlockData");
Method getCombinedId = Block.getMethod("getCombinedId", IBlockData);
return (int)getCombinedId.invoke(null, iBlockData);
}


public static Object getByCombinedId(int id)
throws ClassNotFoundException, SecurityException, NoSuchMethodException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {

Class<?> Block = getNMSClass("Block");
Method getByCombinedId = Block.getMethod("getByCombinedId", int.class);
return getByCombinedId.invoke(null, id);
}


public static Object setSEtoZero(Player player)
throws ClassNotFoundException, SecurityException, NoSuchMethodException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {

Method getHandle = player.getClass().getMethod("getHandle");
Object nmsPlayer = getHandle.invoke(player);
Field conField = nmsPlayer.getClass().getField("playerConnection");
Object con = conField.get(nmsPlayer);

Class<?> PlayerConnection = getNMSClass("PlayerConnection");
Field C = PlayerConnection.getDeclaredField("C");
Field E = PlayerConnection.getDeclaredField("E");
C.setAccessible(true);
E.setAccessible(true);

C.set(con, 0);
E.set(con, 0);
return con;
}
}
19 changes: 19 additions & 0 deletions src/main/java/be4rjp/parallel/nms/PacketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import be4rjp.parallel.Parallel;
import be4rjp.parallel.nms.manager.BlockChangePacketManager;
import be4rjp.parallel.nms.manager.ChunkPacketManager;
import be4rjp.parallel.nms.manager.FlyingPacketManager;
import be4rjp.parallel.nms.manager.MultiBlockChangePacketManager;
import io.netty.channel.*;
import org.bukkit.entity.Player;
Expand All @@ -18,6 +19,13 @@ public PacketHandler(Player player){

@Override
public void channelRead(ChannelHandlerContext channelHandlerContext, Object packet) throws Exception {

if(packet.getClass().getSuperclass().getSimpleName().equalsIgnoreCase("PacketPlayInFlying")){
FlyingPacketManager manager = new FlyingPacketManager(channelHandlerContext, packet, this, player);
manager.runTaskAsynchronously(Parallel.getPlugin());
return;
}

super.channelRead(channelHandlerContext, packet);
}

Expand Down Expand Up @@ -45,6 +53,17 @@ public void write(ChannelHandlerContext channelHandlerContext, Object packet, Ch
super.write(channelHandlerContext, packet, channelPromise);
}

public void doRead(ChannelHandlerContext channelHandlerContext, Object packet) throws Exception{
try {
Channel channel = NMSUtil.getChannel(player);

ChannelHandler channelHandler = channel.pipeline().get(Parallel.getPlugin().getName() + "PacketInjector:" + player.getName());
if(channelHandler != null && player.isOnline()) {
super.channelRead(channelHandlerContext, packet);
}
}catch (ClosedChannelException e){}
}

public void doWrite(ChannelHandlerContext channelHandlerContext, Object packet, ChannelPromise channelPromise) throws Exception{
try {
Channel channel = NMSUtil.getChannel(player);
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/be4rjp/parallel/nms/manager/FlyingPacketManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package be4rjp.parallel.nms.manager;

import be4rjp.parallel.nms.NMSUtil;
import be4rjp.parallel.nms.PacketHandler;
import io.netty.channel.ChannelHandlerContext;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import java.lang.reflect.Field;
import java.nio.channels.ClosedChannelException;

public class FlyingPacketManager extends BukkitRunnable {

private static Class<?> PacketPlayInFlying;
private static Field f;

static {
try {
PacketPlayInFlying = NMSUtil.getNMSClass("PacketPlayInFlying");
f = PacketPlayInFlying.getDeclaredField("f");
f.setAccessible(true);
}catch (Exception e){
e.printStackTrace();
}
}



private final ChannelHandlerContext channelHandlerContext;
private final Object packet;
private final PacketHandler packetHandler;
private final Player player;

public FlyingPacketManager(ChannelHandlerContext channelHandlerContext, Object packet, PacketHandler packetHandler, Player player){
this.channelHandlerContext = channelHandlerContext;
this.packet = packet;
this.packetHandler = packetHandler;
this.player = player;
}

@Override
public void run() {
try{
boolean onGround = (boolean)f.get(packet);

if(onGround) NMSUtil.setSEtoZero(player);

packetHandler.doRead(channelHandlerContext, packet);
}catch (ClosedChannelException e){
}catch (Exception e){
e.printStackTrace();
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ name: Parallel
version: ${project.version}
main: be4rjp.parallel.Parallel
api-version: 1.15
softdepend: [Multiverse-Core]

0 comments on commit 1da9d1d

Please sign in to comment.