Search in sources :

Example 21 with PrettyPrinter

use of org.spongepowered.asm.util.PrettyPrinter in project SpongeCommon by SpongePowered.

the class PhaseTracker method printBlockTrackingException.

private void printBlockTrackingException(PhaseData phaseData, IPhaseState<?> phaseState, Throwable e) {
    if (!SpongeImpl.getGlobalConfig().getConfig().getPhaseTracker().isVerbose() && !this.printedExceptionsForBlocks.isEmpty()) {
        if (this.printedExceptionsForBlocks.contains(phaseState)) {
            return;
        }
    }
    final PrettyPrinter printer = new PrettyPrinter(60).add("Exception attempting to capture a block change!").centre().hr();
    printer.addWrapped(60, "%s :", "PhaseContext");
    CONTEXT_PRINTER.accept(printer, phaseData.context);
    printer.addWrapped(60, "%s :", "Phases remaining");
    this.stack.forEach(data -> PHASE_PRINTER.accept(printer, data));
    printer.add("Stacktrace:");
    printer.add(e);
    printer.trace(System.err, SpongeImpl.getLogger(), Level.ERROR);
    if (!SpongeImpl.getGlobalConfig().getConfig().getPhaseTracker().isVerbose()) {
        this.printedExceptionsForBlocks.add(phaseState);
    }
}
Also used : PrettyPrinter(org.spongepowered.asm.util.PrettyPrinter)

Example 22 with PrettyPrinter

use of org.spongepowered.asm.util.PrettyPrinter in project SpongeCommon by SpongePowered.

the class PhaseTracker method printPhaseIncompatibility.

private void printPhaseIncompatibility(IPhaseState<?> currentState, IPhaseState<?> incompatibleState) {
    if (!SpongeImpl.getGlobalConfig().getConfig().getPhaseTracker().isVerbose() && !this.completedIncorrectStates.isEmpty()) {
        for (Tuple<IPhaseState<?>, IPhaseState<?>> tuple : this.completedIncorrectStates) {
            if ((tuple.getFirst().equals(currentState) && tuple.getSecond().equals(incompatibleState))) {
                // being completed incorrectly. only print it once.
                return;
            }
        }
    }
    PrettyPrinter printer = new PrettyPrinter(60);
    printer.add("Switching Phase").centre().hr();
    printer.add("Phase incompatibility detected! Attempting to switch to an invalid phase!");
    printer.add("  %s : %s", "Current Phase", currentState.getPhase());
    printer.add("  %s : %s", "Current State", currentState);
    printer.add("  %s : %s", "Entering incompatible Phase", incompatibleState.getPhase());
    printer.add("  %s : %s", "Entering incompatible State", incompatibleState);
    printer.add("%s :", "Current phases");
    this.stack.forEach(data -> PHASE_PRINTER.accept(printer, data));
    printer.add("  %s :", "Printing stack trace");
    printer.add(new Exception("Stack trace"));
    printer.add();
    this.generateVersionInfo(printer);
    printer.trace(System.err, SpongeImpl.getLogger(), Level.ERROR);
    if (!SpongeImpl.getGlobalConfig().getConfig().getPhaseTracker().isVerbose()) {
        this.completedIncorrectStates.add(Tuple.of(currentState, incompatibleState));
    }
}
Also used : PrettyPrinter(org.spongepowered.asm.util.PrettyPrinter) ReportedException(net.minecraft.util.ReportedException)

Example 23 with PrettyPrinter

use of org.spongepowered.asm.util.PrettyPrinter in project SpongeCommon by SpongePowered.

the class InteractEntityPacketState method unwind.

@Override
public void unwind(BasicPacketContext phaseContext) {
    final EntityPlayerMP player = phaseContext.getPacketPlayer();
    final CPacketUseEntity useEntityPacket = phaseContext.getPacket();
    final net.minecraft.entity.Entity entity = useEntityPacket.getEntityFromWorld(player.world);
    if (entity == null) {
        // Something happened?
        return;
    }
    final World spongeWorld = EntityUtil.getSpongeWorld(player);
    EntityUtil.toMixin(entity).setNotifier(player.getUniqueID());
    phaseContext.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(blocks -> {
        final PrettyPrinter printer = new PrettyPrinter(80);
        printer.add("Processing Interact Entity").centre().hr();
        printer.add("The blocks captured are:");
        for (BlockSnapshot blockSnapshot : blocks) {
            printer.add("  Block: %s", blockSnapshot);
        }
        printer.trace(System.err);
    });
    phaseContext.getCapturedEntitySupplier().acceptAndClearIfNotEmpty(entities -> {
        final PrettyPrinter printer = new PrettyPrinter(80);
        printer.add("Processing Interact Entity").centre().hr();
        printer.add("The entities captured are:");
        for (Entity capturedEntity : entities) {
            printer.add("  Entity: %s", capturedEntity);
        }
        printer.trace(System.err);
    });
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(player);
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.PLACEMENT);
        phaseContext.getCapturedItemsSupplier().acceptAndClearIfNotEmpty(entities -> {
            final List<Entity> items = entities.stream().map(EntityUtil::fromNative).collect(Collectors.toList());
            SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), items);
            SpongeImpl.postEvent(event);
            if (!event.isCancelled()) {
                processSpawnedEntities(player, event);
            }
        });
    }
    phaseContext.getCapturedEntityDropSupplier().acceptIfNotEmpty(map -> {
        final PrettyPrinter printer = new PrettyPrinter(80);
        printer.add("Processing Interact Entity").centre().hr();
        printer.add("The item stacks captured are: ");
        for (Map.Entry<UUID, Collection<ItemDropData>> entry : map.asMap().entrySet()) {
            printer.add("  - Entity with UUID: %s", entry.getKey());
            for (ItemDropData stack : entry.getValue()) {
                printer.add("    - %s", stack);
            }
        }
        printer.trace(System.err);
    });
    phaseContext.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(snapshots -> TrackingUtil.processBlockCaptures(snapshots, this, phaseContext));
}
Also used : CPacketUseEntity(net.minecraft.network.play.client.CPacketUseEntity) Entity(org.spongepowered.api.entity.Entity) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) World(org.spongepowered.api.world.World) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) CPacketUseEntity(net.minecraft.network.play.client.CPacketUseEntity) PrettyPrinter(org.spongepowered.asm.util.PrettyPrinter) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Collection(java.util.Collection) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) UUID(java.util.UUID) ItemDropData(org.spongepowered.common.event.tracking.context.ItemDropData) Map(java.util.Map)

Example 24 with PrettyPrinter

use of org.spongepowered.asm.util.PrettyPrinter in project SpongeCommon by SpongePowered.

the class MixinMobSpawnerBaseLogic method readEntityFromCompoundAtWorld.

/**
 * @author gabizou - April 10th, 2016
 *
 * This is close to a verbatim copy of {@link AnvilChunkLoader#readWorldEntityPos(NBTTagCompound, World, double, double, double, boolean)}
 * with the added bonus of throwing events before entities are constructed with appropriate causes.
 *
 * @param compound The compound of the entity to spawn with
 * @param world The world to spawn at
 * @param x The x position
 * @param y The y position
 * @param z The z position
 * @param attemptToSpawn If false, the entity is not going to be spawned into the world yet
 * @return The entity, if successfully created
 */
private static Entity readEntityFromCompoundAtWorld(NBTTagCompound compound, World world, double x, double y, double z, boolean attemptToSpawn) {
    final String entityTypeString = compound.getString(NbtDataUtil.ENTITY_TYPE_ID);
    final Class<? extends Entity> clazz = SpongeImplHooks.getEntityClass(new ResourceLocation(entityTypeString));
    if (clazz == null) {
        final PrettyPrinter printer = new PrettyPrinter(60).add("Unknown Entity for MobSpawners").centre().hr().addWrapped(60, "Sponge has found a MobSpawner attempting to locate potentially" + "a foreign entity type for a MobSpawner, unfortunately, there isn't a" + "way to get around the deserialization process looking up unregistered" + "entity types. This may be a bug with a mod or sponge.").add("%s : %s", "Entity Name", entityTypeString).add();
        PhaseTracker.getInstance().generateVersionInfo(printer);
        printer.trace(System.err, SpongeImpl.getLogger(), Level.WARN);
        return null;
    }
    EntityType type = EntityTypeRegistryModule.getInstance().getForClass(clazz);
    if (type == null) {
        return null;
    }
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.MOB_SPAWNER);
        Transform<org.spongepowered.api.world.World> transform = new Transform<>(((org.spongepowered.api.world.World) world), new Vector3d(x, y, z));
        ConstructEntityEvent.Pre event = SpongeEventFactory.createConstructEntityEventPre(Sponge.getCauseStackManager().getCurrentCause(), type, transform);
        SpongeImpl.postEvent(event);
        if (event.isCancelled()) {
            return null;
        }
    }
    Entity entity;
    try {
        entity = EntityList.createEntityFromNBT(compound, world);
    } catch (Exception e) {
        return null;
    }
    if (entity == null) {
        return null;
    }
    entity.setLocationAndAngles(x, y, z, entity.rotationYaw, entity.rotationPitch);
    if (attemptToSpawn && !world.spawnEntity(entity)) {
        return null;
    }
    if (compound.hasKey(NbtDataUtil.Minecraft.PASSENGERS, NbtDataUtil.TAG_LIST)) {
        final NBTTagList passengerList = compound.getTagList(NbtDataUtil.Minecraft.PASSENGERS, NbtDataUtil.TAG_COMPOUND);
        for (int i = 0; i < passengerList.tagCount(); i++) {
            final Entity passenger = readEntityFromCompoundAtWorld(passengerList.getCompoundTagAt(i), world, x, y, z, attemptToSpawn);
            if (passenger != null) {
                passenger.startRiding(entity, true);
            }
        }
    }
    return entity;
}
Also used : Entity(net.minecraft.entity.Entity) World(net.minecraft.world.World) EntityType(org.spongepowered.api.entity.EntityType) ConstructEntityEvent(org.spongepowered.api.event.entity.ConstructEntityEvent) NBTTagList(net.minecraft.nbt.NBTTagList) PrettyPrinter(org.spongepowered.asm.util.PrettyPrinter) Vector3d(com.flowpowered.math.vector.Vector3d) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ResourceLocation(net.minecraft.util.ResourceLocation) Transform(org.spongepowered.api.entity.Transform)

Example 25 with PrettyPrinter

use of org.spongepowered.asm.util.PrettyPrinter in project SpongeCommon by SpongePowered.

the class SpongeCauseStackManager method popCauseFrame.

@Override
public void popCauseFrame(StackFrame oldFrame) {
    enforceMainThread();
    checkNotNull(oldFrame, "oldFrame");
    CauseStackFrameImpl frame = this.frames.peek();
    if (frame != oldFrame) {
        // If the given frame is not the top frame then some form of
        // corruption of the stack has occured and we do our best to correct
        // it.
        // If the target frame is still in the stack then we can pop frames
        // off the stack until we reach it, otherwise we have no choice but
        // to simply throw an error.
        int offset = -1;
        int i = 0;
        for (CauseStackFrameImpl f : this.frames) {
            if (f == oldFrame) {
                offset = i;
                break;
            }
            i++;
        }
        if (!DEBUG_CAUSE_FRAMES && offset == -1) {
            // that was erroneously popped.
            throw new IllegalStateException("Cause Stack Frame Corruption! Attempted to pop a frame that was not on the stack.");
        }
        final PrettyPrinter printer = new PrettyPrinter(100).add("Cause Stack Frame Corruption!").centre().hr().add("Found %d frames left on the stack. Clearing them all.", new Object[] { offset + 1 });
        if (!DEBUG_CAUSE_FRAMES) {
            printer.add().add("Please add -Dsponge.debugcauseframes=true to your startup flags to enable further debugging output.");
            SpongeImpl.getLogger().warn("  Add -Dsponge.debugcauseframes to your startup flags to enable further debugging output.");
        } else {
            printer.add().add("Attempting to pop frame:").add(frame.stack_debug).add().add("Frames being popped are:").add(((CauseStackFrameImpl) oldFrame).stack_debug);
        }
        while (offset >= 0) {
            CauseStackFrameImpl f = this.frames.peek();
            if (DEBUG_CAUSE_FRAMES && offset > 0) {
                printer.add("   Stack frame in position %n:", offset);
                printer.add(f.stack_debug);
            }
            popCauseFrame(f);
            offset--;
        }
        printer.trace(System.err, SpongeImpl.getLogger(), Level.ERROR);
        if (offset == -1) {
            // so we throw an exception.
            throw new IllegalStateException("Cause Stack Frame Corruption! Attempted to pop a frame that was not on the stack.");
        }
        return;
    }
    this.frames.pop();
    // Remove new values
    boolean ctx_invalid = false;
    if (frame.hasNew()) {
        for (EventContextKey<?> key : frame.getNew()) {
            this.ctx.remove(key);
        }
        ctx_invalid = true;
    }
    // Restore old values
    if (frame.hasStoredValues()) {
        for (Map.Entry<EventContextKey<?>, Object> e : frame.getStoredValues()) {
            this.ctx.put(e.getKey(), e.getValue());
        }
        ctx_invalid = true;
    }
    if (ctx_invalid) {
        this.cached_ctx = null;
    }
    // If there were any objects left on the stack then we pop them off
    while (this.cause.size() > this.min_depth) {
        this.cause.pop();
        // and clear the cached causes
        this.cached_cause = null;
    }
    this.min_depth = frame.old_min_depth;
}
Also used : PrettyPrinter(org.spongepowered.asm.util.PrettyPrinter) EventContextKey(org.spongepowered.api.event.cause.EventContextKey) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

PrettyPrinter (org.spongepowered.asm.util.PrettyPrinter)27 Map (java.util.Map)9 Collection (java.util.Collection)7 ReportedException (net.minecraft.util.ReportedException)7 CauseStackManager (org.spongepowered.api.event.CauseStackManager)7 UUID (java.util.UUID)6 Entity (org.spongepowered.api.entity.Entity)6 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)5 SpawnEntityEvent (org.spongepowered.api.event.entity.SpawnEntityEvent)5 DropItemEvent (org.spongepowered.api.event.item.inventory.DropItemEvent)5 ItemDropData (org.spongepowered.common.event.tracking.context.ItemDropData)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 EntityItem (net.minecraft.entity.item.EntityItem)4 Sponge (org.spongepowered.api.Sponge)4 World (org.spongepowered.api.world.World)4 SpongeImpl (org.spongepowered.common.SpongeImpl)4 IOException (java.io.IOException)3 Collectors (java.util.stream.Collectors)3 Nullable (javax.annotation.Nullable)3