Fixes for Java/Scala compatibility bugs

This commit is contained in:
Norbi Peti 2023-04-24 22:54:33 +02:00
parent 5e1f378ec7
commit 260a67506a
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
13 changed files with 32 additions and 19 deletions

View file

@ -247,7 +247,7 @@
<!-- github server corresponds to entry in ~/.m2/settings.xml --> <!-- github server corresponds to entry in ~/.m2/settings.xml -->
<github.global.server>github</github.global.server> <github.global.server>github</github.global.server>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<noprefix.version>1.0.1</noprefix.version> <noprefix.version>2.0.0</noprefix.version>
<kotlin.version>1.8.20</kotlin.version> <kotlin.version>1.8.20</kotlin.version>
</properties> </properties>
<scm> <scm>

View file

@ -26,7 +26,7 @@ import kotlin.properties.Delegates
] ]
) )
class ScheduledRestartCommand : ICommand2MC() { class ScheduledRestartCommand : ICommand2MC() {
private var restartCounter = 0 public var restartCounter = 0
private lateinit var restartTask: BukkitTask private lateinit var restartTask: BukkitTask
@Volatile @Volatile

View file

@ -9,6 +9,7 @@ import org.bukkit.event.Event
import java.util.function.Supplier import java.util.function.Supplier
object ChromaUtils { object ChromaUtils {
@JvmStatic
fun getDisplayName(sender: CommandSender): String { fun getDisplayName(sender: CommandSender): String {
return when (sender) { return when (sender) {
is IHaveFancyName -> sender.fancyName is IHaveFancyName -> sender.fancyName
@ -17,6 +18,7 @@ object ChromaUtils {
} }
} }
@JvmStatic
fun getFullDisplayName(sender: CommandSender): String { fun getFullDisplayName(sender: CommandSender): String {
return when (sender) { return when (sender) {
is IHaveFancyName -> sender.fancyFullName is IHaveFancyName -> sender.fancyFullName
@ -24,6 +26,7 @@ object ChromaUtils {
} }
} }
@JvmStatic
fun convertNumber(number: Number, targetcl: Class<out Number>): Number { fun convertNumber(number: Number, targetcl: Class<out Number>): Number {
return when { return when {
targetcl == Long::class.javaPrimitiveType || Long::class.java.isAssignableFrom(targetcl) -> number.toLong() targetcl == Long::class.javaPrimitiveType || Long::class.java.isAssignableFrom(targetcl) -> number.toLong()

View file

@ -14,40 +14,41 @@ import org.bukkit.event.HandlerList
*/ */
class TBMCChatEvent( class TBMCChatEvent(
channel: Channel, channel: Channel,
private val cm: ChatMessage, public val chatMessage: ChatMessage,
rtr: RecipientTestResult rtr: RecipientTestResult
) : TBMCChatEventBase(channel, cm.message, rtr.score, rtr.groupID!!) { ) : TBMCChatEventBase(channel, chatMessage.message, rtr.score, rtr.groupID!!) {
private val isIgnoreSenderPermissions: Boolean get() = cm.permCheck !== cm.sender private val isIgnoreSenderPermissions: Boolean get() = chatMessage.permCheck !== chatMessage.sender
/** /**
* This will allow the sender of the message if [.isIgnoreSenderPermissions] is true. * This will allow the sender of the message if [.isIgnoreSenderPermissions] is true.
*/ */
override fun shouldSendTo(sender: CommandSender): Boolean { override fun shouldSendTo(sender: CommandSender): Boolean {
return if (isIgnoreSenderPermissions && sender == cm.sender) true else super.shouldSendTo(sender) //Allow sending the message no matter what return if (isIgnoreSenderPermissions && sender == chatMessage.sender) true else super.shouldSendTo(sender) //Allow sending the message no matter what
} }
/** /**
* This will allow the sender of the message if [.isIgnoreSenderPermissions] is true. * This will allow the sender of the message if [.isIgnoreSenderPermissions] is true.
*/ */
override fun getMCScore(sender: CommandSender): Int { override fun getMCScore(sender: CommandSender): Int {
return if (isIgnoreSenderPermissions && sender == cm.sender) score else super.getMCScore(sender) //Send in the correct group no matter what return if (isIgnoreSenderPermissions && sender == chatMessage.sender) score else super.getMCScore(sender) //Send in the correct group no matter what
} }
/** /**
* This will allow the sender of the message if [.isIgnoreSenderPermissions] is true. * This will allow the sender of the message if [.isIgnoreSenderPermissions] is true.
*/ */
override fun getGroupID(sender: CommandSender): String? { override fun getGroupID(sender: CommandSender): String? {
return if (isIgnoreSenderPermissions && sender == cm.sender) groupID else super.getGroupID(sender) //Send in the correct group no matter what return if (isIgnoreSenderPermissions && sender == chatMessage.sender) groupID else super.getGroupID(sender) //Send in the correct group no matter what
} }
override fun getHandlers(): HandlerList { override fun getHandlers(): HandlerList {
return handlerList return handlerList
} }
val sender: CommandSender get() = cm.sender val sender: CommandSender get() = chatMessage.sender
val user: ChromaGamerBase get() = cm.user val user: ChromaGamerBase get() = chatMessage.user
val origin: String get() = cm.origin val origin: String get() = chatMessage.origin
val isFromCommand get() = chatMessage.isFromCommand
companion object { companion object {
val handlerList = HandlerList() val handlerList = HandlerList()

View file

@ -25,9 +25,9 @@ class TBMCSystemChatEvent(
} }
class BroadcastTarget private constructor(val name: String) { class BroadcastTarget private constructor(val name: String) {
companion object { companion object {
private val targets = HashSet<BroadcastTarget?>() private val targets = HashSet<BroadcastTarget?>()
@JvmField
val ALL = BroadcastTarget("ALL") val ALL = BroadcastTarget("ALL")
@JvmStatic @JvmStatic
@ -42,11 +42,13 @@ class TBMCSystemChatEvent(
targets.remove(target) targets.remove(target)
} }
@JvmStatic
operator fun get(name: String?): BroadcastTarget? { operator fun get(name: String?): BroadcastTarget? {
return targets.stream().filter { bt: BroadcastTarget? -> bt!!.name.equals(name, ignoreCase = true) } return targets.stream().filter { bt: BroadcastTarget? -> bt!!.name.equals(name, ignoreCase = true) }
.findAny().orElse(null) .findAny().orElse(null)
} }
@JvmStatic
fun stream(): Stream<BroadcastTarget?> { fun stream(): Stream<BroadcastTarget?> {
return targets.stream() return targets.stream()
} }

View file

@ -37,7 +37,7 @@ abstract class ButtonPlugin : JavaPlugin() {
/** /**
* Called before the components are unregistered * Called before the components are unregistered
*/ */
protected fun pluginPreDisable() {} protected open fun pluginPreDisable() {}
override fun onEnable() { override fun onEnable() {
if (!reloadIConfig()) { if (!reloadIConfig()) {
logger.warning("Please fix the issues and restart the server to load the plugin.") logger.warning("Please fix the issues and restart the server to load the plugin.")

View file

@ -91,7 +91,7 @@ class IHaveConfig(
if (data == null) datamap[path] = ListConfigData( if (data == null) datamap[path] = ListConfigData(
this, this,
path, path,
def, ArrayList(def),
elementGetter ?: Function { it as T }, elementGetter ?: Function { it as T },
elementSetter ?: Function { it }, elementSetter ?: Function { it },
readOnly readOnly

View file

@ -1,21 +1,21 @@
package buttondevteam.lib.architecture package buttondevteam.lib.architecture
import buttondevteam.lib.architecture.config.IConfigData import buttondevteam.lib.architecture.config.IConfigData
import java.util.ArrayList
import java.util.function.Function import java.util.function.Function
import java.util.function.Predicate import java.util.function.Predicate
import java.util.function.UnaryOperator import java.util.function.UnaryOperator
import kotlin.collections.List as KList
class ListConfigData<T> internal constructor( class ListConfigData<T> internal constructor(
config: IHaveConfig?, config: IHaveConfig?,
path: String, path: String,
primitiveDef: kotlin.collections.List<*>, primitiveDef: ArrayList<*>,
private val elementGetter: Function<Any?, T>, private val elementGetter: Function<Any?, T>,
private val elementSetter: Function<T, Any?>, private val elementSetter: Function<T, Any?>,
readOnly: Boolean readOnly: Boolean
) : IConfigData<ListConfigData<T>.List> { ) : IConfigData<ListConfigData<T>.List> {
val listConfig: ConfigData<List> = val listConfig: ConfigData<List> =
ConfigData(config, path, primitiveDef, { List((it as KList<*>).toMutableList()) }, { it }, readOnly) ConfigData(config, path, primitiveDef, { List((it as ArrayList<*>).toMutableList()) }, { it }, readOnly)
override val path: String get() = listConfig.path override val path: String get() = listConfig.path

View file

@ -66,6 +66,7 @@ class ChatMessage internal constructor(
} }
companion object { companion object {
@JvmStatic
fun builder(sender: CommandSender, user: ChromaGamerBase, message: String): ChatMessageBuilder { fun builder(sender: CommandSender, user: ChromaGamerBase, message: String): ChatMessageBuilder {
return ChatMessageBuilder( return ChatMessageBuilder(
sender, user, message, sender, user, message,

View file

@ -374,8 +374,8 @@ abstract class Command2<TC : ICommand2<TP>, TP : Command2Sender>(
* @param command The exact name of the command * @param command The exact name of the command
* @return A command node * @return A command node
*/ */
fun getCommandNode(command: String): CoreCommandNode<TP, NoOpSubcommandData> { // TODO: What should this return? No-op? Executable? What's the use case? fun getCommandNode(command: String): CoreCommandNode<TP, NoOpSubcommandData>? { // TODO: What should this return? No-op? Executable? What's the use case?
return dispatcher.root.getChild(command).core() return dispatcher.root.getChild(command)?.core()
} }
/** /**

View file

@ -25,6 +25,7 @@ object TBMCChatAPI {
* @return The event cancelled state * @return The event cancelled state
*/ */
@JvmOverloads @JvmOverloads
@JvmStatic
fun sendChatMessage(cm: ChatMessage, channel: Channel = cm.user.channel.get()): Boolean { fun sendChatMessage(cm: ChatMessage, channel: Channel = cm.user.channel.get()): Boolean {
if (!channelList.contains(channel)) throw RuntimeException( if (!channelList.contains(channel)) throw RuntimeException(
"Channel " + channel.displayName.get() + " not registered!" "Channel " + channel.displayName.get() + " not registered!"

View file

@ -240,6 +240,7 @@ abstract class ChromaGamerBase {
* @param foldername The folder to get the class from (like "minecraft") * @param foldername The folder to get the class from (like "minecraft")
* @return The type for the given folder name or null if not found * @return The type for the given folder name or null if not found
*/ */
@JvmStatic
fun getTypeForFolder(foldername: String?): Class<out ChromaGamerBase>? { fun getTypeForFolder(foldername: String?): Class<out ChromaGamerBase>? {
synchronized(staticDataMap) { synchronized(staticDataMap) {
return staticDataMap.filter { (_, value) -> value.folder.equals(foldername, ignoreCase = true) } return staticDataMap.filter { (_, value) -> value.folder.equals(foldername, ignoreCase = true) }
@ -302,6 +303,7 @@ abstract class ChromaGamerBase {
* *
* @param converter The converter that returns an object corresponding to the sender or null, if it's not the right type. * @param converter The converter that returns an object corresponding to the sender or null, if it's not the right type.
*/ */
@JvmStatic
fun addConverter(converter: Function<CommandSender, Optional<out ChromaGamerBase>>) { fun addConverter(converter: Function<CommandSender, Optional<out ChromaGamerBase>>) {
senderConverters.add(0, converter) senderConverters.add(0, converter)
} }
@ -312,6 +314,7 @@ abstract class ChromaGamerBase {
* @param sender The sender to use * @param sender The sender to use
* @return A user as returned by a converter or null if none can supply it * @return A user as returned by a converter or null if none can supply it
*/ */
@JvmStatic
fun getFromSender(sender: CommandSender): ChromaGamerBase? { // TODO: Use Command2Sender fun getFromSender(sender: CommandSender): ChromaGamerBase? { // TODO: Use Command2Sender
for (converter in senderConverters) { for (converter in senderConverters) {
val ocg = converter.apply(sender) val ocg = converter.apply(sender)

View file

@ -44,6 +44,7 @@ abstract class TBMCPlayerBase : ChromaGamerBase() {
* @param cl The type of the player * @param cl The type of the player
* @return The requested player object * @return The requested player object
*/ */
@JvmStatic
fun <T : TBMCPlayerBase> getPlayer(uuid: UUID, cl: Class<T>): T { fun <T : TBMCPlayerBase> getPlayer(uuid: UUID, cl: Class<T>): T {
val player = getUser(uuid.toString(), cl) val player = getUser(uuid.toString(), cl)
check(player.uniqueId == uuid) { "Player UUID differs after converting from and to string..." } check(player.uniqueId == uuid) { "Player UUID differs after converting from and to string..." }
@ -57,6 +58,7 @@ abstract class TBMCPlayerBase : ChromaGamerBase() {
* @return The [TBMCPlayer] object for the player * @return The [TBMCPlayer] object for the player
*/ */
@Suppress("deprecation") @Suppress("deprecation")
@JvmStatic
fun <T : TBMCPlayerBase> getFromName(name: String, cl: Class<T>): T { fun <T : TBMCPlayerBase> getFromName(name: String, cl: Class<T>): T {
val p = Bukkit.getOfflinePlayer(name) val p = Bukkit.getOfflinePlayer(name)
return getPlayer(p.uniqueId, cl) return getPlayer(p.uniqueId, cl)