implement riding

This commit is contained in:
Tofaa 2024-01-22 22:53:27 +03:00
parent b4cdf0e9cd
commit 3ad53b6fb4
4 changed files with 167 additions and 31 deletions

View file

@ -6,8 +6,9 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="9d5d9b6f-43c8-41a4-bb42-a66ffc96c9b0" name="Changes" comment=""> <list default="true" id="9d5d9b6f-43c8-41a4-bb42-a66ffc96c9b0" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/meta/display/ItemDisplayMeta.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/meta/display/ItemDisplayMeta.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/entitylib/TestDisplayCommand.java" beforeDir="false" afterPath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/entitylib/TestDisplayCommand.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/entitylib/TestDisplayCommand.java" beforeDir="false" afterPath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/entitylib/TestDisplayCommand.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test-plugin/src/main/resources/plugin.yml" beforeDir="false" afterPath="$PROJECT_DIR$/test-plugin/src/main/resources/plugin.yml" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -70,30 +71,30 @@
<option name="showExcludedFiles" value="false" /> <option name="showExcludedFiles" value="false" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent">{
"keyToString": { &quot;keyToString&quot;: {
"Gradle.Build EntityLib.executor": "Run", &quot;Gradle.Build EntityLib.executor&quot;: &quot;Run&quot;,
"Gradle.EntityLib:test-plugin [runServer].executor": "Debug", &quot;Gradle.EntityLib:test-plugin [runServer].executor&quot;: &quot;Debug&quot;,
"RunOnceActivity.OpenProjectViewOnStart": "true", &quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
"RunOnceActivity.ShowReadmeOnStart": "true", &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
"WebServerToolWindowFactoryState": "false", &quot;WebServerToolWindowFactoryState&quot;: &quot;false&quot;,
"git-widget-placeholder": "master", &quot;git-widget-placeholder&quot;: &quot;master&quot;,
"ignore.virus.scanning.warn.message": "true", &quot;ignore.virus.scanning.warn.message&quot;: &quot;true&quot;,
"jdk.selected.JAVA_MODULE": "corretto-17", &quot;jdk.selected.JAVA_MODULE&quot;: &quot;corretto-17&quot;,
"kotlin-language-version-configured": "true", &quot;kotlin-language-version-configured&quot;: &quot;true&quot;,
"last_opened_file_path": "D:/Github/EntityLib", &quot;last_opened_file_path&quot;: &quot;D:/Github/EntityLib&quot;,
"node.js.detected.package.eslint": "true", &quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
"node.js.detected.package.tslint": "true", &quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
"node.js.selected.package.eslint": "(autodetect)", &quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
"node.js.selected.package.tslint": "(autodetect)", &quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
"nodejs_package_manager_path": "npm", &quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
"project.structure.last.edited": "Modules", &quot;project.structure.last.edited&quot;: &quot;Modules&quot;,
"project.structure.proportion": "0.0", &quot;project.structure.proportion&quot;: &quot;0.0&quot;,
"project.structure.side.proportion": "0.0", &quot;project.structure.side.proportion&quot;: &quot;0.0&quot;,
"settings.editor.selected.configurable": "preferences.pluginManager", &quot;settings.editor.selected.configurable&quot;: &quot;preferences.pluginManager&quot;,
"vue.rearranger.settings.migration": "true" &quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
} }
}]]></component> }</component>
<component name="RecentsManager"> <component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS"> <key name="CopyFile.RECENT_KEYS">
<recent name="D:\Github\EntityLib\test-plugin" /> <recent name="D:\Github\EntityLib\test-plugin" />
@ -212,7 +213,8 @@
<workItem from="1705192736239" duration="496000" /> <workItem from="1705192736239" duration="496000" />
<workItem from="1705534524814" duration="15424000" /> <workItem from="1705534524814" duration="15424000" />
<workItem from="1705578156456" duration="78000" /> <workItem from="1705578156456" duration="78000" />
<workItem from="1705636302508" duration="4010000" /> <workItem from="1705636302508" duration="7111000" />
<workItem from="1705951390204" duration="1790000" />
</task> </task>
<servers /> <servers />
</component> </component>

View file

@ -11,8 +11,6 @@ import me.tofaa.entitylib.Tickable;
import me.tofaa.entitylib.meta.EntityMeta; import me.tofaa.entitylib.meta.EntityMeta;
import me.tofaa.entitylib.meta.types.ObjectData; import me.tofaa.entitylib.meta.types.ObjectData;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.awt.*;
import java.util.*; import java.util.*;
public class WrapperEntity implements Tickable { public class WrapperEntity implements Tickable {
@ -26,6 +24,10 @@ public class WrapperEntity implements Tickable {
private boolean spawned; private boolean spawned;
protected Vector3d velocity = Vector3d.zero(); protected Vector3d velocity = Vector3d.zero();
private int riding = -1;
private Set<Integer> passengers = new HashSet<>();
public WrapperEntity(int entityId, @NotNull UUID uuid, EntityType entityType, EntityMeta meta) { public WrapperEntity(int entityId, @NotNull UUID uuid, EntityType entityType, EntityMeta meta) {
this.uuid = Optional.of(uuid); this.uuid = Optional.of(uuid);
this.entityType = entityType; this.entityType = entityType;
@ -38,6 +40,121 @@ public class WrapperEntity implements Tickable {
sendPacketToViewers(meta.createPacket()); sendPacketToViewers(meta.createPacket());
} }
/**
* Adds a passenger to the entity. The passenger will be visible to all viewers of the entity.
* @param passenger the entity id of the passenger
*/
public void addPassenger(int passenger) {
if (passengers.contains(passenger)) {
throw new IllegalArgumentException("Passenger already exists");
}
passengers.add(passenger);
sendPacketToViewers(createPassengerPacket());
WrapperEntity e = EntityLib.getEntity(passenger);
if (e != null) {
e.riding = this.entityId;
}
}
/**
* Adds multiple passengers to the entity. The passengers will be visible to all viewers of the entity.
* @param passengers the entity ids of the passengers
*/
public void addPassengers(int... passengers) {
for (int passenger : passengers) {
addPassenger(passenger);
}
}
/**
* Adds a passenger to the entity. The passenger will be visible to all viewers of the entity.
* @param passenger the wrapper entity passenger
*/
public void addPassenger(WrapperEntity passenger) {
addPassenger(passenger.getEntityId());
}
/**
* Adds multiple passengers to the entity. The passengers will be visible to all viewers of the entity.
* @param passengers the wrapper entity passengers
*/
public void addPassengers(WrapperEntity... passengers) {
for (WrapperEntity passenger : passengers) {
addPassenger(passenger);
}
}
/**
* Removes a passenger from the entity. The passenger will be removed from the view of all viewers of the entity.
* @param passenger the entity id of the passenger
*/
public void removePassenger(int passenger) {
if (!passengers.contains(passenger)) {
throw new IllegalArgumentException("Passenger does not exist");
}
passengers.remove(passenger);
sendPacketToViewers(createPassengerPacket());
WrapperEntity e = EntityLib.getEntity(passenger);
if (e != null) {
e.riding = -1;
}
}
/**
* Removes multiple passengers from the entity. The passengers will be removed from the view of all viewers of the entity.
* @param passengers the entity ids of the passengers
*/
public void removePassengers(int... passengers) {
for (int passenger : passengers) {
removePassenger(passenger);
}
}
/**
* Removes a passenger from the entity. The passenger will be removed from the view of all viewers of the entity.
* @param passenger the wrapper entity passenger
*/
public void removePassenger(WrapperEntity passenger) {
removePassenger(passenger.getEntityId());
}
/**
* Removes multiple passengers from the entity. The passengers will be removed from the view of all viewers of the entity.
* @param passengers the wrapper entity passengers
*/
public void removePassengers(WrapperEntity... passengers) {
for (WrapperEntity passenger : passengers) {
removePassenger(passenger);
}
}
/**
* @return true if the entity has passengers, false otherwise
*/
public boolean isRiding() {
return riding != -1;
}
/**
* Clears all passengers from the entity. The passengers will be removed from the view of all viewers of the entity.
*/
public void clearPassengers() {
if (passengers.isEmpty()) return;
passengers.clear();
sendPacketToViewers(createPassengerPacket());
}
/**
* @return the entity id of the entity that the entity is riding, -1 if the entity is not riding
*/
public int getRidingId() {
return riding;
}
protected WrapperPlayServerSetPassengers createPassengerPacket() {
return new WrapperPlayServerSetPassengers(entityId, passengers.stream().mapToInt(i -> i).toArray());
}
public boolean spawn(Location location) { public boolean spawn(Location location) {
if (spawned) return false; if (spawned) return false;
this.location = location; this.location = location;
@ -100,7 +217,17 @@ public class WrapperEntity implements Tickable {
rotateHead(entity.getLocation()); rotateHead(entity.getLocation());
} }
/**
* Desyncs if the entity is riding some other entity. TODO: fix. This is a temporary solution.
* @return the location of the entity
*/
public Location getLocation() { public Location getLocation() {
if (isRiding()) {
WrapperEntity riding = EntityLib.getEntity(this.riding);
assert riding != null;
return riding.getLocation();
}
return new Location(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch()); return new Location(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
} }

View file

@ -3,6 +3,8 @@ package me.tofaa.entitylib;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.item.ItemStack; import com.github.retrooper.packetevents.protocol.item.ItemStack;
import com.github.retrooper.packetevents.protocol.item.type.ItemTypes; import com.github.retrooper.packetevents.protocol.item.type.ItemTypes;
import com.github.retrooper.packetevents.protocol.world.Location;
import com.github.retrooper.packetevents.util.Vector3d;
import com.github.retrooper.packetevents.util.Vector3f; import com.github.retrooper.packetevents.util.Vector3f;
import io.github.retrooper.packetevents.util.SpigotConversionUtil; import io.github.retrooper.packetevents.util.SpigotConversionUtil;
import me.tofaa.entitylib.entity.WrapperEntity; import me.tofaa.entitylib.entity.WrapperEntity;
@ -51,11 +53,11 @@ public class TestDisplayCommand implements CommandExecutor, TabCompleter {
WrapperEntity e = EntityLib.createEntity(UUID.randomUUID(), EntityTypes.BLOCK_DISPLAY); WrapperEntity e = EntityLib.createEntity(UUID.randomUUID(), EntityTypes.BLOCK_DISPLAY);
BlockDisplayMeta meta = (BlockDisplayMeta) e.getMeta(); BlockDisplayMeta meta = (BlockDisplayMeta) e.getMeta();
meta.setHasGlowingEffect(true); meta.setHasGlowingEffect(true);
meta.setBlockId(Material.AMETHYST_BLOCK.getId()); meta.setBlockId(SpigotConversionUtil.fromBukkitBlockData(Material.ACACIA_LOG.createBlockData()).getGlobalId());
meta.setBillboardConstraints(DisplayMeta.BillboardConstraints.CENTER); meta.setBillboardConstraints(DisplayMeta.BillboardConstraints.CENTER);
meta.setScale(new Vector3f(2, 2, 2)); meta.setScale(new Vector3f(2, 2, 2));
e.addViewer(player.getUniqueId()); e.addViewer(player.getUniqueId());
e.spawn(SpigotConversionUtil.fromBukkitLocation(player.getLocation())); e.spawn(fromPlayer(player));
} }
private void text(Player player) { private void text(Player player) {
@ -66,7 +68,7 @@ public class TestDisplayCommand implements CommandExecutor, TabCompleter {
meta.setBillboardConstraints(DisplayMeta.BillboardConstraints.CENTER); meta.setBillboardConstraints(DisplayMeta.BillboardConstraints.CENTER);
meta.setScale(new Vector3f(2, 2, 2)); meta.setScale(new Vector3f(2, 2, 2));
e.addViewer(player.getUniqueId()); e.addViewer(player.getUniqueId());
e.spawn(SpigotConversionUtil.fromBukkitLocation(player.getLocation())); e.spawn(fromPlayer(player));
} }
private void item(Player player) { private void item(Player player) {
@ -77,7 +79,11 @@ public class TestDisplayCommand implements CommandExecutor, TabCompleter {
.type(ItemTypes.ACACIA_BOAT).build() .type(ItemTypes.ACACIA_BOAT).build()
); );
e.addViewer(player.getUniqueId()); e.addViewer(player.getUniqueId());
e.spawn(SpigotConversionUtil.fromBukkitLocation(player.getLocation())); e.spawn(fromPlayer(player));
}
private static Location fromPlayer(Player player) {
return new Location(player.getLocation().getX(), player.getLocation().getY() + 2, player.getLocation().getZ(), 0f, 0f);
} }
@Nullable @Nullable

View file

@ -3,6 +3,7 @@ version: 1.0.0
depend: depend:
- packetevents - packetevents
main: me.tofaa.entitylib.EntityLibPlugin main: me.tofaa.entitylib.EntityLibPlugin
api-version: 1.20
commands: commands:
testapi: testapi:
description: Test PEEntityMeta API description: Test PEEntityMeta API