From cd6ad1a85ddfe2c9410c56860d5a31d231662522 Mon Sep 17 00:00:00 2001 From: jojo Date: Thu, 25 Feb 2021 13:50:08 +0100 Subject: [PATCH] Fix Item giving --- .../commands/CommandDebugStick_15.java | 3 +- .../de/steamwar/bausystem/PlayerUtils.java | 36 +++++++++++++++++++ .../bausystem/commands/CommandDetonator.java | 3 +- .../bausystem/commands/CommandGUI.java | 10 ++---- .../bausystem/commands/CommandScript.java | 3 +- .../bausystem/commands/CommandSkull.java | 3 +- 6 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 BauSystem_API/src/de/steamwar/bausystem/PlayerUtils.java diff --git a/BauSystem_15/src/de/steamwar/bausystem/commands/CommandDebugStick_15.java b/BauSystem_15/src/de/steamwar/bausystem/commands/CommandDebugStick_15.java index 3c1d189..8bbf9dd 100644 --- a/BauSystem_15/src/de/steamwar/bausystem/commands/CommandDebugStick_15.java +++ b/BauSystem_15/src/de/steamwar/bausystem/commands/CommandDebugStick_15.java @@ -19,6 +19,7 @@ package de.steamwar.bausystem.commands; +import de.steamwar.bausystem.PlayerUtils; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -27,6 +28,6 @@ class CommandDebugStick_15 { private CommandDebugStick_15(){} static void giveStick(Player player){ - player.getInventory().setItemInMainHand(new ItemStack(Material.DEBUG_STICK)); + PlayerUtils.giveItemToPlayer(player, new ItemStack(Material.DEBUG_STICK)); } } diff --git a/BauSystem_API/src/de/steamwar/bausystem/PlayerUtils.java b/BauSystem_API/src/de/steamwar/bausystem/PlayerUtils.java new file mode 100644 index 0000000..8dd1181 --- /dev/null +++ b/BauSystem_API/src/de/steamwar/bausystem/PlayerUtils.java @@ -0,0 +1,36 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem; + +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +public class PlayerUtils { + + public static void giveItemToPlayer(Player player, ItemStack itemStack) { + ItemStack current = player.getInventory().getItemInMainHand(); + player.getInventory().setItemInMainHand(itemStack); + if (current.getType() != Material.AIR) { + player.getInventory().addItem(current); + } + } + +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonator.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonator.java index 52bc21e..ab6ff39 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonator.java @@ -21,6 +21,7 @@ package de.steamwar.bausystem.commands; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.Permission; +import de.steamwar.bausystem.PlayerUtils; import de.steamwar.bausystem.world.Detonator; import de.steamwar.bausystem.world.Welt; import org.bukkit.command.Command; @@ -62,7 +63,7 @@ public class CommandDetonator implements CommandExecutor { case "wand": case "detonator": case "item": - player.getInventory().setItemInMainHand(Detonator.WAND); + PlayerUtils.giveItemToPlayer(player, Detonator.WAND); player.updateInventory(); Detonator.getDetonator(player); break; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandGUI.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandGUI.java index ef1a29f..be01a34 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandGUI.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandGUI.java @@ -2,6 +2,7 @@ package de.steamwar.bausystem.commands; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.Permission; +import de.steamwar.bausystem.PlayerUtils; import de.steamwar.bausystem.tracer.record.RecordStateMachine; import de.steamwar.bausystem.tracer.show.TraceShowManager; import de.steamwar.bausystem.world.*; @@ -574,13 +575,8 @@ public class CommandGUI implements CommandExecutor, Listener { if (strings.length == 0) { openBauGui(player); OPEN_INVS.add(player); - } else { - if ("item".equalsIgnoreCase(strings[0])) { - if (player.getInventory().getItemInMainHand().getType() == Material.AIR) - player.getInventory().setItemInMainHand(new ItemStack(Material.NETHER_STAR)); - else - player.getInventory().addItem(new ItemStack(Material.NETHER_STAR)); - } + } else if ("item".equalsIgnoreCase(strings[0])) { + PlayerUtils.giveItemToPlayer(player, new ItemStack(Material.NETHER_STAR)); } return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScript.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScript.java index b093046..6f03dbe 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScript.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandScript.java @@ -21,6 +21,7 @@ package de.steamwar.bausystem.commands; +import de.steamwar.bausystem.PlayerUtils; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -67,7 +68,7 @@ public class CommandScript implements CommandExecutor { if (!(sender instanceof Player)) { return false; } - ((Player) sender).getInventory().setItemInMainHand(BOOK); + PlayerUtils.giveItemToPlayer((Player) sender, BOOK); return false; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSkull.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSkull.java index 6ac0d81..bd8d121 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSkull.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSkull.java @@ -20,6 +20,7 @@ package de.steamwar.bausystem.commands; import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.PlayerUtils; import de.steamwar.inventory.SWItem; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -46,7 +47,7 @@ public class CommandSkull implements CommandExecutor { assert sm != null; sm.setDisplayName("§e" + args[0] + "§8s Kopf"); is.setItemMeta(sm); - p.getInventory().setItemInMainHand(is); + PlayerUtils.giveItemToPlayer(p, is); return false; }