make EntityLib#createEntity() and createMeta() not null

This commit is contained in:
Tofaa 2024-01-18 03:05:32 +03:00
parent e8d411d989
commit dd01591996
3 changed files with 12 additions and 21 deletions

View file

@ -5,16 +5,8 @@
</component> </component>
<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 afterPath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/entitylib/TestDisplayCommand.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/build.gradle" beforeDir="false" afterPath="$PROJECT_DIR$/build.gradle" 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$/src/main/java/me/tofaa/entitylib/MetaConverterRegistry.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/MetaConverterRegistry.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/meta/other/BlockDisplayMeta.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/meta/display/BlockDisplayMeta.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/meta/other/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/meta/other/TextDisplayMeta.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/meta/display/TextDisplayMeta.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/meta/types/DisplayMeta.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/me/tofaa/entitylib/meta/types/DisplayMeta.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test-plugin/build.gradle" beforeDir="false" afterPath="$PROJECT_DIR$/test-plugin/build.gradle" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/entitylib/EntityLibPlugin.java" beforeDir="false" afterPath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/entitylib/EntityLibPlugin.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" />
@ -216,7 +208,7 @@
<workItem from="1704485939274" duration="2440000" /> <workItem from="1704485939274" duration="2440000" />
<workItem from="1704502790346" duration="6191000" /> <workItem from="1704502790346" duration="6191000" />
<workItem from="1705192736239" duration="496000" /> <workItem from="1705192736239" duration="496000" />
<workItem from="1705534524814" duration="1048000" /> <workItem from="1705534524814" duration="1694000" />
</task> </task>
<servers /> <servers />
</component> </component>

View file

@ -127,12 +127,11 @@ public final class EntityLib {
* @param entityType the entity type * @param entityType the entity type
* @return the created entity, or null if the entity could not be created * @return the created entity, or null if the entity could not be created
*/ */
public static @Nullable WrapperEntity createEntity(int entityId, UUID uuid, EntityType entityType) { public static @NotNull WrapperEntity createEntity(int entityId, UUID uuid, EntityType entityType) {
checkInit(); checkInit();
if (entities.containsKey(uuid)) throw new RuntimeException("An entity with that uuid already exists"); if (entities.containsKey(uuid)) throw new RuntimeException("An entity with that uuid already exists");
if (entitiesById.containsKey(entityId)) throw new RuntimeException("An entity with that id already exists"); if (entitiesById.containsKey(entityId)) throw new RuntimeException("An entity with that id already exists");
EntityMeta meta = createMeta(entityId, entityType); EntityMeta meta = createMeta(entityId, entityType);
if (meta == null) return null;
WrapperEntity entity; WrapperEntity entity;
if (meta instanceof LivingEntityMeta) { if (meta instanceof LivingEntityMeta) {
entity = new WrapperLivingEntity(entityId, uuid, entityType, meta); entity = new WrapperLivingEntity(entityId, uuid, entityType, meta);
@ -145,7 +144,7 @@ public final class EntityLib {
return entity; return entity;
} }
public static @Nullable WrapperEntity createEntity(@NotNull UUID uuid, EntityType entityType) { public static @NotNull WrapperEntity createEntity(@NotNull UUID uuid, EntityType entityType) {
return createEntity(entityIdProvider.provide(), uuid, entityType); return createEntity(entityIdProvider.provide(), uuid, entityType);
} }
@ -182,7 +181,7 @@ public final class EntityLib {
* @param entityType the entity type * @param entityType the entity type
* @return the created EntityMeta * @return the created EntityMeta
*/ */
public static EntityMeta createMeta(int entityId, EntityType entityType) { public static @NotNull EntityMeta createMeta(int entityId, EntityType entityType) {
checkInit(); checkInit();
Metadata m = new Metadata(entityId); Metadata m = new Metadata(entityId);
BiFunction<Integer, Metadata, EntityMeta> function = metaRegistry.get(entityType); BiFunction<Integer, Metadata, EntityMeta> function = metaRegistry.get(entityType);

View file

@ -6,6 +6,7 @@ import me.tofaa.entitylib.entity.WrapperEntity;
import me.tofaa.entitylib.meta.display.TextDisplayMeta; import me.tofaa.entitylib.meta.display.TextDisplayMeta;
import me.tofaa.entitylib.meta.types.DisplayMeta; import me.tofaa.entitylib.meta.types.DisplayMeta;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -18,14 +19,13 @@ public class TestDisplayCommand implements CommandExecutor {
@Override @Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) { public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
Player player = (Player) commandSender;
WrapperEntity e = EntityLib.createEntity(UUID.randomUUID(), EntityTypes.TEXT_DISPLAY); WrapperEntity e = EntityLib.createEntity(UUID.randomUUID(), EntityTypes.TEXT_DISPLAY);
TextDisplayMeta meta = (TextDisplayMeta) e.getMeta(); TextDisplayMeta meta = (TextDisplayMeta) e.getMeta();
meta.setText(Component.text("Hello World!")); meta.setText(Component.text("Hello World!", NamedTextColor.GOLD));
meta.setBillboardConstraints(DisplayMeta.BillboardConstraints.CENTER); meta.setBillboardConstraints(DisplayMeta.BillboardConstraints.CENTER);
e.addViewer(((Player) commandSender).getUniqueId()); e.addViewer((player.getUniqueId()));
e.spawn(SpigotConversionUtil.fromBukkitLocation(((Player) commandSender).getLocation())); e.spawn(SpigotConversionUtil.fromBukkitLocation((player.getLocation().clone().add(0, 2, 0))));
commandSender.sendMessage("Entity spawned! ID: " + e.getEntityId() + " Type: " + e.getEntityType() + " Text: " + meta.getText());
return true; return true;
} }
} }