-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Improve notify service (Close #9)
- Loading branch information
Showing
19 changed files
with
311 additions
and
38 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
10 changes: 10 additions & 0 deletions
10
bukkit/src/main/java/com/blank038/servermarket/internal/cache/other/NotifyCache.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,10 @@ | ||
package com.blank038.servermarket.internal.cache.other; | ||
|
||
/** | ||
* @author Blank038 | ||
*/ | ||
public class NotifyCache { | ||
public int index; | ||
public long time; | ||
public String message; | ||
} |
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
19 changes: 19 additions & 0 deletions
19
bukkit/src/main/java/com/blank038/servermarket/internal/service/notify/INotifyService.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,19 @@ | ||
package com.blank038.servermarket.internal.service.notify; | ||
|
||
import com.blank038.servermarket.internal.cache.other.NotifyCache; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.configuration.ConfigurationSection; | ||
|
||
/** | ||
* @author Blank038 | ||
*/ | ||
public interface INotifyService { | ||
|
||
void register(ConfigurationSection config); | ||
|
||
void push(NotifyCache notifyCache); | ||
|
||
default void broadcast(String message) { | ||
Bukkit.getOnlinePlayers().forEach((player) -> player.sendMessage(message)); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
bukkit/src/main/java/com/blank038/servermarket/internal/service/notify/NotifyCenter.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,59 @@ | ||
package com.blank038.servermarket.internal.service.notify; | ||
|
||
import com.blank038.servermarket.internal.cache.other.NotifyCache; | ||
import com.blank038.servermarket.internal.service.notify.impl.mysql.MySQLNotifyServiceImpl; | ||
import com.blank038.servermarket.internal.service.notify.impl.self.SelfNotifyServiceImpl; | ||
import org.bukkit.configuration.ConfigurationSection; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Blank038 | ||
*/ | ||
public class NotifyCenter { | ||
private static final Map<String, INotifyService> NOTIFY_SERVICES = new HashMap<>(); | ||
private static final Map<String, Class<? extends INotifyService>> NOTIFY_CLASSES = new HashMap<>(); | ||
|
||
static { | ||
NotifyCenter.register("self", SelfNotifyServiceImpl.class); | ||
NotifyCenter.register("mysql", MySQLNotifyServiceImpl.class); | ||
} | ||
|
||
public static boolean isRegister(String source) { | ||
return NOTIFY_CLASSES.containsKey(source); | ||
} | ||
|
||
public static boolean isCreated(String source) { | ||
return NOTIFY_SERVICES.containsKey(source); | ||
} | ||
|
||
public static void register(String source, Class<? extends INotifyService> classZ) { | ||
NOTIFY_CLASSES.putIfAbsent(source, classZ); | ||
} | ||
|
||
public static INotifyService create(String source, INotifyService service, ConfigurationSection config) { | ||
if (NOTIFY_SERVICES.containsKey(source)) { | ||
return null; | ||
} | ||
service.register(config); | ||
NOTIFY_SERVICES.put(source, service); | ||
return service; | ||
} | ||
|
||
public static Class<? extends INotifyService> findNotifyClass(String source) { | ||
return NOTIFY_CLASSES.getOrDefault(source, null); | ||
} | ||
|
||
public static INotifyService getService(String service) { | ||
return NOTIFY_SERVICES.getOrDefault(service, null); | ||
} | ||
|
||
public static Map<String, INotifyService> getServices() { | ||
return NOTIFY_SERVICES; | ||
} | ||
|
||
public static void pushNotify(NotifyCache cache) { | ||
NotifyCenter.getServices().forEach((k, v) -> v.push(cache)); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...in/java/com/blank038/servermarket/internal/service/notify/impl/AbstractNotifyService.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,9 @@ | ||
package com.blank038.servermarket.internal.service.notify.impl; | ||
|
||
import com.blank038.servermarket.internal.service.notify.INotifyService; | ||
|
||
/** | ||
* @author Blank038 | ||
*/ | ||
public abstract class AbstractNotifyService implements INotifyService { | ||
} |
39 changes: 39 additions & 0 deletions
39
...in/java/com/blank038/servermarket/internal/service/notify/impl/IncreaseNotifyService.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,39 @@ | ||
package com.blank038.servermarket.internal.service.notify.impl; | ||
|
||
import com.blank038.servermarket.api.ServerMarketApi; | ||
import com.blank038.servermarket.api.platform.wrapper.ITaskWrapper; | ||
import com.blank038.servermarket.internal.plugin.ServerMarket; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Blank038 | ||
*/ | ||
public abstract class IncreaseNotifyService extends AbstractNotifyService { | ||
private final List<Integer> pushedIndexes = new ArrayList<>(); | ||
private ITaskWrapper wrapper; | ||
|
||
public boolean hasIndex(int index) { | ||
return this.pushedIndexes.contains(index); | ||
} | ||
|
||
public void addIndex(int index) { | ||
this.pushedIndexes.add(index); | ||
} | ||
|
||
public void update() { | ||
} | ||
|
||
public void runTask(int seconds) { | ||
if (wrapper != null) { | ||
wrapper.cancel(); | ||
} | ||
wrapper = ServerMarketApi.getPlatformApi().runTaskTimerAsynchronously( | ||
ServerMarket.getInstance(), | ||
() -> this.update(), | ||
seconds, | ||
seconds | ||
); | ||
} | ||
} |
Oops, something went wrong.