Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unified-core module and some fixes / minor changes #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
<artifactId>unified-parent</artifactId>
<version>0.1-SNAPSHOT</version>
<modules>
<module>unified-core</module>
<module>unified-energy</module>
<module>unified</module>
<module>unified-plugin</module>
</modules>

<packaging>pom</packaging>
Expand Down Expand Up @@ -108,5 +109,5 @@
</plugin>
</plugins>
</build>

</project>
14 changes: 14 additions & 0 deletions unified-core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>unified-parent</artifactId>
<groupId>io.github.unifiedtechpower.unified</groupId>
<version>0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>unified-core</artifactId>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.unifiedtechpower.unified.core;

import org.bukkit.Chunk;
import org.bukkit.Location;
import org.jetbrains.annotations.NotNull;

public interface UnifiedBlockListener {

/**
* Called when a custom block of a plugin with "unified" integration was placed.
* @param location The {@link Location} of the block
*/
void handleBlockPlaced(@NotNull Location location);

/**
* Called when a custom block of a plugin with "unified" integration was destroyed.
* @param location The {@link Location} of the block
*/
void handleBlockDestroyed(@NotNull Location location);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.github.unifiedtechpower.unified.core;

import org.bukkit.Location;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class UnifiedBlockManager {

private static final UnifiedBlockManager INSTANCE = new UnifiedBlockManager();

private final List<UnifiedBlockListener> listeners = new ArrayList<>();

private UnifiedBlockManager() {
}

/**
* Retrieves the {@link UnifiedBlockManager} instance.
* @return The {@link UnifiedBlockManager} instance.
*/
public static UnifiedBlockManager getInstance() {
return INSTANCE;
}

/**
* Registers a new {@link UnifiedBlockListener}
* @param listener The {@link UnifiedBlockListener} to register
*/
public void registerBlockListener(UnifiedBlockListener listener) {
listeners.add(listener);
}

/**
* Invokes {@link UnifiedBlockListener#handleBlockPlaced(Location)} for all registered {@link UnifiedBlockListener listeners}.
* @param location The {@link Location} of the placed block
* @param ignored A {@link UnifiedBlockListener} that should not be called. Can be null.
*/
public void callBlockPlaced(@NotNull Location location, @Nullable UnifiedBlockListener ignored) {
for (var listener : listeners) {
if (listener == ignored)
continue;

listener.handleBlockPlaced(location);
}
}

/**
* Invokes {@link UnifiedBlockListener#handleBlockDestroyed(Location)} for all registered {@link UnifiedBlockListener listeners}.
* @param location The {@link Location} of the destroyed block
* @param ignored A {@link UnifiedBlockListener} that should not be called. Can be null.
*/
public void callBlockDestroyed(@NotNull Location location, @Nullable UnifiedBlockListener ignored) {
for (var listener : listeners) {
if (listener == ignored)
continue;

listener.handleBlockDestroyed(location);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
Expand All @@ -30,6 +31,6 @@ public interface EnergyNetworkManager {
* @return a {@link CompletableFuture} that completes with the {@link EnergyStorage}s in the given chunk
*/
@NotNull
CompletableFuture<@NotNull List<@NotNull EnergyStorage>> getEnergyStoragesIn(@NotNull Chunk chunk);
CompletableFuture<@NotNull Map<@NotNull Location, @NotNull EnergyStorage>> getEnergyStoragesIn(@NotNull Chunk chunk);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.*;
import java.util.concurrent.CompletableFuture;

/**
Expand Down Expand Up @@ -58,7 +55,7 @@ public boolean removeManager(@NotNull EnergyNetworkManager manager) {
for (var manager : managers)
futures.add(manager.getEnergyStorageAt(location));

for (var future : futures) {
for (var future : new HashSet<>(futures)) {
future.thenAccept(storage -> {
if (mainFuture.isDone())
return;
Expand All @@ -83,18 +80,18 @@ public boolean removeManager(@NotNull EnergyNetworkManager manager) {
* @return a {@link CompletableFuture} that completes with a list of {@link EnergyStorage}s in the given chunk
*/
@NotNull
public CompletableFuture<@NotNull List<@NotNull EnergyStorage>> getEnergyStoragesIn(@NotNull Chunk chunk) {
var futures = Collections.synchronizedSet(new HashSet<CompletableFuture<@NotNull List<@NotNull EnergyStorage>>>());
var energyStorages = Collections.synchronizedList(new ArrayList<@NotNull EnergyStorage>());
var mainFuture = new CompletableFuture<@NotNull List<@NotNull EnergyStorage>>();
public CompletableFuture<@NotNull Map<@NotNull Location, @NotNull EnergyStorage>> getEnergyStoragesIn(@NotNull Chunk chunk) {
var futures = Collections.synchronizedSet(new HashSet<CompletableFuture<@NotNull Map<@NotNull Location, @NotNull EnergyStorage>>>());
var energyStorages = Collections.synchronizedMap(new HashMap<@NotNull Location, @NotNull EnergyStorage>());
var mainFuture = new CompletableFuture<@NotNull Map<@NotNull Location, @NotNull EnergyStorage>>();

for (var manager : managers)
futures.add(manager.getEnergyStoragesIn(chunk));

for (var future : futures) {
for (var future : new HashSet<>(futures)) {
future.thenAccept(storages -> {
if (!storages.isEmpty())
energyStorages.addAll(storages);
energyStorages.putAll(storages);

futures.remove(future);
if (futures.isEmpty())
Expand Down
12 changes: 7 additions & 5 deletions unified/pom.xml → unified-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>unified</artifactId>
<artifactId>unified-plugin</artifactId>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<dir>${project.build.directory}/out</dir>
</properties>

<dependencies>
<dependency>
<groupId>io.github.unifiedtechpower.unified</groupId>
<artifactId>unified-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.github.unifiedtechpower.unified</groupId>
<artifactId>unified-energy</artifactId>
Expand Down Expand Up @@ -53,7 +55,7 @@
</filters>
<artifactSet>
<includes>
<include>io.github.unifiedtechpower.unified:unified-energy</include>
<include>io.github.unifiedtechpower.unified:*</include>
</includes>
</artifactSet>
</configuration>
Expand Down