22
33import com .github .games647 .changeskin .core .ChangeSkinCore ;
44import com .github .games647 .changeskin .core .SkinStorage ;
5+ import com .github .games647 .changeskin .sponge .commands .SetSkinCommand ;
6+ import com .github .games647 .changeskin .sponge .commands .SkinInvalidateCommand ;
7+ import com .google .common .cache .Cache ;
8+ import com .google .common .cache .CacheBuilder ;
59import com .google .common .collect .Lists ;
610import com .google .common .io .Resources ;
711import com .google .inject .Inject ;
1014import java .io .FileOutputStream ;
1115import java .io .IOException ;
1216import java .util .List ;
17+ import java .util .UUID ;
18+ import java .util .concurrent .TimeUnit ;
1319
1420import ninja .leaping .configurate .ConfigurationNode ;
1521import ninja .leaping .configurate .yaml .YAMLConfigurationLoader ;
1622
1723import org .slf4j .Logger ;
1824import org .spongepowered .api .Game ;
1925import org .spongepowered .api .command .CommandManager ;
26+ import org .spongepowered .api .command .CommandSource ;
27+ import org .spongepowered .api .command .args .GenericArguments ;
28+ import org .spongepowered .api .command .spec .CommandSpec ;
2029import org .spongepowered .api .config .DefaultConfig ;
30+ import org .spongepowered .api .entity .living .player .Player ;
2131import org .spongepowered .api .event .Listener ;
2232import org .spongepowered .api .event .game .state .GameInitializationEvent ;
2333import org .spongepowered .api .event .game .state .GamePreInitializationEvent ;
2434import org .spongepowered .api .plugin .Plugin ;
35+ import org .spongepowered .api .text .Text ;
36+ import org .spongepowered .api .text .serializer .TextSerializers ;
2537
2638@ Plugin (id = "changeskin" , name = "ChangeSkin" , version = "1.7"
2739 , url = "https://github.com/games647/ChangeSkin"
2840 , description = "Sponge plugin to change your skin server side" )
2941public class ChangeSkinSponge {
3042
3143 @ Inject
32- @ DefaultConfig (sharedRoot = true )
44+ @ DefaultConfig (sharedRoot = false )
3345 //We will place more than one config there (i.e. H2/SQLite database)
3446 private File defaultConfigFile ;
3547
3648 private final Logger logger ;
3749 private final Game game ;
3850
51+ private Cache <UUID , Object > cooldowns ;
3952 private ChangeSkinCore core ;
4053 private ConfigurationNode rootNode ;
4154
@@ -92,11 +105,15 @@ public void onPreInit(GamePreInitializationEvent preInitEvent) {
92105 }
93106
94107 List <String > defaultSkins = Lists .newArrayList ();
95- rootNode .getNode ("default-skins" ).getChildrenList ().stream ()
96- .forEach ((configurationNode ) -> defaultSkins .add (configurationNode .getString ()));
108+ for (ConfigurationNode node : rootNode .getNode ("default-skins" ).getChildrenMap ().values ()) {
109+ defaultSkins .add (node .getString ());
110+ }
97111
98112 core .loadDefaultSkins (defaultSkins );
99113 loadLocale ();
114+
115+ int cooldown = rootNode .getNode ("cooldown" ).getInt ();
116+ cooldowns = CacheBuilder .newBuilder ().expireAfterWrite (cooldown , TimeUnit .SECONDS ).build ();
100117 } catch (IOException ioEx ) {
101118 logger .error ("Failed to load config" , ioEx );
102119 }
@@ -105,21 +122,52 @@ public void onPreInit(GamePreInitializationEvent preInitEvent) {
105122 @ Listener //command and event register
106123 public void onInit (GameInitializationEvent initEvent ) {
107124 CommandManager commandManager = game .getCommandManager ();
108- // commandManager.register(this, CommandSpec.builder()
109- // .executor(new SetSkinCommand(this))
110- // .arguments(GenericArguments.string(Text.of("skin"))
111- // , GenericArguments.optional(GenericArguments.string(Text.of("target"))))
112- // .build(), "changeskin");
125+ commandManager .register (this , CommandSpec .builder ()
126+ .executor (new SetSkinCommand (this ))
127+ .arguments (GenericArguments .string (Text .of ("skin" )))
128+ .build (), "changeskin" , "setskin" );
113129
114- // commandManager.register(this, CommandSpec.builder()
115- // .executor(new SkinInvalidateCommand(this))
116- // .build(), "skininvalidate");
130+ commandManager .register (this , CommandSpec .builder ()
131+ .executor (new SkinInvalidateCommand (this ))
132+ .build (), "skininvalidate" );
117133
118134 game .getEventManager ().registerListeners (this , new LoginListener (this ));
119135 }
120136
121137 private void loadLocale () {
122- //todo
138+ File messageFile = new File (defaultConfigFile .getParentFile (), "messages.yml" );
139+
140+ if (!messageFile .exists ()) {
141+ FileOutputStream fileOutputStream = null ;
142+ try {
143+ fileOutputStream = new FileOutputStream (messageFile );
144+ Resources .copy (getClass ().getResource ("/messages.yml" ), fileOutputStream );
145+ } catch (IOException ioEx ) {
146+ logger .error ("Error deploying default message" , ioEx );
147+ } finally {
148+ if (fileOutputStream != null ) {
149+ try {
150+ fileOutputStream .close ();
151+ } catch (IOException ex ) {
152+ //ignore
153+ }
154+ }
155+ }
156+ }
157+
158+ YAMLConfigurationLoader messageLoader = YAMLConfigurationLoader .builder ().setFile (messageFile ).build ();
159+ try {
160+ ConfigurationNode messageNode = messageLoader .load ();
161+ for (ConfigurationNode node : messageNode .getChildrenMap ().values ()) {
162+ core .addMessage ((String ) node .getKey (), node .getString ());
163+ }
164+
165+ for (ConfigurationNode configurationNode : messageNode .getChildrenList ()) {
166+
167+ }
168+ } catch (IOException ioEx ) {
169+ logger .error ("Failed to load locale" , ioEx );
170+ }
123171 }
124172
125173 public ChangeSkinCore getCore () {
@@ -130,6 +178,27 @@ public ConfigurationNode getRootNode() {
130178 return rootNode ;
131179 }
132180
181+ public boolean isCooldown (CommandSource sender ) {
182+ if (sender instanceof Player ) {
183+ return cooldowns .asMap ().containsKey (((Player ) sender ).getUniqueId ());
184+ }
185+
186+ return false ;
187+ }
188+
189+ public void sendMessage (CommandSource sender , String key ) {
190+ String message = core .getMessage (key );
191+ if (message != null && sender != null ) {
192+ sender .sendMessage (TextSerializers .LEGACY_FORMATTING_CODE .deserialize (message .replace ('&' , '§' )));
193+ }
194+ }
195+
196+ public void addCooldown (CommandSource sender ) {
197+ if (sender instanceof Player ) {
198+ cooldowns .put (((Player ) sender ).getUniqueId (), new Object ());
199+ }
200+ }
201+
133202 public Game getGame () {
134203 return game ;
135204 }
0 commit comments