use of mc.dragons.core.gameobject.item.Item in project DragonsOnline by UniverseCraft.
the class QuestAction method execute.
/**
* Run this action on the specified user.
*
* @param user
* @return The result of running this action.
*/
public QuestActionResult execute(User user) {
if (quest.isLocked() && !PermissionUtil.verifyActivePermissionLevel(user, PermissionLevel.GM, false))
return new QuestActionResult(false, false);
if (action == QuestActionType.TELEPORT_PLAYER) {
user.getPlayer().teleport(to);
} else if (action == QuestActionType.SPAWN_NPC) {
npcClassDeferredLoad();
World world = user.getPlayer().getWorld();
NPC npc = npcLoader.registerNew(world, to, npcClass);
if (phased) {
npc.phase(user.getPlayer());
}
if (!npc.getNPCType().isPersistent()) {
user.markNPCForCleanup(quest, npc);
}
quest.registerNPCReference(user, npc, npcReferenceName);
} else if (action == QuestActionType.BEGIN_DIALOGUE) {
npcClassDeferredLoad();
user.setDialogueBatch(quest, npcClass.getName(), dialogue);
new BukkitRunnable() {
@Override
public void run() {
if (!user.nextDialogue()) {
user.updateQuests(null);
cancel();
}
}
}.runTaskTimer(dragons, 0L, 40L);
return new QuestActionResult(false, true);
} else if (action == QuestActionType.GIVE_XP) {
user.getPlayer().sendMessage(ChatColor.GRAY + "+ " + ChatColor.LIGHT_PURPLE + xpAmount + " XP" + ChatColor.GRAY + " from quest " + quest.getQuestName());
user.addXP(xpAmount);
} else if (action == QuestActionType.GOTO_STAGE) {
user.debug(" - going to stage " + stage + " (" + quest.getSteps().get(stage).getStepName() + ")");
user.updateQuestProgress(quest, quest.getSteps().get(stage), notify);
return new QuestActionResult(true, false);
} else if (action == QuestActionType.TELEPORT_NPC) {
npcClassDeferredLoad();
NPC npc = quest.getNPCByReference(user, npcReferenceName);
npc.getEntity().teleport(to);
} else if (action == QuestActionType.PATHFIND_NPC) {
npcClassDeferredLoad();
NPC npc = quest.getNPCByReference(user, npcReferenceName);
double speed = 0.25D;
if (npc.getEntity() instanceof Attributable) {
speed = ((Attributable) npc.getEntity()).getAttribute(Attribute.GENERIC_MOVEMENT_SPEED).getValue();
}
boolean hasAI = dragons.getBridge().hasAI(npc.getEntity());
if (hasAI) {
dragons.getBridge().setEntityAI(npc.getEntity(), false);
}
PathfindingUtil.walkToLocation(npc.getEntity(), to, speed, e -> {
if (stage != -1) {
user.updateQuestProgress(quest, quest.getSteps().get(stage), false);
}
if (hasAI) {
dragons.getBridge().setEntityAI(e, true);
}
});
return new QuestActionResult(stage != -1, false);
} else if (action == QuestActionType.TAKE_ITEM) {
int remaining = quantity;
for (ItemStack itemStack : user.getPlayer().getInventory().getContents()) {
Item item = ItemLoader.fromBukkit(itemStack);
if (item != null && item.getClassName().equals(itemClass.getClassName())) {
int removeAmount = Math.min(remaining, item.getQuantity());
user.takeItem(item, removeAmount, true, true, false);
user.markItemForCleanup(quest, item, quantity);
remaining -= item.getQuantity();
if (remaining <= 0) {
user.markItemForCleanup(quest, item, quantity);
return new QuestActionResult(false, false);
}
}
}
} else if (action == QuestActionType.GIVE_ITEM) {
Item item = itemLoader.registerNew(itemClass);
item.setQuantity(quantity);
user.giveItem(item);
user.markItemForCleanup(quest, item, -quantity);
} else if (action == QuestActionType.ADD_POTION_EFFECT) {
user.getPlayer().addPotionEffect(new PotionEffect(effectType, duration, amplifier));
} else if (action == QuestActionType.REMOVE_POTION_EFFECT) {
user.getPlayer().removePotionEffect(effectType);
} else if (action == QuestActionType.COMPLETION_HEADER) {
new BukkitRunnable() {
private float pitch = 1.0F;
@Override
public void run() {
user.getPlayer().playSound(user.getPlayer().getLocation(), Sound.BLOCK_NOTE_BLOCK_BASS, 1.0F, pitch);
pitch = (float) (pitch + 0.05D);
if (pitch > 2.0F) {
cancel();
}
}
}.runTaskTimer(dragons, 0L, 1L);
user.getPlayer().sendMessage(" ");
user.getPlayer().sendMessage(ChatColor.GREEN + "" + ChatColor.MAGIC + "ff" + ChatColor.DARK_GREEN + " Quest Complete: " + quest.getQuestName() + ChatColor.GREEN + " " + ChatColor.MAGIC + "ff");
user.getPlayer().sendMessage(ChatColor.GRAY + "Rewards:");
} else if (action == QuestActionType.WAIT) {
user.debug("Waiting " + waitTime + "s");
user.setQuestPaused(quest, true);
sync(() -> {
user.debug("Resuming quest actions");
user.setQuestPaused(quest, false);
user.updateQuests(null);
}, 20 * waitTime);
return new QuestActionResult(false, true);
} else if (action == QuestActionType.CHOICES) {
user.getPlayer().sendMessage(" ");
for (Entry<String, Integer> choice : choices.entrySet()) {
user.getPlayer().spigot().sendMessage(StringUtil.clickableHoverableText(ChatColor.YELLOW + " • " + ChatColor.GRAY + choice.getKey(), "/questchoice " + quest.getName() + " " + choice.getKey(), ChatColor.YELLOW + "Click to respond", ChatColor.GRAY + "Quest: " + ChatColor.RESET + quest.getQuestName(), ChatColor.GRAY + "Response: " + ChatColor.RESET + choice.getKey()));
}
user.getPlayer().sendMessage(" ");
user.setQuestPaused(quest, true);
return new QuestActionResult(false, true);
} else if (action == QuestActionType.FAIL_QUEST) {
user.getPlayer().sendMessage(ChatColor.RED + "Quest failed: " + quest.getQuestName());
user.removeQuest(quest);
return new QuestActionResult(true, false);
} else if (action == QuestActionType.STASH_ITEMS) {
user.stashItems(quest, materialType);
} else if (action == QuestActionType.UNSTASH_ITEMS) {
user.unstashItems(quest, materialType);
} else if (action == QuestActionType.SPAWN_RELATIVE) {
npcClassDeferredLoad();
Location loc = user.getPlayer().getLocation();
for (int i = 0; i < quantity; i++) {
double angle = Math.random() * Math.PI * 2;
double radius = Math.random() * this.radius;
double dX = radius * Math.cos(angle);
double dZ = radius * Math.sin(angle);
Location spawn = BlockUtil.getAirAboveXZ(loc.clone().add(dX, 0, dZ));
NPC npc = npcLoader.registerNew(spawn.getWorld(), spawn, npcClass);
if (phased) {
npc.phase(user.getPlayer());
}
if (!npc.getNPCType().isPersistent()) {
user.markNPCForCleanup(quest, npc);
}
}
}
return new QuestActionResult(false, false);
}
use of mc.dragons.core.gameobject.item.Item in project DragonsOnline by UniverseCraft.
the class QuestTrigger method test.
/**
* @param user
* @param event
* @return Whether to execute the actions associated with this trigger
*/
public boolean test(User user, Event event) {
if (type == TriggerType.INSTANT) {
return true;
} else if (type == TriggerType.NEVER) {
return false;
} else if (type == TriggerType.HAS_ITEM || type == TriggerType.HAS_NO_ITEM || type == TriggerType.HAS_LESS_ITEM) {
int has = 0;
for (ItemStack itemStack : user.getPlayer().getInventory().getContents()) {
Item item = ItemLoader.fromBukkit(itemStack);
if (item != null && item.getClassName().equals(itemClass.getClassName())) {
has += itemStack.getAmount();
}
}
if (type == TriggerType.HAS_ITEM) {
return has >= quantity;
} else if (type == TriggerType.HAS_NO_ITEM) {
return has == 0;
} else if (type == TriggerType.HAS_LESS_ITEM) {
return has < quantity;
}
} else if (type == TriggerType.ENTER_REGION) {
user.updateState(false, false);
if (user.getRegions().contains(region)) {
return true;
}
} else if (type == TriggerType.WALK_REGION) {
user.updateState(false, false);
if (user.getRegions().contains(region) && user.getContinuousWalkDistance(region) >= minDistance) {
return true;
}
} else if (type == TriggerType.EXIT_REGION) {
user.updateState(false, false);
if (!user.getRegions().contains(region)) {
return true;
}
} else if (type == TriggerType.CLICK_NPC) {
npcClassDeferredLoad();
if (event == null) {
return false;
}
if (event instanceof PlayerInteractEntityEvent) {
PlayerInteractEntityEvent interactEvent = (PlayerInteractEntityEvent) event;
NPC npc = NPCLoader.fromBukkit(interactEvent.getRightClicked());
if (npc == null) {
return false;
}
if (npc.getNPCClass().equals(npcClass)) {
return true;
}
}
} else if (type == TriggerType.KILL_NPC) {
npcClassDeferredLoad();
if (event == null) {
return false;
}
if (event instanceof EntityDeathEvent) {
EntityDeathEvent deathEvent = (EntityDeathEvent) event;
NPC npc = NPCLoader.fromBukkit(deathEvent.getEntity());
if (npc == null) {
return false;
}
if (npc.getNPCClass().equals(npcClass)) {
killQuantity.put(user, killQuantity.getOrDefault(user, 0) + 1);
if (killQuantity.getOrDefault(user, 0) >= quantity) {
killQuantity.remove(user);
return true;
}
}
}
} else if (type == TriggerType.BRANCH_CONDITIONAL) {
for (Entry<QuestTrigger, QuestAction> conditional : branchPoints.entrySet()) {
if (conditional.getKey().test(user, event)) {
QuestActionResult result = conditional.getValue().execute(user);
user.debug(" [ - ran branch conditional actions on trigger " + conditional.getKey().getTriggerType() + ", stageModified=" + result.wasStageModified() + ", shouldPause=" + result.shouldPause());
if (result.shouldPause()) {
user.setQuestPaused(quest, true);
}
return !result.wasStageModified() && !result.shouldPause();
}
}
}
return false;
}
use of mc.dragons.core.gameobject.item.Item in project DragonsOnline by UniverseCraft.
the class PlayerEventListeners method onItemHeldChange.
@EventHandler
public void onItemHeldChange(PlayerItemHeldEvent event) {
LOGGER.trace("Item held change event on " + event.getPlayer().getName() + ": " + event.getPreviousSlot() + " -> " + event.getNewSlot());
ItemStack held = event.getPlayer().getInventory().getItemInMainHand();
Item heldItem = ItemLoader.fromBukkit(held);
if (heldItem != null && !heldItem.hasCooldownRemaining()) {
event.getPlayer().getInventory().setItemInMainHand(heldItem.localRename(heldItem.getDecoratedName()));
}
}
use of mc.dragons.core.gameobject.item.Item in project DragonsOnline by UniverseCraft.
the class PlayerEventListeners method onPickupItem.
@EventHandler
public void onPickupItem(EntityPickupItemEvent event) {
if (event.getEntityType() != EntityType.PLAYER)
return;
final Player player = (Player) event.getEntity();
ItemStack pickup = event.getItem().getItemStack();
User user = UserLoader.fromPlayer(player);
final Item item = ItemLoader.fromBukkit(pickup);
if (item == null)
return;
LOGGER.trace("Pickup item event on " + player.getName() + " of " + (item == null ? "null" : item.getIdentifier()) + " (x" + pickup.getAmount() + ")");
if (item.getItemClass().getClassName().equals(GOLD_CURRENCY_ITEM_CLASS_NAME)) {
int amount = pickup.getAmount();
user.giveGold(amount * 1.0D);
sync(() -> Arrays.asList(player.getInventory().getContents()).stream().filter(i -> (i != null)).filter(i -> (i.getItemMeta() != null)).map(i -> ItemLoader.fromBukkit(i)).filter(Objects::nonNull).filter(i -> i.getClassName().equals(GOLD_CURRENCY_ITEM_CLASS_NAME)).forEach(i -> {
player.getInventory().remove(i.getItemStack());
plugin.getGameObjectRegistry().removeFromDatabase(item);
}));
player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_HARP, 1.0F, 1.3F);
return;
}
item.setQuantity(pickup.getAmount());
user.giveItem(item, true, false, false);
player.playSound(player.getLocation(), Sound.ENTITY_ITEM_PICKUP, 1.0F, 1.0F);
event.setCancelled(true);
event.getItem().remove();
}
use of mc.dragons.core.gameobject.item.Item in project DragonsOnline by UniverseCraft.
the class PlayerEventListeners method onDropItem.
/**
* FIXME Drop quantity sometimes calculated incorrectly when iterated
*
* @param event
*/
@EventHandler
public void onDropItem(PlayerDropItemEvent event) {
ItemStack drop = event.getItemDrop().getItemStack();
int amt = drop.getAmount();
Item item = ItemLoader.fromBukkit(drop);
LOGGER.trace("Drop item event on " + event.getPlayer().getName() + " of " + (item == null ? "null" : item.getIdentifier()) + " (x" + amt + ")");
if (item == null) {
return;
}
User user = UserLoader.fromPlayer(event.getPlayer());
user.debug("held=" + item.getQuantity() + "=" + item.getItemStack().getAmount());
if (item.isUndroppable() && event.getPlayer().getGameMode() != GameMode.CREATIVE) {
user.sendActionBar(ChatColor.DARK_RED + "You can't drop this item!");
event.setCancelled(true);
return;
}
Item dropItem = itemLoader.registerNew(item);
dropItem.setQuantity(amt);
user.debug("drop=" + dropItem.getQuantity() + "=" + dropItem.getItemStack().getAmount());
event.getItemDrop().setItemStack(dropItem.getItemStack());
user.takeItem(item, amt, true, true, true);
}
Aggregations