Advertisement
james1bow

Untitled

Nov 17th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.01 KB | None | 0 0
  1. package itemmods.ui;
  2.  
  3. import net.risingworld.api.Timer;
  4. import net.risingworld.api.assets.TextureAsset;
  5. import net.risingworld.api.definitions.Items;
  6. import net.risingworld.api.objects.Item;
  7. import net.risingworld.api.objects.Player;
  8. import net.risingworld.api.ui.UIElement;
  9. import net.risingworld.api.ui.UITarget;
  10. import net.risingworld.api.ui.style.Pivot;
  11.  
  12. public class CraftingWidget {
  13.    
  14.    
  15.     //private UIElement overlay;
  16.     //private UIElement itemCardBackground;
  17.     //private UIElement progressBarBackground;
  18.     //private UIElement progressBarFill;
  19.     //private Player player;
  20.     //private Item craftedItem;
  21.     //private Timer craftingTimer;
  22.     //private Timer progressTimer = null;
  23.     //private Timer itemCardTimer;
  24.    
  25.    
  26.    
  27.     public void onCraftItem(Player player, Item craftedItem) {
  28.        
  29.        
  30.  
  31.         // Create a timer for the delay since this isn't the main class
  32.         Timer craftingTimer = new Timer(2.0f, 0.0f, 1, () -> {
  33.             completeCrafting(player);
  34.             //craftingTimer.kill(); // Ensure the timer stops after execution
  35.         });
  36.         craftingTimer.start();
  37.         //create ui method
  38.         displayOverlay(player, craftedItem, craftingTimer);
  39.     }
  40.    
  41.     private void displayOverlay(Player player, Item craftedItem, Timer craftingTimer) {
  42.         // ********************* Overlay UI ********************* //
  43.         UIElement overlay = new UIElement();
  44.         overlay.setBackgroundColor(0.0f, 0.0f, 0.0f, 0.9f);
  45.         overlay.setPosition(50.0f, 50.0f, true);
  46.         overlay.setSize(100.0f, 100.0f, true);
  47.         overlay.setPivot(Pivot.MiddleCenter);
  48.         player.setAttribute("Overlay", overlay);
  49.        
  50.         // Transparent container
  51.         UIElement container = new UIElement();
  52.         container.setSize(15.0f, 30.0f, true);
  53.         container.setPosition(50.0f, 50.0f, true);
  54.         container.setBorder(2);
  55.         container.setBorderColor(0.2f, 0.2f, 0.2f, 1);
  56.         container.setPivot(Pivot.MiddleCenter);
  57.         overlay.addChild(container);
  58.        
  59.         // ****************** Progress Bar UI ******************* //
  60.         // Create the progress bar background
  61.         UIElement progressBarBackground = new UIElement();
  62.         progressBarBackground.setVisible(true);
  63.         progressBarBackground.setBackgroundColor(0.02f, 0.02f, 0.02f, 1.0f);
  64.         progressBarBackground.setSize(90.0f, 5.0f, true);
  65.         progressBarBackground.setPosition(50.0f, 50.0f, true);
  66.         progressBarBackground.setBorder(2);
  67.         progressBarBackground.setBorderColor(0.2f, 0.2f, 0.2f, 1.0f);
  68.         progressBarBackground.setBorderEdgeRadius(10, false);
  69.         progressBarBackground.setPivot(Pivot.MiddleCenter);
  70.         container.addChild(progressBarBackground);
  71.         player.setAttribute("ProgressBarBackground", progressBarBackground);
  72.  
  73.         // Progress bar fill
  74.         UIElement progressBarFill = new UIElement();
  75.         progressBarFill.setVisible(true);
  76.         progressBarFill.setSize(0.0f, 90.0f, true); // Start with 0 width
  77.         progressBarFill.setPosition(0.5f, 45.0f, true); // Relative to the background
  78.         progressBarFill.setBackgroundColor(0.0f, 0.8f, 0.0f, 1.0f);
  79.         progressBarFill.setBorderEdgeRadius(10, false);
  80.         progressBarFill.setPivot(Pivot.MiddleLeft); // Anchor to the left for width expansion
  81.         progressBarBackground.addChild(progressBarFill);
  82.         player.setAttribute("ProgressBarFill", progressBarFill);
  83.  
  84.         // Add overlay to the Crafting Menu
  85.         if (player != null) {
  86.             player.addUIElement(overlay, UITarget.Crafting);
  87.         } else {
  88.             System.out.println("Player reference is null, cannot add overlay!");
  89.         }
  90.        
  91.         // Update the progress bar during crafting
  92.         float craftingDuration = 2.0f; // Crafting time in seconds
  93.         Timer progressTimer = new Timer(0.1f, 0.0f, -1, () -> {
  94.             float elapsed = craftingTimer.getPassedSeconds();
  95.             float progress = Math.min(elapsed / craftingDuration, 1.0f); // Clamp to [0, 1]
  96.             progressBarFill.setSize(progress * 100, 90.0f, true); // Update progress bar width
  97.         });
  98.         progressTimer.start();
  99.         player.setAttribute("ProgressTimer", progressTimer);
  100.        
  101.         // ****************** Crafted Item UI ******************* //
  102.         // Create the item card background
  103.         UIElement itemCardBackground = new UIElement();
  104.         itemCardBackground.setVisible(false);
  105.         itemCardBackground.setBackgroundColor(0.02f, 0.02f, 0.02f, 1.0f);
  106.         itemCardBackground.setSize(60.0f, 75.0f, true);
  107.         itemCardBackground.setPosition(50.0f, 50.0f, true);
  108.         itemCardBackground.setBorder(1);
  109.         itemCardBackground.setBorderColor(1.0f, 84.3f, 0.0f, 1.0f);
  110.         itemCardBackground.setBorderEdgeRadius(10, false);
  111.         itemCardBackground.setPivot(Pivot.MiddleCenter);
  112.         container.addChild(itemCardBackground);
  113.         player.setAttribute("ItemCardBackground", itemCardBackground);
  114.        
  115.         UIElement itemCardIcon = new UIElement();
  116.         itemCardIcon.setBackgroundColor(0.02f, 0.02f, 0.02f, 1.0f);
  117.         itemCardIcon.setSize(50.0f, 30.0f, true);
  118.         itemCardIcon.setPosition(50.0f, 5.0f, true);
  119.         itemCardIcon.setBorder(1);
  120.         itemCardIcon.setBorderColor(1.0f, 84.3f, 0.0f, 1.0f);
  121.         itemCardIcon.setBorderEdgeRadius(10, false);
  122.         itemCardIcon.setPivot(Pivot.UpperCenter);
  123.         itemCardBackground.addChild(itemCardIcon);
  124.        
  125.        
  126.         UIElement itemCardBuffContainer = new UIElement();
  127.         itemCardBuffContainer.setBackgroundColor(0.02f, 0.02f, 0.02f, 1.0f);
  128.         itemCardBuffContainer.setSize(80.0f, 55.0f, true);
  129.         itemCardBuffContainer.setPosition(50.0f, 95.0f, true);
  130.         itemCardBuffContainer.setBorder(1);
  131.         itemCardBuffContainer.setBorderColor(1.0f, 84.3f, 0.0f, 1.0f);
  132.         itemCardBuffContainer.setBorderEdgeRadius(10, false);
  133.         itemCardBuffContainer.setPivot(Pivot.LowerCenter);
  134.         itemCardBackground.addChild(itemCardBuffContainer);
  135.        
  136.         // Retrieve the icon during the crafting timer (not working yet)
  137.         if (craftedItem != null) {
  138.             Items.ItemDefinition definition = craftedItem.getDefinition();
  139.             if (definition != null) {
  140.                 TextureAsset iconTexture = definition.getIcon(craftedItem.getVariant());
  141.                 itemCardIcon.style.backgroundImage.set(iconTexture);
  142.             }    
  143.         }
  144.     }
  145.    
  146.     private void completeCrafting(Player player) {
  147.         //get timer as player attribure
  148.         Timer progressTimer = (Timer)player.getAttribute("ProgressTimer");
  149.         UIElement itemCardBackground = (UIElement)player.getAttribute("ItemCardBackground");
  150.         UIElement progressBarBackground = (UIElement)player.getAttribute("ProgressBarBackground");
  151.         UIElement progressBarFill = (UIElement)player.getAttribute("ProgressBarFill");
  152.         UIElement overlay = (UIElement)player.getAttribute("Overlay");
  153.        
  154.         // Stop the progress timer and give crafted item (Not setup yet)
  155.         if (progressTimer != null) {
  156.             progressTimer.kill();
  157.            
  158.         }
  159.        
  160.         itemCardBackground.setVisible(true);
  161.         progressBarBackground.setVisible(false);
  162.         progressBarFill.setVisible(false);
  163.         Timer itemCardTimer = new Timer(3.0f, 0.0f, 1, () -> {
  164.             itemCardBackground.setVisible(false);
  165.             closeOverlay(overlay, player);
  166.            
  167.         });
  168.         itemCardTimer.start();
  169.         player.setAttribute("ItemCardTimer", itemCardTimer);
  170.     }
  171.    
  172.     private void closeOverlay(UIElement overlay, Player player) {
  173.        
  174.         Timer itemCardTimer = (Timer)player.getAttribute("ItemCardTimer");
  175.         itemCardTimer.kill(); // Ensure the timer stops after execution
  176.        
  177.         // Remove overlay
  178.         if (overlay != null) {
  179.             player.removeUIElement(overlay);
  180.         }
  181.         System.out.println("Crafting complete! Item received.");
  182.     }
  183. }
  184.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement