Last active
January 27, 2024 13:20
-
-
Save HeathLoganCampbell/2509cbd485d3a30a350d51a1b95c376b to your computer and use it in GitHub Desktop.
Spawn fishing hook / fishing bob / fishing line without a fishing rod using protocollib
This file contains hidden or 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
// Spawn fishing hook / fishing bob / fishing line without a fishing rod using protocollib | |
// Learnings: | |
// The hook bobbing on the water is done client side. | |
// fishing hook will just sink thru lava | |
private void spawnEntity(Location location, Vector vector, EntityType type, int shooterId) { | |
int entityId = 10000; | |
PacketContainer packet = new PacketContainer(PacketType.Play.Server.SPAWN_ENTITY); | |
packet.getIntegers().write(0, entityId); | |
packet.getUUIDs().write(0, UUID.randomUUID()); | |
packet.getEntityTypeModifier().write(0, type); | |
packet.getDoubles() | |
.write(0, location.getX()) | |
.write(1, location.getY()) | |
.write(2, location.getZ()); | |
packet.getIntegers() | |
.write(1, convertVelocity(vector.getX())) | |
.write(2, convertVelocity(vector.getY())) | |
.write(3, convertVelocity(vector.getZ())) | |
.write(4, shooterId); | |
for(Player player : Bukkit.getOnlinePlayers()) { | |
ProtocolLibrary.getProtocolManager().sendServerPacket(player, packet); | |
} | |
} | |
private int convertVelocity(double velocity) { | |
return (int) (clamp(velocity, -3.9, 3.9) * 8000); | |
} | |
private double clamp(double targetNum, double min, double max) { | |
return Math.max(min, Math.min(targetNum, max)); | |
} | |
@EventHandler | |
public void onShift(PlayerToggleSneakEvent e) | |
{ | |
Location location = e.getPlayer().getLocation(); | |
spawnEntity(location, new Vector(0, 0, 0), EntityType.FISHING_HOOK, e.getPlayer().getEntityId()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment