Skip to content

Commit

Permalink
Update for cause refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Sep 10, 2017
1 parent 6469dca commit ce31c03
Show file tree
Hide file tree
Showing 46 changed files with 135 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.config.DefaultConfig;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.game.GameReloadEvent;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.event.game.state.GameStartedServerEvent;
Expand Down Expand Up @@ -148,7 +147,7 @@ public void onPreInitialization(GamePreInitializationEvent event) {
}

@Listener
public void onInitialization(GameStartedServerEvent event) throws IllegalAccessException {
public void onInitialization(GameStartedServerEvent event) {
if(config.dataOnlyMode.getValue()) {
logger.info("Halting CraftBook Initialization - Data Only Mode! Note: Nothing will work.");
return;
Expand Down Expand Up @@ -354,10 +353,6 @@ private void disableMechanics() {
moduleController.disableModules();
}

public Cause.Builder getCause() {
return Cause.source(this.container);
}

@Override
public Optional<SelfTriggerManager> getSelfTriggerManager() {
return Optional.ofNullable(this.selfTriggerManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.cause.NamedCause;
import org.spongepowered.api.event.filter.cause.Named;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.service.permission.PermissionDescription;
Expand Down Expand Up @@ -67,13 +66,14 @@ public void onInitialize() throws CraftBookException {
}

@Listener
public void onPlayerInteract(InteractBlockEvent.Secondary event, @Named(NamedCause.SOURCE) Player player) {
public void onPlayerInteract(InteractBlockEvent.Secondary event, @First Player player) {
event.getTargetBlock().getLocation().ifPresent((location) -> {
int powerLevel = BlockUtil.getDirectBlockPowerLevel(location).orElse(-1);

HandType hand = event instanceof InteractBlockEvent.Secondary.MainHand ? HandTypes.MAIN_HAND : HandTypes.OFF_HAND;

if (powerLevel >= 0 && permissionNode.hasPermission(player) && player.getItemInHand(hand).isPresent() && player.getItemInHand(hand).get().getItem() == ammeterItem.getValue().getItem()) {
if (powerLevel >= 0 && permissionNode.hasPermission(player) && player.getItemInHand(hand).isPresent()
&& player.getItemInHand(hand).get().getType() == ammeterItem.getValue().getType()) {
player.sendMessage(getCurrentLine(powerLevel));
event.setCancelled(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.ChangeBlockEvent;
import org.spongepowered.api.event.block.NotifyNeighborBlockEvent;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.util.Direction;
import org.spongepowered.api.world.LocatableBlock;
Expand Down Expand Up @@ -131,7 +130,7 @@ public void run () {
FallingBlock fallingBlock = (FallingBlock) ladder.getExtent().createEntity(EntityTypes.FALLING_BLOCK, ladder.getPosition().add(0.5, 0, 0.5));
fallingBlock.offer(Keys.FALLING_BLOCK_STATE, ladder.getBlock());
fallingBlock.offer(Keys.CAN_PLACE_AS_BLOCK, true);
ladder.getExtent().spawnEntity(fallingBlock, Cause.source(CraftBookPlugin.spongeInst().getContainer()).build());
ladder.getExtent().spawnEntity(fallingBlock);

physics.checkForPhysics(ladder.getRelative(Direction.UP));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.ChangeBlockEvent;
import org.spongepowered.api.event.block.TickBlockEvent;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.ItemStack;
Expand Down Expand Up @@ -74,8 +73,8 @@ public void onInitialize() throws CraftBookException {
public void onTick(TickBlockEvent.Random event) {
event.getTargetBlock().getLocation().ifPresent(worldLocation -> {
if (isValidFernFarming(worldLocation) && ThreadLocalRandom.current().nextInt(10) == 0) {
worldLocation.setBlock(BlockState.builder().blockType(BlockTypes.DOUBLE_PLANT).add(Keys.DOUBLE_PLANT_TYPE, DoublePlantTypes.FERN).add(Keys.PORTION_TYPE, PortionTypes.BOTTOM).build(), Cause.source(CraftBookPlugin.spongeInst().getContainer()).build());
worldLocation.getRelative(Direction.UP).setBlock(BlockState.builder().blockType(BlockTypes.DOUBLE_PLANT).add(Keys.DOUBLE_PLANT_TYPE, DoublePlantTypes.FERN).add(Keys.PORTION_TYPE, PortionTypes.TOP).build(), Cause.source(CraftBookPlugin.spongeInst().getContainer()).build());
worldLocation.setBlock(BlockState.builder().blockType(BlockTypes.DOUBLE_PLANT).add(Keys.DOUBLE_PLANT_TYPE, DoublePlantTypes.FERN).add(Keys.PORTION_TYPE, PortionTypes.BOTTOM).build());
worldLocation.getRelative(Direction.UP).setBlock(BlockState.builder().blockType(BlockTypes.DOUBLE_PLANT).add(Keys.DOUBLE_PLANT_TYPE, DoublePlantTypes.FERN).add(Keys.PORTION_TYPE, PortionTypes.TOP).build());
}
});
}
Expand All @@ -89,12 +88,11 @@ public void onBlockBreak(ChangeBlockEvent.Break event, @First Player player) {
.filter(snapshot -> snapshot.getState().get(Keys.PORTION_TYPE).orElse(PortionTypes.BOTTOM).equals(PortionTypes.TOP))
.forEach(snapshot -> Sponge.getScheduler().createTaskBuilder().execute(task -> {
snapshot.getLocation().get().getRelative(Direction.DOWN)
.setBlock(BlockState.builder().blockType(BlockTypes.TALLGRASS).add(Keys.SHRUB_TYPE, ShrubTypes.FERN).build(),
CraftBookPlugin.spongeInst().getCause().build());
.setBlock(BlockState.builder().blockType(BlockTypes.TALLGRASS).add(Keys.SHRUB_TYPE, ShrubTypes.FERN).build());
if (player.get(Keys.GAME_MODE).orElse(GameModes.SURVIVAL) != GameModes.CREATIVE) {
Item item = (Item) snapshot.getLocation().get().getExtent().createEntity(EntityTypes.ITEM, snapshot.getPosition());
item.offer(Keys.REPRESENTED_ITEM, ItemStack.builder().itemType(ItemTypes.TALLGRASS).add(Keys.SHRUB_TYPE, ShrubTypes.FERN).build().createSnapshot());
snapshot.getLocation().get().spawnEntity(item, CraftBookPlugin.spongeInst().getCause().build());
snapshot.getLocation().get().spawnEntity(item);
}
}).submit(CraftBookPlugin.spongeInst().getContainer()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.cause.NamedCause;
import org.spongepowered.api.event.filter.cause.Named;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.service.permission.PermissionDescription;
import org.spongepowered.api.text.Text;
Expand Down Expand Up @@ -97,14 +96,14 @@ public void onInitialize() throws CraftBookException {
}

@Listener
public void onPlayerInteract(InteractBlockEvent.Secondary.MainHand event, @Named(NamedCause.SOURCE) Player player) {
public void onPlayerInteract(InteractBlockEvent.Secondary.MainHand event, @First Player player) {
event.getTargetBlock().getLocation().filter((this::isValid)).ifPresent(location -> {
if (!sneakState.getValue().doesPass(player.get(Keys.IS_SNEAKING).orElse(false) || !usePermissions.hasPermission(player))) {
return; //Don't alert the player with this mechanic.
}

if (!readWhenHoldingBlock.getValue()) {
ItemStack stack = player.getItemInHand(HandTypes.MAIN_HAND).filter((itemStack -> itemStack.getItem().getBlock().isPresent())).orElse(null);
ItemStack stack = player.getItemInHand(HandTypes.MAIN_HAND).filter((itemStack -> itemStack.getType().getBlock().isPresent())).orElse(null);
if (stack != null)
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static com.sk89q.craftbook.sponge.util.locale.TranslationsManager.USE_PERMISSIONS;

import com.flowpowered.math.vector.Vector3d;
import com.google.common.collect.Lists;
import com.google.common.reflect.TypeToken;
import com.google.inject.Inject;
Expand Down Expand Up @@ -47,10 +46,6 @@
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.ChangeBlockEvent;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.cause.NamedCause;
import org.spongepowered.api.event.cause.entity.spawn.SpawnCause;
import org.spongepowered.api.event.cause.entity.spawn.SpawnTypes;
import org.spongepowered.api.event.entity.RideEntityEvent;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.service.permission.PermissionDescription;
Expand Down Expand Up @@ -167,7 +162,9 @@ private void addChair(Player player, Location<World> location) {
entity.offer(Keys.INVISIBLE, true);
entity.offer(Keys.HAS_GRAVITY, false);

location.getExtent().spawnEntity(entity, Cause.of(NamedCause.of("root", SpawnCause.builder().type(SpawnTypes.CUSTOM).build()), NamedCause.source(player)));
Sponge.getCauseStackManager().pushCause(player);
location.getExtent().spawnEntity(entity);
Sponge.getCauseStackManager().popCause();

Chair<?> chair = new Chair<>((ArmorStand) entity, location, player.getLocation());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.block.NotifyNeighborBlockEvent;
import org.spongepowered.api.event.cause.NamedCause;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.service.permission.PermissionDescription;
import org.spongepowered.api.util.Direction;
Expand Down Expand Up @@ -83,7 +82,7 @@ public void onBlockUpdate(NotifyNeighborBlockEvent event, @First LocatableBlock
Location<World> block = source.getLocation();

if (isValid(block)) {
Player player = event.getCause().get(NamedCause.SOURCE, Player.class).orElse(null);
Player player = event.getCause().first(Player.class).orElse(null);

boolean isPowered = BlockUtil.getBlockPowerLevel(block).orElse(0) > 0;
boolean wasPowered = block.get(CraftBookKeys.LAST_POWER).orElse(0) > 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.block.NotifyNeighborBlockEvent;
import org.spongepowered.api.event.cause.NamedCause;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.event.filter.cause.Named;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.Inventory;
Expand Down Expand Up @@ -107,21 +105,21 @@ public boolean verifyLines(Location<World> location, List<Text> lines, @Nullable
}

@Listener
public void onPlayerInteract(InteractBlockEvent.Secondary event, @Named(NamedCause.SOURCE) Player player) {
public void onPlayerInteract(InteractBlockEvent.Secondary event, @First Player player) {
event.getTargetBlock().getLocation().ifPresent(location -> {
if (isValid(location)) {
location.getTileEntity().map(sign -> (Sign) sign).ifPresent(sign -> {
ItemStack itemStack = player.getItemInHand(event.getHandType()).filter(stack -> FuelSource.canCookWith(stack.getItem())).orElse(null);
ItemStack itemStack = player.getItemInHand(event.getHandType()).filter(stack -> FuelSource.canCookWith(stack.getType())).orElse(null);
if (itemStack != null) {
if (refuelPermissions.hasPermission(player)) {
int value = FuelSource.getFuelValue(itemStack.getItem());
int value = FuelSource.getFuelValue(itemStack.getType());
increaseMultiplier(sign, value);

if (itemStack.getQuantity() > 1) {
itemStack.setQuantity(itemStack.getQuantity() - 1);
player.setItemInHand(event.getHandType(), itemStack);
} else {
if (itemStack.getItem() == ItemTypes.LAVA_BUCKET) {
if (itemStack.getType() == ItemTypes.LAVA_BUCKET) {
player.setItemInHand(event.getHandType(), ItemStack.of(ItemTypes.BUCKET, 1));
} else {
player.setItemInHand(event.getHandType(), null);
Expand All @@ -133,7 +131,7 @@ public void onPlayerInteract(InteractBlockEvent.Secondary event, @Named(NamedCau
} else {
Location<?> chestBlock = SignUtil.getBackBlock(location).add(0, 2, 0);
chestBlock.getTileEntity().filter(tileEntity -> tileEntity instanceof Chest).map(tileEntity -> (Chest) tileEntity).ifPresent(chest ->
player.openInventory(chest.getInventory(), CraftBookPlugin.spongeInst().getCause().build()));
player.openInventory(chest.getInventory()));
}
});
}
Expand All @@ -150,7 +148,7 @@ public void onBlockUpdate(NotifyNeighborBlockEvent event, @First LocatableBlock
Sign sign = (Sign) block.getTileEntity().get();

if (isMechanicSign(sign)) {
Player player = event.getCause().get(NamedCause.SOURCE, Player.class).orElse(null);
Player player = event.getCause().first(Player.class).orElse(null);
if(player != null) {
if(!refuelPermissions.hasPermission(player)) {
player.sendMessage(Text.of("You don't have permission to refuel this mechanic!"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.cause.NamedCause;
import org.spongepowered.api.event.entity.MoveEntityEvent;
import org.spongepowered.api.event.filter.cause.Named;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.service.permission.PermissionDescription;
import org.spongepowered.api.service.permission.Subject;
import org.spongepowered.api.text.Text;
Expand Down Expand Up @@ -90,7 +89,7 @@ public SpongePermissionNode getCreatePermission() {
}

@Listener
public void onPlayerInteract(InteractBlockEvent.Secondary.MainHand event, @Named(NamedCause.SOURCE) Player player) {
public void onPlayerInteract(InteractBlockEvent.Secondary.MainHand event, @First Player player) {
event.getTargetBlock().getLocation().ifPresent((location) -> {
Location<World> signLocation = location;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,9 @@
import org.spongepowered.api.event.block.ChangeBlockEvent;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.cause.NamedCause;
import org.spongepowered.api.event.cause.entity.damage.source.EntityDamageSource;
import org.spongepowered.api.event.cause.entity.spawn.EntitySpawnCause;
import org.spongepowered.api.event.cause.entity.spawn.SpawnCause;
import org.spongepowered.api.event.cause.entity.spawn.SpawnTypes;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.event.filter.cause.Named;
import org.spongepowered.api.event.item.inventory.DropItemEvent;
import org.spongepowered.api.item.Enchantments;
import org.spongepowered.api.item.ItemTypes;
Expand Down Expand Up @@ -205,7 +201,7 @@ private Optional<ItemStack> getStackForEntity(EntityType type, @Nullable GamePro
}

@Listener
public void onItemDrops(DropItemEvent.Destruct event, @First EntitySpawnCause spawnCause) {
public void onItemDrops(DropItemEvent.Destruct event, @First Entity spawnCause) {
EntityDamageSource damageSource = event.getCause().first(EntityDamageSource.class).orElse(null);
Entity killer = null;

Expand Down Expand Up @@ -235,20 +231,20 @@ public void onItemDrops(DropItemEvent.Destruct event, @First EntitySpawnCause sp
}

GameProfile profile = null;
if (spawnCause.getEntity() instanceof Player) {
profile = ((Player) spawnCause.getEntity()).getProfile();
if (spawnCause instanceof Player) {
profile = ((Player) spawnCause).getProfile();
}

getStackForEntity(spawnCause.getEntity().getType(), profile).ifPresent(itemStack -> {
Vector3d location = spawnCause.getEntity().getLocation().getPosition();
Item item = (Item) spawnCause.getEntity().getWorld().createEntity(EntityTypes.ITEM, location);
getStackForEntity(spawnCause.getType(), profile).ifPresent(itemStack -> {
Vector3d location = spawnCause.getLocation().getPosition();
Item item = (Item) spawnCause.getWorld().createEntity(EntityTypes.ITEM, location);
item.offer(Keys.REPRESENTED_ITEM, itemStack.createSnapshot());
spawnCause.getEntity().getWorld().spawnEntity(item, Cause.of(NamedCause.of("root", spawnCause)));
spawnCause.getWorld().spawnEntity(item);
});
}

@Listener
public void onPlayerInteract(InteractBlockEvent.Secondary.MainHand event, @Named(NamedCause.SOURCE) Player player) {
public void onPlayerInteract(InteractBlockEvent.Secondary.MainHand event, @First Player player) {
if (!showNameClick.getValue()) {
return;
}
Expand Down Expand Up @@ -292,12 +288,11 @@ public void onBlockBreak(ChangeBlockEvent.Break event) {
getStackForEntity(entityType, profile).ifPresent(itemStack -> {
Item item = (Item) location.getExtent().createEntity(EntityTypes.ITEM, location.getPosition().add(0.5, 0.5, 0.5));
item.offer(Keys.REPRESENTED_ITEM, itemStack.createSnapshot());
location.getExtent().spawnEntity(item, Cause.of(NamedCause.of("root",
SpawnCause.builder().type(SpawnTypes.DROPPED_ITEM).build())));
location.getExtent().spawnEntity(item);

event.setCancelled(true);
Sponge.getScheduler().createTaskBuilder().execute(() ->
location.setBlockType(BlockTypes.AIR, CraftBookPlugin.spongeInst().getCause().build())).submit(CraftBookPlugin.spongeInst().container);
location.setBlockType(BlockTypes.AIR)).submit(CraftBookPlugin.spongeInst().container);
});
}
}
Expand Down
Loading

0 comments on commit ce31c03

Please sign in to comment.