Skip to content

Commit d4358c8

Browse files
v0.2.2 - Add getPlayer()
1 parent 8e0f9b0 commit d4358c8

4 files changed

Lines changed: 102 additions & 1 deletion

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
}
44

55
group 'com.tcoded'
6-
version '0.2.1'
6+
version '0.2.2'
77

88
java {
99
sourceCompatibility = JavaVersion.VERSION_17

src/main/java/com/tcoded/folialib/impl/FoliaImplementation.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
import org.bukkit.Location;
1212
import org.bukkit.World;
1313
import org.bukkit.entity.Entity;
14+
import org.bukkit.entity.Player;
1415
import org.bukkit.plugin.java.JavaPlugin;
1516

17+
import java.util.UUID;
1618
import java.util.concurrent.CompletableFuture;
1719
import java.util.concurrent.TimeUnit;
1820

@@ -194,4 +196,22 @@ public void cancelAllTasks() {
194196
this.globalRegionScheduler.cancelTasks(plugin);
195197
this.asyncScheduler.cancelTasks(plugin);
196198
}
199+
200+
@Override
201+
public Player getPlayer(String name) {
202+
// This is thread-safe in folia
203+
return this.plugin.getServer().getPlayer(name);
204+
}
205+
206+
@Override
207+
public Player getPlayerExact(String name) {
208+
// This is thread-safe in folia
209+
return this.plugin.getServer().getPlayerExact(name);
210+
}
211+
212+
@Override
213+
public Player getPlayer(UUID uuid) {
214+
// This is thread-safe in folia
215+
return this.plugin.getServer().getPlayer(uuid);
216+
}
197217
}

src/main/java/com/tcoded/folialib/impl/ServerImplementation.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import com.tcoded.folialib.wrapper.WrappedTask;
55
import org.bukkit.Location;
66
import org.bukkit.entity.Entity;
7+
import org.bukkit.entity.Player;
78

9+
import java.util.UUID;
810
import java.util.concurrent.CompletableFuture;
911
import java.util.concurrent.TimeUnit;
1012

@@ -177,4 +179,25 @@ public interface ServerImplementation {
177179
* Cancel all tasks
178180
*/
179181
void cancelAllTasks();
182+
183+
/**
184+
* Get a player by name (approximately)
185+
* @param name Name of the player
186+
* @return Player instance
187+
*/
188+
Player getPlayer(String name);
189+
190+
/**
191+
* Get a player by name (exactly)
192+
* @param name Name of the player
193+
* @return Player instance
194+
*/
195+
Player getPlayerExact(String name);
196+
197+
/**
198+
* Get a player by UUID
199+
* @param uuid UUID of the player
200+
* @return Player instance
201+
*/
202+
Player getPlayer(UUID uuid);
180203
}

src/main/java/com/tcoded/folialib/impl/SpigotImplementation.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
import com.tcoded.folialib.wrapper.task.WrappedBukkitTask;
88
import org.bukkit.Location;
99
import org.bukkit.entity.Entity;
10+
import org.bukkit.entity.Player;
1011
import org.bukkit.plugin.java.JavaPlugin;
1112
import org.bukkit.scheduler.BukkitScheduler;
1213
import org.jetbrains.annotations.NotNull;
1314

15+
import java.util.UUID;
1416
import java.util.concurrent.CompletableFuture;
17+
import java.util.concurrent.ExecutionException;
1518
import java.util.concurrent.TimeUnit;
1619

1720
public class SpigotImplementation implements ServerImplementation {
@@ -167,4 +170,59 @@ public void cancelTask(WrappedTask task) {
167170
public void cancelAllTasks() {
168171
this.scheduler.cancelTasks(plugin);
169172
}
173+
174+
@Override
175+
public Player getPlayer(String name) {
176+
// Already on the main thread
177+
if (this.plugin.getServer().isPrimaryThread()) {
178+
return this.plugin.getServer().getPlayer(name);
179+
}
180+
// Not on the main thread, we need to wait until the next tick
181+
else {
182+
try {
183+
return this.scheduler.callSyncMethod(plugin, () -> this.plugin.getServer().getPlayer(name)).get();
184+
} catch (InterruptedException | ExecutionException e) {
185+
e.printStackTrace();
186+
}
187+
}
188+
// Fallback to null
189+
return null;
190+
}
191+
192+
@Override
193+
public Player getPlayerExact(String name) {
194+
// Already on the main thread
195+
if (this.plugin.getServer().isPrimaryThread()) {
196+
return this.plugin.getServer().getPlayerExact(name);
197+
}
198+
// Not on the main thread, we need to wait until the next tick
199+
else {
200+
try {
201+
return this.scheduler.callSyncMethod(plugin, () -> this.plugin.getServer().getPlayerExact(name)).get();
202+
} catch (InterruptedException | ExecutionException e) {
203+
e.printStackTrace();
204+
}
205+
}
206+
// Fallback to null
207+
return null;
208+
}
209+
210+
@SuppressWarnings("DuplicatedCode")
211+
@Override
212+
public Player getPlayer(UUID uuid) {
213+
// Already on the main thread
214+
if (this.plugin.getServer().isPrimaryThread()) {
215+
return this.plugin.getServer().getPlayer(uuid);
216+
}
217+
// Not on the main thread, we need to wait until the next tick
218+
else {
219+
try {
220+
return this.scheduler.callSyncMethod(plugin, () -> this.plugin.getServer().getPlayer(uuid)).get();
221+
} catch (InterruptedException | ExecutionException e) {
222+
e.printStackTrace();
223+
}
224+
}
225+
// Fallback to null
226+
return null;
227+
}
170228
}

0 commit comments

Comments
 (0)