-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from HibiscusMC/development
HMCWraps 1.4.0
- Loading branch information
Showing
62 changed files
with
1,868 additions
and
352 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 17 additions & 135 deletions
152
api/src/main/java/de/skyslycer/hmcwraps/preview/Preview.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.