use of org.spongepowered.api.event.Listener in project modules-extra by CubeEngine.
the class ChopListener method onChop.
@Listener
public void onChop(final ChangeBlockEvent.Break event, @First Player player) {
if (!player.getItemInHand(HandTypes.MAIN_HAND).isPresent() || event.getCause().getContext().containsKey(EventContextKeys.PLAYER_SIMULATED)) {
return;
}
ItemStack axe = player.getItemInHand(HandTypes.MAIN_HAND).orElse(null);
if (axe == null || axe.getType() != DIAMOND_AXE || axe.get(Keys.ITEM_DURABILITY).orElse(0) <= 0 || !axe.get(Keys.ITEM_ENCHANTMENTS).orElse(emptyList()).contains(Enchantment.builder().type(EnchantmentTypes.PUNCH).level(5).build())) {
return;
}
if (!module.use.check(player)) {
return;
}
Sponge.getCauseStackManager().addContext(EventContextKeys.PLAYER_SIMULATED, player.getProfile());
for (Transaction<BlockSnapshot> transaction : event.getTransactions()) {
BlockType type = transaction.getOriginal().getState().getType();
Location<World> orig = transaction.getOriginal().getLocation().get();
BlockType belowType = orig.getBlockRelative(DOWN).getBlockType();
if (isLog(type) && isSoil(belowType)) {
TreeType treeType = transaction.getOriginal().get(TREE_TYPE).get();
Set<Location<World>> treeBlocks = findTreeBlocks(orig, treeType);
if (treeBlocks.isEmpty()) {
return;
}
int logs = 0;
int leaves = 0;
Set<Location> saplings = new HashSet<>();
for (Location<World> block : treeBlocks) {
if (isLog(block.getBlockType())) {
if (!block.equals(orig)) {
block.getExtent().playSound(SoundTypes.BLOCK_WOOD_STEP, block.getPosition(), 1);
}
logs++;
block.setBlockType(BlockTypes.AIR);
BlockType belowTyp = block.getBlockRelative(DOWN).getBlockType();
if (isSoil(belowTyp)) {
saplings.add(block);
}
}
if (block.getBlockType() == LEAVES || block.getBlockType() == LEAVES2) {
block.setBlockType(BlockTypes.AIR);
// TODO leaves sound?
block.getExtent().playSound(SoundTypes.BLOCK_GRASS_STEP, block.getPosition(), 1);
leaves++;
}
}
ItemStack log = ItemStack.builder().itemType(type.getItem().get()).quantity(logs).build();
log.offer(TREE_TYPE, treeType);
int apples = 0;
if (treeType == JUNGLE) {
leaves = leaves / 40;
} else {
if (treeType == DARK_OAK || treeType == OAK) {
apples = leaves / 200;
}
leaves = leaves / 20;
}
if (leaves == 0) {
leaves = 1;
}
BlockState sapState = BlockTypes.SAPLING.getDefaultState().with(TREE_TYPE, treeType).get();
if (this.module.autoplant.check(player)) {
leaves -= saplings.size();
leaves = Math.max(0, leaves);
transaction.setCustom(sapState.snapshotFor(transaction.getOriginal().getLocation().get()));
saplings.forEach(l -> l.setBlock(sapState));
}
final int uses = axe.get(Keys.ITEM_DURABILITY).get() - logs;
axe.offer(Keys.ITEM_DURABILITY, uses);
player.setItemInHand(HandTypes.MAIN_HAND, axe);
World world = player.getWorld();
Entity itemEntity;
Sponge.getCauseStackManager().removeContext(EventContextKeys.PLAYER_SIMULATED);
Sponge.getCauseStackManager().pushCause(player);
if (apples > 0) {
ItemStack apple = ItemStack.builder().itemType(APPLE).quantity(apples).build();
itemEntity = world.createEntity(ITEM, orig.getPosition());
itemEntity.offer(REPRESENTED_ITEM, apple.createSnapshot());
world.spawnEntity(itemEntity);
}
if (leaves > 0) {
ItemStack sap = ItemStack.builder().fromBlockState(sapState).build();
sap.setQuantity(leaves);
itemEntity = world.createEntity(ITEM, orig.getPosition());
itemEntity.offer(REPRESENTED_ITEM, sap.createSnapshot());
world.spawnEntity(itemEntity);
}
itemEntity = world.createEntity(ITEM, orig.getPosition());
itemEntity.offer(REPRESENTED_ITEM, log.createSnapshot());
world.spawnEntity(itemEntity);
return;
}
}
}
use of org.spongepowered.api.event.Listener in project modules-extra by CubeEngine.
the class HideListener method onJoin.
@Listener
public void onJoin(ClientConnectionEvent.Join event) {
Player player = event.getTargetEntity();
if (module.isHidden(player)) {
module.hidePlayer(player, true);
}
if (player.hasPermission(module.perms().AUTO_HIDE.getId())) {
event.setMessageCancelled(true);
module.hidePlayer(player, true);
i18n.send(player, POSITIVE, "You were automatically hidden!");
}
}
use of org.spongepowered.api.event.Listener in project modules-extra by CubeEngine.
the class FlyListener method playerInteract.
@Listener
public void playerInteract(final InteractBlockEvent.Secondary event, @First Player player) {
if (!player.getItemInHand(HandTypes.MAIN_HAND).map(i -> i.getType().equals(ItemTypes.FEATHER)).orElse(false)) {
return;
}
if (!player.hasPermission(FLY_FEATHER.getId())) {
i18n.send(player, NEGATIVE, "You dont have permission to use this!");
// Disable when player is flying
player.offer(Keys.CAN_FLY, false);
player.offer(Keys.IS_FLYING, false);
return;
}
// I Believe I Can Fly ...
player.offer(Keys.CAN_FLY, !player.get(Keys.CAN_FLY).get());
if (player.get(Keys.CAN_FLY).get()) {
final ItemStack feather = ItemStack.of(ItemTypes.FEATHER, -1);
player.getInventory().query(QueryOperationTypes.ITEM_STACK_IGNORE_QUANTITY.of(feather)).poll(1);
player.setVelocity(player.getVelocity().add(0, 1, 0));
// make sure the player stays flying
player.setLocation(player.getLocation().add(0, 0.05, 0));
player.offer(Keys.IS_FLYING, true);
i18n.send(player, POSITIVE, "You can now fly!");
Task flymore = new Task(Fly.class, tm) {
public // 2 feather/min
void run() {
if (!player.get(Keys.IS_FLYING).get()) {
player.offer(Keys.CAN_FLY, false);
this.cancelTask();
return;
}
if (player.getInventory().contains(ItemTypes.FEATHER)) {
player.getInventory().query(feather).poll(1);
} else {
player.offer(Keys.CAN_FLY, false);
this.cancelTask();
}
}
};
flymore.scheduleAsyncRepeatingTask(1000 * 30, 1000 * 30);
Task oldTask = this.tasks.put(player, flymore);
if (oldTask != null) {
oldTask.cancelTask();
}
} else {
// or not
player.offer(Keys.FALL_DISTANCE, 0f);
i18n.send(player, NEUTRAL, "You cannot fly anymore!");
}
}
use of org.spongepowered.api.event.Listener in project modules-extra by CubeEngine.
the class Fun method onEnable.
@Listener
public void onEnable(GamePreInitializationEvent event) {
cm.addCommands(this, new ThrowCommands(this, em, pm, tm, entityMatcher, i18n));
cm.addCommands(this, new NukeCommand(this, i18n, em));
cm.addCommands(this, new PlayerCommands(this, i18n, materialMatcher));
cm.addCommands(this, new DiscoCommand(this, i18n, tm));
cm.addCommands(this, new InvasionCommand(this, i18n, entityMatcher));
cm.addCommands(this, new RocketCommand(this, em, tm));
}
use of org.spongepowered.api.event.Listener in project SkinsRestorerX by DoNotSpamPls.
the class SkinsRestorer method onInitialize.
@Listener
public void onInitialize(GameInitializationEvent e) {
instance = this;
directory = Sponge.getGame().getConfigManager().getPluginConfig(this).getDirectory().toString();
try {
reloadConfigs();
} catch (Exception ex) {
ex.printStackTrace();
}
if (!Sponge.getServer().getOnlineMode())
Sponge.getEventManager().registerListener(this, ClientConnectionEvent.Login.class, new LoginListener());
CommandSpec skinCommand = CommandSpec.builder().description(Text.of("Set your skin")).arguments(GenericArguments.onlyOne(GenericArguments.string(Text.of("skin")))).executor(new SkinCommand()).build();
Sponge.getCommandManager().register(this, skinCommand, "skin");
}
Aggregations