use of com.ichorpowered.guardianapi.util.item.value.mutable.Value in project guardian by ichorpowered.
the class FlightCheck method getSequence.
@Override
public SequenceBlueprint<Event> getSequence(final Detection detection) {
final Double analysisTime = detection.getContentContainer().get(ContentKeys.ANALYSIS_TIME).orElse(GuardianValue.empty()).getDirect().orElse(0d) / 0.05;
final Double analysisIntercept = detection.getContentContainer().get(ContentKeys.ANALYSIS_INTERCEPT).orElse(GuardianValue.empty()).getDirect().orElse(0d);
final Double minimumTickRate = detection.getContentContainer().get(ContentKeys.ANALYSIS_MINIMUM_TICK).orElse(GuardianValue.empty()).getDirect().orElse(0d) / 0.05;
final Double maximumTickRate = detection.getContentContainer().get(ContentKeys.ANALYSIS_MAXIMUM_TICK).orElse(GuardianValue.empty()).getDirect().orElse(0d) / 0.05;
return new GuardianSequenceBuilder().capture(new ControlCapture(detection.getPlugin(), detection)).capture(new AltitudeCapture(detection.getPlugin(), detection)).capture(new MaterialCapture(detection.getPlugin(), detection)).capture(new PotionEffectCapture(detection.getPlugin(), detection)).observe(MoveEntityEvent.class).after().delay(analysisTime.intValue()).condition(sequenceContext -> {
final GuardianPlayerEntry<Player> entityEntry = sequenceContext.get(CommonContextKeys.ENTITY_ENTRY);
final Summary summary = sequenceContext.get(CommonContextKeys.SUMMARY);
final GuardianCaptureRegistry captureRegistry = sequenceContext.get(CommonContextKeys.CAPTURE_REGISTRY);
final long lastActionTime = sequenceContext.get(CommonContextKeys.LAST_ACTION_TIME);
summary.set(SequenceReport.class, new SequenceReport(false, Origin.source(sequenceContext.getRoot()).owner(entityEntry).build()));
if (!entityEntry.getEntity(Player.class).isPresent())
return false;
final Player player = entityEntry.getEntity(Player.class).get();
/*
* Capture Collection
*/
final CaptureContainer captureContainer = captureRegistry.getContainer();
final Optional<Location> initial = captureContainer.get(GuardianSequence.INITIAL_LOCATION);
final Optional<Double> effectLiftAmplifier = captureContainer.get(PotionEffectCapture.VERTICAL_SPEED_MODIFIER);
final Optional<Double> materialSpeedAmplifier = captureContainer.get(MaterialCapture.SPEED_MODIFIER);
final Optional<Double> altitude = captureContainer.get(AltitudeCapture.RELATIVE_ALTITUDE);
final Optional<Map<String, Integer>> materialStateTicks = captureContainer.get(MaterialCapture.ACTIVE_MATERIAL_TICKS);
final Optional<Map<String, Integer>> controlStateTicks = captureContainer.get(ControlCapture.ACTIVE_CONTROL_TICKS);
if (!initial.isPresent() || !materialSpeedAmplifier.isPresent() || !altitude.isPresent() || !materialStateTicks.isPresent())
return false;
final Value<Double> playerBoxWidth = ContentUtil.getFirst(ContentKeys.BOX_PLAYER_WIDTH, entityEntry, detection.getContentContainer()).orElse(GuardianValue.empty());
final Value<Double> playerBoxHeight = ContentUtil.getFirst(ContentKeys.BOX_PLAYER_HEIGHT, entityEntry, detection.getContentContainer()).orElse(GuardianValue.empty());
final Value<Double> playerBoxSafety = ContentUtil.getFirst(ContentKeys.BOX_PLAYER_SAFETY, entityEntry, detection.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);
long current = System.currentTimeMillis();
// Gets the average time between now and the last action.
double averageActionTime = ((current - lastActionTime) / 1000) / 0.05;
if (averageActionTime < minimumTickRate) {
detection.getLogger().warn("The server may be overloaded. A check could not be completed.");
return false;
} else if (averageActionTime > maximumTickRate) {
return false;
}
if (player.get(Keys.VEHICLE).isPresent() || (player.get(Keys.IS_FLYING).isPresent() && player.get(Keys.IS_FLYING).get()) || player.getLocation().getY() < 1)
return false;
final double intercept = analysisIntercept + (effectLiftAmplifier.orElse(0d) / analysisTime);
// Gets the players vertical displacement in the world.
final double verticalDisplacement = ((player.getLocation().getY() - initial.get().getY()) == 0) ? intercept : player.getLocation().getY() - initial.get().getY();
// Gets the players relative altitude to the ground.
final double averageAltitude = altitude.get() / averageActionTime;
// Gets the time the player is on solid ground or a liquid.
final int solidMaterialTime = materialStateTicks.get().get(MaterialCapture.SOLID);
final int liquidMaterialTime = materialStateTicks.get().get(MaterialCapture.LIQUID);
// Gets the time the player is using flight.
final int flightControlTime = controlStateTicks.get().get(ControlCapture.FLY);
if (verticalDisplacement <= 1 || averageAltitude <= 1 || WorldUtil.containsBlocksUnder(player.getLocation(), playerBox, 1d) || solidMaterialTime > 1 || liquidMaterialTime > 1 || flightControlTime > 1)
return false;
if (((verticalDisplacement / averageAltitude) + averageAltitude) > intercept) {
// ------------------------- DEBUG -----------------------------
System.out.println(player.getName() + " has been caught using fly hacks. (" + ((verticalDisplacement / averageAltitude) + averageAltitude) + ")");
// -------------------------------------------------------------
SequenceReport report = new SequenceReport(true, Origin.source(sequenceContext.getRoot()).owner(entityEntry).build());
report.put("type", "Flight");
report.put("information", Collections.singletonList("Gained altitude over " + ((verticalDisplacement / averageAltitude) + averageAltitude) + "."));
report.put("initial_location", initial.get());
report.put("final_location", player.getLocation());
report.put("severity", ((verticalDisplacement / averageAltitude) + averageAltitude) / (verticalDisplacement + averageAltitude));
summary.set(SequenceReport.class, report);
return true;
}
return false;
}, ConditionType.NORMAL).build(SequenceContext.builder().owner(detection).root(this).build());
}
use of com.ichorpowered.guardianapi.util.item.value.mutable.Value in project guardian by ichorpowered.
the class PotionEffectCapture 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();
captureContainer.offerIfEmpty(GuardianValue.builder(PotionEffectCapture.HORIZONTAL_SPEED_MODIFIER).defaultElement(1d).element(1d).create());
captureContainer.offerIfEmpty(GuardianValue.builder(PotionEffectCapture.VERTICAL_SPEED_MODIFIER).defaultElement(1d).element(1d).create());
final List<PotionEffect> potionEffects = player.get(Keys.POTION_EFFECTS).orElse(Lists.newArrayList());
if (!potionEffects.isEmpty()) {
for (PotionEffect potionEffect : potionEffects) {
final String potionName = potionEffect.getType().getName().toLowerCase();
if (this.effectSpeed.containsKey(potionName)) {
final double effectValue = this.effectSpeed.get(potionName);
captureContainer.getValue(PotionEffectCapture.HORIZONTAL_SPEED_MODIFIER).ifPresent(value -> value.transform(original -> original + ((potionEffect.getAmplifier() + 1) * effectValue)));
}
if (this.effectLift.containsKey(potionName)) {
final double effectValue = this.effectLift.get(potionName);
captureContainer.getValue(PotionEffectCapture.VERTICAL_SPEED_MODIFIER).ifPresent(value -> value.transform(original -> original + ((potionEffect.getAmplifier() + 1) * effectValue)));
}
}
}
}
use of com.ichorpowered.guardianapi.util.item.value.mutable.Value in project guardian by ichorpowered.
the class MaterialCapture 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 Location<World> location = player.getLocation();
final MapValue<String, Integer> activeMaterials = GuardianMapValue.builder(MaterialCapture.ACTIVE_MATERIAL_TICKS).defaultElement(Maps.newHashMap()).element(Maps.newHashMap()).create();
activeMaterials.put(GAS, 0);
activeMaterials.put(LIQUID, 0);
activeMaterials.put(SOLID, 0);
captureContainer.offerIfEmpty(activeMaterials);
captureContainer.offerIfEmpty(GuardianValue.builder(MaterialCapture.SPEED_MODIFIER).defaultElement(1d).element(1d).create());
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);
if (!WorldUtil.containsBlocksUnder(location, playerBox, 1.25)) {
final double gasSpeed = this.matterSpeed.get(GAS);
captureContainer.getValue(MaterialCapture.SPEED_MODIFIER).ifPresent(value -> value.transform(original -> original * gasSpeed));
captureContainer.getValue(MaterialCapture.ACTIVE_MATERIAL_TICKS).ifPresent(value -> value.put(GAS, value.get().get(GAS) + 1));
} else if (WorldUtil.anyLiquidAtDepth(location, playerBox, 1d) || WorldUtil.anyLiquidAtDepth(location, playerBox, 0) || WorldUtil.anyLiquidAtDepth(location, playerBox, isSneaking ? -(playerHeight - 0.25) : -playerHeight)) {
final double liquidSpeed = this.matterSpeed.get(LIQUID);
captureContainer.getValue(MaterialCapture.SPEED_MODIFIER).ifPresent(value -> value.transform(original -> original * liquidSpeed));
captureContainer.getValue(MaterialCapture.ACTIVE_MATERIAL_TICKS).ifPresent(value -> value.put(LIQUID, value.get().get(LIQUID) + 1));
} else {
final List<BlockType> surroundingBlockTypes = WorldUtil.getBlocksUnder(location, playerBox, 1.25);
for (final BlockType blockType : surroundingBlockTypes) {
final double speedModifier = this.materialSpeed.getOrDefault(blockType.getName().toLowerCase(), this.matterSpeed.get(SOLID));
captureContainer.getValue(MaterialCapture.SPEED_MODIFIER).ifPresent(value -> value.transform(original -> original * speedModifier));
}
captureContainer.getValue(MaterialCapture.ACTIVE_MATERIAL_TICKS).ifPresent(value -> value.put(SOLID, value.get().get(SOLID) + 1));
}
}
use of com.ichorpowered.guardianapi.util.item.value.mutable.Value in project guardian by ichorpowered.
the class AbstractDetectionContentLoader method save.
@Override
public void save() {
if (this.contentContainer == null)
return;
this.contentContainer.getMap().forEach((key, value) -> {
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();
this.configurationFile.getNode(configurationAssignment.lookup().toArray()).setValue(key.getElementToken());
});
try {
this.configurationFile.save();
} catch (IOException e) {
e.printStackTrace();
}
}
use of com.ichorpowered.guardianapi.util.item.value.mutable.Value in project guardian by ichorpowered.
the class AbstractDetectionContentLoader method acquireAll.
@Override
public void acquireAll(Set<ContentKey<?>> contentKeys) {
if (this.contentContainer == null)
return;
contentKeys.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();
}
}
});
}
Aggregations