You are setting the inventory slot with your item, and then getting the item you just set. You want to do this:
ItemStack slotStack = inventory.getStackInSlot(slot);
ItemStack currentStack = inventory.getStackInSlot(inventory.currentItem);
if (slotStack != null){
inventory.setInventorySlotContents(inventory.currentItem, slotStack);
} else {
//return here? if you want to
}
if (currentStack != null){
inventory.setInventorySlotContents(slot, currentStack);
} else {
//same as above
}
Also, I dont know if you can do these operations on the client side, since you are using Minecraft.getMinecraft().thePlayer (client side player)