Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package itemmods.ui;
- import net.risingworld.api.Timer;
- import net.risingworld.api.assets.TextureAsset;
- import net.risingworld.api.definitions.Items;
- import net.risingworld.api.objects.Item;
- import net.risingworld.api.objects.Player;
- import net.risingworld.api.ui.UIElement;
- import net.risingworld.api.ui.UITarget;
- import net.risingworld.api.ui.style.Pivot;
- public class CraftingWidget {
- //private UIElement overlay;
- //private UIElement itemCardBackground;
- //private UIElement progressBarBackground;
- //private UIElement progressBarFill;
- //private Player player;
- //private Item craftedItem;
- //private Timer craftingTimer;
- //private Timer progressTimer = null;
- //private Timer itemCardTimer;
- public void onCraftItem(Player player, Item craftedItem) {
- // Create a timer for the delay since this isn't the main class
- Timer craftingTimer = new Timer(2.0f, 0.0f, 1, () -> {
- completeCrafting(player);
- //craftingTimer.kill(); // Ensure the timer stops after execution
- });
- craftingTimer.start();
- //create ui method
- displayOverlay(player, craftedItem, craftingTimer);
- }
- private void displayOverlay(Player player, Item craftedItem, Timer craftingTimer) {
- // ********************* Overlay UI ********************* //
- UIElement overlay = new UIElement();
- overlay.setBackgroundColor(0.0f, 0.0f, 0.0f, 0.9f);
- overlay.setPosition(50.0f, 50.0f, true);
- overlay.setSize(100.0f, 100.0f, true);
- overlay.setPivot(Pivot.MiddleCenter);
- player.setAttribute("Overlay", overlay);
- // Transparent container
- UIElement container = new UIElement();
- container.setSize(15.0f, 30.0f, true);
- container.setPosition(50.0f, 50.0f, true);
- container.setBorder(2);
- container.setBorderColor(0.2f, 0.2f, 0.2f, 1);
- container.setPivot(Pivot.MiddleCenter);
- overlay.addChild(container);
- // ****************** Progress Bar UI ******************* //
- // Create the progress bar background
- UIElement progressBarBackground = new UIElement();
- progressBarBackground.setVisible(true);
- progressBarBackground.setBackgroundColor(0.02f, 0.02f, 0.02f, 1.0f);
- progressBarBackground.setSize(90.0f, 5.0f, true);
- progressBarBackground.setPosition(50.0f, 50.0f, true);
- progressBarBackground.setBorder(2);
- progressBarBackground.setBorderColor(0.2f, 0.2f, 0.2f, 1.0f);
- progressBarBackground.setBorderEdgeRadius(10, false);
- progressBarBackground.setPivot(Pivot.MiddleCenter);
- container.addChild(progressBarBackground);
- player.setAttribute("ProgressBarBackground", progressBarBackground);
- // Progress bar fill
- UIElement progressBarFill = new UIElement();
- progressBarFill.setVisible(true);
- progressBarFill.setSize(0.0f, 90.0f, true); // Start with 0 width
- progressBarFill.setPosition(0.5f, 45.0f, true); // Relative to the background
- progressBarFill.setBackgroundColor(0.0f, 0.8f, 0.0f, 1.0f);
- progressBarFill.setBorderEdgeRadius(10, false);
- progressBarFill.setPivot(Pivot.MiddleLeft); // Anchor to the left for width expansion
- progressBarBackground.addChild(progressBarFill);
- player.setAttribute("ProgressBarFill", progressBarFill);
- // Add overlay to the Crafting Menu
- if (player != null) {
- player.addUIElement(overlay, UITarget.Crafting);
- } else {
- System.out.println("Player reference is null, cannot add overlay!");
- }
- // Update the progress bar during crafting
- float craftingDuration = 2.0f; // Crafting time in seconds
- Timer progressTimer = new Timer(0.1f, 0.0f, -1, () -> {
- float elapsed = craftingTimer.getPassedSeconds();
- float progress = Math.min(elapsed / craftingDuration, 1.0f); // Clamp to [0, 1]
- progressBarFill.setSize(progress * 100, 90.0f, true); // Update progress bar width
- });
- progressTimer.start();
- player.setAttribute("ProgressTimer", progressTimer);
- // ****************** Crafted Item UI ******************* //
- // Create the item card background
- UIElement itemCardBackground = new UIElement();
- itemCardBackground.setVisible(false);
- itemCardBackground.setBackgroundColor(0.02f, 0.02f, 0.02f, 1.0f);
- itemCardBackground.setSize(60.0f, 75.0f, true);
- itemCardBackground.setPosition(50.0f, 50.0f, true);
- itemCardBackground.setBorder(1);
- itemCardBackground.setBorderColor(1.0f, 84.3f, 0.0f, 1.0f);
- itemCardBackground.setBorderEdgeRadius(10, false);
- itemCardBackground.setPivot(Pivot.MiddleCenter);
- container.addChild(itemCardBackground);
- player.setAttribute("ItemCardBackground", itemCardBackground);
- UIElement itemCardIcon = new UIElement();
- itemCardIcon.setBackgroundColor(0.02f, 0.02f, 0.02f, 1.0f);
- itemCardIcon.setSize(50.0f, 30.0f, true);
- itemCardIcon.setPosition(50.0f, 5.0f, true);
- itemCardIcon.setBorder(1);
- itemCardIcon.setBorderColor(1.0f, 84.3f, 0.0f, 1.0f);
- itemCardIcon.setBorderEdgeRadius(10, false);
- itemCardIcon.setPivot(Pivot.UpperCenter);
- itemCardBackground.addChild(itemCardIcon);
- UIElement itemCardBuffContainer = new UIElement();
- itemCardBuffContainer.setBackgroundColor(0.02f, 0.02f, 0.02f, 1.0f);
- itemCardBuffContainer.setSize(80.0f, 55.0f, true);
- itemCardBuffContainer.setPosition(50.0f, 95.0f, true);
- itemCardBuffContainer.setBorder(1);
- itemCardBuffContainer.setBorderColor(1.0f, 84.3f, 0.0f, 1.0f);
- itemCardBuffContainer.setBorderEdgeRadius(10, false);
- itemCardBuffContainer.setPivot(Pivot.LowerCenter);
- itemCardBackground.addChild(itemCardBuffContainer);
- // Retrieve the icon during the crafting timer (not working yet)
- if (craftedItem != null) {
- Items.ItemDefinition definition = craftedItem.getDefinition();
- if (definition != null) {
- TextureAsset iconTexture = definition.getIcon(craftedItem.getVariant());
- itemCardIcon.style.backgroundImage.set(iconTexture);
- }
- }
- }
- private void completeCrafting(Player player) {
- //get timer as player attribure
- Timer progressTimer = (Timer)player.getAttribute("ProgressTimer");
- UIElement itemCardBackground = (UIElement)player.getAttribute("ItemCardBackground");
- UIElement progressBarBackground = (UIElement)player.getAttribute("ProgressBarBackground");
- UIElement progressBarFill = (UIElement)player.getAttribute("ProgressBarFill");
- UIElement overlay = (UIElement)player.getAttribute("Overlay");
- // Stop the progress timer and give crafted item (Not setup yet)
- if (progressTimer != null) {
- progressTimer.kill();
- }
- itemCardBackground.setVisible(true);
- progressBarBackground.setVisible(false);
- progressBarFill.setVisible(false);
- Timer itemCardTimer = new Timer(3.0f, 0.0f, 1, () -> {
- itemCardBackground.setVisible(false);
- closeOverlay(overlay, player);
- });
- itemCardTimer.start();
- player.setAttribute("ItemCardTimer", itemCardTimer);
- }
- private void closeOverlay(UIElement overlay, Player player) {
- Timer itemCardTimer = (Timer)player.getAttribute("ItemCardTimer");
- itemCardTimer.kill(); // Ensure the timer stops after execution
- // Remove overlay
- if (overlay != null) {
- player.removeUIElement(overlay);
- }
- System.out.println("Crafting complete! Item received.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement