Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeName0 committed Jul 25, 2024
2 parents edb74e6 + 564b13f commit ce2ddfd
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 41 deletions.
13 changes: 13 additions & 0 deletions client/drops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ end

-- Events

RegisterNetEvent('qb-inventory:client:removeDropTarget', function(dropId)
while not NetworkDoesNetworkIdExist(dropId) do Wait(10) end
local bag = NetworkGetEntityFromNetworkId(dropId)
while not DoesEntityExist(bag) do Wait(10) end
exports['qb-target']:RemoveTargetEntity(bag)
end)

RegisterNetEvent('qb-inventory:client:setupDropTarget', function(dropId)
while not NetworkDoesNetworkIdExist(dropId) do Wait(10) end
local bag = NetworkGetEntityFromNetworkId(dropId)
Expand All @@ -51,6 +58,12 @@ RegisterNetEvent('qb-inventory:client:setupDropTarget', function(dropId)
icon = 'fas fa-hand-pointer',
label = 'Pick up bag',
action = function()
if IsPedArmed(PlayerPedId(), 4) then
return QBCore.Functions.Notify("You can not be holding a Gun and a Bag!", "error", 5500)
end
if holdingDrop then
return QBCore.Functions.Notify("Your already holding a bag, Go Drop it!", "error", 5500)
end
AttachEntityToEntity(
bag,
PlayerPedId(),
Expand Down
7 changes: 6 additions & 1 deletion client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ RegisterNUICallback('GiveItem', function(data, cb)
local playerId = GetPlayerServerId(player)
QBCore.Functions.TriggerCallback('qb-inventory:server:giveItem', function(success)
cb(success)
end, playerId, data.item.name, data.amount)
end, playerId, data.item.name, data.amount, data.slot, data.info)
else
QBCore.Functions.Notify(Lang:t('notify.nonb'), 'error')
cb(false)
Expand Down Expand Up @@ -300,6 +300,11 @@ for i = 1, 5 do
RegisterCommand('slot_' .. i, function()
local itemData = PlayerData.items[i]
if not itemData then return end
if itemData.type == "weapon" then
if holdingDrop then
return QBCore.Functions.Notify("Your already holding a bag, Go Drop it!", "error", 5500)
end
end
TriggerServerEvent('qb-inventory:server:useItem', itemData)
end, false)
RegisterKeyMapping('slot_' .. i, Lang:t('inf_mapping.use_item') .. i, 'keyboard', i)
Expand Down
40 changes: 38 additions & 2 deletions config/vehicles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ VehicleStorage = {
gloveboxSlots = 5,
gloveboxWeight = 10000,
trunkSlots = 50,
maxWeight = 75000
trunkWeight = 75000
},
[3] = { -- Coupes
gloveboxSlots = 5,
gloveboxWeight = 10000,
trunkSlots = 35,
maxWeight = 42000
trunkWeight = 42000
},
[4] = { -- Muscle
gloveboxSlots = 5,
Expand Down Expand Up @@ -95,6 +95,42 @@ VehicleStorage = {
trunkSlots = 50,
trunkWeight = 120000
},
[17] = { -- service
gloveboxSlots = 0,
gloveboxWeight = 0,
trunkSlots = 0,
trunkWeight = 0
},
[18] = { -- Emergency
gloveboxSlots = 4,
gloveboxWeight = 10000,
trunkSlots = 12,
trunkWeight = 150000
},
[19] = { -- Military
gloveboxSlots = 0,
gloveboxWeight = 0,
trunkSlots = 0,
trunkWeight = 0
},
[20] = { -- Commercial
gloveboxSlots = 0,
gloveboxWeight = 0,
trunkSlots = 0,
trunkWeight = 0
},
[21] = { -- trains
gloveboxSlots = 0,
gloveboxWeight = 0,
trunkSlots = 0,
trunkWeight = 0
},
[22] = { -- Commercial
gloveboxSlots = 0,
gloveboxWeight = 0,
trunkSlots = 0,
trunkWeight = 0
},
}

BackEngineVehicles = {
Expand Down
84 changes: 64 additions & 20 deletions html/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ const InventoryContainer = Vue.createApp({
otherInventory: {},
otherInventoryName: "",
otherInventoryLabel: "Drop",
otherInventoryTotalWeight: 0,
otherInventoryMaxWeight: 1000000,
otherInventorySlots: 100,
isShopInventory: false,
Expand Down Expand Up @@ -210,7 +209,13 @@ const InventoryContainer = Vue.createApp({
getHotbarItemInSlot(slot) {
return this.hotbarItems[slot - 1] || null;
},
containerMouseDownAction(event) {
if (event.button === 0 && this.showContextMenu) {
this.showContextMenu = false;
}
},
handleMouseDown(event, slot, inventory) {
if (event.button === 1) return; // skip middle mouse
event.preventDefault();
const itemInSlot = this.getItemInSlot(slot, inventory);
if (event.button === 0) {
Expand All @@ -234,42 +239,58 @@ const InventoryContainer = Vue.createApp({
moveItemBetweenInventories(item, sourceInventoryType) {
const sourceInventory = sourceInventoryType === "player" ? this.playerInventory : this.otherInventory;
const targetInventory = sourceInventoryType === "player" ? this.otherInventory : this.playerInventory;
const targetItemKey = Object.keys(targetInventory).find((key) => targetInventory[key] && targetInventory[key].name === item.name);
const targetItem = targetInventory[targetItemKey];
const amountToTransfer = this.transferAmount !== null ? this.transferAmount : 1;
let targetSlot = null;

const sourceItem = sourceInventory[item.slot];
if (!sourceItem || sourceItem.amount < amountToTransfer) {
console.error("Error: Insufficient amount of item in source inventory");
this.inventoryError(item.slot);
return;
}

if (!targetItem) {
const totalWeightAfterTransfer = this.otherInventoryWeight + sourceItem.weight * amountToTransfer;
if (totalWeightAfterTransfer > this.otherInventoryMaxWeight) {
this.inventoryError(item.slot);
return;
}

if (item.unique) {
targetSlot = this.findNextAvailableSlot(targetInventory);
if (targetSlot === null) {
this.inventoryError(item.slot);
return;
}

const newItem = {
...item,
inventory: sourceInventoryType === "player" ? "other" : "player",
amount: amountToTransfer,
};
targetInventory[targetSlot] = newItem;
newItem.slot = targetSlot;
} else {
const targetItemKey = Object.keys(targetInventory).find((key) => targetInventory[key] && targetInventory[key].name === item.name);
const targetItem = targetInventory[targetItemKey];

if (!targetItem) {
const newItem = {
...item,
inventory: sourceInventoryType === "player" ? "other" : "player",
amount: amountToTransfer,
};

let added = false;
for (let slot = 1; slot <= this.totalSlots; slot++) {
if (!targetInventory[slot]) {
targetInventory[slot] = newItem;
newItem.slot = slot;
targetSlot = slot;
added = true;
break;
targetSlot = this.findNextAvailableSlot(targetInventory);
if (targetSlot === null) {
this.inventoryError(item.slot);
return;
}
}

if (!added) {
console.error("Error: No free slots available in target inventory");
return;
targetInventory[targetSlot] = newItem;
newItem.slot = targetSlot;
} else {
targetItem.amount += amountToTransfer;
targetSlot = targetItem.slot;
}
} else {
targetItem.amount += amountToTransfer;
targetSlot = targetItem.slot;
}

sourceItem.amount -= amountToTransfer;
Expand Down Expand Up @@ -298,6 +319,7 @@ const InventoryContainer = Vue.createApp({
this.dragStartX = event.clientX;
this.dragStartY = event.clientY;
this.dragStartInventoryType = inventoryType;
this.showContextMenu = false;
},
createGhostElement(slotElement) {
const ghostElement = slotElement.cloneNode(true);
Expand Down Expand Up @@ -403,6 +425,11 @@ const InventoryContainer = Vue.createApp({
},
handleItemDrop(targetInventoryType, targetSlot) {
try {
const isShop = this.otherInventoryName.indexOf("shop-");
if (this.dragStartInventoryType === "other" && targetInventoryType === "other" && isShop !== -1) {
return;
}

const targetSlotNumber = parseInt(targetSlot, 10);
if (isNaN(targetSlotNumber)) {
throw new Error("Invalid target slot number");
Expand All @@ -420,6 +447,21 @@ const InventoryContainer = Vue.createApp({
if (sourceItem.amount < amountToTransfer) {
throw new Error("Insufficient amount of item in source inventory");
}

if (targetInventoryType == "other")
{
const totalWeightAfterTransfer = this.otherInventoryWeight + sourceItem.weight * amountToTransfer;
if (totalWeightAfterTransfer > this.otherInventoryMaxWeight) {
throw new Error("Insufficient weight capacity in target inventory");
}
}
else if (targetInventoryType == "player")
{
const totalWeightAfterTransfer = this.playerWeight + sourceItem.weight * amountToTransfer;
if (totalWeightAfterTransfer > this.maxWeight) {
throw new Error("Insufficient weight capacity in player inventory");
}
}

const targetItem = targetInventory[targetSlotNumber];

Expand Down Expand Up @@ -659,6 +701,8 @@ const InventoryContainer = Vue.createApp({
await axios.post("https://qb-inventory/GiveItem", {
item: selectedItem,
amount: amountToGive,
slot: selectedItem.slot,
info: selectedItem.info,
});
this.playerInventory[selectedItem.slot].amount -= amountToGive;
if (this.playerInventory[selectedItem.slot].amount === 0) {
Expand Down
8 changes: 4 additions & 4 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</head>
<body>
<div id="app">
<div class="inventory-container" v-if="isInventoryOpen" @mousemove="drag" @mouseup="endDrag">
<div class="inventory-container" v-if="isInventoryOpen" @mousemove="drag" @mouseup="endDrag" @mousedown="containerMouseDownAction">
<div class="player-inventory-header" :class="{ 'centered-inventory-header': shouldCenterInventory }">
<div class="labels-container">
<div class="inventory-label">
Expand All @@ -27,7 +27,7 @@
</div>
</div>
<div class="weight-bar">
<div class="weight-bar-fill" :style="{ width: (playerWeight / maxWeight) * 100 + '%' }" :class="weightBarClass"></div>
<div class="weight-bar-fill" :style="{ width: Math.min((playerWeight / maxWeight) * 100, 100) + '%' }" :class="weightBarClass"></div>
</div>
</div>
<div class="player-inventory" :class="{ 'centered-player-inventory': shouldCenterInventory }">
Expand Down Expand Up @@ -79,7 +79,7 @@
</div>
</div>
<div class="weight-bar">
<div class="weight-bar-fill" :style="{ width: (otherInventoryWeight / otherInventoryMaxWeight) * 100 + '%' }" :class="otherWeightBarClass"></div>
<div class="weight-bar-fill" :style="{ width: Math.min((otherInventoryWeight / otherInventoryMaxWeight) * 100, 100) + '%' }" :class="otherWeightBarClass"></div>
</div>
</div>
<div class="other-inventory" v-if="!isOtherInventoryEmpty">
Expand Down Expand Up @@ -191,7 +191,7 @@
</div>
</div>
<ul v-if="showContextMenu" class="context-menu" :style="{ top: contextMenuPosition.top, left: contextMenuPosition.left }">
<li @click="useItem(contextMenuItem)">Use</li>
<li v-if="contextMenuItem.useable" @click="useItem(contextMenuItem)">Use</li>
<li @mouseover="showSubmenu = true" @mouseleave="showSubmenu = false">
Give
<ul v-if="showSubmenu" class="submenu">
Expand Down
10 changes: 7 additions & 3 deletions html/main.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
@import url("https://fonts.googleapis.com/css2?family=Open+Sans&display=swap");

body {
margin: 0;
}

div {
font-family: "Open Sans", sans-serif;
}
Expand All @@ -9,8 +13,8 @@ div {
}

#app {
height: 99vh;
width: 99vw;
height: 100vh;
width: 100vw;
background: transparent;
}

Expand Down Expand Up @@ -195,7 +199,7 @@ div {
white-space: nowrap;
bottom: 0;
height: 1vh;
width: 99%;
width: 100%;
background-color: #cccccc;
display: flex;
justify-content: center;
Expand Down
2 changes: 0 additions & 2 deletions server/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ QBCore.Commands.Add('giveitem', 'Give An Item (Admin Only)', { { name = 'id', he
info.uses = 20
elseif itemData['name'] == 'markedbills' then
info.worth = math.random(5000, 10000)
elseif itemData['name'] == 'labkey' then
info.lab = exports['qb-methlab']:GenerateRandomLab()
elseif itemData['name'] == 'printerdocument' then
info.url = 'https://cdn.discordapp.com/attachments/870094209783308299/870104331142189126/Logo_-_Display_Picture_-_Stylized_-_Red.png'
end
Expand Down
15 changes: 15 additions & 0 deletions server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,21 @@ end

exports('CanAddItem', CanAddItem)

--- Gets the total free weight of the player's inventory.
--- @param source number The player's server ID.
--- @return number - Returns the free weight of the players inventory. Error will return 0
function GetFreeWeight(source)
if not source then warn("Source was not passed into GetFreeWeight") return 0 end
local Player = QBCore.Functions.GetPlayer(source)
if not Player then return 0 end

local totalWeight = GetTotalWeight(Player.PlayerData.items)
local freeWeight = Config.MaxWeight - totalWeight
return freeWeight
end

exports('GetFreeWeight', GetFreeWeight)

function ClearInventory(source, filterItems)
local player = QBCore.Functions.GetPlayer(source)
local savedItemData = {}
Expand Down
Loading

0 comments on commit ce2ddfd

Please sign in to comment.