Skip to content

Commit

Permalink
GH-884 Add support for 1.21.3, refactor injector. (#884)
Browse files Browse the repository at this point in the history
* Support new sound serdes after bukkit api update. Drop java 17 support (WiP)

* Fix some issues

* Create universal old enum composer

* Fix local publisher

* @compatibility WiP

* Use CompatibilityService

* Fix

* Improve dependency injector management

* Update multification.

* Prevent redundant delay marking when looping through items

* Do not wrap Supplier in another Supplier

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Fixes

* Fix potion effect

* Fix SHORT_GRASS

* Add Material.LEGACY_GRASS,

* Rename HOW_USE_DI.md to HOW_USE_TO_DI.md

---------

Co-authored-by: Rollczi <ndejlich5@gmail.com>
Co-authored-by: CitralFlo <wojtas.michal90@gmail.com>
Co-authored-by: Jakub Kędziora <77227023+Jakubk15@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
5 people authored Jan 11, 2025
1 parent 663b19c commit 79f6ffc
Showing 67 changed files with 487 additions and 261 deletions.
8 changes: 4 additions & 4 deletions .github/HOW_USE_DI.md → .github/HOW_USE_TO_DI.md
Original file line number Diff line number Diff line change
@@ -109,7 +109,7 @@ public class UserCommand {
#### 7. Subscriber
```java
@Controller // <- marks a class that will be registered as an event subscriber.
public class UserSubscriber implements Subscriber {
public class UserSubscriber {

private final UserService exampleService;

@@ -126,12 +126,12 @@ public class UserSubscriber implements Subscriber {
}
```

#### 8. BeanSetup
BeanSetup is a class that allows you to register beans in the bean container.
#### 8. Setup
Setup is an annotation that allows you to register beans in the bean container.
It is used to register dependencies that are cannot be registered in the bean container using annotations. e.g. MiniMessage, AdventureProvider, HikariDataSource, etc.
```java

@BeanSetup // <- marks a class that will be registered as a bean holder.
@Setup // <- marks a class that will be registered as a bean holder.
public class UserBeanSetup {

@Bean // <- marks a method that will be registered as a bean. (dependency)
5 changes: 2 additions & 3 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
object Versions {

const val SPIGOT_API = "1.19.4-R0.1-SNAPSHOT"
const val PAPER_API = "1.19.4-R0.1-SNAPSHOT"
const val SPIGOT_API = "1.21.3-R0.1-SNAPSHOT"
const val PAPER_API = "1.21.3-R0.1-SNAPSHOT"

const val ETERNALCODE_COMMONS = "1.1.5"
const val MULTIFICATION = "1.2.1"
@@ -36,7 +36,6 @@ object Versions {
const val TRIUMPH_GUI = "3.1.10"

const val BSTATS = "3.1.0"
const val PIXEL_WIDTH = "1.1.0"

const val CAFFEINE = "3.1.8"

2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/eternalcode-java.gradle.kts
Original file line number Diff line number Diff line change
@@ -35,5 +35,5 @@ java {
tasks.withType<JavaCompile>() {
options.compilerArgs = listOf("-Xlint:deprecation", "-parameters")
options.encoding = "UTF-8"
options.release = 17
options.release = 21
}
Original file line number Diff line number Diff line change
@@ -100,7 +100,7 @@ void onHomeTeleport(HomeTeleportEvent event) {

player.sendMessage("Teleporting to home...");

if (player.hasPotionEffect(PotionEffectType.SLOW)) {
if (player.hasPotionEffect(PotionEffectType.SLOWNESS)) {
player.sendMessage("You are slowed down!");
player.teleport(player.getWorld().getSpawnLocation());
}
Original file line number Diff line number Diff line change
@@ -13,11 +13,6 @@ public class Delay<T> {

private final Supplier<Duration> delaySettings;

@Deprecated
public Delay(DelaySettings delaySettings) {
this((Supplier<Duration>) () -> delaySettings.delay());
}

public Delay(Supplier<Duration> delayProvider) {
this.delaySettings = delayProvider;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
package com.eternalcode.core;

import com.eternalcode.core.configuration.ReloadableConfig;
import com.eternalcode.core.compatibility.CompatibilityService;
import com.eternalcode.core.injector.DependencyInjector;
import com.eternalcode.core.injector.annotations.component.Component;
import com.eternalcode.core.injector.annotations.component.ConfigurationFile;
import com.eternalcode.core.injector.annotations.component.Controller;
import com.eternalcode.core.injector.annotations.component.Repository;
import com.eternalcode.core.injector.annotations.component.Service;
import com.eternalcode.core.injector.annotations.component.Setup;
import com.eternalcode.core.injector.annotations.component.Task;
import com.eternalcode.core.injector.annotations.lite.LiteArgument;
import com.eternalcode.core.injector.annotations.lite.LiteCommandEditor;
import com.eternalcode.core.injector.annotations.lite.LiteContextual;
import com.eternalcode.core.injector.annotations.lite.LiteHandler;
import com.eternalcode.core.injector.bean.BeanCandidate;
import com.eternalcode.core.injector.bean.BeanFactory;
import com.eternalcode.core.injector.bean.BeanHolder;
import com.eternalcode.core.injector.bean.LazyFieldBeanCandidate;
import com.eternalcode.core.injector.bean.BeanCandidatePriorityProvider;
import com.eternalcode.core.injector.bean.processor.BeanProcessor;
import com.eternalcode.core.injector.bean.processor.BeanProcessorFactory;
import com.eternalcode.core.injector.scan.DependencyScanner;
import com.eternalcode.core.injector.scan.DependencyScannerFactory;
import com.eternalcode.core.publish.Publisher;
import com.eternalcode.core.publish.event.EternalInitializeEvent;
import com.eternalcode.core.publish.event.EternalShutdownEvent;
import net.dzikoysk.cdn.entity.Contextual;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.command.RootCommand;
import org.bukkit.Server;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;

import java.io.File;
import java.lang.reflect.Field;
import java.util.List;
import java.util.logging.Logger;

class EternalCore {
@@ -32,6 +40,7 @@ class EternalCore {
public EternalCore(Plugin plugin) {
this.eternalCoreEnvironment = new EternalCoreEnvironment(plugin.getLogger());

CompatibilityService compatibilityService = new CompatibilityService();
BeanProcessor beanProcessor = BeanProcessorFactory.defaultProcessors(plugin);
BeanFactory beanFactory = new BeanFactory(beanProcessor)
.withCandidateSelf()
@@ -40,19 +49,35 @@ public EternalCore(Plugin plugin) {
.addCandidate(Logger.class, () -> plugin.getLogger())
.addCandidate(PluginDescriptionFile.class, () -> plugin.getDescription())
.addCandidate(File.class, () -> plugin.getDataFolder())
.addCandidate(PluginManager.class, () -> plugin.getServer().getPluginManager());

DependencyInjector dependencyInjector = new DependencyInjector(beanFactory);
DependencyScanner scanner = DependencyScannerFactory.createDefault(dependencyInjector);

beanFactory.addCandidate(DependencyInjector.class, () -> dependencyInjector);
.addCandidate(PluginManager.class, () -> plugin.getServer().getPluginManager())
.priorityProvider(new BeanCandidatePriorityProvider());

DependencyInjector injector = new DependencyInjector(beanFactory);
DependencyScanner scanner = new DependencyScanner(injector)
.includeType(type -> compatibilityService.isCompatible(type))
.includeAnnotations(
Component.class,
Service.class,
Repository.class,
Task.class,
Controller.class,
ConfigurationFile.class,
Setup.class,

Command.class,
RootCommand.class,
LiteArgument.class,
LiteHandler.class,
LiteContextual.class,
LiteCommandEditor.class
);

beanFactory.addCandidate(DependencyInjector.class, () -> injector);

for (BeanCandidate beanCandidate : scanner.scan(EternalCore.class.getPackage())) {
beanFactory.addCandidate(beanCandidate);
}

this.loadConfigContextual(beanFactory);

beanFactory.initializeCandidates();

this.publisher = beanFactory.getDependency(Publisher.class);
@@ -67,22 +92,4 @@ public void disable() {
EternalCoreApiProvider.deinitialize();
}

private void loadConfigContextual(BeanFactory beanFactory) {
List<BeanHolder<ReloadableConfig>> beans = beanFactory
.initializeCandidates(ReloadableConfig.class)
.getBeans(ReloadableConfig.class);

for (BeanHolder<ReloadableConfig> bean : beans) {
ReloadableConfig config = bean.get();

for (Field field : config.getClass().getDeclaredFields()) {
if (!field.getType().isAnnotationPresent(Contextual.class)) {
continue;
}

beanFactory.addCandidate(new LazyFieldBeanCandidate(config, field));
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.eternalcode.core.bridge;

import com.eternalcode.core.injector.annotations.Bean;
import com.eternalcode.core.injector.annotations.component.BeanSetup;
import com.eternalcode.core.injector.annotations.component.Setup;
import com.eternalcode.core.placeholder.PlaceholderRegistry;
import java.util.logging.Logger;
import org.bukkit.Server;
import org.bukkit.plugin.PluginDescriptionFile;

@BeanSetup
@Setup
class BridgeManagerInitializer {

@Bean
Original file line number Diff line number Diff line change
@@ -4,13 +4,13 @@
import com.eternalcode.commons.adventure.AdventureLegacyColorPreProcessor;
import com.eternalcode.commons.adventure.AdventureUrlPostProcessor;
import com.eternalcode.core.injector.annotations.Bean;
import com.eternalcode.core.injector.annotations.component.BeanSetup;
import com.eternalcode.core.injector.annotations.component.Setup;
import net.kyori.adventure.platform.AudienceProvider;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.plugin.Plugin;

@BeanSetup
@Setup
class AdventureSetup {

@Bean
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.eternalcode.core.bridge.litecommand;

import com.eternalcode.core.injector.annotations.Bean;
import com.eternalcode.core.injector.annotations.component.BeanSetup;
import com.eternalcode.core.injector.annotations.component.Setup;
import com.eternalcode.core.injector.bean.BeanFactory;
import com.eternalcode.core.notice.NoticeService;
import com.eternalcode.core.publish.Subscribe;
import com.eternalcode.core.publish.Subscriber;
import com.eternalcode.core.publish.event.EternalInitializeEvent;
import com.eternalcode.core.publish.event.EternalShutdownEvent;
import dev.rollczi.litecommands.LiteCommands;
@@ -20,8 +19,8 @@
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;

@BeanSetup
class LiteCommandsSetup implements Subscriber {
@Setup
class LiteCommandsSetup {

@Bean
public LiteCommandsBuilder<CommandSender, ?, ?> liteCommandsBuilder(
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

import com.eternalcode.core.configuration.ReloadableConfig;
import com.eternalcode.core.feature.essentials.gamemode.GameModeArgumentSettings;
import com.eternalcode.core.injector.annotations.Bean;
import com.eternalcode.core.injector.annotations.component.ConfigurationFile;
import net.dzikoysk.cdn.entity.Contextual;
import net.dzikoysk.cdn.entity.Description;
@@ -19,6 +20,7 @@
@ConfigurationFile
public class CommandConfiguration implements ReloadableConfig {

@Bean
public Argument argument = new Argument();

@Contextual
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.eternalcode.core.bridge.metrics;

import com.eternalcode.core.injector.annotations.component.Controller;
import com.eternalcode.core.publish.Subscriber;
import com.eternalcode.core.publish.event.EternalInitializeEvent;
import com.eternalcode.core.publish.Subscribe;
import org.bstats.bukkit.Metrics;
import org.bukkit.plugin.java.JavaPlugin;

@Controller
class BStatsMetricsSetup implements Subscriber {
class BStatsMetricsSetup {

@Subscribe(EternalInitializeEvent.class)
public void onInitialize(JavaPlugin javaPlugin) {
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.eternalcode.core.bridge.skullapi;

import com.eternalcode.core.publish.Subscriber;
import com.eternalcode.core.publish.event.EternalShutdownEvent;
import com.eternalcode.core.injector.annotations.Bean;
import com.eternalcode.core.injector.annotations.component.BeanSetup;
import com.eternalcode.core.injector.annotations.component.Setup;
import com.eternalcode.core.publish.Subscribe;
import dev.rollczi.liteskullapi.LiteSkullFactory;
import dev.rollczi.liteskullapi.SkullAPI;
import org.bukkit.plugin.Plugin;

@BeanSetup
class SkullAPISetup implements Subscriber {
@Setup
class SkullAPISetup {

@Bean
public SkullAPI skullAPI(Plugin plugin) {
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.eternalcode.core.compatibility;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Compatibility {

Version from() default @Version(minor = Integer.MIN_VALUE, patch = Integer.MIN_VALUE);

Version to() default @Version(minor = Integer.MAX_VALUE, patch = Integer.MAX_VALUE);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.eternalcode.core.compatibility;

import io.papermc.lib.PaperLib;

public class CompatibilityService {

public boolean isCompatible(Class<?> type) {
Compatibility compatibility = type.getAnnotation(Compatibility.class);
if (compatibility == null) {
return true;
}

Version from = compatibility.from();
Version to = compatibility.to();

int minor = PaperLib.getMinecraftVersion();
int patch = PaperLib.getMinecraftPatchVersion();

return isCompatibleFrom(from, minor, patch) && isCompatibleTo(to, minor, patch);
}

private boolean isCompatibleTo(Version to, int minor, int patch) {
return minor < to.minor() || minor == to.minor() && patch <= to.patch();
}

private boolean isCompatibleFrom(Version from, int minor, int patch) {
return minor > from.minor() || minor == from.minor() && patch >= from.patch();
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.eternalcode.core.compatibility;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Version {

int minor();
int patch();

}
Loading

0 comments on commit 79f6ffc

Please sign in to comment.