Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some folia fixes #2460

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/main/java/com/comphenix/protocol/PacketType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.comphenix.protocol.PacketTypeLookup.ClassLookup;
import com.comphenix.protocol.events.ConnectionSide;
import com.comphenix.protocol.injector.packet.PacketRegistry;
import com.comphenix.protocol.scheduler.UniversalRunnable;
import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.utility.MinecraftVersion;
import com.google.common.base.Preconditions;
Expand All @@ -19,8 +20,6 @@

import org.apache.commons.lang.WordUtils;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitRunnable;

/**
* Represents the type of a packet in a specific protocol.
* <p>
Expand Down Expand Up @@ -1022,7 +1021,7 @@ public static boolean hasClass(Class<?> packetClass) {
* @param name - the name of the packet.
*/
public static void scheduleRegister(final PacketType type, final String name) {
BukkitRunnable runnable = new BukkitRunnable() {
UniversalRunnable runnable = new UniversalRunnable() {
@Override
public void run() {
PacketTypeEnum objEnum;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,8 @@ public void sendServerPacket(Player receiver, PacketContainer packet, NetworkMar
// ensure we are on the main thread if any listener requires that
if (this.playerInjectionHandler.hasMainThreadListener(packet.getType()) && !this.server.isPrimaryThread()) {
NetworkMarker copy = marker; // okay fine
this.server.getScheduler().scheduleSyncDelayedTask(
this.plugin,
() -> this.sendServerPacket(receiver, packet, copy, false));
ProtocolLibrary.getScheduler().scheduleSyncDelayedTask(
() -> this.sendServerPacket(receiver, packet, copy, false), 1L);
return;
}

Expand Down Expand Up @@ -218,8 +217,7 @@ public void receiveClientPacket(Player sender, PacketContainer packet, NetworkMa
if (!this.closed) {
// make sure we are on the main thread if any listener of the packet needs it
if (this.playerInjectionHandler.hasMainThreadListener(packet.getType()) && !this.server.isPrimaryThread()) {
this.server.getScheduler().scheduleSyncDelayedTask(
this.plugin,
ProtocolLibrary.getScheduler().runTask(
() -> this.receiveClientPacket(sender, packet, marker, filters));
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.PacketType.Protocol;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.error.ErrorReporter;
import com.comphenix.protocol.error.Report;
import com.comphenix.protocol.error.ReportType;
Expand Down Expand Up @@ -490,8 +491,7 @@ private void ensureInEventLoop(EventLoop eventLoop, Runnable runnable) {
void processInboundPacket(ChannelHandlerContext ctx, Object packet, Class<?> packetClass) {
if (this.channelListener.hasMainThreadListener(packetClass) && !this.server.isPrimaryThread()) {
// not on the main thread but we are required to be - re-schedule the packet on the main thread
this.server.getScheduler().runTask(
this.injectionFactory.getPlugin(),
ProtocolLibrary.getScheduler().runTask(
() -> this.processInboundPacket(ctx, packet, packetClass));
return;
}
Expand Down Expand Up @@ -555,8 +555,7 @@ <T> T processOutbound(T action) {
// ensure that we are on the main thread if we need to
if (this.channelListener.hasMainThreadListener(packet.getClass()) && !this.server.isPrimaryThread()) {
// not on the main thread but we are required to be - re-schedule the packet on the main thread
this.server.getScheduler().scheduleSyncDelayedTask(
this.injectionFactory.getPlugin(),
ProtocolLibrary.getScheduler().runTask(
() -> this.sendServerPacket(packet, null, true));
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.comphenix.protocol.scheduler;

import com.comphenix.protocol.ProtocolLibrary;
import org.bukkit.plugin.Plugin;

/** Just modified BukkitRunnable */
public abstract class UniversalRunnable implements Runnable {
Task task;

public synchronized void cancel() throws IllegalStateException {
checkScheduled();
task.cancel();
}

/**
* Schedules this in the scheduler to run on next tick.
*
* @param plugin the reference to the plugin scheduling task
* @return {@link Task}
* @throws IllegalArgumentException if plugin is null
* @throws IllegalStateException if this was already scheduled
* @see ProtocolScheduler#runTask(Runnable)
*/

public synchronized Task runTask(Plugin plugin) throws IllegalArgumentException, IllegalStateException {
checkNotYetScheduled();
return setupTask(ProtocolLibrary.getScheduler().runTask(this));
}

/**
* Schedules this to run after the specified number of server ticks.
*
* @param plugin the reference to the plugin scheduling task
* @param delay the ticks to wait before running the task
* @return {@link Task}
* @throws IllegalArgumentException if plugin is null
* @throws IllegalStateException if this was already scheduled
* @see ProtocolScheduler#scheduleSyncDelayedTask(Runnable, long)
*/

public synchronized Task runTaskLater(Plugin plugin, long delay) throws IllegalArgumentException, IllegalStateException {
checkNotYetScheduled();
return setupTask(ProtocolLibrary.getScheduler().scheduleSyncDelayedTask(this, delay));
}

private void checkScheduled() {
if (task == null) {
throw new IllegalStateException("Not scheduled yet");
}
}

private void checkNotYetScheduled() {
if (task != null) {
throw new IllegalStateException("Already scheduled");
}
}


private Task setupTask(final Task task) {
this.task = task;
return task;
}


}