Skip to content

Commit eb40d4b

Browse files
authored
Packet based (#1)
* packet fuckery * less packets * less packets * less packets * less packets * less packets * less packets * less packets
1 parent 46d802f commit eb40d4b

File tree

3 files changed

+204
-18
lines changed

3 files changed

+204
-18
lines changed

src/main/java/org/cloudanarchy/queueplugin/EventCanceler.java

+14-18
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
import org.bukkit.entity.EntityType;
77
import org.bukkit.event.EventHandler;
88
import org.bukkit.event.Listener;
9-
import org.bukkit.event.block.BlockPhysicsEvent;
109
import org.bukkit.event.entity.EntitySpawnEvent;
11-
import org.bukkit.event.player.PlayerAttemptPickupItemEvent;
1210
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
13-
import org.bukkit.event.raid.RaidTriggerEvent;
1411
import org.bukkit.plugin.java.JavaPlugin;
12+
import org.cloudanarchy.queueplugin.packets.PacketS2CUpdateTime;
1513

1614
import java.util.HashSet;
1715
import java.util.Set;
@@ -38,34 +36,32 @@ public void onPacketReceiving(PacketEvent ev) {
3836

3937
@Override
4038
public void onPacketSending(PacketEvent ev) {
39+
// these are needed or a notchian client will not join
4140
if (ev.getPacketType() == PacketType.Play.Server.KEEP_ALIVE) return;
41+
if (ev.getPacketType() == PacketType.Play.Server.POSITION) return;
42+
if (ev.getPacketType() == PacketType.Play.Server.LOGIN) return;
4243
if (ev.getPacketType() == PacketType.Play.Server.CHAT) return;
43-
ev.setCancelled(true);
44-
}
4544

46-
@EventHandler
47-
public void onEntitySpawn(EntitySpawnEvent ev) {
48-
if (ev.getEntityType() == EntityType.PLAYER) return;
49-
ev.setCancelled(true);
50-
}
45+
// if we dont send this, player has default skin lol
46+
if (ev.getPacketType() == PacketType.Play.Server.PLAYER_INFO) return;
5147

52-
@EventHandler
53-
public void onBlockPhysics(BlockPhysicsEvent ev) {
5448
ev.setCancelled(true);
55-
}
5649

57-
@EventHandler
58-
public void onCommand(PlayerCommandPreprocessEvent ev) {
59-
ev.setCancelled(true);
50+
if (ev.getPacketType() == PacketType.Play.Server.UPDATE_TIME) {
51+
PacketS2CUpdateTime p = new PacketS2CUpdateTime(ev.getPacket());
52+
p.setAgeOfTheWorld(p.getAgeOfTheWorld() - 420);
53+
p.sendPacket(ev.getPlayer());
54+
}
6055
}
6156

6257
@EventHandler
63-
public void onPlayerAttemptPickupItem(PlayerAttemptPickupItemEvent ev) {
58+
public void onEntitySpawn(EntitySpawnEvent ev) {
59+
if (ev.getEntityType() == EntityType.PLAYER) return;
6460
ev.setCancelled(true);
6561
}
6662

6763
@EventHandler
68-
public void onRaidTrigger(RaidTriggerEvent ev) {
64+
public void onCommand(PlayerCommandPreprocessEvent ev) {
6965
ev.setCancelled(true);
7066
}
7167

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* PacketWrapper - ProtocolLib wrappers for Minecraft packets
3+
* Copyright (C) dmulloy2 <http://dmulloy2.net>
4+
* Copyright (C) Kristian S. Strangeland
5+
* <p>
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
* <p>
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
* <p>
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package org.cloudanarchy.queueplugin.packets;
20+
21+
import com.comphenix.protocol.PacketType;
22+
import com.comphenix.protocol.ProtocolLibrary;
23+
import com.comphenix.protocol.events.PacketContainer;
24+
import com.google.common.base.Objects;
25+
import org.bukkit.entity.Player;
26+
27+
import java.lang.reflect.InvocationTargetException;
28+
29+
public abstract class AbstractPacket {
30+
// The packet we will be modifying
31+
protected PacketContainer handle;
32+
33+
/**
34+
* Constructs a new strongly typed wrapper for the given packet.
35+
*
36+
* @param handle - handle to the raw packet data.
37+
* @param type - the packet type.
38+
*/
39+
protected AbstractPacket(PacketContainer handle, PacketType type) {
40+
// Make sure we're given a valid packet
41+
if (handle == null)
42+
throw new IllegalArgumentException("Packet handle cannot be NULL.");
43+
if (!Objects.equal(handle.getType(), type))
44+
throw new IllegalArgumentException(handle.getHandle()
45+
+ " is not a packet of type " + type);
46+
47+
this.handle = handle;
48+
}
49+
50+
/**
51+
* Retrieve a handle to the raw packet data.
52+
*
53+
* @return Raw packet data.
54+
*/
55+
public PacketContainer getHandle() {
56+
return handle;
57+
}
58+
59+
/**
60+
* Send the current packet to the given receiver.
61+
*
62+
* @param receiver - the receiver.
63+
* @throws RuntimeException If the packet cannot be sent.
64+
*/
65+
public void sendPacket(Player receiver) {
66+
try {
67+
ProtocolLibrary.getProtocolManager().sendServerPacket(receiver,
68+
getHandle());
69+
} catch (InvocationTargetException e) {
70+
throw new RuntimeException("Cannot send packet.", e);
71+
}
72+
}
73+
74+
/**
75+
* Send the current packet to all online players.
76+
*/
77+
public void broadcastPacket() {
78+
ProtocolLibrary.getProtocolManager().broadcastServerPacket(getHandle());
79+
}
80+
81+
/**
82+
* Simulate receiving the current packet from the given sender.
83+
*
84+
* @param sender - the sender.
85+
* @throws RuntimeException If the packet cannot be received.
86+
* @deprecated Misspelled. recieve to receive
87+
* @see #receivePacket(Player)
88+
*/
89+
@Deprecated
90+
public void recievePacket(Player sender) {
91+
try {
92+
ProtocolLibrary.getProtocolManager().recieveClientPacket(sender,
93+
getHandle());
94+
} catch (Exception e) {
95+
throw new RuntimeException("Cannot recieve packet.", e);
96+
}
97+
}
98+
99+
/**
100+
* Simulate receiving the current packet from the given sender.
101+
*
102+
* @param sender - the sender.
103+
* @throws RuntimeException if the packet cannot be received.
104+
*/
105+
public void receivePacket(Player sender) {
106+
try {
107+
ProtocolLibrary.getProtocolManager().recieveClientPacket(sender,
108+
getHandle());
109+
} catch (Exception e) {
110+
throw new RuntimeException("Cannot receive packet.", e);
111+
}
112+
}
113+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* PacketWrapper - ProtocolLib wrappers for Minecraft packets
3+
* Copyright (C) dmulloy2 <http://dmulloy2.net>
4+
* Copyright (C) Kristian S. Strangeland
5+
* <p>
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
* <p>
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
* <p>
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package org.cloudanarchy.queueplugin.packets;
20+
21+
import com.comphenix.protocol.PacketType;
22+
import com.comphenix.protocol.events.PacketContainer;
23+
24+
public class PacketS2CUpdateTime extends AbstractPacket {
25+
public static final PacketType TYPE = PacketType.Play.Server.UPDATE_TIME;
26+
27+
public PacketS2CUpdateTime() {
28+
super(new PacketContainer(TYPE), TYPE);
29+
handle.getModifier().writeDefaults();
30+
}
31+
32+
public PacketS2CUpdateTime(PacketContainer packet) {
33+
super(packet, TYPE);
34+
}
35+
36+
/**
37+
* Retrieve Age of the world.
38+
* <p>
39+
* Notes: in ticks; not changed by server commands
40+
*
41+
* @return The current Age of the world
42+
*/
43+
public long getAgeOfTheWorld() {
44+
return handle.getLongs().read(0);
45+
}
46+
47+
/**
48+
* Set Age of the world.
49+
*
50+
* @param value - new value.
51+
*/
52+
public void setAgeOfTheWorld(long value) {
53+
handle.getLongs().write(0, value);
54+
}
55+
56+
/**
57+
* Retrieve Time of day.
58+
* <p>
59+
* Notes: the world (or region) time, in ticks. If negative the sun will
60+
* stop moving at the Math.abs of the time
61+
*
62+
* @return The current Time of day
63+
*/
64+
public long getTimeOfDay() {
65+
return handle.getLongs().read(1);
66+
}
67+
68+
/**
69+
* Set Time of day.
70+
*
71+
* @param value - new value.
72+
*/
73+
public void setTimeOfDay(long value) {
74+
handle.getLongs().write(1, value);
75+
}
76+
77+
}

0 commit comments

Comments
 (0)