Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package WTGTimeHud;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import net.risingworld.api.Plugin;
- import net.risingworld.api.Server;
- import static net.risingworld.api.Server.getPlayerCount;
- import net.risingworld.api.Timer;
- import net.risingworld.api.World;
- import net.risingworld.api.database.Database;
- import net.risingworld.api.events.EventMethod;
- import net.risingworld.api.events.Listener;
- import net.risingworld.api.events.player.PlayerCommandEvent;
- import net.risingworld.api.events.player.PlayerConnectEvent;
- import net.risingworld.api.events.player.PlayerSpawnEvent;
- import net.risingworld.api.objects.Player;
- import net.risingworld.api.objects.Time.Unit;
- import net.risingworld.api.ui.UIElement;
- import net.risingworld.api.ui.UILabel;
- import net.risingworld.api.ui.style.Pivot;
- public class wtgtimehud extends Plugin implements Listener {
- public Timer caltimer;
- public Player player;
- String WorldName = World.getName();
- @Override
- public void onEnable() {
- this.registerEventListener(this);
- createTimer();
- try (Database timehud = getSQLiteConnection(getPath() + "/"+WorldName+"/timehud.db")){
- timehud.execute("CREATE TABLE IF NOT EXISTS `timehud` (`ID` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `playerid` BIGINT, `isvis` INTEGER)");
- System.out.println("TimeHud.db loaded");
- }
- }
- @Override
- public void onDisable() {
- }
- @EventMethod
- public void playerConnect(PlayerConnectEvent evt) throws SQLException{
- player = evt.getPlayer();
- ui(player);
- }
- @EventMethod
- public void playerspawn(PlayerSpawnEvent evt){
- player = evt.getPlayer();
- player.sendTextMessage("<color=yellow>Type /Time Help in chat for Time Hud info</color>");
- }
- public void vis(Player player) throws SQLException{
- long playerid = Long.parseLong(player.getUID());
- UIElement calpanel = (UIElement)player.getAttribute("calpanel");
- Database timehud = getSQLiteConnection(getPath() + "/"+WorldName+"/timehud.db");
- try(ResultSet result = timehud.executeQuery("SELECT * FROM `timehud` WHERE `playerid` = '"+playerid+"'")){
- if(result.next()==true){
- int isvis=result.getInt("isvis");
- if(isvis==0){
- calpanel.setVisible(true);
- }if(isvis==1){
- calpanel.setVisible(false);
- }
- }else if (result.next()==false){
- timehud.execute("INSERT INTO `timehud`(playerid, isvis)values('"+playerid+"', '"+0+"')");
- calpanel.setVisible(true);
- }
- }
- }
- public void createTimer(){
- caltimer = new Timer(5f, 0f, -1, () -> {
- try {
- update();
- } catch (SQLException ex) {
- Logger.getLogger(wtgtimehud.class.getName()).log(Level.SEVERE, null, ex);
- }
- });
- caltimer.start();
- }
- public void ui(Player player) throws SQLException{
- int Minutes = Server.getGameTime(Unit.Minutes);
- int Hours = Server.getGameTime(Unit.Hours);
- int Days = Server.getGameTime(Unit.Days);
- int Months = Server.getGameTime(Unit.Months);
- int Years = Server.getGameTime(Unit.Years);
- //main panel
- UIElement calpanel = new UIElement();
- calpanel.setPivot(Pivot.UpperLeft);
- calpanel.setPosition(1, 40, true);
- calpanel.setSize(150, 60, false);
- calpanel.setBackgroundColor(0, 0, 0, 0.7f);
- calpanel.setBorder(3);
- calpanel.setBorderColor(255);
- calpanel.setBorderEdgeRadius(5, true);
- //calpanel.setVisible(true);
- player.addUIElement(calpanel);
- //years
- UILabel years = new UILabel();
- years.setSize(100, 5, true);
- years.setPosition(50, 14, true);
- years.setText("<color=white>Year: "+String.valueOf(Years)+"</color>");
- years.setFontSize(15);
- years.setPivot(Pivot.MiddleCenter);
- years.setVisible(true);
- calpanel.addChild(years);
- player.setAttribute("years", years);
- //months
- UILabel months = new UILabel();
- months.setSize(50, 5, true);
- months.setPosition(5, 48, true);
- months.setText("<color=white>Month: "+String.valueOf(Months)+"</color>");
- months.setFontSize(15);
- months.setPivot(Pivot.MiddleLeft);
- months.setVisible(true);
- calpanel.addChild(months);
- player.setAttribute("months", months);
- //days
- UILabel days = new UILabel();
- days.setSize(50, 5, true);
- days.setPosition(60, 48, true);
- days.setText("<color=white>Day: "+String.valueOf(Days)+"</color>");
- days.setFontSize(15);
- days.setPivot(Pivot.MiddleLeft);
- days.setVisible(true);
- calpanel.addChild(days);
- player.setAttribute("days", days);
- //current time
- UILabel time = new UILabel();
- time.setSize(50, 5, true);
- time.setPosition(50, 82, true);
- time.setText("<color=white> Time "+Hours+" : "+Minutes+"</color>");
- time.setFontSize(15);
- time.setPivot(Pivot.MiddleCenter);
- time.setVisible(true);
- calpanel.addChild(time);
- player.setAttribute("time", time);
- player.setAttribute("calpanel", calpanel);
- vis(player);
- update();
- }
- public void update() throws SQLException{
- String minutetext;
- String hourstext;
- int Minutes = Server.getGameTime(Unit.Minutes);
- int Hours = Server.getGameTime(Unit.Hours)+1;
- int Days = Server.getGameTime(Unit.Days);
- int Months = Server.getGameTime(Unit.Months);
- int Years = Server.getGameTime(Unit.Years);
- if(getPlayerCount()>0){
- if(Minutes<=9){
- minutetext= "0"+String.valueOf(Minutes);
- }else{
- minutetext= String.valueOf(Minutes);
- }if(Hours<=9){
- hourstext = "0"+String.valueOf(Hours);
- }else{
- hourstext = String.valueOf(Hours);
- }
- for(Player player : Server.getAllPlayers()){
- //System.out.println("players online updateing ui");
- UILabel years = (UILabel)player.getAttribute("years");
- years.setText("<color=white>Year: "+String.valueOf(Years)+"</color>");
- UILabel months = (UILabel)player.getAttribute("months");
- months.setText("<color=white>Month: "+String.valueOf(Months)+"</color>");
- UILabel days = (UILabel)player.getAttribute("days");
- days.setText("<color=white>Day: "+String.valueOf(Days)+"</color>");
- UILabel time = (UILabel)player.getAttribute("time");
- time.setText("<color=white> Time "+hourstext+" : "+minutetext+"</color>");
- }
- }else{
- return;
- }
- }
- @EventMethod
- public void onCommand(PlayerCommandEvent command) {
- int isvis;
- Player player = command.getPlayer();
- long playerid = Long.parseLong(player.getUID());
- UIElement calpanel = (UIElement)player.getAttribute("calpanel");
- String[] cmd = command.getCommand().split(" ");
- if (cmd[0].equalsIgnoreCase("/Time")){
- if(cmd.length<2)return;
- Database timehud = getSQLiteConnection(getPath() + "/"+WorldName+"/timehud.db");
- if(cmd[1].equalsIgnoreCase("Show")){
- calpanel.setVisible(true);
- isvis=0;
- timehud.executeUpdate("UPDATE `timehud` SET isvis = '"+isvis+"'where playerid = '"+playerid+"'");
- }if(cmd[1].equalsIgnoreCase("Hide")){
- calpanel.setVisible(false);
- isvis = 1;
- timehud.executeUpdate("UPDATE `timehud` SET isvis = '"+isvis+"'where playerid = '"+playerid+"'");
- }if(cmd[1].equalsIgnoreCase("Help")){
- player.sendTextMessage("<color=yellow>/Time Hide to hide hud</color>");
- player.sendTextMessage("<color=yellow>/Time Show to show hud</color>");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement