From d8f7319bc78e5b0fd6055d8ae37d74c8e374bd7e Mon Sep 17 00:00:00 2001
From: JappaWakkaP3D <jasper.speelman@outlook.com>
Date: Sat, 30 Jan 2021 22:34:02 +0100
Subject: [PATCH] Permanent item loss caused by attacks fix
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

If an item is swapped/removed/whatever in a battle
  If it's a PvP battle or a Trainer battle
    Every Pokémon that has its item removed gets its original item back
  If it's a Wild battle
    If a Pokémon that had its original item removed is holding
    a different item (because of the attack Trick for example)
      The Pokémon keeps the new item and the original item is added to
      the inventory.
---
 P3D/Battle/BattleSystemV2/BattleScreen.vb | 15 +++++++++++++--
 P3D/Pokemon/Attacks/Psychic/Trick.vb      |  5 +++++
 P3D/Pokemon/Monster/Pokemon.vb            | 17 ++++++++++++-----
 3 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/P3D/Battle/BattleSystemV2/BattleScreen.vb b/P3D/Battle/BattleSystemV2/BattleScreen.vb
index 7feaa4d86..238fc202b 100644
--- a/P3D/Battle/BattleSystemV2/BattleScreen.vb
+++ b/P3D/Battle/BattleSystemV2/BattleScreen.vb
@@ -1036,8 +1036,6 @@ nextIndex:
             End If
             TempPokeFile = ""
 
-            OwnPokemon.ResetTemp()
-
             If IsRemoteBattle = False Then
                 If ConnectScreen.Connected = True Then
                     If Battle.Won = False Then
@@ -1081,6 +1079,11 @@ nextIndex:
                     If p.hasLeveledUp = True Then
                         hasLevelUp = True
                     End If
+                    If IsRemoteBattle = True Or IsTrainerBattle = True Then
+                        p.Item = P3D.Item.GetItemByID(p.OriginalItem.ID)
+                        p.Item.AdditionalData = p.OriginalItem.AdditionalData
+                        Screen.TextBox.Show(Core.Player.Name & " received" & p.OriginalItem.Name & "*and gave it back to~" & p.GetDisplayName)
+                    End If
                     p.ResetTemp()
                 Next
                 If hasLevelUp = False Then
@@ -1118,6 +1121,14 @@ nextIndex:
                     End If
                 Next
             Else
+                For Each p As Pokemon In Core.Player.Pokemons
+                    If IsRemoteBattle = True Or IsTrainerBattle = True Then
+                        p.Item = P3D.Item.GetItemByID(p.OriginalItem.ID)
+                        p.Item.AdditionalData = p.OriginalItem.AdditionalData
+                        Screen.TextBox.Show(Core.Player.Name & " received~" & p.OriginalItem.Name & "*and gave it back to~" & p.GetDisplayName)
+                    End If
+                    p.ResetTemp()
+                Next
                 ResetVars()
                 Core.SetScreen(New TransitionScreen(Me, New BlackOutScreen(Me), Color.Black, False))
             End If
diff --git a/P3D/Pokemon/Attacks/Psychic/Trick.vb b/P3D/Pokemon/Attacks/Psychic/Trick.vb
index 3031e4c58..e0954467c 100644
--- a/P3D/Pokemon/Attacks/Psychic/Trick.vb
+++ b/P3D/Pokemon/Attacks/Psychic/Trick.vb
@@ -106,6 +106,11 @@ Namespace BattleSystem.Moves.Psychic
             If CanSwitchItems Then
                 Dim i1 As Item = Nothing
                 Dim i2 As Item = Nothing
+				If own = True Then
+                    p.OriginalItem = p.Item
+                Else
+                    op.OriginalItem = op.Item
+                End If
                 If p.Item IsNot Nothing Then
                     i1 = p.Item
                 End If
diff --git a/P3D/Pokemon/Monster/Pokemon.vb b/P3D/Pokemon/Monster/Pokemon.vb
index ef2ebcb6b..1fbdda351 100644
--- a/P3D/Pokemon/Monster/Pokemon.vb
+++ b/P3D/Pokemon/Monster/Pokemon.vb
@@ -1014,11 +1014,18 @@ Public Class Pokemon
 
         Me.Ability = Me._originalAbility
 
-        'If Not Me._originalItem Is Nothing Then
-        '    Me.Item = P3D.Item.GetItemByID(Me._originalItem.ID)
-        '    Me.Item.AdditionalData = Me._originalItem.AdditionalData
-        '    Me._originalItem = Nothing
-        'End If
+        If Not Me._originalItem Is Nothing Then
+            If Me.Item IsNot Nothing Then
+                Core.Player.Inventory.AddItem(Me._originalItem.ID, 1)
+                SoundManager.PlaySound("item_found", True)
+                Screen.TextBox.Show(Core.Player.Name & " found~" & Me._originalItem.Name & "!*" & Core.Player.Inventory.GetMessageReceive(_originalItem, 1))
+            Else
+                Me.Item = P3D.Item.GetItemByID(Me._originalItem.ID)
+                Me.Item.AdditionalData = Me._originalItem.AdditionalData
+                Screen.TextBox.Show(Core.Player.Name & " found~" & Me._originalItem.Name & "*and gave it back to~" & Me.GetDisplayName)
+            End If
+            Me._originalItem = Nothing
+        End If
 
         Me.IsTransformed = False