Added BackgroundItem

This commit is contained in:
NichtStudioCode 2021-01-26 20:13:47 +01:00
parent dd9232fc37
commit 1f2ac4a651
4 changed files with 117 additions and 17 deletions

@ -0,0 +1,24 @@
package de.studiocode.invgui.item.impl.customtexture;
import de.studiocode.invgui.item.impl.SimpleItem;
import de.studiocode.invgui.item.itembuilder.ItemBuilder;
import de.studiocode.invgui.resourcepack.ForceResourcePack;
import org.bukkit.Material;
public class BackgroundItem extends SimpleItem {
private static final BackgroundItem INSTANCE = new BackgroundItem();
static {
ForceResourcePack.getInstance(); // initializes ForceResourcePack which automatically activates forced resource packs (if not disabled)
}
private BackgroundItem() {
super(new ItemBuilder(Material.POPPY).setDisplayName("§f").setCustomModelData(10000000));
}
public static BackgroundItem getInstance() {
return INSTANCE;
}
}

@ -0,0 +1,77 @@
package de.studiocode.invgui.resourcepack;
import de.studiocode.invgui.InvGui;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerResourcePackStatusEvent;
import org.bukkit.scheduler.BukkitTask;
import java.util.HashMap;
import static org.bukkit.event.player.PlayerResourcePackStatusEvent.Status.DECLINED;
public class ForceResourcePack implements Listener {
private static final ForceResourcePack INSTANCE = new ForceResourcePack();
private final HashMap<Player, BukkitTask> tasks = new HashMap<>();
private String resourcePackUrl = "https://github.com/NichtStudioCode/InvGuiRP/releases/download/v0.1/InvGuiRP.zip";
private boolean activated = true;
private ForceResourcePack() {
Bukkit.getPluginManager().registerEvents(this, InvGui.getInstance().getPlugin());
Bukkit.getOnlinePlayers().forEach(this::sendResourcePack);
}
public static ForceResourcePack getInstance() {
return INSTANCE;
}
@EventHandler
public void handleJoin(PlayerJoinEvent event) {
if (activated) sendResourcePack(event.getPlayer());
}
@EventHandler
public void handleResourcePackStatus(PlayerResourcePackStatusEvent event) {
if (activated) {
Player player = event.getPlayer();
if (tasks.containsKey(player)) {
if (event.getStatus() == DECLINED) kickPlayer(player);
else tasks.get(player).cancel();
tasks.remove(player);
}
}
}
private void sendResourcePack(Player player) {
player.setResourcePack(resourcePackUrl);
tasks.put(player, Bukkit.getScheduler().runTaskLater(InvGui.getInstance().getPlugin(),
() -> kickPlayer(player), 20 * 5));
}
private void kickPlayer(Player player) {
player.kickPlayer("§cPlease accept the custom resource pack");
}
public String getResourcePackUrl() {
return resourcePackUrl;
}
public void setResourcePackUrl(String resourcePackUrl) {
this.resourcePackUrl = resourcePackUrl;
}
public boolean isActivated() {
return activated;
}
public void setActivated(boolean activated) {
this.activated = activated;
}
}

@ -2,6 +2,7 @@ package de.studiocode.invgui.window;
import de.studiocode.invgui.animation.Animation; import de.studiocode.invgui.animation.Animation;
import de.studiocode.invgui.gui.GUI; import de.studiocode.invgui.gui.GUI;
import de.studiocode.invgui.gui.SlotElement.ItemStackHolder;
import de.studiocode.invgui.item.Item; import de.studiocode.invgui.item.Item;
import de.studiocode.invgui.item.itembuilder.ItemBuilder; import de.studiocode.invgui.item.itembuilder.ItemBuilder;
import de.studiocode.invgui.virtualinventory.VirtualInventory; import de.studiocode.invgui.virtualinventory.VirtualInventory;
@ -16,6 +17,7 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.UUID; import java.util.UUID;
import java.util.function.Predicate;
/** /**
* A window is the way to show a player a GUI. * A window is the way to show a player a GUI.
@ -126,8 +128,9 @@ public interface Window {
* Plays an animation. * Plays an animation.
* *
* @param animation The animation to play. * @param animation The animation to play.
* @param filter The filter that selects which Items should be animated.
*/ */
void playAnimation(Animation animation); void playAnimation(Animation animation, Predicate<ItemStackHolder> filter);
/** /**
* Gets if the player is able to close the {@link Inventory}. * Gets if the player is able to close the {@link Inventory}.

@ -20,8 +20,7 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.function.Predicate;
import java.util.stream.IntStream;
public abstract class BaseWindow implements Window { public abstract class BaseWindow implements Window {
@ -187,24 +186,25 @@ public abstract class BaseWindow implements Window {
} }
@Override @Override
public void playAnimation(Animation animation) { public void playAnimation(Animation animation, Predicate<ItemStackHolder> filter) {
if (getViewer() != null) { if (getViewer() != null) {
this.animation = animation; this.animation = animation;
List<Integer> slots = new ArrayList<>();
for (int i = 0; i < size; i++) {
ItemStackHolder holder = itemsDisplayed[i];
if (holder != null && filter.test(holder)) {
slots.add(i);
inventory.setItem(i, null);
}
}
animation.setBounds(getGui().getWidth(), getGui().getHeight()); animation.setBounds(getGui().getWidth(), getGui().getHeight());
animation.setPlayer(getViewer()); animation.setPlayer(getViewer());
animation.addShowHandler((frame, index) -> redrawItem(index, itemsDisplayed[index], false)); animation.addShowHandler((frame, index) -> redrawItem(index, itemsDisplayed[index], false));
animation.addFinishHandler(() -> this.animation = null); animation.addFinishHandler(() -> this.animation = null);
animation.setSlots(IntStream.range(0, size) animation.setSlots(slots);
.filter(i -> {
ItemStackHolder element = itemsDisplayed[i];
return !(element == null || (element instanceof VISlotElement
&& !((VISlotElement) element).getVirtualInventory().hasItem(((VISlotElement) element).getIndex())));
})
.boxed()
.collect(Collectors.toCollection(ArrayList::new)));
clearItemStacks();
animation.start(); animation.start();
} }
} }
@ -221,10 +221,6 @@ public abstract class BaseWindow implements Window {
} }
} }
private void clearItemStacks() {
for (int i = 0; i < inventory.getSize(); i++) inventory.setItem(i, null);
}
@Override @Override
public Player getViewer() { public Player getViewer() {
return Bukkit.getPlayer(viewerUUID); return Bukkit.getPlayer(viewerUUID);