2012-01-09 20:00:13 +01:00
package com.gmail.nossr50.spout ;
import java.io.BufferedOutputStream ;
import java.io.File ;
import java.io.FileNotFoundException ;
import java.io.FileOutputStream ;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.OutputStream ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.jar.JarEntry ;
import java.util.jar.JarFile ;
import org.bukkit.ChatColor ;
import org.bukkit.Material ;
import org.bukkit.entity.Player ;
2012-04-21 20:05:39 +02:00
2012-01-09 20:00:13 +01:00
import org.getspout.spoutapi.SpoutManager ;
import org.getspout.spoutapi.keyboard.Keyboard ;
import org.getspout.spoutapi.player.SpoutPlayer ;
import com.gmail.nossr50.mcMMO ;
import com.gmail.nossr50.datatypes.HUDmmo ;
import com.gmail.nossr50.datatypes.PlayerProfile ;
import com.gmail.nossr50.datatypes.popups.PopupMMO ;
import com.gmail.nossr50.datatypes.SkillType ;
2012-04-26 19:42:09 +02:00
import com.gmail.nossr50.listeners.SpoutInputListener ;
import com.gmail.nossr50.listeners.SpoutListener ;
import com.gmail.nossr50.listeners.SpoutScreenListener ;
2012-04-27 11:47:11 +02:00
import com.gmail.nossr50.util.Misc ;
import com.gmail.nossr50.util.Users ;
2012-01-09 20:00:13 +01:00
2012-03-21 03:33:58 +01:00
public class SpoutStuff {
2012-04-21 20:05:39 +02:00
private static mcMMO plugin = mcMMO . p ;
public final static String spoutDirectory = mcMMO . mainDirectory + " Resources " + File . separator ;
public final static String hudDirectory = spoutDirectory + " HUD " + File . separator ;
public final static String hudStandardDirectory = hudDirectory + " Standard " + File . separator ;
public final static String hudRetroDirectory = hudDirectory + " Retro " + File . separator ;
public final static String soundDirectory = spoutDirectory + " Sound " + File . separator ;
2012-03-21 03:33:58 +01:00
2012-04-26 19:42:09 +02:00
private final static SpoutListener spoutListener = new SpoutListener ( plugin ) ;
private final static SpoutInputListener spoutInputListener = new SpoutInputListener ( plugin ) ;
private final static SpoutScreenListener spoutScreenListener = new SpoutScreenListener ( plugin ) ;
2012-03-21 03:33:58 +01:00
public static HashMap < Player , HUDmmo > playerHUDs = new HashMap < Player , HUDmmo > ( ) ;
public static HashMap < SpoutPlayer , PopupMMO > playerScreens = new HashMap < SpoutPlayer , PopupMMO > ( ) ;
public static Keyboard keypress ;
/ * *
* Write file to disk .
*
* @param theFileName The name of the file
* @param theFilePath The name of the file path
* /
2012-04-21 20:05:39 +02:00
private static void writeFile ( String theFileName , String theFilePath ) {
2012-03-21 03:33:58 +01:00
try {
2012-04-21 20:05:39 +02:00
File currentFile = new File ( theFilePath + theFileName ) ;
2012-03-21 03:33:58 +01:00
2012-04-21 20:05:39 +02:00
JarFile jar = new JarFile ( mcMMO . mcmmo ) ;
2012-03-21 03:33:58 +01:00
JarEntry entry = jar . getJarEntry ( " resources/ " + theFileName ) ;
InputStream is = jar . getInputStream ( entry ) ;
byte [ ] buf = new byte [ 2048 ] ;
int nbRead ;
OutputStream os = new BufferedOutputStream ( new FileOutputStream ( currentFile ) ) ;
while ( ( nbRead = is . read ( buf ) ) ! = - 1 ) {
os . write ( buf , 0 , nbRead ) ;
}
os . flush ( ) ;
os . close ( ) ;
}
catch ( FileNotFoundException e ) {
e . printStackTrace ( ) ;
}
catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
/ * *
* Extract Spout files to the Resources directory .
* /
public static void extractFiles ( ) {
//Setup directories
2012-04-21 20:05:39 +02:00
new File ( spoutDirectory ) . mkdir ( ) ;
new File ( hudDirectory ) . mkdir ( ) ;
new File ( hudStandardDirectory ) . mkdir ( ) ;
new File ( hudRetroDirectory ) . mkdir ( ) ;
new File ( soundDirectory ) . mkdir ( ) ;
2012-03-21 03:33:58 +01:00
//XP Bar images
for ( int x = 0 ; x < 255 ; x + + ) {
String theFileName ;
if ( x < 10 ) {
theFileName = " xpbar_inc00 " + x + " .png " ;
}
else if ( x < 100 ) {
theFileName = " xpbar_inc0 " + x + " .png " ;
}
else {
theFileName = " xpbar_inc " + x + " .png " ;
}
2012-04-21 20:05:39 +02:00
writeFile ( theFileName , hudStandardDirectory ) ;
2012-03-21 03:33:58 +01:00
}
//Standard XP Icons
for ( SkillType y : SkillType . values ( ) ) {
if ( y . equals ( SkillType . ALL ) ) {
continue ;
}
2012-04-27 11:47:11 +02:00
String standardFileName = Misc . getCapitalized ( y . toString ( ) ) + " .png " ;
String retroFileName = Misc . getCapitalized ( y . toString ( ) ) + " _r.png " ;
2012-03-21 03:33:58 +01:00
2012-04-21 20:05:39 +02:00
writeFile ( standardFileName , hudStandardDirectory ) ;
writeFile ( retroFileName , hudRetroDirectory ) ;
2012-03-21 03:33:58 +01:00
}
//Blank icons
2012-04-21 20:05:39 +02:00
writeFile ( " Icon.png " , hudStandardDirectory ) ;
writeFile ( " Icon_r.png " , hudRetroDirectory ) ;
2012-03-21 03:33:58 +01:00
//Sound FX
2012-04-21 20:05:39 +02:00
writeFile ( " repair.wav " , soundDirectory ) ;
writeFile ( " level.wav " , soundDirectory ) ;
2012-03-21 03:33:58 +01:00
}
/ * *
* Setup Spout config options
* /
public static void setupSpoutConfigs ( ) {
String temp = plugin . getConfig ( ) . getString ( " Spout.Menu.Key " , " KEY_M " ) ;
for ( Keyboard x : Keyboard . values ( ) ) {
if ( x . toString ( ) . equalsIgnoreCase ( temp ) ) {
keypress = x ;
}
}
if ( keypress = = null ) {
System . out . println ( " Invalid KEY for Spout.Menu.Key, using KEY_M " ) ;
keypress = Keyboard . KEY_M ;
}
}
/ * *
* Get all the Spout files in the Resources folder .
*
* @return a list of all files is the Resources folder
* /
public static ArrayList < File > getFiles ( ) {
ArrayList < File > files = new ArrayList < File > ( ) ;
/* XP BAR */
for ( int x = 0 ; x < 255 ; x + + ) {
if ( x < 10 ) {
2012-04-21 20:05:39 +02:00
files . add ( new File ( hudStandardDirectory + " xpbar_inc00 " + x + " .png " ) ) ;
2012-03-21 03:33:58 +01:00
}
else if ( x < 100 ) {
2012-04-21 20:05:39 +02:00
files . add ( new File ( hudStandardDirectory + " xpbar_inc0 " + x + " .png " ) ) ;
2012-03-21 03:33:58 +01:00
}
else {
2012-04-21 20:05:39 +02:00
files . add ( new File ( hudStandardDirectory + " xpbar_inc " + x + " .png " ) ) ;
2012-03-21 03:33:58 +01:00
}
}
/* Standard XP Icons */
for ( SkillType y : SkillType . values ( ) ) {
if ( y . equals ( SkillType . ALL ) ) {
continue ;
}
2012-04-27 11:47:11 +02:00
files . add ( new File ( hudStandardDirectory + Misc . getCapitalized ( y . toString ( ) ) + " .png " ) ) ;
files . add ( new File ( hudRetroDirectory + Misc . getCapitalized ( y . toString ( ) ) + " _r.png " ) ) ;
2012-03-21 03:33:58 +01:00
}
/* Blank icons */
2012-04-21 20:05:39 +02:00
files . add ( new File ( hudStandardDirectory + " Icon.png " ) ) ;
files . add ( new File ( hudRetroDirectory + " Icon_r.png " ) ) ;
2012-03-21 03:33:58 +01:00
//Repair SFX
2012-04-21 20:05:39 +02:00
files . add ( new File ( soundDirectory + " repair.wav " ) ) ;
2012-03-21 03:33:58 +01:00
//Level SFX
2012-04-21 20:05:39 +02:00
files . add ( new File ( soundDirectory + " level.wav " ) ) ;
2012-03-21 03:33:58 +01:00
return files ;
}
/ * *
* Register custom Spout events .
* /
public static void registerCustomEvent ( ) {
2012-04-21 20:05:39 +02:00
plugin . getServer ( ) . getPluginManager ( ) . registerEvents ( spoutListener , plugin ) ;
plugin . getServer ( ) . getPluginManager ( ) . registerEvents ( spoutInputListener , plugin ) ;
plugin . getServer ( ) . getPluginManager ( ) . registerEvents ( spoutScreenListener , plugin ) ;
2012-03-21 03:33:58 +01:00
}
/ * *
* Gets a Spout player from a player name .
*
* @param playerName The player name
* @return the SpoutPlayer related to this player name , null if there ' s no player online with that name .
* /
public static SpoutPlayer getSpoutPlayer ( String playerName ) {
2012-04-21 20:05:39 +02:00
for ( Player x : plugin . getServer ( ) . getOnlinePlayers ( ) ) {
2012-03-21 03:33:58 +01:00
if ( x . getName ( ) . equalsIgnoreCase ( playerName ) ) {
return SpoutManager . getPlayer ( x ) ;
}
}
return null ;
}
/ * *
* Handle level - up notifications through Spout .
*
* @param skillType The skill that leveled up
* @param sPlayer The player that leveled up
* /
public static void levelUpNotification ( SkillType skillType , SpoutPlayer sPlayer ) {
PlayerProfile PP = Users . getProfile ( sPlayer ) ;
int notificationTier = getNotificationTier ( PP . getSkillLevel ( skillType ) ) ;
Material mat = null ;
switch ( skillType ) {
case TAMING :
switch ( notificationTier ) {
case 1 :
case 2 :
mat = Material . PORK ;
break ;
case 3 :
case 4 :
mat = Material . GRILLED_PORK ;
break ;
case 5 :
mat = Material . BONE ;
break ;
default :
break ;
}
break ;
case MINING :
switch ( notificationTier ) {
case 1 :
mat = Material . COAL_ORE ;
break ;
case 2 :
mat = Material . IRON_ORE ;
break ;
case 3 :
mat = Material . GOLD_ORE ;
break ;
case 4 :
mat = Material . LAPIS_ORE ;
break ;
case 5 :
mat = Material . DIAMOND_ORE ;
break ;
default :
break ;
}
break ;
case WOODCUTTING :
switch ( notificationTier ) {
case 1 :
case 2 :
case 3 :
mat = Material . WOOD ;
break ;
case 4 :
case 5 :
mat = Material . LOG ;
break ;
default :
break ;
}
break ;
case REPAIR :
switch ( notificationTier ) {
case 1 :
mat = Material . COBBLESTONE ;
break ;
case 2 :
mat = Material . IRON_BLOCK ;
break ;
case 3 :
mat = Material . GOLD_BLOCK ;
break ;
case 4 :
mat = Material . LAPIS_BLOCK ;
break ;
case 5 :
mat = Material . DIAMOND_BLOCK ;
break ;
default :
break ;
}
break ;
case HERBALISM :
switch ( notificationTier ) {
case 1 :
mat = Material . YELLOW_FLOWER ;
break ;
case 2 :
mat = Material . RED_ROSE ;
break ;
case 3 :
mat = Material . BROWN_MUSHROOM ;
break ;
case 4 :
mat = Material . RED_MUSHROOM ;
break ;
case 5 :
mat = Material . PUMPKIN ;
break ;
default :
break ;
}
break ;
case ACROBATICS :
switch ( notificationTier ) {
case 1 :
mat = Material . LEATHER_BOOTS ;
break ;
case 2 :
mat = Material . CHAINMAIL_BOOTS ;
break ;
case 3 :
mat = Material . IRON_BOOTS ;
break ;
case 4 :
mat = Material . GOLD_BOOTS ;
break ;
case 5 :
mat = Material . DIAMOND_BOOTS ;
break ;
default :
break ;
}
break ;
case SWORDS :
switch ( notificationTier ) {
case 1 :
mat = Material . WOOD_SWORD ;
break ;
case 2 :
mat = Material . STONE_SWORD ;
break ;
case 3 :
mat = Material . IRON_SWORD ;
break ;
case 4 :
mat = Material . GOLD_SWORD ;
break ;
case 5 :
mat = Material . DIAMOND_SWORD ;
break ;
default :
break ;
}
break ;
case ARCHERY :
switch ( notificationTier ) {
case 1 :
case 2 :
case 3 :
mat = Material . ARROW ;
break ;
case 4 :
case 5 :
mat = Material . BOW ;
break ;
default :
break ;
}
break ;
case UNARMED :
switch ( notificationTier ) {
case 1 :
mat = Material . LEATHER_HELMET ;
break ;
case 2 :
mat = Material . CHAINMAIL_HELMET ;
break ;
case 3 :
mat = Material . IRON_HELMET ;
break ;
case 4 :
mat = Material . GOLD_HELMET ;
break ;
case 5 :
mat = Material . DIAMOND_HELMET ;
break ;
default :
break ;
}
break ;
case EXCAVATION :
switch ( notificationTier ) {
case 1 :
mat = Material . WOOD_SPADE ;
break ;
case 2 :
mat = Material . STONE_SPADE ;
break ;
case 3 :
mat = Material . IRON_SPADE ;
break ;
case 4 :
mat = Material . GOLD_SPADE ;
break ;
case 5 :
mat = Material . DIAMOND_SPADE ;
break ;
default :
break ;
}
break ;
case AXES :
switch ( notificationTier ) {
case 1 :
mat = Material . WOOD_AXE ;
break ;
case 2 :
mat = Material . STONE_AXE ;
break ;
case 3 :
mat = Material . IRON_AXE ;
break ;
case 4 :
mat = Material . GOLD_AXE ;
break ;
case 5 :
mat = Material . DIAMOND_AXE ;
break ;
default :
break ;
}
break ;
case FISHING :
switch ( notificationTier ) {
case 1 :
case 2 :
mat = Material . RAW_FISH ;
break ;
case 3 :
case 4 :
mat = Material . COOKED_FISH ;
break ;
case 5 :
mat = Material . FISHING_ROD ;
break ;
default :
break ;
}
break ;
default :
mat = Material . WATCH ;
break ;
}
//TODO: Use Locale
2012-04-27 11:47:11 +02:00
sPlayer . sendNotification ( ChatColor . GREEN + " Level Up! " , ChatColor . YELLOW + Misc . getCapitalized ( skillType . toString ( ) ) + ChatColor . DARK_AQUA + " ( " + ChatColor . GREEN + PP . getSkillLevel ( skillType ) + ChatColor . DARK_AQUA + " ) " , mat ) ;
2012-04-21 00:09:50 +02:00
SpoutSounds . playLevelUpNoise ( sPlayer , plugin ) ;
2012-03-21 03:33:58 +01:00
}
/ * *
* Gets the notification tier of a skill .
*
* @param level The level of the skill
* @return the notification tier of the skill
* /
private static Integer getNotificationTier ( Integer level ) {
if ( level > = 800 ) {
return 5 ;
}
else if ( level > = 600 ) {
return 4 ;
}
else if ( level > = 400 ) {
return 3 ;
}
else if ( level > = 200 ) {
return 2 ;
}
else {
return 1 ;
}
}
/ * *
* Update a player ' s Spout XP bar .
*
* @param player The player whose bar to update
* /
public static void updateXpBar ( Player player ) {
2012-03-26 15:06:48 +02:00
playerHUDs . get ( player ) . updateXpBarDisplay ( Users . getProfile ( player ) . getHUDType ( ) , player ) ; //Is there a reason we can't just do HUDmmo.updateXpBarDisplay?
2012-03-21 03:33:58 +01:00
}
2012-01-09 20:00:13 +01:00
}