Search in sources :

Example 6 with Value

use of com.ichorpowered.guardianapi.util.item.value.mutable.Value in project guardian by ichorpowered.

the class AbstractDetectionContentLoader method acquireSingle.

@Override
public void acquireSingle(ContentKey<?> key) {
    if (this.contentContainer == null)
        return;
    final Optional<ConfigurationAssignment> assignment = key.getAssignments().stream().filter(contentAssignment -> contentAssignment.getClass().equals(ConfigurationAssignment.class)).map(contentAssignment -> (ConfigurationAssignment) contentAssignment).findFirst();
    if (!assignment.isPresent())
        return;
    final ConfigurationAssignment configurationAssignment = assignment.get();
    final CommentedConfigurationNode node = this.configurationFile.getNode(configurationAssignment.lookup().toArray());
    if (MapValue.class.isAssignableFrom(key.getDefaultValue().getClass())) {
        final Map<Object, Object> collect = Maps.newHashMap();
        if (node.hasMapChildren()) {
            for (final Map.Entry<Object, ? extends ConfigurationNode> entry : node.getChildrenMap().entrySet()) {
                collect.put(entry.getKey(), entry.getValue().getValue());
                this.contentContainer.attempt(key, GuardianMapValue.builder((Key) key).defaultElement(collect).element(collect).create());
            }
            return;
        }
    }
    if (Value.class.isAssignableFrom(key.getDefaultValue().getClass())) {
        try {
            Object value = node.getValue(key.getElementToken());
            this.contentContainer.attempt(key, GuardianValue.builder((Key) key).defaultElement(value).element(value).create());
        } catch (ObjectMappingException e) {
            e.printStackTrace();
        }
    }
}
Also used : ConfigFile(tech.ferus.util.config.ConfigFile) DetectionContentLoader(com.ichorpowered.guardianapi.detection.DetectionContentLoader) ConfigurationAssignment(com.ichorpowered.guardian.content.assignment.ConfigurationAssignment) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) Set(java.util.Set) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) Key(com.ichorpowered.guardianapi.util.item.key.Key) GuardianValue(com.ichorpowered.guardian.util.item.mutable.GuardianValue) GuardianMapValue(com.ichorpowered.guardian.util.item.mutable.GuardianMapValue) ContentKey(com.ichorpowered.guardianapi.content.key.ContentKey) Detection(com.ichorpowered.guardianapi.detection.Detection) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) Value(com.ichorpowered.guardianapi.util.item.value.mutable.Value) MapValue(com.ichorpowered.guardianapi.util.item.value.mutable.MapValue) Map(java.util.Map) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) ContentContainer(com.ichorpowered.guardianapi.content.ContentContainer) Optional(java.util.Optional) Path(java.nio.file.Path) ConfigurationAssignment(com.ichorpowered.guardian.content.assignment.ConfigurationAssignment) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) Map(java.util.Map) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 7 with Value

use of com.ichorpowered.guardianapi.util.item.value.mutable.Value in project guardian by ichorpowered.

the class AltitudeCapture method update.

@Override
public void update(PlayerEntry entry, CaptureContainer captureContainer) {
    if (!entry.getEntity(Player.class).isPresent() || !captureContainer.get(GuardianSequence.INITIAL_LOCATION).isPresent())
        return;
    final Player player = entry.getEntity(Player.class).get();
    final Value<Double> playerBoxWidth = ContentUtil.getFirst(ContentKeys.BOX_PLAYER_WIDTH, entry, this.getDetection().getContentContainer()).orElse(GuardianValue.empty());
    final Value<Double> playerBoxHeight = ContentUtil.getFirst(ContentKeys.BOX_PLAYER_HEIGHT, entry, this.getDetection().getContentContainer()).orElse(GuardianValue.empty());
    final Value<Double> playerBoxSafety = ContentUtil.getFirst(ContentKeys.BOX_PLAYER_SAFETY, entry, this.getDetection().getContentContainer()).orElse(GuardianValue.empty());
    final double playerWidth = playerBoxWidth.getDirect().orElse(1.2) + playerBoxSafety.getDirect().orElse(0.05);
    final double playerHeight = playerBoxHeight.getDirect().orElse(1.8) + playerBoxSafety.getDirect().orElse(0.05);
    final boolean isSneaking = player.get(Keys.IS_SNEAKING).isPresent() && player.get(Keys.IS_SNEAKING).get();
    final BoundingBox playerBox = WorldUtil.getBoundingBox(playerWidth, isSneaking ? (playerHeight - 0.15) : playerHeight);
    final Location location = player.getLocation();
    Location relativeAltitude = null;
    double blockDepthOffset = 0;
    captureContainer.offerIfEmpty(GuardianValue.builder(AltitudeCapture.RELATIVE_ALTITUDE).defaultElement(1d).element(1d).create());
    for (int n = 0; n < Math.abs(location.getY()); n++) {
        double i = this.amount * n;
        Optional<Location> maximumDepth = captureContainer.get(AltitudeCapture.INITIAL_DEPTH);
        Location currentDepth = location.sub(0, i, 0);
        if (!WorldUtil.isEmptyAtDepth(location, playerBox, i)) {
            if (maximumDepth.isPresent() && maximumDepth.get().getY() == currentDepth.getY()) {
                relativeAltitude = currentDepth.add(0, this.amount, 0);
                blockDepthOffset = 1;
                break;
            } else if (maximumDepth.isPresent() && maximumDepth.get().getY() < currentDepth.getY()) {
                relativeAltitude = currentDepth.add(0, this.amount, 0);
                blockDepthOffset = (currentDepth.getY() - maximumDepth.get().getY()) > -1 ? -1 : currentDepth.getY() - maximumDepth.get().getY();
                break;
            } else if (maximumDepth.isPresent() && maximumDepth.get().getY() > currentDepth.getY()) {
                relativeAltitude = currentDepth.add(0, this.amount, 0);
                blockDepthOffset = (maximumDepth.get().getY() - currentDepth.getY()) < 1 ? 1 : maximumDepth.get().getY() - currentDepth.getY();
                break;
            } else if (!maximumDepth.isPresent()) {
                captureContainer.offer(GuardianValue.builder(AltitudeCapture.INITIAL_DEPTH).defaultElement(currentDepth).element(currentDepth).create());
                relativeAltitude = currentDepth.add(0, this.amount, 0);
                break;
            }
        } else if ((currentDepth.getY() - 1) < 0) {
            if (!maximumDepth.isPresent()) {
                captureContainer.offer(GuardianValue.builder(AltitudeCapture.INITIAL_DEPTH).defaultElement(location.getExtent().getLocation(currentDepth.getX(), -256, currentDepth.getZ())).element(location.getExtent().getLocation(currentDepth.getX(), -256, currentDepth.getZ())).create());
            }
            if (maximumDepth.isPresent() && maximumDepth.get().getY() == currentDepth.getY()) {
                relativeAltitude = currentDepth.add(0, this.amount, 0);
                blockDepthOffset = -1;
                break;
            }
        }
    }
    if (!captureContainer.get(AltitudeCapture.INITIAL_DEPTH).isPresent() || relativeAltitude == null) {
        relativeAltitude = player.getLocation().setPosition(new Vector3d(player.getLocation().getX(), 0, player.getLocation().getZ()));
    }
    double relativeAltitudeOffset = (player.getLocation().getY() - relativeAltitude.getY()) - Math.abs(blockDepthOffset);
    if (this.liftOnly && relativeAltitudeOffset < 0)
        return;
    final double offset = relativeAltitudeOffset;
    captureContainer.getValue(AltitudeCapture.RELATIVE_ALTITUDE).ifPresent(value -> value.transform(original -> original + offset));
    captureContainer.getValue(AltitudeCapture.LAST_ALTITUDE).ifPresent(value -> value.set(offset));
}
Also used : Location(org.spongepowered.api.world.Location) GuardianSequence(com.ichorpowered.guardian.sequence.GuardianSequence) ContentKeys(com.ichorpowered.guardianapi.content.ContentKeys) Keys(org.spongepowered.api.data.key.Keys) ContentUtil(com.ichorpowered.guardian.util.ContentUtil) CaptureKey(com.ichorpowered.guardianapi.detection.capture.CaptureKey) GuardianCaptureKey(com.ichorpowered.guardian.sequence.capture.GuardianCaptureKey) Vector3d(com.flowpowered.math.vector.Vector3d) TypeToken(com.google.common.reflect.TypeToken) BoundingBox(com.ichorpowered.guardian.util.entity.BoundingBox) GuardianValue(com.ichorpowered.guardian.util.item.mutable.GuardianValue) Detection(com.ichorpowered.guardianapi.detection.Detection) AbstractCapture(com.ichorpowered.guardian.sequence.capture.AbstractCapture) Value(com.ichorpowered.guardianapi.util.item.value.mutable.Value) CaptureContainer(com.ichorpowered.guardianapi.detection.capture.CaptureContainer) Optional(java.util.Optional) PlayerEntry(com.ichorpowered.guardianapi.entry.entity.PlayerEntry) WorldUtil(com.ichorpowered.guardian.util.WorldUtil) Player(org.spongepowered.api.entity.living.player.Player) Nonnull(javax.annotation.Nonnull) Player(org.spongepowered.api.entity.living.player.Player) Vector3d(com.flowpowered.math.vector.Vector3d) BoundingBox(com.ichorpowered.guardian.util.entity.BoundingBox) Location(org.spongepowered.api.world.Location)

Example 8 with Value

use of com.ichorpowered.guardianapi.util.item.value.mutable.Value in project guardian by ichorpowered.

the class ControlCapture method update.

@Override
public void update(@Nonnull PlayerEntry entry, @Nonnull CaptureContainer captureContainer) {
    if (!entry.getEntity(Player.class).isPresent() || !captureContainer.get(GuardianSequence.INITIAL_LOCATION).isPresent())
        return;
    final Player player = entry.getEntity(Player.class).get();
    final MapValue<String, Integer> activeControls = GuardianMapValue.builder(ControlCapture.ACTIVE_CONTROL_TICKS).defaultElement(Maps.newHashMap()).element(Maps.newHashMap()).create();
    activeControls.put(FLY, 0);
    activeControls.put(WALK, 0);
    activeControls.put(SNEAK, 0);
    activeControls.put(SPRINT, 0);
    captureContainer.offerIfEmpty(activeControls);
    captureContainer.offerIfEmpty(GuardianValue.builder(ControlCapture.HORIZONTAL_DISTANCE).defaultElement(1d).element(1d).create());
    captureContainer.offerIfEmpty(GuardianValue.builder(ControlCapture.VERTICAL_DISTANCE).defaultElement(1d).element(1d).create());
    double walkSpeedData = player.get(Keys.WALKING_SPEED).orElse(1d) * 10;
    double flySpeedData = player.get(Keys.FLYING_SPEED).orElse(0.5) * 10;
    if (player.getLocation().getY() != captureContainer.get(GuardianSequence.INITIAL_LOCATION).get().getY()) {
        captureContainer.getValue(ControlCapture.VERTICAL_DISTANCE).ifPresent(value -> value.transform(original -> original * this.liftOffset));
    }
    if ((player.get(Keys.IS_FLYING).isPresent() && player.get(Keys.IS_FLYING).get())) {
        captureContainer.getValue(ControlCapture.HORIZONTAL_DISTANCE).ifPresent(value -> value.transform(original -> original * (this.flyOffset * flySpeedData)));
        captureContainer.getValue(ControlCapture.ACTIVE_CONTROL_TICKS).ifPresent(value -> value.put(FLY, value.get().get(FLY) + 1));
    } else if (player.get(Keys.IS_SPRINTING).isPresent() && player.get(Keys.IS_SPRINTING).get()) {
        captureContainer.getValue(ControlCapture.HORIZONTAL_DISTANCE).ifPresent(value -> value.transform(original -> original * (this.sprintOffset * walkSpeedData)));
        captureContainer.getValue(ControlCapture.ACTIVE_CONTROL_TICKS).ifPresent(value -> value.put(SPRINT, value.get().get(SPRINT) + 1));
    } else if (player.get(Keys.IS_SNEAKING).isPresent() && player.get(Keys.IS_SNEAKING).get()) {
        captureContainer.getValue(ControlCapture.HORIZONTAL_DISTANCE).ifPresent(value -> value.transform(original -> original * (this.sneakOffset * walkSpeedData)));
        captureContainer.getValue(ControlCapture.ACTIVE_CONTROL_TICKS).ifPresent(value -> value.put(SNEAK, value.get().get(SNEAK) + 1));
    } else {
        captureContainer.getValue(ControlCapture.HORIZONTAL_DISTANCE).ifPresent(value -> value.transform(original -> original * (this.walkOffset * walkSpeedData)));
        captureContainer.getValue(ControlCapture.ACTIVE_CONTROL_TICKS).ifPresent(value -> value.put(WALK, value.get().get(WALK) + 1));
    }
}
Also used : GuardianSequence(com.ichorpowered.guardian.sequence.GuardianSequence) ContentKeys(com.ichorpowered.guardianapi.content.ContentKeys) Keys(org.spongepowered.api.data.key.Keys) CaptureKey(com.ichorpowered.guardianapi.detection.capture.CaptureKey) GuardianCaptureKey(com.ichorpowered.guardian.sequence.capture.GuardianCaptureKey) TypeToken(com.google.common.reflect.TypeToken) Maps(com.google.common.collect.Maps) GuardianValue(com.ichorpowered.guardian.util.item.mutable.GuardianValue) GuardianMapValue(com.ichorpowered.guardian.util.item.mutable.GuardianMapValue) Detection(com.ichorpowered.guardianapi.detection.Detection) AbstractCapture(com.ichorpowered.guardian.sequence.capture.AbstractCapture) Value(com.ichorpowered.guardianapi.util.item.value.mutable.Value) CaptureContainer(com.ichorpowered.guardianapi.detection.capture.CaptureContainer) MapValue(com.ichorpowered.guardianapi.util.item.value.mutable.MapValue) Map(java.util.Map) PlayerEntry(com.ichorpowered.guardianapi.entry.entity.PlayerEntry) Player(org.spongepowered.api.entity.living.player.Player) Nonnull(javax.annotation.Nonnull) Player(org.spongepowered.api.entity.living.player.Player)

Example 9 with Value

use of com.ichorpowered.guardianapi.util.item.value.mutable.Value in project guardian by ichorpowered.

the class AbstractDetectionContentLoader method acquireAll.

@Override
public void acquireAll() {
    if (this.contentContainer == null || this.contentContainer.getPossibleKeys() == null)
        return;
    this.contentContainer.getPossibleKeys().forEach(key -> {
        final Optional<ConfigurationAssignment> assignment = key.getAssignments().stream().filter(contentAssignment -> contentAssignment.getClass().equals(ConfigurationAssignment.class)).map(contentAssignment -> (ConfigurationAssignment) contentAssignment).findFirst();
        if (!assignment.isPresent())
            return;
        final ConfigurationAssignment configurationAssignment = assignment.get();
        final CommentedConfigurationNode node = this.configurationFile.getNode(configurationAssignment.lookup().toArray());
        if (MapValue.class.isAssignableFrom(key.getDefaultValue().getClass())) {
            final Map<Object, Object> collect = Maps.newHashMap();
            if (node.hasMapChildren()) {
                for (final Map.Entry<Object, ? extends ConfigurationNode> entry : node.getChildrenMap().entrySet()) {
                    collect.put(entry.getKey(), entry.getValue().getValue());
                    this.contentContainer.attempt(key, GuardianMapValue.builder((Key) key).defaultElement(collect).element(collect).create());
                }
                return;
            }
        }
        if (Value.class.isAssignableFrom(key.getDefaultValue().getClass())) {
            try {
                Object value = node.getValue(key.getElementToken());
                this.contentContainer.attempt(key, GuardianValue.builder((Key) key).defaultElement(value).element(value).create());
            } catch (ObjectMappingException e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : ConfigFile(tech.ferus.util.config.ConfigFile) DetectionContentLoader(com.ichorpowered.guardianapi.detection.DetectionContentLoader) ConfigurationAssignment(com.ichorpowered.guardian.content.assignment.ConfigurationAssignment) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) Set(java.util.Set) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) Key(com.ichorpowered.guardianapi.util.item.key.Key) GuardianValue(com.ichorpowered.guardian.util.item.mutable.GuardianValue) GuardianMapValue(com.ichorpowered.guardian.util.item.mutable.GuardianMapValue) ContentKey(com.ichorpowered.guardianapi.content.key.ContentKey) Detection(com.ichorpowered.guardianapi.detection.Detection) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) Value(com.ichorpowered.guardianapi.util.item.value.mutable.Value) MapValue(com.ichorpowered.guardianapi.util.item.value.mutable.MapValue) Map(java.util.Map) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) ContentContainer(com.ichorpowered.guardianapi.content.ContentContainer) Optional(java.util.Optional) Path(java.nio.file.Path) ConfigurationAssignment(com.ichorpowered.guardian.content.assignment.ConfigurationAssignment) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) Map(java.util.Map) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Aggregations

GuardianValue (com.ichorpowered.guardian.util.item.mutable.GuardianValue)9 Detection (com.ichorpowered.guardianapi.detection.Detection)9 Value (com.ichorpowered.guardianapi.util.item.value.mutable.Value)9 Map (java.util.Map)8 Maps (com.google.common.collect.Maps)7 GuardianMapValue (com.ichorpowered.guardian.util.item.mutable.GuardianMapValue)7 MapValue (com.ichorpowered.guardianapi.util.item.value.mutable.MapValue)6 Optional (java.util.Optional)6 GuardianSequence (com.ichorpowered.guardian.sequence.GuardianSequence)5 ContentKeys (com.ichorpowered.guardianapi.content.ContentKeys)5 CaptureContainer (com.ichorpowered.guardianapi.detection.capture.CaptureContainer)5 Set (java.util.Set)5 Keys (org.spongepowered.api.data.key.Keys)5 Player (org.spongepowered.api.entity.living.player.Player)5 TypeToken (com.google.common.reflect.TypeToken)4 ConfigurationAssignment (com.ichorpowered.guardian.content.assignment.ConfigurationAssignment)4 AbstractCapture (com.ichorpowered.guardian.sequence.capture.AbstractCapture)4 GuardianCaptureKey (com.ichorpowered.guardian.sequence.capture.GuardianCaptureKey)4 ContentContainer (com.ichorpowered.guardianapi.content.ContentContainer)4 ContentKey (com.ichorpowered.guardianapi.content.key.ContentKey)4