generated from xpdustry/template-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
430 additions
and
27 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
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
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
44 changes: 44 additions & 0 deletions
44
...or-common/src/main/java/com/xpdustry/distributor/core/permission/EmptyPermissionTree.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,44 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* 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 com.xpdustry.distributor.core.permission; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
final class EmptyPermissionTree implements PermissionTree { | ||
|
||
static EmptyPermissionTree INSTANCE = new EmptyPermissionTree(); | ||
|
||
private EmptyPermissionTree() {} | ||
|
||
@Override | ||
public TriState getPermission(final String permission) { | ||
return TriState.UNDEFINED; | ||
} | ||
|
||
@Override | ||
public void setPermission(final String permission, final TriState state) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Map<String, Boolean> getPermissions() { | ||
return Collections.emptyMap(); | ||
} | ||
} |
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
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
61 changes: 61 additions & 0 deletions
61
distributor-common/src/main/java/com/xpdustry/distributor/core/service/ServiceManager.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,61 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* 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 com.xpdustry.distributor.core.service; | ||
|
||
import com.xpdustry.distributor.core.internal.GeneratedDataClass; | ||
import com.xpdustry.distributor.core.plugin.MindustryPlugin; | ||
import com.xpdustry.distributor.core.util.Priority; | ||
import java.util.List; | ||
import java.util.function.Supplier; | ||
import org.immutables.value.Value; | ||
|
||
public interface ServiceManager { | ||
|
||
static ServiceManager simple() { | ||
return new SimpleServiceManager(); | ||
} | ||
|
||
<T> void register( | ||
final MindustryPlugin plugin, final Class<T> clazz, final Priority priority, final Supplier<T> factory); | ||
|
||
<T> T provide(final Class<T> clazz); | ||
|
||
<T> List<Provider<T>> getProviders(final Class<T> clazz); | ||
|
||
@GeneratedDataClass | ||
@Value.Immutable | ||
sealed interface Provider<T> permits ImmutableProvider { | ||
|
||
static <T> Provider<T> of( | ||
final MindustryPlugin plugin, | ||
final Class<T> clazz, | ||
final Priority priority, | ||
final Supplier<T> factory) { | ||
return ImmutableProvider.of(plugin, clazz, priority, factory); | ||
} | ||
|
||
MindustryPlugin getPlugin(); | ||
|
||
Class<T> getClazz(); | ||
|
||
Priority getPriority(); | ||
|
||
Supplier<T> getFactory(); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...utor-common/src/main/java/com/xpdustry/distributor/core/service/SimpleServiceManager.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,68 @@ | ||
/* | ||
* Distributor, a feature-rich framework for Mindustry plugins. | ||
* | ||
* Copyright (C) 2024 Xpdustry | ||
* | ||
* 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 com.xpdustry.distributor.core.service; | ||
|
||
import com.xpdustry.distributor.core.plugin.MindustryPlugin; | ||
import com.xpdustry.distributor.core.util.Priority; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.PriorityQueue; | ||
import java.util.Queue; | ||
import java.util.function.Supplier; | ||
|
||
final class SimpleServiceManager implements ServiceManager { | ||
|
||
private final Map<Class<?>, Queue<Provider<?>>> services = new HashMap<>(); | ||
private final Object lock = new Object(); | ||
|
||
@Override | ||
public <T> void register( | ||
final MindustryPlugin plugin, final Class<T> clazz, final Priority priority, final Supplier<T> instance) { | ||
synchronized (this.lock) { | ||
this.services | ||
.computeIfAbsent( | ||
clazz, k -> new PriorityQueue<Provider<?>>(Comparator.comparing(Provider::getPriority))) | ||
.add(Provider.of(plugin, clazz, priority, instance)); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> T provide(final Class<T> clazz) { | ||
final var providers = this.getProviders(clazz); | ||
if (providers.isEmpty()) { | ||
throw new IllegalStateException("Expected provider for " + clazz.getCanonicalName() + ", got nothing."); | ||
} | ||
return providers.get(0).getFactory().get(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public <T> List<Provider<T>> getProviders(final Class<T> clazz) { | ||
synchronized (this.lock) { | ||
final Object implementation = this.services.get(clazz); | ||
return implementation != null | ||
? List.copyOf((Collection<? extends Provider<T>>) implementation) | ||
: Collections.emptyList(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.