2020-03-17 16:43:09 +00:00
/ *
2021-01-01 15:10:36 +00:00
* Copyright ( c ) 2019 - 2021 GeyserMC . http : //geysermc.org
2020-03-17 16:43:09 +00:00
*
* Permission is hereby granted , free of charge , to any person obtaining a copy
* of this software and associated documentation files ( the " Software " ) , to deal
* in the Software without restriction , including without limitation the rights
* to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
* copies of the Software , and to permit persons to whom the Software is
* furnished to do so , subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software .
*
* THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
* IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
* LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE .
*
* @author GeyserMC
* @link https : //github.com/GeyserMC/Geyser
* /
2021-11-20 21:34:30 +00:00
package org.geysermc.geyser.platform.spigot.command ;
2020-03-17 16:43:09 +00:00
import org.bukkit.command.ConsoleCommandSender ;
2020-10-29 22:30:52 +00:00
import org.bukkit.entity.Player ;
2021-11-20 21:34:30 +00:00
import org.geysermc.geyser.GeyserImpl ;
import org.geysermc.geyser.command.CommandSender ;
2021-11-20 23:29:46 +00:00
import org.geysermc.geyser.text.GeyserLocale ;
2020-10-29 22:30:52 +00:00
import java.lang.reflect.InvocationTargetException ;
import java.lang.reflect.Method ;
2020-03-17 16:43:09 +00:00
2020-05-26 14:05:25 +00:00
public class SpigotCommandSender implements CommandSender {
2020-03-17 16:43:09 +00:00
2020-10-29 22:30:52 +00:00
/ * *
* Whether to use { @code Player . getLocale ( ) } or { @code Player . spigot ( ) . getLocale ( ) } , depending on version .
* 1 . 12 or greater should not use the legacy method .
* /
private static boolean USE_LEGACY_METHOD = false ;
private static Method LOCALE_METHOD ;
private final org . bukkit . command . CommandSender handle ;
private final String locale ;
public SpigotCommandSender ( org . bukkit . command . CommandSender handle ) {
this . handle = handle ;
this . locale = getSpigotLocale ( ) ;
// Ensure even Java players' languages are loaded
2021-11-20 23:29:46 +00:00
GeyserLocale . loadGeyserLocale ( locale ) ;
2020-10-29 22:30:52 +00:00
}
2020-03-17 16:43:09 +00:00
@Override
public String getName ( ) {
return handle . getName ( ) ;
}
@Override
public void sendMessage ( String message ) {
handle . sendMessage ( message ) ;
}
@Override
public boolean isConsole ( ) {
return handle instanceof ConsoleCommandSender ;
}
2020-10-29 22:30:52 +00:00
@Override
public String getLocale ( ) {
return locale ;
}
2021-10-31 01:57:54 +00:00
@Override
public boolean hasPermission ( String permission ) {
return handle . hasPermission ( permission ) ;
}
2020-10-29 22:30:52 +00:00
/ * *
* Set if we are on pre - 1 . 12 , and therefore { @code player . getLocale ( ) } doesn ' t exist and we have to get
* { @code player . spigot ( ) . getLocale ( ) } .
*
* @param useLegacyMethod if we are running pre - 1 . 12 and therefore need to use reflection to get the player locale
* /
public static void setUseLegacyLocaleMethod ( boolean useLegacyMethod ) {
USE_LEGACY_METHOD = useLegacyMethod ;
if ( USE_LEGACY_METHOD ) {
try {
//noinspection JavaReflectionMemberAccess - of course it doesn't exist; that's why we're doing it
LOCALE_METHOD = Player . Spigot . class . getMethod ( " getLocale " ) ;
} catch ( NoSuchMethodException e ) {
2021-11-20 21:34:30 +00:00
GeyserImpl . getInstance ( ) . getLogger ( ) . debug ( " Player.Spigot.getLocale() doesn't exist? Not a big deal but if you're seeing this please report it to the developers! " ) ;
2020-10-29 22:30:52 +00:00
}
}
}
/ * *
* So we only have to do nasty reflection stuff once per command
*
* @return the locale of the Spigot player
* /
private String getSpigotLocale ( ) {
2021-09-10 18:10:56 +00:00
if ( handle instanceof Player player ) {
2020-10-29 22:30:52 +00:00
if ( USE_LEGACY_METHOD ) {
try {
// sigh
// This was the only option on older Spigot instances and now it's gone
return ( String ) LOCALE_METHOD . invoke ( player . spigot ( ) ) ;
} catch ( IllegalAccessException | InvocationTargetException ignored ) {
}
} else {
return player . getLocale ( ) ;
}
}
2021-11-20 23:29:46 +00:00
return GeyserLocale . getDefaultLocale ( ) ;
2020-10-29 22:30:52 +00:00
}
2020-03-17 16:43:09 +00:00
}