use of com.google.common.base.Optional in project MyPet by xXKeyleXx.
the class EntityListener method on.
@EventHandler(priority = EventPriority.MONITOR)
public void on(final EntityDamageByEntityEvent event) {
if (!event.getEntity().isDead() && !(event.getEntity() instanceof MyPetBukkitEntity)) {
if (MyPetApi.getMyPetInfo().isLeashableEntityType(event.getEntity().getType())) {
ItemStack leashItem = null;
ItemStack leashItemArrow = null;
Player player;
if (Configuration.Misc.ALLOW_RANGED_LEASHING && event.getDamager() instanceof Projectile) {
Projectile projectile = (Projectile) event.getDamager();
if (!(projectile.getShooter() instanceof Player)) {
return;
}
player = (Player) projectile.getShooter();
List<MetadataValue> metaList;
if (projectile.hasMetadata("MyPetLeashItem")) {
metaList = projectile.getMetadata("MyPetLeashItem");
for (MetadataValue meta : metaList) {
if (meta.getOwningPlugin() == MyPetApi.getPlugin()) {
leashItem = (ItemStack) meta.value();
break;
}
}
if (leashItem == null) {
return;
}
}
if (projectile.hasMetadata("")) {
metaList = projectile.getMetadata("MyPetLeashItemArrow");
for (MetadataValue meta : metaList) {
if (meta.getOwningPlugin() == MyPetApi.getPlugin()) {
leashItemArrow = (ItemStack) meta.value();
break;
}
}
if (leashItemArrow == null) {
return;
}
}
} else if (event.getDamager() instanceof Player) {
player = (Player) event.getDamager();
if (MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.9") >= 0) {
leashItem = player.getEquipment().getItemInMainHand();
} else {
leashItem = player.getItemInHand();
}
} else {
return;
}
if (!MyPetApi.getMyPetManager().hasActiveMyPet(player)) {
LivingEntity leashTarget = (LivingEntity) event.getEntity();
MyPetType petType = MyPetType.byEntityTypeName(leashTarget.getType().name());
ConfigItem neededLeashItem = MyPetApi.getMyPetInfo().getLeashItem(petType);
if (!Permissions.hasLegacy(player, "MyPet.leash.", petType.name())) {
return;
}
boolean usedArrow = false;
if (!neededLeashItem.compare(leashItem)) {
if (leashItemArrow == null || !neededLeashItem.compare(leashItemArrow)) {
return;
} else {
usedArrow = true;
}
}
for (PlayerLeashEntityHook hook : MyPetApi.getPluginHookManager().getHooks(PlayerLeashEntityHook.class)) {
if (!hook.canLeash(player, leashTarget)) {
return;
}
}
boolean willBeLeashed = true;
flagLoop: for (LeashFlag flag : MyPetApi.getMyPetInfo().getLeashFlags(petType)) {
switch(flag) {
case Adult:
if (leashTarget instanceof Ageable) {
willBeLeashed = ((Ageable) leashTarget).isAdult();
} else if (leashTarget instanceof Zombie) {
willBeLeashed = !((Zombie) leashTarget).isBaby();
}
break;
case Baby:
if (leashTarget instanceof Ageable) {
willBeLeashed = !((Ageable) leashTarget).isAdult();
} else if (leashTarget instanceof Zombie) {
willBeLeashed = ((Zombie) leashTarget).isBaby();
}
break;
case LowHp:
willBeLeashed = ((leashTarget.getHealth() - event.getDamage()) * 100) / leashTarget.getMaxHealth() <= 10;
break;
case UserCreated:
if (leashTarget instanceof IronGolem) {
willBeLeashed = ((IronGolem) leashTarget).isPlayerCreated();
}
break;
case Wild:
if (leashTarget instanceof IronGolem) {
willBeLeashed = !((IronGolem) leashTarget).isPlayerCreated();
} else if (leashTarget instanceof Tameable) {
willBeLeashed = !((Tameable) leashTarget).isTamed();
} else if (leashTarget instanceof Horse) {
willBeLeashed = !((Horse) leashTarget).isTamed();
}
break;
case Tamed:
if (leashTarget instanceof Tameable) {
willBeLeashed = ((Tameable) leashTarget).isTamed() && ((Tameable) leashTarget).getOwner() == player;
} else if (leashTarget instanceof Horse) {
willBeLeashed = ((Horse) leashTarget).isTamed() && ((Horse) leashTarget).getOwner() == player;
}
break;
case CanBreed:
if (leashTarget instanceof Ageable) {
willBeLeashed = ((Ageable) leashTarget).canBreed();
}
break;
case Angry:
if (leashTarget instanceof Wolf) {
willBeLeashed = ((Wolf) leashTarget).isAngry();
}
break;
case Impossible:
willBeLeashed = false;
break flagLoop;
case None:
willBeLeashed = true;
break flagLoop;
}
if (!willBeLeashed) {
break;
}
}
if (willBeLeashed) {
event.setCancelled(true);
final MyPetPlayer owner;
if (MyPetApi.getPlayerManager().isMyPetPlayer(player)) {
owner = MyPetApi.getPlayerManager().getMyPetPlayer(player);
} else {
owner = MyPetApi.getPlayerManager().registerMyPetPlayer(player);
}
final InactiveMyPet inactiveMyPet = new InactiveMyPet(owner);
inactiveMyPet.setPetType(petType);
inactiveMyPet.setPetName(Translation.getString("Name." + petType.name(), inactiveMyPet.getOwner().getLanguage()));
WorldGroup worldGroup = WorldGroup.getGroupByWorld(player.getWorld().getName());
inactiveMyPet.setWorldGroup(worldGroup.getName());
inactiveMyPet.getOwner().setMyPetForWorldGroup(worldGroup, inactiveMyPet.getUUID());
/*
if(leashTarget.getCustomName() != null)
{
inactiveMyPet.setPetName(leashTarget.getCustomName());
}
*/
Optional<EntityConverterService> converter = MyPetApi.getServiceManager().getService(EntityConverterService.class);
if (converter.isPresent()) {
inactiveMyPet.setInfo(converter.get().convertEntity(leashTarget));
}
leashTarget.remove();
if (!usedArrow) {
if (Configuration.Misc.CONSUME_LEASH_ITEM && player.getGameMode() != GameMode.CREATIVE && leashItem != null) {
if (leashItem.getAmount() > 1) {
leashItem.setAmount(leashItem.getAmount() - 1);
} else {
if (MyPetApi.getCompatUtil().compareWithMinecraftVersion("1.9") >= 0) {
player.getEquipment().setItemInMainHand(null);
} else {
player.setItemInHand(null);
}
}
}
}
MyPetCreateEvent createEvent = new MyPetCreateEvent(inactiveMyPet, MyPetCreateEvent.Source.Leash);
Bukkit.getServer().getPluginManager().callEvent(createEvent);
MyPetSaveEvent saveEvent = new MyPetSaveEvent(inactiveMyPet);
Bukkit.getServer().getPluginManager().callEvent(saveEvent);
MyPetApi.getPlugin().getRepository().addMyPet(inactiveMyPet, new RepositoryCallback<Boolean>() {
@Override
public void callback(Boolean value) {
owner.sendMessage(Translation.getString("Message.Leash.Add", owner));
Optional<MyPet> myPet = MyPetApi.getMyPetManager().activateMyPet(inactiveMyPet);
if (myPet.isPresent()) {
myPet.get().createEntity();
}
}
});
}
}
}
}
}
use of com.google.common.base.Optional in project MyPet by xXKeyleXx.
the class CommandOptionClone method onCommandOption.
@Override
public boolean onCommandOption(CommandSender sender, String[] args) {
if (args.length < 2) {
return false;
}
String lang = MyPetApi.getPlatformHelper().getCommandSenderLanguage(sender);
Player oldOwner = Bukkit.getPlayer(args[0]);
if (oldOwner == null || !oldOwner.isOnline()) {
sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Translation.getString("Message.No.PlayerOnline", lang));
return true;
}
final Player newOwner = Bukkit.getPlayer(args[1]);
if (newOwner == null || !newOwner.isOnline()) {
sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Translation.getString("Message.No.PlayerOnline", lang));
return true;
}
if (!MyPetApi.getPlayerManager().isMyPetPlayer(oldOwner)) {
sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Util.formatText(Translation.getString("Message.No.UserHavePet", lang), oldOwner.getName()));
return true;
}
MyPetPlayer oldPetOwner = MyPetApi.getPlayerManager().getMyPetPlayer(oldOwner);
if (!oldPetOwner.hasMyPet()) {
sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Util.formatText(Translation.getString("Message.No.UserHavePet", lang), oldOwner.getName()));
return true;
}
final MyPetPlayer newPetOwner;
if (MyPetApi.getPlayerManager().isMyPetPlayer(newOwner)) {
newPetOwner = MyPetApi.getPlayerManager().getMyPetPlayer(newOwner);
} else {
newPetOwner = MyPetApi.getPlayerManager().registerMyPetPlayer(newOwner);
}
if (newPetOwner.hasMyPet()) {
sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + newOwner.getName() + " has already an active MyPet!");
return true;
}
MyPet oldPet = oldPetOwner.getMyPet();
final InactiveMyPet newPet = new InactiveMyPet(newPetOwner);
newPet.setPetName(oldPet.getPetName());
newPet.setExp(oldPet.getExperience().getExp());
newPet.setHealth(oldPet.getHealth());
newPet.setHungerValue(oldPet.getHungerValue());
newPet.setRespawnTime(oldPet.getRespawnTime());
newPet.setInfo(oldPet.getInfo());
newPet.setPetType(oldPet.getPetType());
newPet.setSkilltree(oldPet.getSkilltree());
newPet.setWorldGroup(oldPet.getWorldGroup());
newPet.setSkills(oldPet.getSkillInfo());
MyPetSaveEvent event = new MyPetSaveEvent(newPet);
Bukkit.getServer().getPluginManager().callEvent(event);
MyPetApi.getRepository().addMyPet(newPet, new RepositoryCallback<Boolean>() {
@Override
public void callback(Boolean value) {
Optional<MyPet> myPet = MyPetApi.getMyPetManager().activateMyPet(newPet);
if (myPet.isPresent()) {
WorldGroup worldGroup = WorldGroup.getGroupByWorld(newPet.getOwner().getPlayer().getWorld().getName());
newPet.setWorldGroup(worldGroup.getName());
newPet.getOwner().setMyPetForWorldGroup(worldGroup, newPet.getUUID());
MyPetApi.getRepository().updateMyPetPlayer(newPetOwner, null);
newOwner.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] MyPet owned by " + newPetOwner.getName() + " successfully cloned!");
}
}
});
return true;
}
use of com.google.common.base.Optional in project MyPet by xXKeyleXx.
the class CommandOptionCreate method onCommandOption.
@Override
public boolean onCommandOption(final CommandSender sender, String[] args) {
if (args.length < 2) {
return false;
}
String lang = MyPetApi.getPlatformHelper().getCommandSenderLanguage(sender);
int forceOffset = 0;
if (args[0].equalsIgnoreCase("-f")) {
forceOffset = 1;
if (args.length < 3) {
return false;
}
}
try {
MyPetType myPetType = MyPetType.byName(args[1 + forceOffset]);
if (MyPetApi.getMyPetInfo().isLeashableEntityType(EntityType.valueOf(myPetType.getBukkitName()))) {
Player owner = Bukkit.getPlayer(args[forceOffset]);
if (owner == null || !owner.isOnline()) {
sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] " + Translation.getString("Message.No.PlayerOnline", lang));
return true;
}
final MyPetPlayer newOwner;
if (MyPetApi.getPlayerManager().isMyPetPlayer(owner)) {
newOwner = MyPetApi.getPlayerManager().getMyPetPlayer(owner);
if (newOwner.hasMyPet() && forceOffset == 1) {
MyPetApi.getMyPetManager().deactivateMyPet(newOwner, true);
}
} else {
newOwner = MyPetApi.getPlayerManager().registerMyPetPlayer(owner);
}
final InactiveMyPet inactiveMyPet = new InactiveMyPet(newOwner);
inactiveMyPet.setPetType(myPetType);
inactiveMyPet.setPetName(Translation.getString("Name." + inactiveMyPet.getPetType().name(), inactiveMyPet.getOwner().getLanguage()));
TagCompound compound = inactiveMyPet.getInfo();
createInfo(myPetType, Arrays.copyOfRange(args, 2 + forceOffset, args.length), compound);
final WorldGroup wg = WorldGroup.getGroupByWorld(owner.getWorld().getName());
inactiveMyPet.setWorldGroup(wg.getName());
Optional<RepositoryMyPetConverterService> converter = MyPetApi.getServiceManager().getService(RepositoryMyPetConverterService.class);
if (converter.isPresent()) {
converter.get().convert(inactiveMyPet);
}
MyPetCreateEvent createEvent = new MyPetCreateEvent(inactiveMyPet, MyPetCreateEvent.Source.AdminCommand);
Bukkit.getServer().getPluginManager().callEvent(createEvent);
MyPetSaveEvent saveEvent = new MyPetSaveEvent(inactiveMyPet);
Bukkit.getServer().getPluginManager().callEvent(saveEvent);
MyPetApi.getRepository().addMyPet(inactiveMyPet, new RepositoryCallback<Boolean>() {
@Override
public void callback(Boolean added) {
if (added) {
if (!newOwner.hasMyPet()) {
inactiveMyPet.getOwner().setMyPetForWorldGroup(wg, inactiveMyPet.getUUID());
MyPetApi.getRepository().updateMyPetPlayer(inactiveMyPet.getOwner(), null);
Optional<MyPet> myPet = MyPetApi.getMyPetManager().activateMyPet(inactiveMyPet);
if (myPet.isPresent()) {
myPet.get().createEntity();
sender.sendMessage(Translation.getString("Message.Command.Success", sender));
} else {
sender.sendMessage("[" + ChatColor.AQUA + "MyPet" + ChatColor.RESET + "] Can't create MyPet for " + newOwner.getName() + ". Is this player online?");
}
} else {
sender.sendMessage(Translation.getString("Message.Command.Success", sender));
}
}
}
});
}
} catch (MyPetTypeNotFoundException e) {
sender.sendMessage(Translation.getString("Message.Command.PetType.Unknown", lang));
}
return true;
}
use of com.google.common.base.Optional in project GeoGig by boundlessgeo.
the class ApplyPatchOp method applyPatch.
private void applyPatch(Patch patch) {
final WorkingTree workTree = workingTree();
final StagingDatabase indexDb = stagingDatabase();
if (reverse) {
patch = patch.reversed();
}
List<FeatureInfo> removed = patch.getRemovedFeatures();
for (FeatureInfo feature : removed) {
workTree.delete(NodeRef.parentPath(feature.getPath()), NodeRef.nodeFromPath(feature.getPath()));
}
List<FeatureInfo> added = patch.getAddedFeatures();
for (FeatureInfo feature : added) {
workTree.insert(NodeRef.parentPath(feature.getPath()), feature.getFeature());
}
List<FeatureDiff> diffs = patch.getModifiedFeatures();
for (FeatureDiff diff : diffs) {
String path = diff.getPath();
DepthSearch depthSearch = new DepthSearch(indexDb);
Optional<NodeRef> noderef = depthSearch.find(workTree.getTree(), path);
RevFeatureType oldRevFeatureType = command(RevObjectParse.class).setObjectId(noderef.get().getMetadataId()).call(RevFeatureType.class).get();
String refSpec = Ref.WORK_HEAD + ":" + path;
RevFeature feature = command(RevObjectParse.class).setRefSpec(refSpec).call(RevFeature.class).get();
RevFeatureType newRevFeatureType = getFeatureType(diff, feature, oldRevFeatureType);
ImmutableList<Optional<Object>> values = feature.getValues();
ImmutableList<PropertyDescriptor> oldDescriptors = oldRevFeatureType.sortedDescriptors();
ImmutableList<PropertyDescriptor> newDescriptors = newRevFeatureType.sortedDescriptors();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) newRevFeatureType.type());
Map<Name, Optional<?>> attrs = Maps.newHashMap();
for (int i = 0; i < oldDescriptors.size(); i++) {
PropertyDescriptor descriptor = oldDescriptors.get(i);
if (newDescriptors.contains(descriptor)) {
Optional<Object> value = values.get(i);
attrs.put(descriptor.getName(), value);
}
}
Set<Entry<PropertyDescriptor, AttributeDiff>> featureDiffs = diff.getDiffs().entrySet();
for (Iterator<Entry<PropertyDescriptor, AttributeDiff>> iterator = featureDiffs.iterator(); iterator.hasNext(); ) {
Entry<PropertyDescriptor, AttributeDiff> entry = iterator.next();
if (!entry.getValue().getType().equals(TYPE.REMOVED)) {
Optional<?> oldValue = attrs.get(entry.getKey().getName());
attrs.put(entry.getKey().getName(), entry.getValue().applyOn(oldValue));
}
}
Set<Entry<Name, Optional<?>>> entries = attrs.entrySet();
for (Iterator<Entry<Name, Optional<?>>> iterator = entries.iterator(); iterator.hasNext(); ) {
Entry<Name, Optional<?>> entry = iterator.next();
featureBuilder.set(entry.getKey(), entry.getValue().orNull());
}
SimpleFeature featureToInsert = featureBuilder.buildFeature(NodeRef.nodeFromPath(path));
workTree.insert(NodeRef.parentPath(path), featureToInsert);
}
ImmutableList<FeatureTypeDiff> alteredTrees = patch.getAlteredTrees();
for (FeatureTypeDiff diff : alteredTrees) {
Optional<RevFeatureType> featureType;
if (diff.getOldFeatureType().isNull()) {
featureType = patch.getFeatureTypeFromId(diff.getNewFeatureType());
workTree.createTypeTree(diff.getPath(), featureType.get().type());
} else if (diff.getNewFeatureType().isNull()) {
workTree.delete(diff.getPath());
} else {
featureType = patch.getFeatureTypeFromId(diff.getNewFeatureType());
workTree.updateTypeTree(diff.getPath(), featureType.get().type());
}
}
}
use of com.google.common.base.Optional in project GeoGig by boundlessgeo.
the class CommitOpTest method testAmend.
@Test
public void testAmend() throws Exception {
final ObjectId id = insertAndAdd(points1);
final RevCommit commit1 = geogig.command(CommitOp.class).setMessage("Message").call();
{
assertCommit(commit1, null, null, null);
assertEquals(id, repo.getRootTreeChild(appendChild(pointsName, idP1)).get().getObjectId());
assertNotNull(repo.objectDatabase().get(id));
}
final ObjectId id2 = insertAndAdd(points2);
final RevCommit commit2 = geogig.command(CommitOp.class).setAmend(true).call();
{
assertCommit(commit2, null, "groldan", "Message");
Optional<RevFeature> p2 = geogig.command(RevObjectParse.class).setRefSpec("HEAD:" + appendChild(pointsName, idP2)).call(RevFeature.class);
assertTrue(p2.isPresent());
assertEquals(id2, p2.get().getId());
Optional<RevFeature> p1 = geogig.command(RevObjectParse.class).setRefSpec("HEAD:" + appendChild(pointsName, idP1)).call(RevFeature.class);
assertTrue(p1.isPresent());
assertEquals(id, p1.get().getId());
}
Iterator<RevCommit> log = geogig.command(LogOp.class).call();
assertTrue(log.hasNext());
log.next();
assertFalse(log.hasNext());
}
Aggregations