Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/move entities list #705

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.InventoryView;

import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -270,7 +271,8 @@ private void rotateEntitiesOnCraft(Location tOP) {
(oldHitBox.getMaxY() + oldHitBox.getMinY())/2.0,
(oldHitBox.getMaxZ() + oldHitBox.getMinZ())/2.0);

List<EntityType> entityList = List.of(EntityType.PLAYER, EntityType.PRIMED_TNT);
EnumSet<EntityType> entityList = EnumSet.of(EntityType.PLAYER, EntityType.PRIMED_TNT);
EnumSet<EntityType> shouldBeProcessed = (EnumSet<EntityType>) craft.getType().getObjectProperty(CraftType.MOVE_ENTITIES_LIST);
for(Entity entity : craft.getWorld().getNearbyEntities(midpoint,
oldHitBox.getXLength() / 2.0 + 1,
oldHitBox.getYLength() / 2.0 + 2,
Expand All @@ -284,6 +286,10 @@ private void rotateEntitiesOnCraft(Location tOP) {
continue;
}// Player is onboard this craft

if(!shouldBeProcessed.contains(entity)) {
continue;
}

Location adjustedPLoc = entity.getLocation().subtract(tOP);

double[] rotatedCoords = MathUtils.rotateVecNoRound(rotation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ private void preventsTorpedoRocketsPilots() {
(oldHitBox.getMaxX() + oldHitBox.getMinX()) / 2.0,
(oldHitBox.getMaxY() + oldHitBox.getMinY()) / 2.0,
(oldHitBox.getMaxZ() + oldHitBox.getMinZ()) / 2.0);

EnumSet<EntityType> shouldBeProcessed = (EnumSet<EntityType>) craft.getType().getObjectProperty(CraftType.MOVE_ENTITIES_LIST);
for (Entity entity : craft.getWorld().getNearbyEntities(midpoint,
oldHitBox.getXLength() / 2.0 + 1,
oldHitBox.getYLength() / 2.0 + 2,
Expand All @@ -385,6 +387,10 @@ private void preventsTorpedoRocketsPilots() {
continue;
}

if (!shouldBeProcessed.contains(entity)) {
continue;
}

CraftTeleportEntityEvent e = new CraftTeleportEntityEvent(craft, entity);
Bukkit.getServer().getPluginManager().callEvent(e);
if (e.isCancelled())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@
import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound;
import org.bukkit.*;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
Expand Down Expand Up @@ -155,6 +158,7 @@ final public class CraftType {
public static final NamespacedKey CAN_HOVER = buildKey("can_hover");
public static final NamespacedKey CAN_HOVER_OVER_WATER = buildKey("can_hover_over_water");
public static final NamespacedKey MOVE_ENTITIES = buildKey("move_entities");
public static final NamespacedKey MOVE_ENTITIES_LIST = buildKey("move_entities_list");
public static final NamespacedKey ONLY_MOVE_PLAYERS = buildKey("only_move_players");
public static final NamespacedKey USE_GRAVITY = buildKey("use_gravity");
public static final NamespacedKey HOVER_LIMIT = buildKey("hover_limit");
Expand Down Expand Up @@ -190,6 +194,7 @@ final public class CraftType {
public static final NamespacedKey EXPLOSION_ARMING_TIME = buildKey("explosion_arming_time");
public static final NamespacedKey DIRECTIONAL_DEPENDENT_MATERIALS = buildKey("directional_dependent_materials");
public static final NamespacedKey ALLOW_INTERNAL_COLLISION_EXPLOSION = buildKey("allow_internal_collision_explosion");
private static final Logger log = LoggerFactory.getLogger(CraftType.class);
Intybyte marked this conversation as resolved.
Show resolved Hide resolved
//endregion

@Contract("_ -> new")
Expand Down Expand Up @@ -495,6 +500,16 @@ public static void registerTypeValidator(Predicate<CraftType> validator, String
registerProperty(new BooleanProperty("canHover", CAN_HOVER, type -> false));
registerProperty(new BooleanProperty("canHoverOverWater", CAN_HOVER_OVER_WATER, type -> true));
registerProperty(new BooleanProperty("moveEntities", MOVE_ENTITIES, type -> true));
registerProperty(new ObjectPropertyImpl("moveEntitiesList", MOVE_ENTITIES_LIST,
(data, type, fileKey, namespacedKey) -> {
var entityStringList = data.getStringList(fileKey);
EnumSet<EntityType> entityList = EnumSet.noneOf(EntityType.class);
for (String entityString : entityStringList) {
entityList.addAll(Tags.parseEntities(entityString));
}

return entityList;
}, type -> EnumSet.noneOf(EntityType.class)));
registerProperty(new BooleanProperty("onlyMovePlayers", ONLY_MOVE_PLAYERS, type -> true));
registerProperty(new BooleanProperty("useGravity", USE_GRAVITY, type -> false));
registerProperty(new IntegerProperty("hoverLimit", HOVER_LIMIT, type -> 0));
Expand Down
37 changes: 37 additions & 0 deletions api/src/main/java/net/countercraft/movecraft/util/Tags.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Tag;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -135,4 +136,40 @@ public static EnumSet<Material> parseMaterials(@NotNull String materialName) {
}
return returnSet;
}

@Nullable
public static EnumSet<EntityType> parseEntityRegistry(@NotNull String string) {
if (!string.startsWith("#"))
return null;

String nameKey = string.substring(1);
var key = keyFromString(nameKey);
if (key == null)
throw new IllegalArgumentException("Entry " + string + " is not a valid namespace key!");

var tag = Bukkit.getTag(Tag.REGISTRY_ENTITY_TYPES, key, EntityType.class);
if (tag == null)
throw new IllegalArgumentException("Entry " + string + " is not a valid tag!");

var tags = tag.getValues();
return tags.isEmpty() ? EnumSet.noneOf(EntityType.class) : EnumSet.copyOf(tags);
}

/**
* Searches for a tag which matches the provided entityName. Failing that, it attempts to load a matching singular EntityType directly.
*
* @param entityName EntityType name or tag
* @return the set of EntityType the tag/EntityType resolves to
*/
@NotNull
public static EnumSet<EntityType> parseEntities(@NotNull String entityName) {
EnumSet<EntityType> returnSet = EnumSet.noneOf(EntityType.class);
EnumSet<EntityType> tagged = parseEntityRegistry(entityName);
if (tagged != null) {
returnSet.addAll(tagged);
} else {
returnSet.add(EntityType.valueOf(entityName.toUpperCase()));
}
return returnSet;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"values": ["elder_guardian", "wither_skeleton", "stray", "husk", "zombie_villager", "evoker", "vex", "vindicator", "illusioner", "creeper", "skeleton", "spider", "giant", "zombie", "zombified_piglin", "enderman", "cave_spider", "silverfish", "blaze", "wither", "witch", "endermite", "guardian", "drowned", "pillager", "ravager", "piglin", "zoglin", "piglin_brute", "warden", "breeze", "bogged"]
Intybyte marked this conversation as resolved.
Show resolved Hide resolved
}