Search in sources :

Example 1 with Sponge

use of org.spongepowered.api.Sponge in project SpongeCommon by SpongePowered.

the class MixinEntityPlayer method attackTargetEntityWithCurrentItem.

/**
 * @author gabizou - April 8th, 2016
 * @author gabizou - April 11th, 2016 - Update for 1.9 - This enitre method was rewritten
 *
 * @reason Rewrites the attackTargetEntityWithCurrentItem to throw an {@link AttackEntityEvent} prior
 * to the ensuing {@link DamageEntityEvent}. This should cover all cases where players are
 * attacking entities and those entities override {@link EntityLivingBase#attackEntityFrom(DamageSource, float)}
 * and effectively bypass our damage event hooks.
 *
 * LVT Rename Table:
 * float f        | damage               |
 * float f1       | enchantmentDamage    |
 * float f2       | attackStrength       |
 * boolean flag   | isStrongAttack       |
 * boolean flag1  | isSprintingAttack    |
 * boolean flag2  | isCriticalAttack     | Whether critical particles will spawn and of course, multiply the output damage
 * boolean flag3  | isSweapingAttack     | Whether the player is sweaping an attack and will deal AoE damage
 * int i          | knockbackModifier    | The knockback modifier, must be set from the event after it has been thrown
 * float f4       | targetOriginalHealth | This is initially set as the entity original health
 * boolean flag4  | litEntityOnFire      | This is an internal flag to where if the attack failed, the entity is no longer set on fire
 * int j          | fireAspectModifier   | Literally just to check that the weapon used has fire aspect enchantments
 * double d0      | distanceWalkedDelta  | This checks that the distance walked delta is more than the normal walking speed to evaluate if you're making a sweaping attack
 * double d1      | targetMotionX        | Current target entity motion x vector
 * double d2      | targetMotionY        | Current target entity motion y vector
 * double d3      | targetMotionZ        | Current target entity motion z vector
 * boolean flag5  | attackSucceeded      | Whether the attack event succeeded
 *
 * @param targetEntity The target entity
 */
@Overwrite
public void attackTargetEntityWithCurrentItem(Entity targetEntity) {
    // Sponge Start - Add SpongeImpl hook to override in forge as necessary
    if (!SpongeImplHooks.checkAttackEntity((EntityPlayer) (Object) this, targetEntity)) {
        return;
    }
    // Sponge End
    if (targetEntity.canBeAttackedWithItem()) {
        if (!targetEntity.hitByEntity((EntityPlayer) (Object) this)) {
            // Sponge Start - Prepare our event values
            // float damage = (float) this.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).getAttributeValue();
            final double originalBaseDamage = this.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).getAttributeValue();
            float damage = (float) originalBaseDamage;
            // Sponge End
            float enchantmentDamage = 0.0F;
            // Spogne Start - Redirect getting enchantments for our damage event handlers
            // if (targetEntity instanceof EntityLivingBase) {
            // enchantmentDamage = EnchantmentHelper.getModifierForCreature(this.getHeldItemMainhand(), ((EntityLivingBase) targetEntity).getCreatureAttribute());
            // } else {
            // enchantmentDamage = EnchantmentHelper.getModifierForCreature(this.getHeldItemMainhand(), EnumCreatureAttribute.UNDEFINED);
            // }
            float attackStrength = this.getCooledAttackStrength(0.5F);
            final List<ModifierFunction<DamageModifier>> originalFunctions = new ArrayList<>();
            final EnumCreatureAttribute creatureAttribute = targetEntity instanceof EntityLivingBase ? ((EntityLivingBase) targetEntity).getCreatureAttribute() : EnumCreatureAttribute.UNDEFINED;
            final List<DamageFunction> enchantmentModifierFunctions = DamageEventHandler.createAttackEnchantmentFunction(this.getHeldItemMainhand(), creatureAttribute, attackStrength);
            // This is kept for the post-damage event handling
            final List<DamageModifier> enchantmentModifiers = enchantmentModifierFunctions.stream().map(ModifierFunction::getModifier).collect(Collectors.toList());
            enchantmentDamage = (float) enchantmentModifierFunctions.stream().map(ModifierFunction::getFunction).mapToDouble(function -> function.applyAsDouble(originalBaseDamage)).sum();
            originalFunctions.addAll(enchantmentModifierFunctions);
            // Sponge End
            originalFunctions.add(DamageEventHandler.provideCooldownAttackStrengthFunction((EntityPlayer) (Object) this, attackStrength));
            damage = damage * (0.2F + attackStrength * attackStrength * 0.8F);
            enchantmentDamage = enchantmentDamage * attackStrength;
            this.resetCooldown();
            if (damage > 0.0F || enchantmentDamage > 0.0F) {
                boolean isStrongAttack = attackStrength > 0.9F;
                boolean isSprintingAttack = false;
                boolean isCriticalAttack = false;
                boolean isSweapingAttack = false;
                int knockbackModifier = 0;
                knockbackModifier = knockbackModifier + EnchantmentHelper.getKnockbackModifier((EntityPlayer) (Object) this);
                if (this.isSprinting() && isStrongAttack) {
                    // Sponge - Only play sound after the event has be thrown and not cancelled.
                    // this.world.playSound((EntityPlayer) null, this.posX, this.posY, this.posZ, SoundEvents.entity_player_attack_knockback, this.getSoundCategory(), 1.0F, 1.0F);
                    ++knockbackModifier;
                    isSprintingAttack = true;
                }
                isCriticalAttack = isStrongAttack && this.fallDistance > 0.0F && !this.onGround && !this.isOnLadder() && !this.isInWater() && !this.isPotionActive(MobEffects.BLINDNESS) && !this.isRiding() && targetEntity instanceof EntityLivingBase;
                isCriticalAttack = isCriticalAttack && !this.isSprinting();
                if (isCriticalAttack) {
                    // Sponge Start - add critical attack tuple
                    // damage *= 1.5F; // Sponge - This is handled in the event
                    originalFunctions.add(DamageEventHandler.provideCriticalAttackTuple((EntityPlayer) (Object) this));
                // Sponge End
                }
                // damage = damage + enchantmentDamage; // Sponge - We don't need this since our event will re-assign the damage to deal
                double distanceWalkedDelta = (double) (this.distanceWalkedModified - this.prevDistanceWalkedModified);
                final ItemStack heldItem = this.getHeldItem(EnumHand.MAIN_HAND);
                if (isStrongAttack && !isCriticalAttack && !isSprintingAttack && this.onGround && distanceWalkedDelta < (double) this.getAIMoveSpeed()) {
                    ItemStack itemstack = heldItem;
                    if (itemstack.getItem() instanceof ItemSword) {
                        isSweapingAttack = true;
                    }
                }
                // Sponge Start - Create the event and throw it
                final DamageSource damageSource = DamageSource.causePlayerDamage((EntityPlayer) (Object) this);
                final boolean isMainthread = !this.world.isRemote;
                if (isMainthread) {
                    Sponge.getCauseStackManager().pushCause(damageSource);
                }
                final Cause currentCause = isMainthread ? Sponge.getCauseStackManager().getCurrentCause() : Cause.of(EventContext.empty(), damageSource);
                final AttackEntityEvent event = SpongeEventFactory.createAttackEntityEvent(currentCause, originalFunctions, EntityUtil.fromNative(targetEntity), knockbackModifier, originalBaseDamage);
                SpongeImpl.postEvent(event);
                if (isMainthread) {
                    Sponge.getCauseStackManager().popCause();
                }
                if (event.isCancelled()) {
                    return;
                }
                damage = (float) event.getFinalOutputDamage();
                // sponge - need final for later events
                final double attackDamage = damage;
                knockbackModifier = event.getKnockbackModifier();
                enchantmentDamage = (float) enchantmentModifiers.stream().mapToDouble(event::getOutputDamage).sum();
                // Sponge End
                float targetOriginalHealth = 0.0F;
                boolean litEntityOnFire = false;
                int fireAspectModifier = EnchantmentHelper.getFireAspectModifier((EntityPlayer) (Object) this);
                if (targetEntity instanceof EntityLivingBase) {
                    targetOriginalHealth = ((EntityLivingBase) targetEntity).getHealth();
                    if (fireAspectModifier > 0 && !targetEntity.isBurning()) {
                        litEntityOnFire = true;
                        targetEntity.setFire(1);
                    }
                }
                double targetMotionX = targetEntity.motionX;
                double targetMotionY = targetEntity.motionY;
                double targetMotionZ = targetEntity.motionZ;
                boolean attackSucceeded = targetEntity.attackEntityFrom(DamageSource.causePlayerDamage((EntityPlayer) (Object) this), damage);
                if (attackSucceeded) {
                    if (knockbackModifier > 0) {
                        if (targetEntity instanceof EntityLivingBase) {
                            ((EntityLivingBase) targetEntity).knockBack((EntityPlayer) (Object) this, (float) knockbackModifier * 0.5F, (double) MathHelper.sin(this.rotationYaw * 0.017453292F), (double) (-MathHelper.cos(this.rotationYaw * 0.017453292F)));
                        } else {
                            targetEntity.addVelocity((double) (-MathHelper.sin(this.rotationYaw * 0.017453292F) * (float) knockbackModifier * 0.5F), 0.1D, (double) (MathHelper.cos(this.rotationYaw * 0.017453292F) * (float) knockbackModifier * 0.5F));
                        }
                        this.motionX *= 0.6D;
                        this.motionZ *= 0.6D;
                        this.setSprinting(false);
                    }
                    if (isSweapingAttack) {
                        for (EntityLivingBase entitylivingbase : this.world.getEntitiesWithinAABB(EntityLivingBase.class, targetEntity.getEntityBoundingBox().grow(1.0D, 0.25D, 1.0D))) {
                            if (entitylivingbase != (EntityPlayer) (Object) this && entitylivingbase != targetEntity && !this.isOnSameTeam(entitylivingbase) && this.getDistanceSq(entitylivingbase) < 9.0D) {
                                // Sponge Start - Do a small event for these entities
                                // entitylivingbase.knockBack(this, 0.4F, (double)MathHelper.sin(this.rotationYaw * 0.017453292F), (double)(-MathHelper.cos(this.rotationYaw * 0.017453292F)));
                                // entitylivingbase.attackEntityFrom(DamageSource.causePlayerDamage(this), 1.0F);
                                final EntityDamageSource sweepingAttackSource = EntityDamageSource.builder().entity(this).type(DamageTypes.SWEEPING_ATTACK).build();
                                try (final StackFrame frame = isMainthread ? Sponge.getCauseStackManager().pushCauseFrame() : null) {
                                    if (isMainthread) {
                                        Sponge.getCauseStackManager().pushCause(sweepingAttackSource);
                                    }
                                    final ItemStackSnapshot heldSnapshot = ItemStackUtil.snapshotOf(heldItem);
                                    if (isMainthread) {
                                        Sponge.getCauseStackManager().addContext(EventContextKeys.WEAPON, heldSnapshot);
                                    }
                                    final DamageFunction sweapingFunction = DamageFunction.of(DamageModifier.builder().cause(Cause.of(EventContext.empty(), heldSnapshot)).item(heldSnapshot).type(DamageModifierTypes.SWEEPING).build(), (incoming) -> EnchantmentHelper.getSweepingDamageRatio((EntityPlayer) (Object) this) * attackDamage);
                                    final List<DamageFunction> sweapingFunctions = new ArrayList<>();
                                    sweapingFunctions.add(sweapingFunction);
                                    AttackEntityEvent sweepingAttackEvent = SpongeEventFactory.createAttackEntityEvent(currentCause, sweapingFunctions, EntityUtil.fromNative(entitylivingbase), 1, 1.0D);
                                    SpongeImpl.postEvent(sweepingAttackEvent);
                                    if (!sweepingAttackEvent.isCancelled()) {
                                        entitylivingbase.knockBack((EntityPlayer) (Object) this, sweepingAttackEvent.getKnockbackModifier() * 0.4F, (double) MathHelper.sin(this.rotationYaw * 0.017453292F), (double) (-MathHelper.cos(this.rotationYaw * 0.017453292F)));
                                        entitylivingbase.attackEntityFrom(DamageSource.causePlayerDamage((EntityPlayer) (Object) this), (float) sweepingAttackEvent.getFinalOutputDamage());
                                    }
                                }
                            // Sponge End
                            }
                        }
                        this.world.playSound((EntityPlayer) null, this.posX, this.posY, this.posZ, SoundEvents.ENTITY_PLAYER_ATTACK_SWEEP, this.getSoundCategory(), 1.0F, 1.0F);
                        this.spawnSweepParticles();
                    }
                    if (targetEntity instanceof EntityPlayerMP && targetEntity.velocityChanged) {
                        ((EntityPlayerMP) targetEntity).connection.sendPacket(new SPacketEntityVelocity(targetEntity));
                        targetEntity.velocityChanged = false;
                        targetEntity.motionX = targetMotionX;
                        targetEntity.motionY = targetMotionY;
                        targetEntity.motionZ = targetMotionZ;
                    }
                    if (isCriticalAttack) {
                        this.world.playSound((EntityPlayer) null, this.posX, this.posY, this.posZ, SoundEvents.ENTITY_PLAYER_ATTACK_CRIT, this.getSoundCategory(), 1.0F, 1.0F);
                        this.onCriticalHit(targetEntity);
                    }
                    if (!isCriticalAttack && !isSweapingAttack) {
                        if (isStrongAttack) {
                            this.world.playSound((EntityPlayer) null, this.posX, this.posY, this.posZ, SoundEvents.ENTITY_PLAYER_ATTACK_STRONG, this.getSoundCategory(), 1.0F, 1.0F);
                        } else {
                            this.world.playSound((EntityPlayer) null, this.posX, this.posY, this.posZ, SoundEvents.ENTITY_PLAYER_ATTACK_WEAK, this.getSoundCategory(), 1.0F, 1.0F);
                        }
                    }
                    if (enchantmentDamage > 0.0F) {
                        this.onEnchantmentCritical(targetEntity);
                    }
                    this.setLastAttackedEntity(targetEntity);
                    if (targetEntity instanceof EntityLivingBase) {
                        EnchantmentHelper.applyThornEnchantments((EntityLivingBase) targetEntity, (EntityPlayer) (Object) this);
                    }
                    EnchantmentHelper.applyArthropodEnchantments((EntityPlayer) (Object) this, targetEntity);
                    ItemStack itemstack1 = this.getHeldItemMainhand();
                    Entity entity = targetEntity;
                    if (targetEntity instanceof MultiPartEntityPart) {
                        IEntityMultiPart ientitymultipart = ((MultiPartEntityPart) targetEntity).parent;
                        if (ientitymultipart instanceof EntityLivingBase) {
                            entity = (EntityLivingBase) ientitymultipart;
                        }
                    }
                    if (!itemstack1.isEmpty() && targetEntity instanceof EntityLivingBase) {
                        itemstack1.hitEntity((EntityLivingBase) targetEntity, (EntityPlayer) (Object) this);
                        if (itemstack1.isEmpty()) {
                            this.setHeldItem(EnumHand.MAIN_HAND, ItemStack.EMPTY);
                        }
                    }
                    if (targetEntity instanceof EntityLivingBase) {
                        float f5 = targetOriginalHealth - ((EntityLivingBase) targetEntity).getHealth();
                        this.addStat(StatList.DAMAGE_DEALT, Math.round(f5 * 10.0F));
                        if (fireAspectModifier > 0) {
                            targetEntity.setFire(fireAspectModifier * 4);
                        }
                        if (this.world instanceof WorldServer && f5 > 2.0F) {
                            int k = (int) ((double) f5 * 0.5D);
                            ((WorldServer) this.world).spawnParticle(EnumParticleTypes.DAMAGE_INDICATOR, targetEntity.posX, targetEntity.posY + (double) (targetEntity.height * 0.5F), targetEntity.posZ, k, 0.1D, 0.0D, 0.1D, 0.2D, new int[0]);
                        }
                    }
                    this.addExhaustion(0.3F);
                } else {
                    this.world.playSound((EntityPlayer) null, this.posX, this.posY, this.posZ, SoundEvents.ENTITY_PLAYER_ATTACK_NODAMAGE, this.getSoundCategory(), 1.0F, 1.0F);
                    if (litEntityOnFire) {
                        targetEntity.extinguish();
                    }
                }
            }
        }
    }
}
Also used : EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) Item(net.minecraft.item.Item) Inject(org.spongepowered.asm.mixin.injection.Inject) GameProfile(com.mojang.authlib.GameProfile) EnumHand(net.minecraft.util.EnumHand) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SpongeCommonEventFactory(org.spongepowered.common.event.SpongeCommonEventFactory) ItemStackUtil(org.spongepowered.common.item.inventory.util.ItemStackUtil) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) CallbackInfoReturnable(org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable) CallbackInfo(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Mixin(org.spongepowered.asm.mixin.Mixin) DamageEntityEvent(org.spongepowered.api.event.entity.DamageEntityEvent) LockCode(net.minecraft.world.LockCode) SoundCategory(net.minecraft.util.SoundCategory) DamageSourceRegistryModule(org.spongepowered.common.registry.type.event.DamageSourceRegistryModule) At(org.spongepowered.asm.mixin.injection.At) IMixinEntityPlayer(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayer) EntityItem(net.minecraft.entity.item.EntityItem) InventoryEnderChest(net.minecraft.inventory.InventoryEnderChest) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) SPacketEntityVelocity(net.minecraft.network.play.server.SPacketEntityVelocity) AttackEntityEvent(org.spongepowered.api.event.entity.AttackEntityEvent) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) Team(net.minecraft.scoreboard.Team) Sponge(org.spongepowered.api.Sponge) StatBase(net.minecraft.stats.StatBase) DamageTypes(org.spongepowered.api.event.cause.entity.damage.DamageTypes) SpongeHealthData(org.spongepowered.common.data.manipulator.mutable.entity.SpongeHealthData) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) EnchantmentHelper(net.minecraft.enchantment.EnchantmentHelper) PacketPhase(org.spongepowered.common.event.tracking.phase.packet.PacketPhase) EntityUtil(org.spongepowered.common.entity.EntityUtil) Cause(org.spongepowered.api.event.cause.Cause) List(java.util.List) PhaseContext(org.spongepowered.common.event.tracking.PhaseContext) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Shadow(org.spongepowered.asm.mixin.Shadow) LegacyTexts(org.spongepowered.common.text.serializer.LegacyTexts) EnumParticleTypes(net.minecraft.util.EnumParticleTypes) MobEffects(net.minecraft.init.MobEffects) Container(net.minecraft.inventory.Container) Scoreboard(net.minecraft.scoreboard.Scoreboard) EventContext(org.spongepowered.api.event.cause.EventContext) PlayerCapabilities(net.minecraft.entity.player.PlayerCapabilities) ModifierFunction(org.spongepowered.api.event.cause.entity.ModifierFunction) SpongeImpl(org.spongepowered.common.SpongeImpl) EntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource) PlayerInventory(org.spongepowered.api.item.inventory.entity.PlayerInventory) IEntityMultiPart(net.minecraft.entity.IEntityMultiPart) SpongeImplHooks(org.spongepowered.common.SpongeImplHooks) ITargetedLocation(org.spongepowered.common.interfaces.ITargetedLocation) SpongeTexts(org.spongepowered.common.text.SpongeTexts) Vector3d(com.flowpowered.math.vector.Vector3d) EntityEquipmentSlot(net.minecraft.inventory.EntityEquipmentSlot) CooldownTracker(net.minecraft.util.CooldownTracker) DamageModifierTypes(org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes) Overwrite(org.spongepowered.asm.mixin.Overwrite) ExperienceHolderUtils(org.spongepowered.common.data.processor.common.ExperienceHolderUtils) ITextComponent(net.minecraft.util.text.ITextComponent) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) IMixinInventoryPlayer(org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer) MultiPartEntityPart(net.minecraft.entity.MultiPartEntityPart) WorldServer(net.minecraft.world.WorldServer) ItemSword(net.minecraft.item.ItemSword) Nullable(javax.annotation.Nullable) Entity(net.minecraft.entity.Entity) MixinEntityLivingBase(org.spongepowered.common.mixin.core.entity.MixinEntityLivingBase) SoundEvents(net.minecraft.init.SoundEvents) StatList(net.minecraft.stats.StatList) Items(net.minecraft.init.Items) World(net.minecraft.world.World) Redirect(org.spongepowered.asm.mixin.injection.Redirect) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) BlockPos(net.minecraft.util.math.BlockPos) InventoryPlayer(net.minecraft.entity.player.InventoryPlayer) Slot(org.spongepowered.api.item.inventory.Slot) DamageSource(net.minecraft.util.DamageSource) SharedMonsterAttributes(net.minecraft.entity.SharedMonsterAttributes) TextComponentString(net.minecraft.util.text.TextComponentString) DamageEventHandler(org.spongepowered.common.event.damage.DamageEventHandler) FoodStats(net.minecraft.util.FoodStats) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) EntityLivingBase(net.minecraft.entity.EntityLivingBase) MathHelper(net.minecraft.util.math.MathHelper) VecHelper(org.spongepowered.common.util.VecHelper) EnumCreatureAttribute(net.minecraft.entity.EnumCreatureAttribute) SlotIndex(org.spongepowered.api.item.inventory.property.SlotIndex) SoundEvent(net.minecraft.util.SoundEvent) Entity(net.minecraft.entity.Entity) ArrayList(java.util.ArrayList) MultiPartEntityPart(net.minecraft.entity.MultiPartEntityPart) WorldServer(net.minecraft.world.WorldServer) EntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource) AttackEntityEvent(org.spongepowered.api.event.entity.AttackEntityEvent) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) SPacketEntityVelocity(net.minecraft.network.play.server.SPacketEntityVelocity) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) Cause(org.spongepowered.api.event.cause.Cause) ItemSword(net.minecraft.item.ItemSword) EnumCreatureAttribute(net.minecraft.entity.EnumCreatureAttribute) EntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource) DamageSource(net.minecraft.util.DamageSource) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) MixinEntityLivingBase(org.spongepowered.common.mixin.core.entity.MixinEntityLivingBase) EntityLivingBase(net.minecraft.entity.EntityLivingBase) IMixinEntityPlayer(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayer) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) ModifierFunction(org.spongepowered.api.event.cause.entity.ModifierFunction) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) IEntityMultiPart(net.minecraft.entity.IEntityMultiPart) ItemStack(net.minecraft.item.ItemStack) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 2 with Sponge

use of org.spongepowered.api.Sponge in project guardian by ichorpowered.

the class CorePluginFacet method startup.

public Boolean startup(SimpleFacetMessage componentMessage) {
    this.facetState = FacetState.PREPARE;
    SimpleGuardian.setInstance(this.plugin);
    PropertyInjector propertyInjector = this.plugin.getPropertyInjector();
    propertyInjector.inject("state", GuardianState.PRE_INITIALIZATION);
    // STATE: PRE_INITIALIZATION
    if (PluginInfo.EXPERIMENTAL) {
        this.logger.warn(ConsoleUtil.of(Ansi.Color.RED, "You are using an experimental build of Guardian."));
        this.logger.warn(ConsoleUtil.of(Ansi.Color.RED, "This may not be ready for a production environment. Use at your own risk!"));
    }
    this.logger.info(ConsoleUtil.of("Guardian v{} for Sponge {} and Minecraft {}", PluginInfo.VERSION, Sponge.getPlatform().getContainer(Platform.Component.API).getVersion().map(version -> version.substring(0, 5)).orElse("?"), Sponge.getPlatform().getContainer(Platform.Component.GAME).getVersion().orElse("?")));
    Sponge.getServiceManager().setProvider(this.plugin, AntiCheatService.class, new GuardianBypassService(this.plugin));
    ModuleController<GuardianPlugin> moduleController = ShadedModularFramework.registerModuleController(this.plugin, Sponge.getGame());
    SimpleEventBus<GuardianEvent, GuardianListener> eventBus = new SimpleEventBus<>(new ASMEventExecutorFactory<GuardianEvent, GuardianListener>());
    this.logger.info(ConsoleUtil.of("Loaded pre-facet systems: { me4502/modularframework v1.8.5, kyoripowered/event v1.0.0 }"));
    // PROVIDE: PRE_INITIALIZATION
    propertyInjector.inject("coreTime", componentMessage.getTime());
    propertyInjector.inject("moduleController", moduleController);
    propertyInjector.inject("eventBus", eventBus);
    this.plugin.getEventBus().post(new GuardianPreInitializationEvent(Origin.source(this.plugin.getPluginContainer()).build()));
    propertyInjector.inject("state", GuardianState.INITIALIZATION);
    // State: INITIALIZATION
    this.logger.info(this.facetPrefix + "Initializing storage.");
    Configuration configuration = new Configuration(this.plugin, this.plugin.getConfigDirectory());
    configuration.load();
    this.logger.info(this.facetPrefix + "Initializing registries.");
    InternalBypassService internalBypassService = new InternalBypassService(this.plugin);
    GuardianDetectionManager detectionManager = new GuardianDetectionManager(this.plugin);
    SequenceRegistry<Event> sequenceRegistry = new SequenceRegistry<>();
    this.logger.info(this.facetPrefix + "Initializing systems.");
    GuardianSequenceManager sequenceManager = new GuardianSequenceManager(this.plugin, sequenceRegistry);
    GuardianSequenceManager.SequenceTask sequenceTask = new GuardianSequenceManager.SequenceTask(this.plugin, sequenceManager);
    GuardianSequenceListener sequenceListener = new GuardianSequenceListener(this.plugin);
    Common common = new Common(this.plugin);
    GuardianContentKeys contentKeys = new GuardianContentKeys();
    contentKeys.createKeys();
    // PROVIDE: INITIALIZATION
    propertyInjector.inject("common", common);
    propertyInjector.inject("configuration", configuration);
    propertyInjector.inject("internalBypassService", internalBypassService);
    propertyInjector.inject("detectionManager", detectionManager);
    propertyInjector.inject("sequenceManager", sequenceManager);
    propertyInjector.inject("sequenceTask", sequenceTask);
    propertyInjector.inject("sequenceRegistry", sequenceRegistry);
    propertyInjector.inject("sequenceListener", sequenceListener);
    this.plugin.getEventBus().post(new GuardianInitializationEvent(Origin.source(this.plugin.getPluginContainer()).build()));
    this.facetState = FacetState.START;
    return true;
}
Also used : Ansi(org.fusesource.jansi.Ansi) FacetMessage(com.ichorpowered.guardian.launch.message.FacetMessage) AntiCheatService(com.me4502.precogs.service.AntiCheatService) SimpleEventBus(net.kyori.event.SimpleEventBus) SimpleFacetMessage(com.ichorpowered.guardian.launch.message.SimpleFacetMessage) GuardianSequenceListener(com.ichorpowered.guardian.sequence.GuardianSequenceListener) ASMEventExecutorFactory(net.kyori.event.ASMEventExecutorFactory) GuardianPlugin(com.ichorpowered.guardian.GuardianPlugin) GuardianListener(com.ichorpowered.guardianapi.event.GuardianListener) PluginInfo(com.ichorpowered.guardian.PluginInfo) Platform(org.spongepowered.api.Platform) Origin(com.ichorpowered.guardianapi.event.origin.Origin) InternalBypassService(com.ichorpowered.guardian.service.InternalBypassService) Common(com.ichorpowered.guardian.Common) GuardianInitializationEvent(com.ichorpowered.guardian.event.GuardianInitializationEvent) GuardianSequenceManager(com.ichorpowered.guardian.sequence.GuardianSequenceManager) FacetState(com.ichorpowered.guardian.launch.FacetState) GuardianState(com.ichorpowered.guardianapi.GuardianState) GuardianBypassService(com.ichorpowered.guardian.service.GuardianBypassService) PropertyInjector(com.ichorpowered.guardian.util.property.PropertyInjector) Configuration(com.ichorpowered.guardian.Configuration) SequenceRegistry(com.abilityapi.sequenceapi.SequenceRegistry) FacetBootstrap(com.ichorpowered.guardian.launch.FacetBootstrap) SimpleGuardian(com.ichorpowered.guardianapi.SimpleGuardian) Logger(org.slf4j.Logger) GuardianDetectionManager(com.ichorpowered.guardian.detection.GuardianDetectionManager) Event(org.spongepowered.api.event.Event) ConsoleUtil(com.ichorpowered.guardian.util.ConsoleUtil) Sponge(org.spongepowered.api.Sponge) GuardianEvent(com.ichorpowered.guardianapi.event.GuardianEvent) Facet(com.ichorpowered.guardian.launch.Facet) GuardianPreInitializationEvent(com.ichorpowered.guardian.event.GuardianPreInitializationEvent) ModuleController(com.me4502.modularframework.ModuleController) GuardianContentKeys(com.ichorpowered.guardian.content.GuardianContentKeys) FacetException(com.ichorpowered.guardian.launch.exception.FacetException) ShadedModularFramework(com.me4502.modularframework.ShadedModularFramework) InternalBypassService(com.ichorpowered.guardian.service.InternalBypassService) Configuration(com.ichorpowered.guardian.Configuration) SequenceRegistry(com.abilityapi.sequenceapi.SequenceRegistry) GuardianEvent(com.ichorpowered.guardianapi.event.GuardianEvent) GuardianListener(com.ichorpowered.guardianapi.event.GuardianListener) GuardianContentKeys(com.ichorpowered.guardian.content.GuardianContentKeys) GuardianPreInitializationEvent(com.ichorpowered.guardian.event.GuardianPreInitializationEvent) GuardianSequenceManager(com.ichorpowered.guardian.sequence.GuardianSequenceManager) Common(com.ichorpowered.guardian.Common) PropertyInjector(com.ichorpowered.guardian.util.property.PropertyInjector) GuardianBypassService(com.ichorpowered.guardian.service.GuardianBypassService) SimpleEventBus(net.kyori.event.SimpleEventBus) GuardianInitializationEvent(com.ichorpowered.guardian.event.GuardianInitializationEvent) Event(org.spongepowered.api.event.Event) GuardianEvent(com.ichorpowered.guardianapi.event.GuardianEvent) GuardianPreInitializationEvent(com.ichorpowered.guardian.event.GuardianPreInitializationEvent) GuardianDetectionManager(com.ichorpowered.guardian.detection.GuardianDetectionManager) GuardianSequenceListener(com.ichorpowered.guardian.sequence.GuardianSequenceListener) GuardianPlugin(com.ichorpowered.guardian.GuardianPlugin) GuardianInitializationEvent(com.ichorpowered.guardian.event.GuardianInitializationEvent)

Example 3 with Sponge

use of org.spongepowered.api.Sponge in project LanternServer by LanternPowered.

the class LanternPaginationService method buildPaginationCommand.

@SuppressWarnings("ConstantConditions")
private CommandSpec buildPaginationCommand() {
    // TODO Completely redo this once command refactor is out and PR changes to Sponge as well
    final ActivePaginationCommandElement paginationElement = new ActivePaginationCommandElement(t("pagination-id"));
    CommandSpec next = CommandSpec.builder().description(t("Go to the next page")).executor((src, args) -> {
        args.<ActivePagination>getOne("pagination-id").get().nextPage();
        return CommandResult.success();
    }).build();
    CommandSpec prev = CommandSpec.builder().description(t("Go to the previous page")).executor((src, args) -> {
        args.<ActivePagination>getOne("pagination-id").get().previousPage();
        return CommandResult.success();
    }).build();
    CommandElement pageArgs = integer(t("page"));
    CommandExecutor pageExecutor = (src, args) -> {
        args.<ActivePagination>getOne("pagination-id").get().specificPage(args.<Integer>getOne("page").get());
        return CommandResult.success();
    };
    CommandSpec page = CommandSpec.builder().description(t("Go to a specific page")).arguments(pageArgs).executor(pageExecutor).build();
    // Fallback to page arguments
    ChildCommandElementExecutor childDispatcher = new ChildCommandElementExecutor(pageExecutor);
    childDispatcher.register(next, "next", "n");
    childDispatcher.register(prev, "prev", "p", "previous");
    childDispatcher.register(page, "page");
    // https://github.com/SpongePowered/SpongeAPI/issues/1272
    return CommandSpec.builder().arguments(paginationElement, firstParsing(childDispatcher, pageArgs)).executor(childDispatcher).description(t("Helper command for paginations occurring")).build();
}
Also used : MessageReceiver(org.spongepowered.api.text.channel.MessageReceiver) Iterables(com.google.common.collect.Iterables) GenericArguments.firstParsing(org.spongepowered.api.command.args.GenericArguments.firstParsing) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) ConcurrentMap(java.util.concurrent.ConcurrentMap) PaginationList(org.spongepowered.api.service.pagination.PaginationList) ImmutableList(com.google.common.collect.ImmutableList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Map(java.util.Map) Nullable(javax.annotation.Nullable) CommandResult(org.spongepowered.api.command.CommandResult) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) CommandSource(org.spongepowered.api.command.CommandSource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) UUID(java.util.UUID) PaginationService(org.spongepowered.api.service.pagination.PaginationService) CommandElement(org.spongepowered.api.command.args.CommandElement) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) GenericArguments.integer(org.spongepowered.api.command.args.GenericArguments.integer) List(java.util.List) ChildCommandElementExecutor(org.spongepowered.api.command.args.ChildCommandElementExecutor) Lantern(org.lanternpowered.server.game.Lantern) Optional(java.util.Optional) MapMaker(com.google.common.collect.MapMaker) ChildCommandElementExecutor(org.spongepowered.api.command.args.ChildCommandElementExecutor) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) CommandElement(org.spongepowered.api.command.args.CommandElement)

Example 4 with Sponge

use of org.spongepowered.api.Sponge in project LanternServer by LanternPowered.

the class LanternWorldPropertiesIO method read.

static LevelData read(Path directory, @Nullable String worldName, @Nullable UUID uniqueId) throws IOException {
    final DataView rootDataView = IOHelper.read(directory.resolve(LEVEL_DATA), file -> NbtStreamUtils.read(Files.newInputStream(file), true)).orElseThrow(() -> new FileNotFoundException("Unable to find " + LEVEL_DATA + "!"));
    final DataView dataView = rootDataView.getView(DATA).get();
    if (worldName == null) {
        worldName = dataView.getString(NAME).get();
    }
    final DataView spongeRootDataView = IOHelper.read(directory.resolve(SPONGE_LEVEL_DATA), file -> NbtStreamUtils.read(Files.newInputStream(file), true)).orElse(null);
    final DataView spongeContainer = spongeRootDataView != null ? spongeRootDataView.getView(DataQueries.SPONGE_DATA).orElse(null) : null;
    if (uniqueId == null) {
        // Try for the sponge (lantern) storage format
        if (spongeContainer != null) {
            final Long most = spongeContainer.getLong(UUID_MOST).orElseGet(() -> spongeContainer.getLong(OLD_UUID_MOST).orElse(null));
            final Long least = spongeContainer.getLong(UUID_LEAST).orElseGet(() -> spongeContainer.getLong(OLD_UUID_LEAST).orElse(null));
            if (most != null && least != null) {
                uniqueId = new UUID(most, least);
            }
        }
        // The uuid storage bukkit used, try this one first
        final Path uuidFile;
        if (uniqueId == null && Files.exists((uuidFile = directory.resolve(BUKKIT_UUID_DATA)))) {
            try (DataInputStream in = new DataInputStream(Files.newInputStream(uuidFile))) {
                uniqueId = new UUID(in.readLong(), in.readLong());
            } catch (IOException e) {
                Lantern.getLogger().error("Unable to access {}, ignoring...", BUKKIT_UUID_DATA, e);
            }
            Files.delete(uuidFile);
        }
        if (uniqueId == null) {
            uniqueId = UUID.randomUUID();
        }
    }
    BitSet dimensionMap = null;
    if (rootDataView.contains(FORGE)) {
        final DataView forgeView = rootDataView.getView(FORGE).get();
        if (forgeView.contains(DIMENSION_DATA)) {
            dimensionMap = new BitSet(LanternWorldManager.DIMENSION_MAP_SIZE);
            final int[] intArray = (int[]) forgeView.getView(DIMENSION_DATA).get().get(DIMENSION_ARRAY).get();
            for (int i = 0; i < intArray.length; i++) {
                for (int j = 0; j < Integer.SIZE; j++) {
                    dimensionMap.set(i * Integer.SIZE + j, (intArray[i] & (1 << j)) != 0);
                }
            }
        }
    }
    final Integer dimensionId = spongeContainer != null ? spongeContainer.getInt(DIMENSION_INDEX).orElse(null) : null;
    return new LevelData(worldName, uniqueId, rootDataView, spongeRootDataView, dimensionId, dimensionMap);
}
Also used : DataInputStream(java.io.DataInputStream) MoonPhase(org.lanternpowered.server.data.world.MoonPhase) DimensionType(org.spongepowered.api.world.DimensionType) DifficultyRegistryModule(org.lanternpowered.server.game.registry.type.world.DifficultyRegistryModule) DataQuery(org.spongepowered.api.data.DataQuery) GameModes(org.spongepowered.api.entity.living.player.gamemode.GameModes) WorldConfig(org.lanternpowered.server.config.world.WorldConfig) DataQueries(org.lanternpowered.server.data.DataQueries) LanternDifficulty(org.lanternpowered.server.world.difficulty.LanternDifficulty) ArrayList(java.util.ArrayList) NbtStreamUtils(org.lanternpowered.server.data.persistence.nbt.NbtStreamUtils) Gson(com.google.gson.Gson) SerializationBehaviors(org.spongepowered.api.world.SerializationBehaviors) Difficulties(org.spongepowered.api.world.difficulty.Difficulties) GameModeRegistryModule(org.lanternpowered.server.game.registry.type.entity.player.GameModeRegistryModule) Path(java.nio.file.Path) RuleDataTypes(org.lanternpowered.server.world.rules.RuleDataTypes) DimensionTypes(org.spongepowered.api.world.DimensionTypes) PortalAgentType(org.spongepowered.api.world.PortalAgentType) Nullable(javax.annotation.Nullable) GeneratorTypes(org.spongepowered.api.world.GeneratorTypes) RuleType(org.lanternpowered.server.world.rules.RuleType) Weather(org.spongepowered.api.world.weather.Weather) LanternWeather(org.lanternpowered.server.world.weather.LanternWeather) Files(java.nio.file.Files) PortalAgentTypes(org.spongepowered.api.world.PortalAgentTypes) IOHelper(org.lanternpowered.server.data.io.IOHelper) GeneratorType(org.spongepowered.api.world.GeneratorType) CatalogType(org.spongepowered.api.CatalogType) Sponge(org.spongepowered.api.Sponge) DataContainer(org.spongepowered.api.data.DataContainer) AbstractFlatGeneratorType(org.lanternpowered.server.world.gen.flat.AbstractFlatGeneratorType) IOException(java.io.IOException) UUID(java.util.UUID) JsonDataFormat(org.lanternpowered.server.data.persistence.json.JsonDataFormat) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Weathers(org.spongepowered.api.world.weather.Weathers) LanternGameMode(org.lanternpowered.server.entity.living.player.gamemode.LanternGameMode) List(java.util.List) Object2IntMap(it.unimi.dsi.fastutil.objects.Object2IntMap) DataView(org.spongepowered.api.data.DataView) Lantern(org.lanternpowered.server.game.Lantern) LanternDimensionType(org.lanternpowered.server.world.dimension.LanternDimensionType) Vector3i(com.flowpowered.math.vector.Vector3i) Entry(java.util.Map.Entry) Optional(java.util.Optional) BitSet(java.util.BitSet) Path(java.nio.file.Path) FileNotFoundException(java.io.FileNotFoundException) BitSet(java.util.BitSet) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) DataView(org.spongepowered.api.data.DataView) UUID(java.util.UUID)

Example 5 with Sponge

use of org.spongepowered.api.Sponge in project SpongeCommon by SpongePowered.

the class SpongePlaceholderContextBuilder method associatedObject.

@Override
@SuppressWarnings("unchecked")
public PlaceholderContext.Builder associatedObject(@Nullable final Object associatedObject) {
    if (associatedObject == null) {
        this.associatedObjectSupplier = null;
    } else if (associatedObject instanceof Supplier) {
        return this.associatedObject((Supplier<Object>) associatedObject);
    } else if (associatedObject instanceof SystemSubject) {
        this.associatedObjectSupplier = Sponge::systemSubject;
    } else if (associatedObject instanceof Server) {
        this.associatedObjectSupplier = Sponge::server;
    } else if (associatedObject instanceof Player) {
        return this.associatedObject((Player) associatedObject);
    } else if (associatedObject instanceof ServerWorld) {
        final ResourceKey key = ((ServerWorld) associatedObject).key();
        this.associatedObjectSupplier = () -> SpongeCommon.game().server().worldManager().world(key).orElse(null);
    } else if (associatedObject instanceof Entity) {
        final Entity entity = ((Entity) associatedObject);
        final ResourceKey key = entity.serverLocation().world().key();
        final UUID entityUuid = ((Entity) associatedObject).uniqueId();
        this.associatedObjectSupplier = () -> SpongeCommon.game().server().worldManager().world(key).flatMap(x -> x.entity(entityUuid)).orElse(null);
    } else {
        // We create a weak reference here so we don't hold on to game objects.
        final WeakReference<Object> objectWeakReference = new WeakReference<>(associatedObject);
        this.associatedObjectSupplier = objectWeakReference::get;
    }
    return this;
}
Also used : Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) Server(org.spongepowered.api.Server) SystemSubject(org.spongepowered.api.SystemSubject) ResourceKey(org.spongepowered.api.ResourceKey) ServerWorld(org.spongepowered.api.world.server.ServerWorld) WeakReference(java.lang.ref.WeakReference) Supplier(java.util.function.Supplier) Sponge(org.spongepowered.api.Sponge) UUID(java.util.UUID)

Aggregations

Sponge (org.spongepowered.api.Sponge)17 List (java.util.List)11 Optional (java.util.Optional)9 CommandResult (org.spongepowered.api.command.CommandResult)8 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 Collectors (java.util.stream.Collectors)7 Component (net.kyori.adventure.text.Component)7 IOException (java.io.IOException)6 Collection (java.util.Collection)6 UUID (java.util.UUID)6 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)6 Inject (com.google.inject.Inject)5 TypeToken (io.leangen.geantyref.TypeToken)5 Identity (net.kyori.adventure.identity.Identity)5 NamedTextColor (net.kyori.adventure.text.format.NamedTextColor)5 ResourceKey (org.spongepowered.api.ResourceKey)5 RegistryTypes (org.spongepowered.api.registry.RegistryTypes)5 Nullable (javax.annotation.Nullable)4 EntityType (org.spongepowered.api.entity.EntityType)4