/** * Gets the {@link CauseStackManager} for handling the current event cause * stack and context information. * * @return The cause stack manager */ default CauseStackManager getCauseStackManager() { return Sponge.getCauseStackManager(); }
@Override public <T> void setProvider(Object plugin, Class<T> service, T provider) { checkNotNull(plugin, "plugin"); checkNotNull(service, "service"); checkNotNull(provider, "provider"); Optional<PluginContainer> containerOptional = this.pluginManager.fromInstance(plugin); if (!containerOptional.isPresent()) { throw new IllegalArgumentException( "The provided plugin object does not have an associated plugin container " + "(in other words, is 'plugin' actually your plugin object?)"); } PluginContainer container = containerOptional.get(); ProviderRegistration<?> oldProvider = this.providers.put(service, new Provider<>(container, service, provider)); try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) { frame.addContext(EventContextKeys.SERVICE_MANAGER, this); frame.pushCause(container); Sponge.getEventManager().post(SpongeEventFactory.createChangeServiceProviderEvent(frame.getCurrentCause(), this.providers.get(service), Optional.ofNullable(oldProvider))); } }
@Override public CommandResult process(CommandSource source, String commandLine) throws CommandException { final String[] argSplit = commandLine.split(" ", 2); Optional<CommandMapping> cmdOptional = get(argSplit[0], source); if (!cmdOptional.isPresent()) { throw new CommandNotFoundException(t("commands.generic.notFound"), argSplit[0]); // TODO: Fix properly to use a SpongeTranslation?? } final String arguments = argSplit.length > 1 ? argSplit[1] : ""; CommandMapping mapping = cmdOptional.get(); Optional<PluginContainer> pluginOwner = Sponge.getCommandManager().getOwner(mapping); pluginOwner.ifPresent(pluginContainer -> Sponge.getCauseStackManager().pushCause(pluginContainer)); final CommandCallable spec = mapping.getCallable(); Sponge.getCauseStackManager().pushCause(spec); try { return spec.process(source, arguments); } catch (CommandNotFoundException e) { throw new CommandException(t("No such child command: %s", e.getCommand())); } finally { if (pluginOwner.isPresent()) { Sponge.getCauseStackManager().popCause(); } Sponge.getCauseStackManager().popCause(); } }
default DataRegistration<T, I> buildAndRegister(PluginContainer container) throws IllegalStateException, IllegalArgumentException, DataAlreadyRegisteredException { try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) { frame.pushCause(container); return build();
/** * Builds a {@link ShapedCraftingRecipe} from this builder. * * @param id The id of this recipe. * @param plugin The owning plugin. * @return A new {@link ShapedCraftingRecipe} * @throws IllegalStateException If not all required options * were specified * @deprecated Use {@link #build()} instead in combination with {@link #id(String)} */ @Deprecated default ShapedCraftingRecipe build(String id, Object plugin) { try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) { final PluginContainer container; if (plugin instanceof PluginContainer) { container = (PluginContainer) plugin; } else { container = Sponge.getPluginManager().fromInstance(plugin).orElseThrow(() -> new IllegalArgumentException( "Plugin must be a PluginContainer or plugin instance, not " + plugin)); } frame.pushCause(container); return id(id).build(); } } }
/** * Builds a new {@link ShapelessCraftingRecipe} from this builder. * * @param id The id of this recipe. * @param plugin The owning plugin. * @return A new {@link ShapelessCraftingRecipe} * @throws IllegalStateException If not all required options * were specified * @deprecated Use {@link #build()} instead in combination with {@link #id(String)} */ @Deprecated default ShapelessCraftingRecipe build(String id, Object plugin) { try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) { final PluginContainer container; if (plugin instanceof PluginContainer) { container = (PluginContainer) plugin; } else { container = Sponge.getPluginManager().fromInstance(plugin).orElseThrow(() -> new IllegalArgumentException( "Plugin must be a PluginContainer or plugin instance, not " + plugin)); } frame.pushCause(container); return id(id).build(); } } }
private void spawn(final SnipeData v) { try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) { Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.PLUGIN); for (int x = 0; x < v.getBrushSize(); x++) { Entity e = this.world.createEntity(this.entityType, this.lastBlock.getBlockPosition()); this.world.spawnEntity(e); } } }
/** * Register a brush for VoxelSniper to be able to use. * * @param clazz Brush implementing IBrush interface. * @param handles Handles under which the brush can be accessed ingame. */ public void registerSniperBrush(Class<? extends IBrush> clazz, String... handles) { checkNotNull(clazz, "Cannot register null as a brush."); RegisterBrushEvent event = new RegisterBrushEvent(Sponge.getCauseStackManager().getCurrentCause(), clazz, handles); Sponge.getEventManager().post(event); for (String handle : event.getAliases()) { this.brushes.put(handle.toLowerCase(), clazz); } this.brush_count++; }
private static void toggleSwitches(Location<World> location, boolean state, int maxRange, int maxLights, Player player) { int toggledLights = 0; for (int x = -maxRange; x < maxRange; x++) { for (int y = -maxRange; y < maxRange; y++) { for (int z = -maxRange; z < maxRange; z++) { if (Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2) > Math.pow(maxRange, 2)) continue; Location<World> offset = location.add(x, y, z); if (offset.getBlockType() == (state ? BlockTypes.REDSTONE_TORCH : BlockTypes.TORCH)) { toggledLights ++; Optional<Direction> connectedDirection = offset.get(Keys.DIRECTION); if (player != null) Sponge.getCauseStackManager().pushCause(player); BlockState newState = (state ? BlockTypes.TORCH : BlockTypes.REDSTONE_TORCH).getDefaultState(); if (connectedDirection.isPresent()) { newState = newState.with(Keys.DIRECTION, connectedDirection.get()).get(); } offset.setBlock(newState); if (player != null) Sponge.getCauseStackManager().popCause(); if (toggledLights > maxLights) return; } } } } }
public void setOutput(int outputId, boolean powered, IC ic) { if(outputId == -1) return; if (getOutput(outputId, ic) != powered) { Location<?> block = getPinLocation(outputId + getInputCount(), ic); if (!block.supports(Keys.POWERED)) return; // Can't set this. Sponge.getCauseStackManager().pushCause(ic); block.offer(Keys.POWERED, powered); Sponge.getCauseStackManager().popCause(); } }
@Override public void onVoteReceived(Channel channel, final Vote vote, VotifierSession.ProtocolVersion protocolVersion) { if (debug) { if (protocolVersion == VotifierSession.ProtocolVersion.ONE) { logger.info("Got a protocol v1 vote record from " + channel.remoteAddress() + " -> " + vote); } else { logger.info("Got a protocol v2 vote record from " + channel.remoteAddress() + " -> " + vote); } } Sponge.getScheduler().createTaskBuilder() .execute(() -> { VotifierEvent event = new VotifierEvent(vote, Sponge.getCauseStackManager().getCurrentCause()); Sponge.getEventManager().post(event); }) .submit(this); }
@Override public void perform(SnipeAction action, SnipeData data, Location<World> targetBlock, Location<World> lastBlock) { this.world = targetBlock.getExtent(); this.targetBlock = targetBlock; this.lastBlock = lastBlock; Sponge.getCauseStackManager().pushCause(data.owner().getPlayer()); switch (action) { case ARROW: this.arrow(data); break; case GUNPOWDER: this.powder(data); break; default: } Sponge.getCauseStackManager().popCause(); this.world = null; this.targetBlock = null; this.lastBlock = null; }
public IBrush setCurrentBrush(Class<? extends IBrush> brush) { Preconditions.checkNotNull(brush, "Can't set brush to null."); IBrush brushInstance = this.brushes.get(brush); if (brushInstance == null) { brushInstance = instanciateBrush(brush); Preconditions.checkNotNull(brushInstance, "Could not instanciate brush class."); this.brushes.put(brush, brushInstance); } if (this.snipeData.owner().getPlayer().hasPermission(brushInstance.getPermissionNode())) { Sponge.getCauseStackManager().pushCause(VoxelSniper.getInstance()); Sponge.getCauseStackManager().pushCause(this.snipeData.owner().getPlayer()); ChangeBrushEvent event = new ChangeBrushEvent(Sponge.getCauseStackManager().getCurrentCause(), this.snipeData, brushInstance); Sponge.getEventManager().post(event); Sponge.getCauseStackManager().popCauses(2); this.previousBrush = this.currentBrush; this.currentBrush = brush; return brushInstance; } return null; }
public static void setupSpongeMock() { Cause mockCause = Cause.of(EventContext.empty(), "test"); CauseStackManager csm = Mockito.mock(CauseStackManager.class); Mockito.when(csm.getCurrentCause()).thenReturn(mockCause); PowerMockito.mockStatic(Sponge.class); PowerMockito.when(Sponge.getCauseStackManager()).thenReturn(csm); }
@Override public void onForward(final Vote v) { if (debug) { logger.info("Got a forwarded vote -> " + v); } Sponge.getScheduler().createTaskBuilder() .execute(() -> { VotifierEvent event = new VotifierEvent(v, Sponge.getCauseStackManager().getCurrentCause()); Sponge.getEventManager().post(event); }) .submit(this); } }
@Override public boolean doAction(Dispenser dispenser, ItemStack[] recipe) { Direction face = dispenser.getLocation().get(Keys.DIRECTION).orElse(Direction.NONE); if (face != Direction.NONE) { Location<World> location = dispenser.getLocation().getRelative(face).add(0.5, 0.5, 0.5); ThrownExpBottle bottle = (ThrownExpBottle) dispenser.getWorld().createEntity(EntityTypes.THROWN_EXP_BOTTLE, location.getPosition()); Vector3d bottleVelocity = face.asOffset().add(0, 0.1f, 0).normalize().mul(1.5f); bottle.setVelocity(bottleVelocity); try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) { Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.CUSTOM); dispenser.getWorld().spawnEntity(bottle); } return true; } return false; } }
@Override public boolean doAction(Dispenser dispenser, ItemStack[] recipe) { Direction face = dispenser.getLocation().get(Keys.DIRECTION).orElse(Direction.NONE); if (face != Direction.NONE) { Location<World> location = dispenser.getLocation().getRelative(face).add(0.5, 0.5, 0.5); PrimedTNT tnt = (PrimedTNT) dispenser.getWorld().createEntity(EntityTypes.PRIMED_TNT, location.getPosition()); Vector3d tntVelocity = face.asOffset().add(0, 0.1f, 0).normalize().mul(2f); tnt.setVelocity(tntVelocity); try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) { Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.CUSTOM); dispenser.getWorld().spawnEntity(tnt); } return true; } return false; } }
@Override public boolean doAction(Dispenser dispenser, ItemStack[] recipe) { Direction face = dispenser.getLocation().get(Keys.DIRECTION).orElse(Direction.NONE); if (face != Direction.NONE) { Location<World> location = dispenser.getLocation().getRelative(face).add(0.5, 0.5, 0.5); Snowball snowball = (Snowball) dispenser.getWorld().createEntityNaturally(EntityTypes.SNOWBALL, location.getPosition()); Vector3d arrowVelocity = face.asOffset().add(0, 0.1f, 0).normalize().mul(1.5f); snowball.setVelocity(arrowVelocity); try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) { Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.CUSTOM); dispenser.getWorld().spawnEntity(snowball); } return true; } return false; } }
@Override public boolean doAction(Dispenser dispenser, ItemStack[] recipe) { Direction face = dispenser.getLocation().get(Keys.DIRECTION).orElse(Direction.NONE); if (face != Direction.NONE) { Location<World> location = dispenser.getLocation().getRelative(face).add(0.5, 0.5, 0.5); TippedArrow arrow = (TippedArrow) dispenser.getWorld().createEntityNaturally(EntityTypes.TIPPED_ARROW, location.getPosition()); arrow.offer(Keys.FIRE_TICKS, 5000); Vector3d arrowVelocity = face.asOffset().add(0, 0.1f, 0).normalize().mul(1.5f); arrow.setVelocity(arrowVelocity); try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) { Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.CUSTOM); dispenser.getWorld().spawnEntity(arrow); } return true; } return false; } }
private void addChair(Player player, Location<World> location) { Entity entity = location.getExtent().createEntity(EntityTypes.ARMOR_STAND, location.getBlockPosition().toDouble().sub(-0.5, 1.2, -0.5)); entity.offer(Keys.INVISIBLE, true); entity.offer(Keys.HAS_GRAVITY, false); if (faceCorrectDirection.getValue() && location.supports(Keys.DIRECTION)) { Vector3d euler = LocationUtil.cartesianToEuler(location.get(Keys.DIRECTION).orElse(Direction.NONE).getOpposite().asOffset()); entity.setRotation(euler); player.setRotation(euler); } Sponge.getCauseStackManager().pushCause(player); location.getExtent().spawnEntity(entity); Sponge.getCauseStackManager().popCause(); Chair<?> chair = new Chair<>((ArmorStand) entity, location, player.getLocation()); entity.addPassenger(player); player.sendMessage(Text.of(TextColors.YELLOW, "You sit down!")); chairs.put(player.getUniqueId(), chair); }