Last active
June 19, 2017 21:10
-
-
Save effective-light/39668b45e83425d9a2d6bc37d8b03968 to your computer and use it in GitHub Desktop.
BossBar
This file contains hidden or 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 program is free software; you can redistribute it and/or | |
| * modify it under the terms of the GNU General Public License | |
| * as published by the Free Software Foundation; either version 3 | |
| * of the License, or (at your option) any later version. | |
| * | |
| * This program is distributed in the hope that it will be useful, | |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| * GNU General Public License for more details. | |
| * | |
| * You should have received a copy of the GNU General Public License | |
| * along with this program; if not, write to the Free Software | |
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
| * | |
| */ | |
| package packagename; | |
| import net.md_5.bungee.api.chat.BaseComponent; | |
| import net.md_5.bungee.chat.ComponentSerializer; | |
| import org.bukkit.Bukkit; | |
| import org.bukkit.entity.Player; | |
| import java.lang.reflect.Constructor; | |
| import java.lang.reflect.Field; | |
| import java.lang.reflect.InvocationTargetException; | |
| import java.lang.reflect.Method; | |
| public class BossBar | |
| { | |
| private static Constructor<?> bossBattleServerConstructor; | |
| private static Method toIChatBaseComponent; | |
| private static Method addPlayer; | |
| private static Method removePlayer; | |
| private static Method setName; | |
| private static Method setProgress; | |
| private static Method setDarkenSky; | |
| private static Method setPlayMusic; | |
| private static Method setCreateFog; | |
| private static Method setVisible; | |
| private static Method sendUpdate; | |
| private static Method getHandle; | |
| private static Field color; | |
| private static Field style; | |
| private static Object[] colors; | |
| private static Object[] styles; | |
| private static Object[] actions; | |
| private Object bossBattleServer; | |
| static | |
| { | |
| String version = Bukkit.getServer().getClass().getPackage().getName().split( "\\." )[ 3 ]; | |
| String nms = "net.minecraft.server." + version + "."; | |
| try | |
| { | |
| toIChatBaseComponent = Class.forName( nms + "IChatBaseComponent$ChatSerializer" ).getMethod( "a", String.class ); | |
| Class<?> barColor = Class.forName( nms + "BossBattle$BarColor" ); | |
| colors = (Object[]) barColor.getMethod( "values" ).invoke( null ); | |
| Class<?> barStyle = Class.forName( nms + "BossBattle$BarStyle" ); | |
| styles = (Object[]) barStyle.getMethod( "values" ).invoke( null ); | |
| Class<?> bossBattleServer = Class.forName( nms + "BossBattleServer" ); | |
| color = bossBattleServer.getField( "color" ); | |
| style = bossBattleServer.getField( "style" ); | |
| Class<?> iChatBaseComponent = Class.forName( nms + "IChatBaseComponent" ); | |
| bossBattleServerConstructor = bossBattleServer.getConstructor( iChatBaseComponent, barColor, barStyle ); | |
| setName = bossBattleServer.getMethod( "a", iChatBaseComponent ); | |
| setProgress = bossBattleServer.getMethod( "setProgress", float.class ); | |
| setDarkenSky = bossBattleServer.getMethod( "setDarkenSky", boolean.class ); | |
| setPlayMusic = bossBattleServer.getMethod( "setPlayMusic", boolean.class ); | |
| setCreateFog = bossBattleServer.getMethod( "setCreateFog", boolean.class ); | |
| setVisible = bossBattleServer.getMethod( "setVisible", boolean.class ); | |
| Class<?> action = Class.forName( nms + "PacketPlayOutBoss$Action" ); | |
| actions = ( (Object[]) action.getMethod( "values" ).invoke( null ) ); | |
| sendUpdate = bossBattleServer.getMethod( "sendUpdate", action ); | |
| Class<?> entityPlayer = Class.forName( nms + "EntityPlayer" ); | |
| addPlayer = bossBattleServer.getMethod( "addPlayer", entityPlayer ); | |
| removePlayer = bossBattleServer.getMethod( "removePlayer", entityPlayer ); | |
| getHandle = Class.forName( "org.bukkit.craftbukkit." + version + ".entity.CraftPlayer" ).getMethod( "getHandle" ); | |
| } catch ( ClassNotFoundException | NoSuchMethodException | IllegalAccessException | |
| | InvocationTargetException | NoSuchFieldException e ) | |
| { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * @param baseComponents the message displayed on the bar | |
| * @param barColor the color of the bar | |
| * @param barStyle the style of the bar | |
| */ | |
| public BossBar(BaseComponent[] baseComponents, BarColor barColor, BarStyle barStyle) | |
| { | |
| try | |
| { | |
| bossBattleServer = bossBattleServerConstructor.newInstance( toIChatBaseComponent.invoke( null, | |
| ComponentSerializer.toString( baseComponents ) ), colors[ barColor.ordinal() ], styles[ barStyle.ordinal() ] ); | |
| } catch ( InstantiationException | IllegalAccessException | InvocationTargetException e ) | |
| { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * Changes the message being displayed on the {@link BossBar}. | |
| * | |
| * @param baseComponents the message being set | |
| */ | |
| public void setName(BaseComponent[] baseComponents) | |
| { | |
| try | |
| { | |
| setName.invoke( bossBattleServer, toIChatBaseComponent.invoke( null, ComponentSerializer.toString( baseComponents ) ) ); | |
| } catch ( IllegalAccessException | InvocationTargetException e ) | |
| { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * Allows the player to view the {@link BossBar}. | |
| * | |
| * @param player the player being added | |
| */ | |
| public void addPlayer(Player player) | |
| { | |
| try | |
| { | |
| addPlayer.invoke( bossBattleServer, getHandle.invoke( player ) ); | |
| } catch ( IllegalAccessException | InvocationTargetException e ) | |
| { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * Prevents the player from viewing the {@link BossBar} any further. | |
| * <p> | |
| * NOTE: a player must be removed before they leave the server otherwise, there is risk for a memory leak. | |
| * <p/> | |
| * | |
| * @param player the player being removed | |
| */ | |
| public void removePlayer(Player player) | |
| { | |
| try | |
| { | |
| removePlayer.invoke( bossBattleServer, getHandle.invoke( player ) ); | |
| } catch ( IllegalAccessException | InvocationTargetException e ) | |
| { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * Sets the {@link BossBar}'s progress. | |
| * | |
| * @param progress the progress being set | |
| */ | |
| public void setProgress(float progress) | |
| { | |
| try | |
| { | |
| setProgress.invoke( bossBattleServer, progress ); | |
| } catch ( IllegalAccessException | InvocationTargetException e ) | |
| { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * Darkens the sky. | |
| * | |
| * @param darkenSky should the sky be darkened | |
| */ | |
| public void setDarkenSky(boolean darkenSky) | |
| { | |
| try | |
| { | |
| setDarkenSky.invoke( bossBattleServer, darkenSky ); | |
| } catch ( IllegalAccessException | InvocationTargetException e ) | |
| { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * Changes the play music state of the {@link BossBar}. | |
| * | |
| * @param playMusic should music be played | |
| */ | |
| public void setPlayMusic(boolean playMusic) | |
| { | |
| try | |
| { | |
| setPlayMusic.invoke( bossBattleServer, playMusic ); | |
| } catch ( IllegalAccessException | InvocationTargetException e ) | |
| { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * Changes the create fog state of the {@link BossBar}. | |
| * | |
| * @param createFog should fog be created | |
| */ | |
| public void setCreateFog(boolean createFog) | |
| { | |
| try | |
| { | |
| setCreateFog.invoke( bossBattleServer, createFog ); | |
| } catch ( IllegalAccessException | InvocationTargetException e ) | |
| { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * Changes the visibility state of the {@link BossBar}. | |
| * | |
| * @param visible should the {@link BossBar} be visible | |
| */ | |
| public void setVisible(boolean visible) | |
| { | |
| try | |
| { | |
| setVisible.invoke( bossBattleServer, visible ); | |
| } catch ( IllegalAccessException | InvocationTargetException e ) | |
| { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * Sets the {@link BossBar}'s {@link BarColor}. | |
| * | |
| * @param barColor the {@link BarColor} that is to be set | |
| */ | |
| public void setColor(BarColor barColor) | |
| { | |
| try | |
| { | |
| color.set( bossBattleServer, colors[ barColor.ordinal() ] ); | |
| sendUpdate( actions[ 4 ] ); | |
| } catch ( IllegalAccessException e ) | |
| { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * Sets the {@link BossBar}'s {@link BarStyle}. | |
| * | |
| * @param barStyle the {@link BarStyle} that is to be set | |
| */ | |
| public void setStyle(BarStyle barStyle) | |
| { | |
| try | |
| { | |
| style.set( bossBattleServer, styles[ barStyle.ordinal() ] ); | |
| sendUpdate( actions[ 4 ] ); | |
| } catch ( IllegalAccessException e ) | |
| { | |
| e.printStackTrace(); | |
| } | |
| } | |
| private void sendUpdate(Object action) | |
| { | |
| try | |
| { | |
| sendUpdate.invoke( bossBattleServer, action ); | |
| } catch ( IllegalAccessException | InvocationTargetException e ) | |
| { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public enum BarColor | |
| { | |
| PINK, BLUE, RED, GREEN, YELLOW, PURPLE, WHITE | |
| } | |
| public enum BarStyle | |
| { | |
| PROGRESS, NOTCHED_6, NOTCHED_10, NOTCHED_12, NOTCHED_20 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment