Advertisement
james1bow

Time HUd Code

Mar 9th, 2024
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.32 KB | None | 0 0
  1.  
  2. package WTGTimeHud;
  3.  
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import net.risingworld.api.Plugin;
  9. import net.risingworld.api.Server;
  10. import static net.risingworld.api.Server.getPlayerCount;
  11. import net.risingworld.api.Timer;
  12. import net.risingworld.api.World;
  13. import net.risingworld.api.database.Database;
  14. import net.risingworld.api.events.EventMethod;
  15. import net.risingworld.api.events.Listener;
  16. import net.risingworld.api.events.player.PlayerCommandEvent;
  17. import net.risingworld.api.events.player.PlayerConnectEvent;
  18. import net.risingworld.api.events.player.PlayerSpawnEvent;
  19. import net.risingworld.api.objects.Player;
  20. import net.risingworld.api.objects.Time.Unit;
  21. import net.risingworld.api.ui.UIElement;
  22. import net.risingworld.api.ui.UILabel;
  23. import net.risingworld.api.ui.style.Pivot;
  24.  
  25.  
  26. public class wtgtimehud extends Plugin implements Listener {
  27.     public Timer caltimer;
  28.     public Player player;
  29.     String WorldName = World.getName();
  30.    
  31.     @Override
  32.     public void onEnable() {
  33.         this.registerEventListener(this);
  34.         createTimer();
  35.         try (Database timehud = getSQLiteConnection(getPath() + "/"+WorldName+"/timehud.db")){
  36.              timehud.execute("CREATE TABLE IF NOT EXISTS `timehud` (`ID` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `playerid` BIGINT, `isvis` INTEGER)");
  37.              System.out.println("TimeHud.db loaded");
  38.         }
  39.     }
  40.  
  41.     @Override
  42.     public void onDisable() {
  43.        
  44.     }
  45.    
  46.     @EventMethod
  47.     public void playerConnect(PlayerConnectEvent evt) throws SQLException{
  48.         player = evt.getPlayer();
  49.         ui(player);
  50.        
  51.        
  52.     }
  53.     @EventMethod
  54.     public void playerspawn(PlayerSpawnEvent evt){
  55.         player = evt.getPlayer();
  56.         player.sendTextMessage("<color=yellow>Type /Time Help in chat for Time Hud info</color>");
  57.     }
  58.    
  59.     public void vis(Player player) throws SQLException{
  60.         long playerid = Long.parseLong(player.getUID());
  61.         UIElement calpanel = (UIElement)player.getAttribute("calpanel");
  62.         Database timehud = getSQLiteConnection(getPath() + "/"+WorldName+"/timehud.db");
  63.         try(ResultSet result = timehud.executeQuery("SELECT * FROM `timehud` WHERE `playerid` = '"+playerid+"'")){
  64.             if(result.next()==true){
  65.                 int isvis=result.getInt("isvis");
  66.                 if(isvis==0){
  67.                     calpanel.setVisible(true);
  68.                 }if(isvis==1){
  69.                     calpanel.setVisible(false);
  70.                 }
  71.                
  72.             }else if (result.next()==false){
  73.                 timehud.execute("INSERT INTO `timehud`(playerid, isvis)values('"+playerid+"', '"+0+"')");
  74.                 calpanel.setVisible(true);
  75.             }
  76.         }
  77.     }
  78.    
  79.    
  80.     public void createTimer(){
  81.         caltimer = new Timer(5f, 0f, -1, () -> {
  82.             try {
  83.                 update();
  84.             } catch (SQLException ex) {
  85.                 Logger.getLogger(wtgtimehud.class.getName()).log(Level.SEVERE, null, ex);
  86.             }
  87.         });
  88.         caltimer.start();
  89.     }
  90.    
  91.    
  92.     public void ui(Player player) throws SQLException{
  93.         int Minutes = Server.getGameTime(Unit.Minutes);
  94.         int Hours = Server.getGameTime(Unit.Hours);
  95.         int Days = Server.getGameTime(Unit.Days);
  96.         int Months = Server.getGameTime(Unit.Months);
  97.         int Years = Server.getGameTime(Unit.Years);
  98.        
  99.         //main panel
  100.         UIElement calpanel = new UIElement();
  101.         calpanel.setPivot(Pivot.UpperLeft);
  102.         calpanel.setPosition(1, 40, true);
  103.         calpanel.setSize(150, 60, false);
  104.         calpanel.setBackgroundColor(0, 0, 0, 0.7f);
  105.         calpanel.setBorder(3);
  106.         calpanel.setBorderColor(255);
  107.         calpanel.setBorderEdgeRadius(5, true);
  108.         //calpanel.setVisible(true);
  109.         player.addUIElement(calpanel);
  110.         //years
  111.         UILabel years = new UILabel();
  112.         years.setSize(100, 5, true);
  113.         years.setPosition(50, 14, true);
  114.         years.setText("<color=white>Year: "+String.valueOf(Years)+"</color>");
  115.         years.setFontSize(15);
  116.         years.setPivot(Pivot.MiddleCenter);
  117.         years.setVisible(true);
  118.         calpanel.addChild(years);
  119.         player.setAttribute("years", years);
  120.         //months
  121.         UILabel months = new UILabel();
  122.         months.setSize(50, 5, true);
  123.         months.setPosition(5, 48, true);
  124.         months.setText("<color=white>Month: "+String.valueOf(Months)+"</color>");
  125.         months.setFontSize(15);
  126.         months.setPivot(Pivot.MiddleLeft);
  127.         months.setVisible(true);
  128.         calpanel.addChild(months);
  129.         player.setAttribute("months", months);
  130.         //days
  131.         UILabel days = new UILabel();
  132.         days.setSize(50, 5, true);
  133.         days.setPosition(60, 48, true);
  134.         days.setText("<color=white>Day: "+String.valueOf(Days)+"</color>");
  135.         days.setFontSize(15);
  136.         days.setPivot(Pivot.MiddleLeft);
  137.         days.setVisible(true);
  138.         calpanel.addChild(days);
  139.         player.setAttribute("days", days);
  140.         //current time
  141.         UILabel time = new UILabel();
  142.         time.setSize(50, 5, true);
  143.         time.setPosition(50, 82, true);
  144.         time.setText("<color=white> Time "+Hours+" : "+Minutes+"</color>");
  145.         time.setFontSize(15);
  146.         time.setPivot(Pivot.MiddleCenter);
  147.         time.setVisible(true);
  148.         calpanel.addChild(time);
  149.         player.setAttribute("time", time);
  150.         player.setAttribute("calpanel", calpanel);
  151.         vis(player);
  152.         update();
  153.     }
  154.    
  155.     public void update() throws SQLException{
  156.         String minutetext;
  157.         String hourstext;
  158.         int Minutes = Server.getGameTime(Unit.Minutes);
  159.         int Hours = Server.getGameTime(Unit.Hours)+1;
  160.         int Days = Server.getGameTime(Unit.Days);
  161.         int Months = Server.getGameTime(Unit.Months);
  162.         int Years = Server.getGameTime(Unit.Years);
  163.         if(getPlayerCount()>0){
  164.             if(Minutes<=9){
  165.                 minutetext= "0"+String.valueOf(Minutes);
  166.             }else{
  167.                 minutetext= String.valueOf(Minutes);
  168.             }if(Hours<=9){
  169.                 hourstext = "0"+String.valueOf(Hours);
  170.             }else{
  171.                 hourstext = String.valueOf(Hours);
  172.             }  
  173.         for(Player player : Server.getAllPlayers()){
  174.             //System.out.println("players online updateing ui");
  175.         UILabel years = (UILabel)player.getAttribute("years");
  176.         years.setText("<color=white>Year: "+String.valueOf(Years)+"</color>");
  177.         UILabel months = (UILabel)player.getAttribute("months");
  178.         months.setText("<color=white>Month: "+String.valueOf(Months)+"</color>");
  179.         UILabel days = (UILabel)player.getAttribute("days");
  180.         days.setText("<color=white>Day: "+String.valueOf(Days)+"</color>");
  181.         UILabel time = (UILabel)player.getAttribute("time");
  182.         time.setText("<color=white> Time "+hourstext+" : "+minutetext+"</color>");
  183.         }
  184.         }else{
  185.            return;
  186.         }
  187.        
  188.     }
  189.    
  190.    
  191.    @EventMethod
  192.     public void onCommand(PlayerCommandEvent command) {
  193.         int isvis;
  194.         Player player = command.getPlayer();
  195.         long playerid = Long.parseLong(player.getUID());
  196.        
  197.         UIElement calpanel = (UIElement)player.getAttribute("calpanel");
  198.         String[] cmd = command.getCommand().split(" ");
  199.         if (cmd[0].equalsIgnoreCase("/Time")){
  200.             if(cmd.length<2)return;
  201.             Database timehud = getSQLiteConnection(getPath() + "/"+WorldName+"/timehud.db");
  202.             if(cmd[1].equalsIgnoreCase("Show")){
  203.                 calpanel.setVisible(true);
  204.                 isvis=0;
  205.                 timehud.executeUpdate("UPDATE `timehud` SET isvis = '"+isvis+"'where playerid = '"+playerid+"'");
  206.             }if(cmd[1].equalsIgnoreCase("Hide")){
  207.                 calpanel.setVisible(false);
  208.                 isvis = 1;
  209.                 timehud.executeUpdate("UPDATE `timehud` SET isvis = '"+isvis+"'where playerid = '"+playerid+"'");
  210.             }if(cmd[1].equalsIgnoreCase("Help")){
  211.                 player.sendTextMessage("<color=yellow>/Time Hide to hide hud</color>");
  212.                 player.sendTextMessage("<color=yellow>/Time Show to show hud</color>");
  213.             }
  214.         }
  215.     }  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement