-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-55 Support for multiple paper versions with dependency injector. (R…
…esolve #54)
- Loading branch information
Showing
5 changed files
with
178 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
paper-support/src/com/eternalcode/formatter/paper/adventure/PaperSignedMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.eternalcode.formatter.paper.adventure; | ||
|
||
import net.kyori.adventure.chat.SignedMessage; | ||
import net.kyori.adventure.identity.Identity; | ||
import net.kyori.adventure.text.Component; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.security.SecureRandom; | ||
import java.time.Instant; | ||
|
||
public class PaperSignedMessage implements SignedMessage { | ||
|
||
private static final SecureRandom RANDOM = new SecureRandom(); | ||
|
||
private final Instant instant; | ||
private final long salt; | ||
private final Component unsignedContent; | ||
private final String message; | ||
private final Identity identity; | ||
|
||
public PaperSignedMessage(Component unsignedContent, Identity identity) { | ||
this.identity = identity; | ||
this.instant = Instant.now(); | ||
this.salt = RANDOM.nextLong(); | ||
this.unsignedContent = unsignedContent; | ||
this.message = "-"; | ||
} | ||
|
||
@Override | ||
public @NotNull Instant timestamp() { | ||
return this.instant; | ||
} | ||
|
||
@Override | ||
public long salt() { | ||
return this.salt; | ||
} | ||
|
||
@Override | ||
public Signature signature() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public @Nullable Component unsignedContent() { | ||
return this.unsignedContent; | ||
} | ||
|
||
@Override | ||
public @NotNull String message() { | ||
return this.message; | ||
} | ||
|
||
@Override | ||
public @NotNull Identity identity() { | ||
return identity; | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
paper-support/src/com/eternalcode/formatter/paper/injector/DependencyContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.eternalcode.formatter.paper.injector; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
class DependencyContainer<T> { | ||
|
||
private final T value; | ||
private final List<T> extraValues = new ArrayList<>(); | ||
|
||
DependencyContainer(T value) { | ||
this.value = value; | ||
} | ||
|
||
public void addExtraValue(T value) { | ||
this.extraValues.add(value); | ||
} | ||
|
||
public T getExtraOrNormal(int index) { | ||
if (index == 0) { | ||
return this.value; | ||
} | ||
|
||
if (index >= this.extraValues.size()) { | ||
if (this.extraValues.isEmpty()) { | ||
return this.value; | ||
} | ||
|
||
return this.extraValues.get(this.extraValues.size() - 1); | ||
} | ||
|
||
return this.extraValues.get(index); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
paper-support/src/com/eternalcode/formatter/paper/injector/DependencyInjector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.eternalcode.formatter.paper.injector; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class DependencyInjector { | ||
|
||
private final Map<Class<?>, DependencyContainer<?>> dependencies = new HashMap<>(); | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T> DependencyInjector register(Class<T> clazz, T instance) { | ||
DependencyContainer<T> container = (DependencyContainer<T>) this.dependencies.get(clazz); | ||
|
||
if (container == null) { | ||
this.dependencies.put(clazz, new DependencyContainer<>(instance)); | ||
return this; | ||
} | ||
|
||
container.addExtraValue(instance); | ||
return this; | ||
} | ||
|
||
public <T> T newInstance(Class<T> clazz) { | ||
for (Constructor<?> constructor : clazz.getConstructors()) { | ||
List<Object> parameters = new ArrayList<>(); | ||
Map<Class<?>, Integer> parameterCount = new HashMap<>(); | ||
|
||
for (Class<?> parameterType : constructor.getParameterTypes()) { | ||
parameters.add(this.getDependency(parameterType, parameterCount.getOrDefault(parameterType, 0))); | ||
parameterCount.merge(parameterType, 1, Integer::sum); | ||
} | ||
|
||
try { | ||
return clazz.cast(constructor.newInstance(parameters.toArray(new Object[0]))); | ||
} catch (ReflectiveOperationException e) { | ||
throw new IllegalStateException("Failed to instantiate " + clazz, e); | ||
} | ||
} | ||
|
||
throw new IllegalStateException("No constructor found for " + clazz); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private <T> T getDependency(Class<T> clazz, int index) { | ||
DependencyContainer<?> container = this.dependencies.get(clazz); | ||
|
||
if (container != null) { | ||
return (T) container.getExtraOrNormal(index); | ||
} | ||
|
||
for (Map.Entry<Class<?>, DependencyContainer<?>> entry : this.dependencies.entrySet()) { | ||
Class<?> key = entry.getKey(); | ||
|
||
if (clazz.isAssignableFrom(key)) { | ||
return (T) entry.getValue().getExtraOrNormal(index); | ||
} | ||
} | ||
|
||
throw new IllegalStateException("No dependency found for " + clazz); | ||
} | ||
|
||
} |