Xogos

inventory.js

Jan 5th, 2022
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var type = "normal";
  2. var disabled = false;
  3. var disabledFunction = null;
  4.  
  5. window.addEventListener("message", function (event) {
  6.     if (event.data.action == "display") {
  7.         type = event.data.type;
  8.         disabled = false;
  9.  
  10.         if (type === "normal") {
  11.             $(".info-div").hide();
  12.         } else if (type === "trunk") {
  13.             $(".info-div").show();
  14.         } else if (type === "property") {
  15.             $(".info-div").hide();
  16.         } else if (type === "storage") {
  17.             $(".info-div").hide();
  18.         } else if (type === "player") {
  19.             $(".info-div").show();
  20.         } else if (type === "shop") {
  21.             $(".info-div").show();
  22.         } else if (type === "rcore_hotel") {
  23.             $(".info-div").show();
  24.         }
  25.  
  26.         $(".ui").fadeIn();
  27.     } else if (event.data.action == "hide") {
  28.         $("#dialog").dialog("close");
  29.         $(".ui").fadeOut();
  30.         $(".item").remove();
  31.         $("#otherInventory").html("<div id=\"noSecondInventoryMessage\"></div>");
  32.         $("#noSecondInventoryMessage").html(invLocale.secondInventoryNotAvailable);
  33.     } else if (event.data.action == "setType") {
  34.         type = event.data.type;
  35.     } else if (event.data.action == "setItems") {
  36.         inventorySetup(event.data.itemList);
  37.  
  38.         $('.item').draggable({
  39.             helper: 'clone',
  40.             appendTo: 'body',
  41.             zIndex: 99999,
  42.             revert: 'invalid',
  43.             start: function (event, ui) {
  44.                 if (disabled) {
  45.                     $(this).stop();
  46.                     return;
  47.                 }
  48.                 $(this).css('background-image', 'none');
  49.                 itemData = $(this).data("item");
  50.                 itemInventory = $(this).data("inventory");
  51.  
  52.                 if (itemInventory == "second" || !itemData.canRemove) {
  53.                     $("#drop").addClass("disabled");
  54.                     $("#give").addClass("disabled");
  55.                 }
  56.  
  57.                 if (itemInventory == "second" || !itemData.usable) {
  58.                     $("#use").addClass("disabled");
  59.                 }
  60.             },
  61.             stop: function () {
  62.                 itemData = $(this).data("item");
  63.  
  64.                 if (itemData !== undefined && itemData.name !== undefined) {
  65.                     $(this).css('background-image', 'url(\'img/items/' + itemData.name + '.png\'');
  66.                     $("#drop").removeClass("disabled");
  67.                     $("#use").removeClass("disabled");
  68.                     $("#give").removeClass("disabled");
  69.                 }
  70.             }
  71.         });
  72.     } else if (event.data.action == "setSecondInventoryItems") {
  73.         secondInventorySetup(event.data.itemList);
  74.     } else if (event.data.action == "setInfoText") {
  75.         $(".info-div").html(event.data.text);
  76.     } else if (event.data.action == "nearPlayers") {
  77.         $("#nearPlayers").html("");
  78.  
  79.         $.each(event.data.players, function (index, player) {
  80.             $("#nearPlayers").append('<button class="nearbyPlayerButton" data-player="' + player.player + '">ID ' + player.player + '</button>');
  81.         });
  82.  
  83.         $("#dialog").dialog("open");
  84.  
  85.         $(".nearbyPlayerButton").click(function () {
  86.             $("#dialog").dialog("close");
  87.             player = $(this).data("player");
  88.             $.post("http://esx_inventoryhud/GiveItem", JSON.stringify({
  89.                 player: player,
  90.                 item: event.data.item,
  91.                 number: parseInt($("#count").val())
  92.             }));
  93.         });
  94.     }
  95. });
  96.  
  97. function closeInventory() {
  98.     $.post("http://esx_inventoryhud/NUIFocusOff", JSON.stringify({
  99.         type: type
  100.     }));
  101. }
  102.  
  103. function inventorySetup(items) {
  104.     $("#playerInventory").html("");
  105.     $.each(items, function (index, item) {
  106.         count = setCount(item, false);
  107.  
  108.         var bgColor = "none";
  109.         if (item.rare !== undefined) {
  110.             if (item.rare == 1) {
  111.                 bgColor = "rgba(205, 127, 50, 0.4)";
  112.             } else if (item.rare == 2) {
  113.                 bgColor = "rgba(192, 192, 192, 0.4)";
  114.             } else if (item.rare == 3) {
  115.                 bgColor = "rgba(218, 165, 32, 0.4)";
  116.             }
  117.         }
  118.  
  119.         $("#playerInventory").append('<div class="slot" style="background-color: ' + bgColor + ';"><div id="item-' + index + '" class="item" style = "background-image: url(\'img/items/' + item.name + '.png\')">' +
  120.             '<div class="item-count">' + count + '</div> <div class="item-name">' + item.label + '</div> </div ><div class="item-name-bg"></div></div>');
  121.         $('#item-' + index).data('item', item);
  122.         $('#item-' + index).data('inventory', "main");
  123.     });
  124. }
  125.  
  126. function secondInventorySetup(items) {
  127.     $("#otherInventory").html("");
  128.     $.each(items, function (index, item) {
  129.         count = setCount(item, true);
  130.  
  131.         var bgColor = "none";
  132.         if (item.rare !== undefined) {
  133.             if (item.rare == 1) {
  134.                 bgColor = "rgba(205, 127, 50, 0.4)";
  135.             } else if (item.rare == 2) {
  136.                 bgColor = "rgba(192, 192, 192, 0.4)";
  137.             } else if (item.rare == 3) {
  138.                 bgColor = "rgba(218, 165, 32, 0.4)";
  139.             }
  140.         }
  141.         $("#otherInventory").append('<div class="slot" style="background-color: ' + bgColor + ';"><div id="itemOther-' + index + '" class="item" style = "background-image: url(\'img/items/' + item.name + '.png\')">' +
  142.             '<div class="item-count">' + count + '</div> <div class="item-name">' + item.label + '</div> </div ><div class="item-name-bg"></div></div>');
  143.         $('#itemOther-' + index).data('item', item);
  144.         $('#itemOther-' + index).data('inventory', "second");
  145.     });
  146. }
  147.  
  148. function Interval(time) {
  149.     var timer = false;
  150.     this.start = function () {
  151.         if (this.isRunning()) {
  152.             clearInterval(timer);
  153.             timer = false;
  154.         }
  155.  
  156.         timer = setInterval(function () {
  157.             disabled = false;
  158.         }, time);
  159.     };
  160.     this.stop = function () {
  161.         clearInterval(timer);
  162.         timer = false;
  163.     };
  164.     this.isRunning = function () {
  165.         return timer !== false;
  166.     };
  167. }
  168.  
  169. function disableInventory(ms) {
  170.     disabled = true;
  171.  
  172.     if (disabledFunction === null) {
  173.         disabledFunction = new Interval(ms);
  174.         disabledFunction.start();
  175.     } else {
  176.         if (disabledFunction.isRunning()) {
  177.             disabledFunction.stop();
  178.         }
  179.  
  180.         disabledFunction.start();
  181.     }
  182. }
  183.  
  184. function setCount(item, second) {
  185.     if (second && type === "shop") {
  186.         return "$" + formatMoney(item.price);
  187.     }
  188.  
  189.     count = item.count
  190.  
  191.     if (item.limit > 0) {
  192.         count = item.count + " / " + item.limit;
  193.     }
  194.  
  195.  
  196.     if (item.type === "item_weapon") {
  197.         if (count == 0) {
  198.             count = "";
  199.         } else {
  200.             count = '<img src="img/bullet.png" class="ammoIcon"> ' + item.count;
  201.         }
  202.     }
  203.  
  204.     if (item.type === "item_account" || item.type === "item_money") {
  205.         count = formatMoney(item.count);
  206.     }
  207.  
  208.     return count;
  209. }
  210.  
  211. function formatMoney(n, c, d, t) {
  212.     var c = isNaN(c = Math.abs(c)) ? 2 : c,
  213.         d = d == undefined ? "." : d,
  214.         t = t == undefined ? "," : t,
  215.         s = n < 0 ? "-" : "",
  216.         i = String(parseInt(n = Math.abs(Number(n) || 0).toFixed(c))),
  217.         j = (j = i.length) > 3 ? j % 3 : 0;
  218.  
  219.     return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t);
  220. };
  221.  
  222. $(document).ready(function () {
  223.     $("#count").focus(function () {
  224.         $(this).val("")
  225.     }).blur(function () {
  226.         if ($(this).val() == "") {
  227.             $(this).val("1")
  228.         }
  229.     });
  230.  
  231.     $("body").on("keyup", function (key) {
  232.         if (Config.closeKeys.includes(key.which)) {
  233.             closeInventory();
  234.         }
  235.     });
  236.  
  237.     $('#use').droppable({
  238.         hoverClass: 'hoverControl',
  239.         drop: function (event, ui) {
  240.             itemData = ui.draggable.data("item");
  241.  
  242.             if (itemData == undefined || itemData.usable == undefined) {
  243.                 return;
  244.             }
  245.  
  246.             itemInventory = ui.draggable.data("inventory");
  247.  
  248.             if (itemInventory == undefined || itemInventory == "second") {
  249.                 return;
  250.             }
  251.  
  252.             if (itemData.usable) {
  253.                 disableInventory(300);
  254.                 $.post("http://esx_inventoryhud/UseItem", JSON.stringify({
  255.                     item: itemData
  256.                 }));
  257.             }
  258.         }
  259.     });
  260.  
  261.     $('#playerInventory').on('dblclick', '.item', function () {
  262.         itemData = $(this).data("item");
  263.  
  264.         if (itemData == undefined || itemData.usable == undefined) {
  265.             return;
  266.         }
  267.  
  268.         itemInventory = $(this).data("inventory");
  269.  
  270.         if (itemInventory == undefined || itemInventory == "second") {
  271.             return;
  272.         }
  273.  
  274.         if (itemData.usable) {
  275.             disableInventory(300);
  276.             $.post("http://esx_inventoryhud/UseItem", JSON.stringify({
  277.                 item: itemData
  278.             }));
  279.         }
  280.     });
  281.  
  282.     $('#give').droppable({
  283.         hoverClass: 'hoverControl',
  284.         drop: function (event, ui) {
  285.             itemData = ui.draggable.data("item");
  286.  
  287.             if (itemData == undefined || itemData.canRemove == undefined) {
  288.                 return;
  289.             }
  290.  
  291.             itemInventory = ui.draggable.data("inventory");
  292.  
  293.             if (itemInventory == undefined || itemInventory == "second") {
  294.                 return;
  295.             }
  296.  
  297.             if (itemData.canRemove) {
  298.                 disableInventory(300);
  299.                 $.post("http://esx_inventoryhud/GetNearPlayers", JSON.stringify({
  300.                     item: itemData
  301.                 }));
  302.             }
  303.         }
  304.     });
  305.  
  306.     $('#drop').droppable({
  307.         hoverClass: 'hoverControl',
  308.         drop: function (event, ui) {
  309.             itemData = ui.draggable.data("item");
  310.  
  311.             if (itemData == undefined || itemData.canRemove == undefined) {
  312.                 return;
  313.             }
  314.  
  315.             itemInventory = ui.draggable.data("inventory");
  316.  
  317.             if (itemInventory == undefined || itemInventory == "second") {
  318.                 return;
  319.             }
  320.  
  321.             if (itemData.canRemove) {
  322.                 disableInventory(300);
  323.                 $.post("http://esx_inventoryhud/DropItem", JSON.stringify({
  324.                     item: itemData,
  325.                     number: parseInt($("#count").val())
  326.                 }));
  327.             }
  328.         }
  329.     });
  330.  
  331.     $('#playerInventory').droppable({
  332.         drop: function (event, ui) {
  333.             itemData = ui.draggable.data("item");
  334.             itemInventory = ui.draggable.data("inventory");
  335.  
  336.             if (type === "trunk" && itemInventory === "second") {
  337.                 disableInventory(500);
  338.                 $.post("http://esx_inventoryhud/TakeFromTrunk", JSON.stringify({
  339.                     item: itemData,
  340.                     number: parseInt($("#count").val())
  341.                 }));
  342.             } else if (type === "shop" && itemInventory === "second") {
  343.                 disableInventory(500);
  344.                 $.post("http://esx_inventoryhud/BuyItem", JSON.stringify({
  345.                     item: itemData,
  346.                     number: parseInt($("#count").val())
  347.                 }));
  348.             } else if (type === "property" && itemInventory === "second") {
  349.                 disableInventory(500);
  350.                 $.post("http://esx_inventoryhud/TakeFromProperty", JSON.stringify({
  351.                     item: itemData,
  352.                     number: parseInt($("#count").val())
  353.                 }));
  354.             } else if (type === "storage" && itemInventory === "second") {
  355.                 disableInventory(500);
  356.                 $.post("http://esx_inventoryhud/TakeFromStorage", JSON.stringify({
  357.                     item: itemData,
  358.                     number: parseInt($("#count").val())
  359.                 }));
  360.             } else if (type === "player" && itemInventory === "second") {
  361.                 disableInventory(500);
  362.                 $.post("http://esx_inventoryhud/TakeFromPlayer", JSON.stringify({
  363.                     item: itemData,
  364.                     number: parseInt($("#count").val())
  365.                 }));
  366.             } else if (type === "rcore_hotel" && itemInventory === "second") {
  367.                 disableInventory(250);
  368.                 $.post("http://esx_inventoryhud/TakeFromRcoreHotelRoom", JSON.stringify({
  369.                     item: itemData,
  370.                     number: parseInt($("#count").val())
  371.                 }));
  372.             }
  373.         }
  374.     });
  375.  
  376.     $('#otherInventory').droppable({
  377.         drop: function (event, ui) {
  378.             itemData = ui.draggable.data("item");
  379.             itemInventory = ui.draggable.data("inventory");
  380.  
  381.             if (type === "trunk" && itemInventory === "main") {
  382.                 disableInventory(500);
  383.                 $.post("http://esx_inventoryhud/PutIntoTrunk", JSON.stringify({
  384.                     item: itemData,
  385.                     number: parseInt($("#count").val())
  386.                 }));
  387.             } else if (type === "property" && itemInventory === "main") {
  388.                 disableInventory(500);
  389.                 $.post("http://esx_inventoryhud/PutIntoProperty", JSON.stringify({
  390.                     item: itemData,
  391.                     number: parseInt($("#count").val())
  392.                 }));
  393.             } else if (type === "storage" && itemInventory === "main") {
  394.                 disableInventory(500);
  395.                 $.post("http://esx_inventoryhud/PutIntoStorage", JSON.stringify({
  396.                     item: itemData,
  397.                     number: parseInt($("#count").val())
  398.                 }));
  399.             } else if (type === "player" && itemInventory === "main") {
  400.                 disableInventory(500);
  401.                 $.post("http://esx_inventoryhud/PutIntoPlayer", JSON.stringify({
  402.                     item: itemData,
  403.                     number: parseInt($("#count").val())
  404.                 }));
  405.             }
  406.         }
  407.     });
  408.  
  409.     $("#count").on("keypress keyup blur", function (event) {
  410.         $(this).val($(this).val().replace(/[^\d].+/, ""));
  411.         if ((event.which < 48 || event.which > 57)) {
  412.             event.preventDefault();
  413.         }
  414.     });
  415. });
  416.  
  417. $.widget('ui.dialog', $.ui.dialog, {
  418.     options: {
  419.         // Determine if clicking outside the dialog shall close it
  420.         clickOutside: false,
  421.         // Element (id or class) that triggers the dialog opening
  422.         clickOutsideTrigger: ''
  423.     },
  424.     open: function () {
  425.         var clickOutsideTriggerEl = $(this.options.clickOutsideTrigger),
  426.             that = this;
  427.         if (this.options.clickOutside) {
  428.             // Add document wide click handler for the current dialog namespace
  429.             $(document).on('click.ui.dialogClickOutside' + that.eventNamespace, function (event) {
  430.                 var $target = $(event.target);
  431.                 if ($target.closest($(clickOutsideTriggerEl)).length === 0 &&
  432.                     $target.closest($(that.uiDialog)).length === 0) {
  433.                     that.close();
  434.                 }
  435.             });
  436.         }
  437.         // Invoke parent open method
  438.         this._super();
  439.     },
  440.     close: function () {
  441.         // Remove document wide click handler for the current dialog
  442.         $(document).off('click.ui.dialogClickOutside' + this.eventNamespace);
  443.         // Invoke parent close method
  444.         this._super();
  445.     },
  446. });
Add Comment
Please, Sign In to add comment