Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Apr 24, 2023
1 parent 9d6c646 commit bf2ac40
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ protected void shutdown() {
this.commandExecutor.shutdown();
}

public abstract PlatformScheduler platformScheduler();

@Override
public UUID serverId() {
return this.serverId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import net.draycia.carbon.api.users.CarbonPlayer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;

@DefaultQualifier(NonNull.class)
public interface PlatformScheduler {

void scheduleForPlayer(final CarbonPlayer carbonPlayer, final Runnable runnable);
void scheduleForPlayer(CarbonPlayer carbonPlayer, Runnable runnable);

@Singleton
final class RunImmediately implements PlatformScheduler {
@Inject
private RunImmediately() {
}

@Override
public void scheduleForPlayer(final CarbonPlayer carbonPlayer, final Runnable runnable) {
runnable.run();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.InventorySlot;
import net.draycia.carbon.common.PlatformScheduler;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.identity.Identity;
Expand All @@ -51,6 +52,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing

private transient @MonotonicNonNull @Inject CarbonChat carbonChat;
private transient @MonotonicNonNull @Inject ProfileResolver profileResolver;
private transient @MonotonicNonNull @Inject PlatformScheduler scheduler;
private transient long transientLoadedSince = -1;

protected final PersistentUserProperty<Boolean> muted;
Expand Down Expand Up @@ -141,6 +143,10 @@ private Stream<PersistentUserProperty<?>> properties() {
);
}

public void schedule(final Runnable task) {
this.scheduler.scheduleForPlayer(this, task);
}

public void registerPropertyUpdateListener(final Runnable task) {
this.properties().forEach(prop -> prop.registerUpdateListener(task));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.CarbonCommonModule;
import net.draycia.carbon.common.DataDirectory;
import net.draycia.carbon.common.PlatformScheduler;
import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.argument.PlayerSuggestions;
import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
Expand Down Expand Up @@ -122,6 +123,7 @@ public void configure() {
this.bind(Path.class).annotatedWith(DataDirectory.class).toInstance(FabricLoader.getInstance().getConfigDir().resolve(this.modContainer.getMetadata().getId()));
this.bind(CarbonServer.class).to(CarbonServerFabric.class);
this.bind(ProfileResolver.class).to(FabricProfileResolver.class);
this.bind(PlatformScheduler.class).to(FabricScheduler.class);
this.bind(new TypeLiteral<UserManager<?>>() {}).to(FabricUserManager.class);
this.bind(new TypeLiteral<UserManagerInternal<?>>() {}).to(FabricUserManager.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.fabric;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.PlatformScheduler;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;

@DefaultQualifier(NonNull.class)
@Singleton
public final class FabricScheduler implements PlatformScheduler {

private final MinecraftServerHolder serverHolder;

@Inject
private FabricScheduler(final MinecraftServerHolder serverHolder) {
this.serverHolder = serverHolder;
}

@Override
public void scheduleForPlayer(final CarbonPlayer carbonPlayer, final Runnable runnable) {
this.serverHolder.requireServer().execute(runnable);
}

}
13 changes: 0 additions & 13 deletions paper/src/main/java/net/draycia/carbon/paper/CarbonChatPaper.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import net.draycia.carbon.common.CarbonChatInternal;
import net.draycia.carbon.common.DataDirectory;
import net.draycia.carbon.common.PeriodicTasks;
import net.draycia.carbon.common.PlatformScheduler;
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
import net.draycia.carbon.common.messages.CarbonMessages;
Expand Down Expand Up @@ -67,8 +66,6 @@ public final class CarbonChatPaper extends CarbonChatInternal<CarbonPlayerPaper>

private final JavaPlugin plugin;

private final PlatformScheduler platformScheduler;

@Inject
private CarbonChatPaper(
final Injector injector,
Expand Down Expand Up @@ -101,7 +98,6 @@ private CarbonChatPaper(
messagingManager
);
this.plugin = plugin;
this.platformScheduler = new ContextualPlatformScheduler(this);
}

void onEnable() {
Expand Down Expand Up @@ -133,19 +129,10 @@ private void discoverDiscordHooks() {
}
}

public JavaPlugin bukkitPlugin() {
return this.plugin;
}

void onDisable() {
this.shutdown();
}

@Override
public PlatformScheduler platformScheduler() {
return this.platformScheduler;
}

public static boolean papiLoaded() {
return Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.CarbonCommonModule;
import net.draycia.carbon.common.DataDirectory;
import net.draycia.carbon.common.PlatformScheduler;
import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.argument.PlayerSuggestions;
import net.draycia.carbon.common.command.commands.ExecutionCoordinatorHolder;
Expand Down Expand Up @@ -128,6 +129,7 @@ public void configure() {
this.bind(CarbonServer.class).to(CarbonServerPaper.class);
this.bind(PlayerSuggestions.class).toInstance(new PlayerArgument.PlayerParser<Commander>()::suggestions);
this.bind(ProfileResolver.class).to(PaperProfileResolver.class);
this.bind(PlatformScheduler.class).to(PaperScheduler.class);
this.bind(new TypeLiteral<UserManager<?>>() {}).to(PaperUserManager.class);
this.bind(new TypeLiteral<UserManagerInternal<?>>() {}).to(PaperUserManager.class);
}
Expand Down

This file was deleted.

82 changes: 82 additions & 0 deletions paper/src/main/java/net/draycia/carbon/paper/PaperScheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.paper;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.PlatformScheduler;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;

@Singleton
@DefaultQualifier(NonNull.class)
public final class PaperScheduler implements PlatformScheduler {

private static final boolean FOLIA;

static {
boolean folia;
try {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
folia = true;
} catch (final ClassNotFoundException exception) {
folia = false;
}
FOLIA = folia;
}

private final JavaPlugin plugin;
private final Server server;

@Inject
private PaperScheduler(final JavaPlugin plugin, final Server server) {
this.plugin = plugin;
this.server = server;
}

public void scheduleForPlayer(final CarbonPlayer carbonPlayer, final Runnable runnable) {
if (!FOLIA) {
if (this.server.isPrimaryThread()) {
runnable.run();
} else {
this.server.getScheduler().runTask(this.plugin, runnable);
}
} else {
final @Nullable Player player = this.server.getPlayer(carbonPlayer.uuid());

if (player == null) {
runnable.run();
return;
}

if (this.server.isOwnedByCurrentRegion(player)) {
runnable.run();
} else {
player.getScheduler().run(this.plugin, $ -> runnable.run(), null);
}
}
}

}
Loading

0 comments on commit bf2ac40

Please sign in to comment.