added auto hotbar refill
This commit is contained in:
parent
62703ffd12
commit
f187a867af
@ -12,6 +12,7 @@ import eu.mhsl.craftattack.spawn.appliances.displayName.DisplayName;
|
|||||||
import eu.mhsl.craftattack.spawn.appliances.event.Event;
|
import eu.mhsl.craftattack.spawn.appliances.event.Event;
|
||||||
import eu.mhsl.craftattack.spawn.appliances.fleischerchest.Fleischerchest;
|
import eu.mhsl.craftattack.spawn.appliances.fleischerchest.Fleischerchest;
|
||||||
import eu.mhsl.craftattack.spawn.appliances.help.Help;
|
import eu.mhsl.craftattack.spawn.appliances.help.Help;
|
||||||
|
import eu.mhsl.craftattack.spawn.appliances.hotbarRefill.HotbarRefill;
|
||||||
import eu.mhsl.craftattack.spawn.appliances.kick.Kick;
|
import eu.mhsl.craftattack.spawn.appliances.kick.Kick;
|
||||||
import eu.mhsl.craftattack.spawn.appliances.outlawed.Outlawed;
|
import eu.mhsl.craftattack.spawn.appliances.outlawed.Outlawed;
|
||||||
import eu.mhsl.craftattack.spawn.appliances.panicBan.PanicBan;
|
import eu.mhsl.craftattack.spawn.appliances.panicBan.PanicBan;
|
||||||
@ -71,7 +72,8 @@ public final class Main extends JavaPlugin {
|
|||||||
new Settings(),
|
new Settings(),
|
||||||
new PortableCrafting(),
|
new PortableCrafting(),
|
||||||
new AutoShulker(),
|
new AutoShulker(),
|
||||||
new AntiSignEdit()
|
new AntiSignEdit(),
|
||||||
|
new HotbarRefill()
|
||||||
);
|
);
|
||||||
|
|
||||||
Main.logger.info("Loading appliances...");
|
Main.logger.info("Loading appliances...");
|
||||||
|
@ -0,0 +1,50 @@
|
|||||||
|
package eu.mhsl.craftattack.spawn.appliances.hotbarRefill;
|
||||||
|
|
||||||
|
import eu.mhsl.craftattack.spawn.Main;
|
||||||
|
import eu.mhsl.craftattack.spawn.appliance.Appliance;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.inventory.Inventory;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
|
public class HotbarRefill extends Appliance {
|
||||||
|
public void handleHotbarChange(Player player, ItemStack item) {
|
||||||
|
if (item.getAmount() != 1) return;
|
||||||
|
Inventory inventory = player.getInventory();
|
||||||
|
|
||||||
|
int itemSlot = inventory.first(item);
|
||||||
|
if (itemSlot > 8) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
int replacementSlot = inventory.all(item.getType()).entrySet().stream()
|
||||||
|
.filter(entry -> entry.getKey() > 8)
|
||||||
|
.findFirst()
|
||||||
|
.orElseThrow()
|
||||||
|
.getKey();
|
||||||
|
|
||||||
|
Bukkit.getScheduler().scheduleSyncDelayedTask(Main.instance(), () -> {
|
||||||
|
ItemStack firstItem = inventory.getItem(itemSlot);
|
||||||
|
ItemStack secondItem = inventory.getItem(replacementSlot);
|
||||||
|
|
||||||
|
inventory.setItem(itemSlot, secondItem);
|
||||||
|
inventory.setItem(replacementSlot, firstItem);
|
||||||
|
|
||||||
|
player.sendActionBar(Component.text("Die Hotbar wurde aufgefüllt", NamedTextColor.GREEN));
|
||||||
|
}, 1);
|
||||||
|
} catch(NoSuchElementException ignored) {
|
||||||
|
player.sendActionBar(Component.text("Keine weiteren Items dieser Art im Inventar vorhanden", NamedTextColor.RED));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected @NotNull List<Listener> eventHandlers() {
|
||||||
|
return List.of(new ItemRefillListener());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package eu.mhsl.craftattack.spawn.appliances.hotbarRefill;
|
||||||
|
|
||||||
|
import eu.mhsl.craftattack.spawn.appliance.ApplianceListener;
|
||||||
|
import eu.mhsl.craftattack.spawn.appliances.settings.Settings;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.block.BlockPlaceEvent;
|
||||||
|
import org.bukkit.event.player.PlayerItemBreakEvent;
|
||||||
|
import org.bukkit.event.player.PlayerItemConsumeEvent;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
public class ItemRefillListener extends ApplianceListener<HotbarRefill> {
|
||||||
|
@EventHandler
|
||||||
|
public void blockPlace(BlockPlaceEvent event) {
|
||||||
|
if(!Settings.instance().getSetting(event.getPlayer(), Settings.Key.HotbarReplacer, Boolean.class)) return;
|
||||||
|
|
||||||
|
ItemStack stackInHand = event.getItemInHand();
|
||||||
|
if(stackInHand.getType().getMaxDurability() > 0) return;
|
||||||
|
getAppliance().handleHotbarChange(event.getPlayer(), stackInHand);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onPlayerItemBreak(PlayerItemBreakEvent event) {
|
||||||
|
if(!Settings.instance().getSetting(event.getPlayer(), Settings.Key.HotbarReplacer, Boolean.class)) return;
|
||||||
|
|
||||||
|
getAppliance().handleHotbarChange(event.getPlayer(), event.getBrokenItem());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onPlayerItemConsume(PlayerItemConsumeEvent event) {
|
||||||
|
if(!Settings.instance().getSetting(event.getPlayer(), Settings.Key.HotbarReplacer, Boolean.class)) return;
|
||||||
|
|
||||||
|
getAppliance().handleHotbarChange(event.getPlayer(), event.getItem());
|
||||||
|
}
|
||||||
|
}
|
@ -27,6 +27,7 @@ public class Settings extends Appliance {
|
|||||||
EnableSettingsShortcut,
|
EnableSettingsShortcut,
|
||||||
AutoShulker,
|
AutoShulker,
|
||||||
SignEdit,
|
SignEdit,
|
||||||
|
HotbarReplacer,
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Settings instance() {
|
public static Settings instance() {
|
||||||
@ -51,7 +52,8 @@ public class Settings extends Appliance {
|
|||||||
new EnablePortableCraftingSetting(),
|
new EnablePortableCraftingSetting(),
|
||||||
new EnableSettingsShortcutSetting(),
|
new EnableSettingsShortcutSetting(),
|
||||||
new AutoShulkerSetting(),
|
new AutoShulkerSetting(),
|
||||||
new SignEditSetting()
|
new SignEditSetting(),
|
||||||
|
new HotbarReplaceSetting()
|
||||||
);
|
);
|
||||||
|
|
||||||
settings.forEach(setting -> setting.initializeFromPlayer(player));
|
settings.forEach(setting -> setting.initializeFromPlayer(player));
|
||||||
@ -86,12 +88,12 @@ public class Settings extends Appliance {
|
|||||||
player.updateInventory();
|
player.updateInventory();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasSettingsOpen(Player player) {
|
public boolean hasSettingsNotOpen(Player player) {
|
||||||
return this.openSettingsInventories.containsKey(player);
|
return !this.openSettingsInventories.containsKey(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
public OpenSettingsInventory getOpenInventory(Player player) {
|
public OpenSettingsInventory getOpenInventory(Player player) {
|
||||||
if(!hasSettingsOpen(player)) throw new RuntimeException("Cannot retrieve data from closed Settings inventory!");
|
if(hasSettingsNotOpen(player)) throw new RuntimeException("Cannot retrieve data from closed Settings inventory!");
|
||||||
return this.openSettingsInventories.get(player);
|
return this.openSettingsInventories.get(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ public class SettingsInventoryListener extends ApplianceListener<Settings> {
|
|||||||
@EventHandler
|
@EventHandler
|
||||||
public void onInventoryClick(InventoryClickEvent event) {
|
public void onInventoryClick(InventoryClickEvent event) {
|
||||||
Player player = (Player) event.getWhoClicked();
|
Player player = (Player) event.getWhoClicked();
|
||||||
if(!getAppliance().hasSettingsOpen(player)) return;
|
if(getAppliance().hasSettingsNotOpen(player)) return;
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
|
|
||||||
Settings.OpenSettingsInventory openInventory = getAppliance().getOpenInventory(player);
|
Settings.OpenSettingsInventory openInventory = getAppliance().getOpenInventory(player);
|
||||||
|
@ -0,0 +1,31 @@
|
|||||||
|
package eu.mhsl.craftattack.spawn.appliances.settings.settings;
|
||||||
|
|
||||||
|
import eu.mhsl.craftattack.spawn.appliances.settings.Settings;
|
||||||
|
import eu.mhsl.craftattack.spawn.appliances.settings.datatypes.BoolSetting;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
|
||||||
|
public class HotbarReplaceSetting extends BoolSetting {
|
||||||
|
public HotbarReplaceSetting() {
|
||||||
|
super(Settings.Key.HotbarReplacer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String title() {
|
||||||
|
return "Automatische Hotbar";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String description() {
|
||||||
|
return "Verschiebe automatisch Blöcke von deinem Inventar in die Hotbar, wenn diese verbraucht werden";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Material icon() {
|
||||||
|
return Material.CHEST;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Boolean defaultValue() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user