Skip to content

Commit

Permalink
Merge pull request #101 from HibiscusMC/development
Browse files Browse the repository at this point in the history
HMCWraps 1.4.0
  • Loading branch information
Skyslycer authored Dec 19, 2023
2 parents bed0364 + aefbee0 commit f05bd6f
Show file tree
Hide file tree
Showing 62 changed files with 1,868 additions and 352 deletions.
6 changes: 3 additions & 3 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
plugins {
java
`maven-publish`
kotlin("jvm") version "1.8.22"
id("org.jetbrains.dokka") version ("1.8.20")
kotlin("jvm") version "1.9.10"
id("org.jetbrains.dokka") version ("1.9.0")
}

group = "de.skyslycer.hmcwraps"
Expand All @@ -21,7 +21,7 @@ repositories {
}

dependencies {
dokkaHtmlPlugin("org.jetbrains.dokka:kotlin-as-java-plugin:1.8.20")
dokkaHtmlPlugin("org.jetbrains.dokka:kotlin-as-java-plugin:1.9.0")
compileOnly(depends.spigot)
compileOnly(depends.annotations)
compileOnly(depends.placeholderapi)
Expand Down
8 changes: 4 additions & 4 deletions api/src/main/java/de/skyslycer/hmcwraps/HMCWraps.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
import de.skyslycer.hmcwraps.wrap.CollectionHelper;
import de.skyslycer.hmcwraps.wrap.Wrapper;
import de.skyslycer.hmcwraps.wrap.WrapsLoader;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.nio.file.Path;
import java.util.List;
import java.util.UUID;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

public interface HMCWraps extends Plugin {

Expand All @@ -26,6 +25,7 @@ public interface HMCWraps extends Plugin {
Path WRAP_FILES_PATH = PLUGIN_PATH.resolve("wraps");
Path MESSAGES_PATH = PLUGIN_PATH.resolve("messages.properties");
Path CONVERT_PATH = PLUGIN_PATH.resolve("convert");
Path COMMAND_PATH = WRAP_FILES_PATH.resolve("command");
Path COLLECTION_FILES_PATH = PLUGIN_PATH.resolve("collections");

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ public class GuiActionInformation implements ActionInformation {
private PaginatedGui gui;
private Player player;
private String arguments;
private int slot;

public GuiActionInformation(Player player, String arguments, PaginatedGui gui) {
public GuiActionInformation(Player player, String arguments, PaginatedGui gui, int slot) {
this.player = player;
this.arguments = arguments;
this.gui = gui;
this.slot = slot;
}

@Override
Expand All @@ -29,6 +31,14 @@ public PaginatedGui getGui() {
return gui;
}

public int getSlot() {
return slot;
}

public void setSlot(int slot) {
this.slot = slot;
}

@Override
public void setPlayer(Player player) {
this.player = player;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ public class WrapGuiActionInformation implements ActionInformation {
private Wrap wrap;
private Player player;
private String arguments;
private int slot;

public WrapGuiActionInformation(PaginatedGui gui, Wrap wrap, Player player, String arguments) {
public WrapGuiActionInformation(PaginatedGui gui, Wrap wrap, Player player, int slot, String arguments) {
this.gui = gui;
this.wrap = wrap;
this.player = player;
this.arguments = arguments;
this.slot = slot;
}

public PaginatedGui getGui() {
Expand All @@ -34,6 +36,14 @@ public void setWrap(Wrap wrap) {
this.wrap = wrap;
}

public int getSlot() {
return slot;
}

public void setSlot(int slot) {
this.slot = slot;
}

@Override
public Player getPlayer() {
return player;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ public enum Messages {
REMOVE_WRAP("remove-wrap"),
NO_ITEM("no-item"),
NO_WRAPS("no-wraps"),
NO_REWRAP("no-rewrap"),
ARMOR_IMITATION_FORBIDDEN_INVENTORY("armor-imitation.forbidden-inventory"),
INVENTORY_FILTER_ACTIVE("inventory.filter.active"),
INVENTORY_FILTER_INACTIVE("inventory.filter.inactive"),
PREVIEW_DISABLED("preview.disabled"),
PREVIEW_BAR("preview.bar"),
FAVORITES_SET("favorites.set"),
FAVORITES_CLEAR("favorites.clear"),
COMMAND_MISSING_ARGUMENT("command.missing-argument"),
COMMAND_PLAYER_ONLY("command.player-only"),
COMMAND_INVALID_WRAP("command.invalid-wrap"),
Expand All @@ -40,7 +44,9 @@ public enum Messages {
COMMAND_CONVERT_SUCCESS("command.convert.success"),
COMMAND_CONVERT_CONFIRM("command.convert.confirm"),
COMMAND_CONVERT_NO_CONFIRM("command.convert.no-confirm"),
COMMAND_CONVERT_FAILED("command.convert.failed");
COMMAND_CONVERT_FAILED("command.convert.failed"),
COMMAND_CREATE_FAILED("command.create.failed"),
COMMAND_CREATE_SUCCESS("command.create.success");

private final String key;

Expand Down
152 changes: 17 additions & 135 deletions api/src/main/java/de/skyslycer/hmcwraps/preview/Preview.java
Original file line number Diff line number Diff line change
@@ -1,135 +1,17 @@
package de.skyslycer.hmcwraps.preview;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.player.Equipment;
import com.github.retrooper.packetevents.protocol.player.EquipmentSlot;
import com.github.retrooper.packetevents.util.Vector3f;
import com.github.retrooper.packetevents.wrapper.play.server.*;
import de.skyslycer.hmcwraps.HMCWraps;
import de.skyslycer.hmcwraps.util.PlayerUtil;
import de.skyslycer.hmcwraps.util.VectorUtil;
import io.github.retrooper.packetevents.util.SpigotConversionUtil;
import io.github.retrooper.packetevents.util.SpigotReflectionUtil;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitTask;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Consumer;

public class Preview {

private final int entityId = SpigotReflectionUtil.generateEntityId();
private final Player player;
private final ItemStack item;
private final Consumer<Player> onClose;
private final HMCWraps plugin;
private BukkitTask task;
private BukkitTask cancelTask;

Preview(Player player, ItemStack item, Consumer<Player> onClose, HMCWraps plugin) {
this.player = player;
this.item = item;
this.onClose = onClose;
this.plugin = plugin;
}

/**
* Start the preview.
*/
public void preview() {
player.closeInventory();

sendSpawnPacket();
sendMetadataPacket();
sendTeleportPacket();
sendEquipPacket();

task = Bukkit.getScheduler()
.runTaskTimerAsynchronously(plugin, new RotateRunnable(player, entityId, plugin), 3, 1);

cancelTask = Bukkit.getScheduler()
.runTaskLater(plugin, () -> plugin.getPreviewManager().remove(player.getUniqueId(), true),
plugin.getConfiguration().getPreview().getDuration() * 20L);
}

/**
* Cancel the preview.
*
* @param open If the inventory should open again
*/
public void cancel(boolean open) {
task.cancel();
cancelTask.cancel();
if (open && onClose != null) {
onClose.accept(player);
}
Bukkit.getScheduler().runTaskLater(plugin, () -> {
PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerDestroyEntities(entityId));
if (plugin.getConfiguration().getPreview().getSneakCancel().isActionBar()) {
player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(" "));
}
}, 1L);
}

private void sendSpawnPacket() {
if (SpigotReflectionUtil.V_1_19_OR_HIGHER) {
PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerSpawnEntity(entityId,
Optional.of(UUID.randomUUID()),
EntityTypes.ARMOR_STAND,
VectorUtil.fromLocation(PlayerUtil.getOpposite(player)),
0f,
0f,
0f,
0,
Optional.empty()));
PacketEvents.getAPI().getPlayerManager()
.sendPacket(player, new WrapperPlayServerEntityMetadata(entityId,
List.of(new EntityData(0, EntityDataTypes.BYTE, (byte) 0x20),
new EntityData(16, EntityDataTypes.ROTATION, new Vector3f(180, 0, 0)),
new EntityData(5, EntityDataTypes.BOOLEAN, true))));
} else {
PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerSpawnLivingEntity(
entityId,
UUID.randomUUID(),
EntityTypes.ARMOR_STAND,
VectorUtil.fromLocation(PlayerUtil.getOpposite(player)),
0f,
0f,
0f,
VectorUtil.zeroVector(),
List.of(new EntityData(0, EntityDataTypes.BYTE, (byte) 0x20),
new EntityData(16, EntityDataTypes.ROTATION, new Vector3f(180, 0, 0)),
new EntityData(5, EntityDataTypes.BOOLEAN, true))));
}
}

private void sendMetadataPacket() {
PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerEntityMetadata(
entityId,
List.of(new EntityData(0, EntityDataTypes.BYTE, (byte) 0x20),
new EntityData(16, EntityDataTypes.ROTATION, new Vector3f(180, 0, 0)),
new EntityData(5, EntityDataTypes.BOOLEAN, true)))
);
}

private void sendTeleportPacket() {
PacketEvents.getAPI().getPlayerManager().sendPacket(player,
new WrapperPlayServerEntityTeleport(entityId, VectorUtil.fromLocation(PlayerUtil.getLookBlock(player)),
0f, 0f, false));
}

private void sendEquipPacket() {
PacketEvents.getAPI().getPlayerManager().sendPacket(player, new WrapperPlayServerEntityEquipment(
entityId, List.of(new Equipment(EquipmentSlot.HELMET, SpigotConversionUtil.fromBukkitItemStack(item)))));
}

}
package de.skyslycer.hmcwraps.preview;

public interface Preview {

/**
* Start the preview.
*/
void preview();

/**
* Cancel the preview.
*
* @param open If the inventory should open again
*/
void cancel(boolean open);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import de.skyslycer.hmcwraps.HMCWraps;
import de.skyslycer.hmcwraps.events.ItemPreviewEvent;
import de.skyslycer.hmcwraps.preview.floating.FloatingPreview;
import de.skyslycer.hmcwraps.preview.hand.HandPreview;
import de.skyslycer.hmcwraps.serialization.preview.PreviewType;
import de.skyslycer.hmcwraps.serialization.wrap.Wrap;
import de.skyslycer.hmcwraps.util.MaterialUtil;
import dev.triumphteam.gui.builder.item.ItemBuilder;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -44,7 +48,9 @@ public void remove(UUID uuid, boolean open) {
* @param wrap The wrap to preview
*/
public void create(Player player, Consumer<Player> onClose, Wrap wrap) {
var item = ItemBuilder.from(plugin.getCollectionHelper().getMaterial(wrap)).model(wrap.getModelId());
var wrapType = plugin.getCollectionHelper().getMaterial(wrap);
var type = wrap.isArmorImitationEnabled() ? MaterialUtil.getLeatherAlternative(wrapType) : wrapType;
var item = ItemBuilder.from(type).model(wrap.getModelId());
if (wrap.getColor() != null) {
item.color(wrap.getColor());
}
Expand All @@ -58,7 +64,13 @@ public void create(Player player, Consumer<Player> onClose, Wrap wrap) {


private void createPrivate(Player player, ItemStack item, Consumer<Player> onClose) {
var preview = new Preview(player, item, onClose, plugin);
this.remove(player.getUniqueId(), false);
Preview preview;
if (plugin.getConfiguration().getPreview().getType() == PreviewType.HAND) {
preview = new HandPreview(player, item, onClose, plugin);
} else {
preview = new FloatingPreview(player, item, onClose, plugin);
}
previews.put(player.getUniqueId(), preview);
preview.preview();
}
Expand All @@ -72,4 +84,14 @@ public void removeAll(boolean open) {
previews.keySet().forEach(uuid -> this.remove(uuid, open));
}

/**
* Check if a player is previewing.
*
* @param player The player
* @return If the player is previewing
*/
public boolean isPreviewing(Player player) {
return previews.containsKey(player.getUniqueId());
}

}
Loading

0 comments on commit f05bd6f

Please sign in to comment.