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 3 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 @@ -39,21 +39,28 @@
import net.countercraft.movecraft.craft.type.transform.StringTransform;
import net.countercraft.movecraft.craft.type.transform.Transform;
import net.countercraft.movecraft.processing.MovecraftWorld;
import net.countercraft.movecraft.util.EntityUtil;
import net.countercraft.movecraft.util.Pair;
import net.countercraft.movecraft.util.Tags;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound;
import org.bukkit.*;
import org.bukkit.entity.Animals;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Monster;
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;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -155,6 +162,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 +198,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);
//endregion

@Contract("_ -> new")
Expand Down Expand Up @@ -495,6 +504,40 @@ 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) {
String upper = entityString.toUpperCase(Locale.ROOT);

// User wants a premade list
if(upper.startsWith("@")) {

switch (upper.substring(1)) {
case "monsters" -> {
entityList.addAll(EntityUtil.getClassEntities(Monster.class));
continue;
}

case "animals" -> {
entityList.addAll(EntityUtil.getClassEntities(Animals.class));
continue;
}

default -> throw new TypeData.InvalidValueException("Value for " + fileKey + " is not a valid EntityType list");
}
}

try {
EntityType.valueOf(upper);
} catch (Exception e) {
throw new TypeData.InvalidValueException("Value for " + fileKey + " must be an EntityType");
}
}

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
25 changes: 25 additions & 0 deletions api/src/main/java/net/countercraft/movecraft/util/EntityUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.countercraft.movecraft.util;

import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;

import java.util.EnumSet;

public class EntityUtil {
public static EnumSet<EntityType> getClassEntities(Class<? extends Entity> clazz) {
var entityList = EnumSet.noneOf(EntityType.class);

for(EntityType type : EntityType.values()) {
var entityClazz = type.getEntityClass();

if (entityClazz == null)
continue;

if (entityClazz.isAssignableFrom(clazz)) {
entityList.add(type);
}
}

return entityList;
}
}