Skip to content

Commit 65a9ef5

Browse files
mani1232dmulloy2
andauthored
Initial Folia support (#2346)
Co-authored-by: Dan Mulloy <[email protected]>
1 parent fbf6120 commit 65a9ef5

File tree

6 files changed

+192
-82
lines changed

6 files changed

+192
-82
lines changed

src/main/java/com/comphenix/protocol/ProtocolLib.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,10 @@
2727
import com.comphenix.protocol.metrics.Statistics;
2828
import com.comphenix.protocol.updater.Updater;
2929
import com.comphenix.protocol.updater.Updater.UpdateType;
30-
import com.comphenix.protocol.utility.ByteBuddyFactory;
31-
import com.comphenix.protocol.utility.ChatExtensions;
32-
import com.comphenix.protocol.utility.MinecraftVersion;
33-
import com.comphenix.protocol.utility.Util;
30+
import com.comphenix.protocol.utility.*;
3431
import com.google.common.base.Splitter;
3532
import com.google.common.collect.Iterables;
33+
3634
import java.io.File;
3735
import java.io.IOException;
3836
import java.util.HashSet;
@@ -44,6 +42,7 @@
4442
import java.util.logging.Logger;
4543
import java.util.regex.Matcher;
4644
import java.util.regex.Pattern;
45+
4746
import org.bukkit.Server;
4847
import org.bukkit.command.CommandExecutor;
4948
import org.bukkit.command.PluginCommand;
@@ -57,7 +56,6 @@
5756
* @author Kristian
5857
*/
5958
public class ProtocolLib extends JavaPlugin {
60-
6159
// Every possible error or warning report type
6260
public static final ReportType REPORT_CANNOT_DELETE_CONFIG = new ReportType(
6361
"Cannot delete old ProtocolLib configuration.");
@@ -490,9 +488,8 @@ private void createPacketTask(Server server) {
490488
}
491489

492490
// Attempt to create task
493-
this.packetTask = server.getScheduler().scheduleSyncRepeatingTask(this, () -> {
491+
this.packetTask = SchedulerUtil.scheduleSyncRepeatingTask(this, () -> {
494492
AsyncFilterManager manager = (AsyncFilterManager) protocolManager.getAsynchronousManager();
495-
496493
// We KNOW we're on the main thread at the moment
497494
manager.sendProcessedPackets(ProtocolLib.this.tickCounter++, true);
498495

@@ -567,7 +564,7 @@ public void onDisable() {
567564

568565
// Clean up
569566
if (this.packetTask >= 0) {
570-
this.getServer().getScheduler().cancelTask(this.packetTask);
567+
SchedulerUtil.cancelTask(this, packetTask);
571568
this.packetTask = -1;
572569
}
573570

src/main/java/com/comphenix/protocol/events/SerializedOfflinePlayer.java

Lines changed: 91 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,25 @@
22
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
33
* Copyright (C) 2012 Kristian S. Stangeland
44
*
5-
* This program is free software; you can redistribute it and/or modify it under the terms of the
6-
* GNU General Public License as published by the Free Software Foundation; either version 2 of
5+
* This program is free software; you can redistribute it and/or modify it under the terms of the
6+
* GNU General Public License as published by the Free Software Foundation; either version 2 of
77
* the License, or (at your option) any later version.
88
*
9-
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10-
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1111
* See the GNU General Public License for more details.
1212
*
13-
* You should have received a copy of the GNU General Public License along with this program;
14-
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
13+
* You should have received a copy of the GNU General Public License along with this program;
14+
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1515
* 02111-1307 USA
1616
*/
1717

1818
package com.comphenix.protocol.events;
1919

20-
import java.io.IOException;
21-
import java.io.ObjectInputStream;
22-
import java.io.ObjectOutputStream;
23-
import java.io.Serializable;
24-
import java.lang.reflect.Constructor;
25-
import java.lang.reflect.InvocationTargetException;
26-
import java.lang.reflect.Method;
27-
import java.util.Map;
28-
import java.util.UUID;
29-
import java.util.concurrent.ConcurrentHashMap;
30-
3120
import com.comphenix.protocol.reflect.accessors.Accessors;
3221
import com.comphenix.protocol.reflect.accessors.MethodAccessor;
3322
import com.comphenix.protocol.utility.ByteBuddyFactory;
34-
35-
import org.bukkit.*;
36-
import org.bukkit.entity.EntityType;
37-
import org.bukkit.entity.Player;
38-
import org.bukkit.profile.PlayerProfile;
39-
23+
import com.comphenix.protocol.utility.Util;
4024
import net.bytebuddy.description.ByteCodeElement;
4125
import net.bytebuddy.description.modifier.Visibility;
4226
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
@@ -46,15 +30,31 @@
4630
import net.bytebuddy.implementation.MethodCall;
4731
import net.bytebuddy.implementation.MethodDelegation;
4832
import net.bytebuddy.implementation.bind.annotation.AllArguments;
49-
import net.bytebuddy.implementation.bind.annotation.Origin;
5033
import net.bytebuddy.implementation.bind.annotation.FieldValue;
34+
import net.bytebuddy.implementation.bind.annotation.Origin;
5135
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
5236
import net.bytebuddy.matcher.ElementMatcher;
5337
import net.bytebuddy.matcher.ElementMatchers;
38+
import org.bukkit.*;
39+
import org.bukkit.entity.EntityType;
40+
import org.bukkit.entity.Player;
41+
import org.bukkit.profile.PlayerProfile;
42+
import org.jetbrains.annotations.NotNull;
43+
44+
import java.io.IOException;
45+
import java.io.ObjectInputStream;
46+
import java.io.ObjectOutputStream;
47+
import java.io.Serializable;
48+
import java.lang.reflect.Constructor;
49+
import java.lang.reflect.InvocationTargetException;
50+
import java.lang.reflect.Method;
51+
import java.util.Map;
52+
import java.util.UUID;
53+
import java.util.concurrent.ConcurrentHashMap;
5454

5555
/**
5656
* Represents a player object that can be serialized by Java.
57-
*
57+
*
5858
* @author Kristian
5959
*/
6060
class SerializedOfflinePlayer implements OfflinePlayer, Serializable {
@@ -65,7 +65,7 @@ class SerializedOfflinePlayer implements OfflinePlayer, Serializable {
6565
private static final long serialVersionUID = -2728976288470282810L;
6666

6767
private transient Location bedSpawnLocation;
68-
68+
6969
// Relevant data about an offline player
7070
private String name;
7171
private UUID uuid;
@@ -76,6 +76,8 @@ class SerializedOfflinePlayer implements OfflinePlayer, Serializable {
7676
private boolean playedBefore;
7777
private boolean online;
7878
private boolean whitelisted;
79+
private long lastLogin;
80+
private long lastSeen;
7981

8082
private static final Constructor<?> proxyPlayerConstructor = setupProxyPlayerConstructor();
8183

@@ -85,9 +87,10 @@ class SerializedOfflinePlayer implements OfflinePlayer, Serializable {
8587
public SerializedOfflinePlayer() {
8688
// Do nothing
8789
}
88-
90+
8991
/**
9092
* Initialize this serializable offline player from another player.
93+
*
9194
* @param offline - another player.
9295
*/
9396
public SerializedOfflinePlayer(OfflinePlayer offline) {
@@ -100,8 +103,14 @@ public SerializedOfflinePlayer(OfflinePlayer offline) {
100103
this.playedBefore = offline.hasPlayedBefore();
101104
this.online = offline.isOnline();
102105
this.whitelisted = offline.isWhitelisted();
106+
107+
// TODO needs to be reflectively obtained
108+
if (Util.isUsingFolia()) {
109+
// this.lastSeen = offline.getLastSeen();
110+
// this.lastLogin = offline.getLastLogin();
111+
}
103112
}
104-
113+
105114
@Override
106115
public boolean isOp() {
107116
return operator;
@@ -122,49 +131,74 @@ public Location getBedSpawnLocation() {
122131
return bedSpawnLocation;
123132
}
124133

134+
// @Override
135+
public long getLastLogin() {
136+
return lastLogin;
137+
}
138+
139+
// @Override
140+
public long getLastSeen() {
141+
return lastSeen;
142+
}
143+
125144
// TODO do we need to implement this?
126-
127-
public void incrementStatistic(Statistic statistic) throws IllegalArgumentException { }
128145

129-
public void decrementStatistic(Statistic statistic) throws IllegalArgumentException { }
146+
public void incrementStatistic(Statistic statistic) throws IllegalArgumentException {
147+
}
148+
149+
public void decrementStatistic(Statistic statistic) throws IllegalArgumentException {
150+
}
130151

131-
public void incrementStatistic(Statistic statistic, int i) throws IllegalArgumentException { }
152+
public void incrementStatistic(Statistic statistic, int i) throws IllegalArgumentException {
153+
}
132154

133-
public void decrementStatistic(Statistic statistic, int i) throws IllegalArgumentException { }
155+
public void decrementStatistic(Statistic statistic, int i) throws IllegalArgumentException {
156+
}
134157

135-
public void setStatistic(Statistic statistic, int i) throws IllegalArgumentException { }
158+
public void setStatistic(Statistic statistic, int i) throws IllegalArgumentException {
159+
}
136160

137161
public int getStatistic(Statistic statistic) throws IllegalArgumentException {
138162
return 0;
139163
}
140164

141-
public void incrementStatistic(Statistic statistic, Material material) throws IllegalArgumentException { }
165+
public void incrementStatistic(Statistic statistic, Material material) throws IllegalArgumentException {
166+
}
142167

143-
public void decrementStatistic(Statistic statistic, Material material) throws IllegalArgumentException { }
168+
public void decrementStatistic(Statistic statistic, Material material) throws IllegalArgumentException {
169+
}
144170

145171
public int getStatistic(Statistic statistic, Material material) throws IllegalArgumentException {
146172
return 0;
147173
}
148174

149-
public void incrementStatistic(Statistic statistic, Material material, int i) throws IllegalArgumentException { }
175+
public void incrementStatistic(Statistic statistic, Material material, int i) throws IllegalArgumentException {
176+
}
150177

151-
public void decrementStatistic(Statistic statistic, Material material, int i) throws IllegalArgumentException { }
178+
public void decrementStatistic(Statistic statistic, Material material, int i) throws IllegalArgumentException {
179+
}
152180

153-
public void setStatistic(Statistic statistic, Material material, int i) throws IllegalArgumentException { }
181+
public void setStatistic(Statistic statistic, Material material, int i) throws IllegalArgumentException {
182+
}
154183

155-
public void incrementStatistic(Statistic statistic, EntityType entityType) throws IllegalArgumentException { }
184+
public void incrementStatistic(Statistic statistic, EntityType entityType) throws IllegalArgumentException {
185+
}
156186

157-
public void decrementStatistic(Statistic statistic, EntityType entityType) throws IllegalArgumentException { }
187+
public void decrementStatistic(Statistic statistic, EntityType entityType) throws IllegalArgumentException {
188+
}
158189

159190
public int getStatistic(Statistic statistic, EntityType entityType) throws IllegalArgumentException {
160191
return 0;
161192
}
162193

163-
public void incrementStatistic(Statistic statistic, EntityType entityType, int i) throws IllegalArgumentException { }
194+
public void incrementStatistic(Statistic statistic, EntityType entityType, int i) throws IllegalArgumentException {
195+
}
164196

165-
public void decrementStatistic(Statistic statistic, EntityType entityType, int i) { }
197+
public void decrementStatistic(Statistic statistic, EntityType entityType, int i) {
198+
}
166199

167-
public void setStatistic(Statistic statistic, EntityType entityType, int i) { }
200+
public void setStatistic(Statistic statistic, EntityType entityType, int i) {
201+
}
168202

169203
@Override
170204
public Location getLastDeathLocation() {
@@ -187,15 +221,15 @@ public UUID getUniqueId() {
187221
}
188222

189223
@Override
190-
public PlayerProfile getPlayerProfile() {
224+
public @NotNull PlayerProfile getPlayerProfile() {
191225
return null;
192226
}
193227

194228
@Override
195229
public String getName() {
196230
return name;
197231
}
198-
232+
199233
@Override
200234
public boolean hasPlayedBefore() {
201235
return playedBefore;
@@ -209,7 +243,7 @@ public boolean isBanned() {
209243
public void setBanned(boolean banned) {
210244
this.banned = banned;
211245
}
212-
246+
213247
@Override
214248
public boolean isOnline() {
215249
return online;
@@ -227,14 +261,14 @@ public void setWhitelisted(boolean whitelisted) {
227261

228262
private void writeObject(ObjectOutputStream output) throws IOException {
229263
output.defaultWriteObject();
230-
264+
231265
// Serialize the bed spawn location
232266
output.writeUTF(bedSpawnLocation.getWorld().getName());
233267
output.writeDouble(bedSpawnLocation.getX());
234268
output.writeDouble(bedSpawnLocation.getY());
235269
output.writeDouble(bedSpawnLocation.getZ());
236270
}
237-
271+
238272
private void readObject(ObjectInputStream input) throws ClassNotFoundException, IOException {
239273
input.defaultReadObject();
240274

@@ -246,7 +280,7 @@ private void readObject(ObjectInputStream input) throws ClassNotFoundException,
246280
input.readDouble()
247281
);
248282
}
249-
283+
250284
private World getWorld(String name) {
251285
try {
252286
// Try to get the world at least
@@ -256,7 +290,7 @@ private World getWorld(String name) {
256290
return null;
257291
}
258292
}
259-
293+
260294
@Override
261295
public Player getPlayer() {
262296
try {
@@ -266,11 +300,12 @@ public Player getPlayer() {
266300
return getProxyPlayer();
267301
}
268302
}
269-
303+
270304
/**
271305
* Retrieve a player object that implements OfflinePlayer by referring to this object.
272306
* <p>
273307
* All other methods cause an exception.
308+
*
274309
* @return Proxy object.
275310
*/
276311
public Player getProxyPlayer() {
@@ -285,8 +320,7 @@ public Player getProxyPlayer() {
285320
}
286321
}
287322

288-
private static Constructor<? extends Player> setupProxyPlayerConstructor()
289-
{
323+
private static Constructor<? extends Player> setupProxyPlayerConstructor() {
290324
final Method[] offlinePlayerMethods = OfflinePlayer.class.getMethods();
291325
final String[] methodNames = new String[offlinePlayerMethods.length];
292326
for (int idx = 0; idx < offlinePlayerMethods.length; ++idx)
@@ -321,9 +355,9 @@ public Object intercept(
321355
});
322356

323357
final InvocationHandlerAdapter throwException = InvocationHandlerAdapter.of((obj, method, args) -> {
324-
throw new UnsupportedOperationException(
358+
throw new UnsupportedOperationException(
325359
"The method " + method.getName() + " is not supported for offline players.");
326-
});
360+
});
327361

328362
return ByteBuddyFactory.getInstance()
329363
.createSubclass(PlayerUnion.class, ConstructorStrategy.Default.NO_CONSTRUCTORS)
@@ -354,7 +388,6 @@ public Object intercept(
354388
* This interface extends both OfflinePlayer and Player (in that order) so that the class generated by ByteBuddy
355389
* looks at OfflinePlayer's methods first while still being a Player.
356390
*/
357-
private interface PlayerUnion extends OfflinePlayer, Player
358-
{
391+
private interface PlayerUnion extends OfflinePlayer, Player {
359392
}
360393
}

0 commit comments

Comments
 (0)