Skip to content

Commit 5917a72

Browse files
committed
Move to service locator
1 parent aae5519 commit 5917a72

File tree

7 files changed

+69
-56
lines changed

7 files changed

+69
-56
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.papermc.paper;
2+
3+
import net.kyori.adventure.util.Services;
4+
import org.bukkit.damage.DamageEffect;
5+
import org.jetbrains.annotations.ApiStatus;
6+
import org.jspecify.annotations.NullMarked;
7+
8+
/**
9+
* Static bridge to the server internals.
10+
* <p>
11+
* Any and all methods in here are *not* to be called by plugin developers, may change at any time and may generally
12+
* cause issues when called under unexpected circumstances.
13+
*/
14+
@ApiStatus.Internal
15+
@NullMarked
16+
public interface InternalAPIBridge {
17+
18+
/**
19+
* Yields the instance of this API bridge by lazily requesting it from the java service loader API.
20+
*
21+
* @return the instance.
22+
*/
23+
static InternalAPIBridge get() {
24+
class Holder {
25+
public static final InternalAPIBridge INSTANCE = Services.service(InternalAPIBridge.class).orElseThrow();
26+
}
27+
28+
return Holder.INSTANCE;
29+
}
30+
31+
/**
32+
* Creates a damage effect instance for the passed key.
33+
*
34+
* @param mojangKey the string key.
35+
* @return the damage effect.
36+
*/
37+
DamageEffect createDamageEffectInstance(String mojangKey);
38+
}
39+

paper-api/src/main/java/io/papermc/paper/StaticUnsafeValues.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

paper-api/src/main/java/org/bukkit/damage/DamageEffect.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package org.bukkit.damage;
22

33
import com.google.common.base.Preconditions;
4-
import io.papermc.paper.StaticUnsafeValues;
5-
import org.bukkit.Bukkit;
4+
import io.papermc.paper.InternalAPIBridge;
65
import org.bukkit.Sound;
76
import org.jetbrains.annotations.ApiStatus;
87
import org.jetbrains.annotations.NotNull;
@@ -41,7 +40,7 @@ public interface DamageEffect {
4140

4241
@NotNull
4342
private static DamageEffect getDamageEffect(@NotNull String key) {
44-
return Preconditions.checkNotNull(StaticUnsafeValues.getProvider().getDamageEffect(key), "No DamageEffect found for %s. This is a bug.", key);
43+
return Preconditions.checkNotNull(InternalAPIBridge.getProvider().getDamageEffect(key), "No DamageEffect found for %s. This is a bug.", key);
4544
}
4645

4746
/**
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package io.papermc.paper;
22

3-
import com.google.common.base.Preconditions;
43
import org.bukkit.craftbukkit.damage.CraftDamageEffect;
54
import org.bukkit.damage.DamageEffect;
5+
import org.jspecify.annotations.NullMarked;
66

7-
public class PaperUnsafeValuesProvider implements StaticUnsafeValues.UnsafeValuesProvider {
7+
@NullMarked
8+
public class PaperUnsafeValuesProvider implements InternalAPIBridge {
89
public static final PaperUnsafeValuesProvider INSTANCE = new PaperUnsafeValuesProvider();
910

1011
@Override
11-
public DamageEffect getDamageEffect(String key) {
12-
Preconditions.checkArgument(key != null, "key cannot be null");
13-
return CraftDamageEffect.getById(key);
12+
public DamageEffect createDamageEffectInstance(final String mojangKey) {
13+
return CraftDamageEffect.getById(mojangKey);
1414
}
1515
}

paper-server/src/main/java/io/papermc/paper/plugin/entrypoint/LaunchEntryPointHandler.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package io.papermc.paper.plugin.entrypoint;
22

3-
import io.papermc.paper.PaperUnsafeValuesProvider;
4-
import io.papermc.paper.StaticUnsafeValues;
53
import io.papermc.paper.plugin.provider.PluginProvider;
64
import io.papermc.paper.plugin.storage.BootstrapProviderStorage;
75
import io.papermc.paper.plugin.storage.ProviderStorage;
86
import io.papermc.paper.plugin.storage.ServerPluginProviderStorage;
97
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
108
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
11-
import org.jetbrains.annotations.ApiStatus;
12-
139
import java.util.HashMap;
1410
import java.util.Map;
11+
import org.jetbrains.annotations.ApiStatus;
1512

1613
/**
1714
* Used by the server to register/load plugin bootstrappers and plugins.
@@ -34,7 +31,6 @@ public static void enterBootstrappers() {
3431

3532
@Override
3633
public void enter(Entrypoint<?> entrypoint) {
37-
StaticUnsafeValues.setProvider(PaperUnsafeValuesProvider.INSTANCE);
3834
ProviderStorage<?> storage = this.storage.get(entrypoint);
3935
if (storage == null) {
4036
throw new IllegalArgumentException("No storage registered for entrypoint %s.".formatted(entrypoint));

paper-server/src/main/java/io/papermc/paper/registry/data/PaperDamageTypeRegistryEntry.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import io.papermc.paper.registry.PaperRegistryBuilder;
44
import io.papermc.paper.registry.data.util.Conversions;
5-
import net.minecraft.world.damagesource.*;
5+
import net.minecraft.world.damagesource.DamageEffects;
6+
import net.minecraft.world.damagesource.DamageScaling;
7+
import net.minecraft.world.damagesource.DamageType;
8+
import net.minecraft.world.damagesource.DeathMessageType;
69
import org.bukkit.craftbukkit.damage.CraftDamageEffect;
710
import org.bukkit.craftbukkit.damage.CraftDamageType;
811
import org.bukkit.damage.DamageEffect;
@@ -11,24 +14,21 @@
1114
import static io.papermc.paper.registry.data.util.Checks.asConfigured;
1215

1316
public class PaperDamageTypeRegistryEntry implements DamageTypeRegistryEntry {
17+
1418
protected @Nullable String messageId;
1519
protected @Nullable Float exhaustion;
1620
protected @Nullable DamageScaling damageScaling;
17-
protected DamageEffects damageEffects;
18-
protected DeathMessageType deathMessageType;
21+
protected DamageEffects damageEffects = DamageEffects.HURT;
22+
protected DeathMessageType deathMessageType = DeathMessageType.DEFAULT;
1923

2024
protected final Conversions conversions;
2125

2226
public PaperDamageTypeRegistryEntry(
23-
final Conversions conversions,
24-
final @Nullable DamageType internal
27+
final Conversions conversions,
28+
final @Nullable DamageType internal
2529
) {
2630
this.conversions = conversions;
27-
if (internal == null) {
28-
this.damageEffects = DamageEffects.HURT;
29-
this.deathMessageType = DeathMessageType.DEFAULT;
30-
return;
31-
}
31+
if (internal == null) return;
3232

3333
this.messageId = internal.msgId();
3434
this.exhaustion = internal.exhaustion();
@@ -69,43 +69,44 @@ public PaperBuilder(final Conversions conversions, final @Nullable DamageType in
6969
}
7070

7171
@Override
72-
public Builder messageId(String messageId) {
72+
public Builder messageId(final String messageId) {
7373
this.messageId = messageId;
7474
return this;
7575
}
7676

7777
@Override
78-
public Builder exhaustion(float exhaustion) {
78+
public Builder exhaustion(final float exhaustion) {
7979
this.exhaustion = exhaustion;
8080
return this;
8181
}
8282

8383
@Override
84-
public Builder damageScaling(org.bukkit.damage.DamageScaling scaling) {
84+
public Builder damageScaling(final org.bukkit.damage.DamageScaling scaling) {
8585
this.damageScaling = CraftDamageType.damageScalingToNMS(scaling);
8686
return this;
8787
}
8888

8989
@Override
90-
public Builder damageEffect(DamageEffect effect) {
90+
public Builder damageEffect(final DamageEffect effect) {
9191
this.damageEffects = ((CraftDamageEffect) effect).getHandle();
9292
return this;
9393
}
9494

9595
@Override
96-
public Builder deathMessageType(org.bukkit.damage.DeathMessageType deathMessageType) {
96+
public Builder deathMessageType(final org.bukkit.damage.DeathMessageType deathMessageType) {
9797
this.deathMessageType = CraftDamageType.deathMessageTypeToNMS(deathMessageType);
9898
return this;
9999
}
100100

101101
@Override
102102
public DamageType build() {
103103
return new DamageType(
104-
asConfigured(this.messageId, "messsageId"),
105-
asConfigured(this.damageScaling, "scaling"),
106-
asConfigured(this.exhaustion, "exhaustion"),
107-
this.damageEffects,
108-
this.deathMessageType);
104+
asConfigured(this.messageId, "messsageId"),
105+
asConfigured(this.damageScaling, "scaling"),
106+
asConfigured(this.exhaustion, "exhaustion"),
107+
this.damageEffects,
108+
this.deathMessageType
109+
);
109110
}
110111
}
111112
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
io.papermc.paper.PaperUnsafeValuesProvider

0 commit comments

Comments
 (0)