Merge branch 'master' of github.com:Akarin-project/Akarin

This commit is contained in:
Sotr
2019-04-22 13:46:31 +08:00
4 changed files with 78 additions and 2 deletions

View File

@@ -3,7 +3,11 @@ package org.bukkit.entity;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.server.Village;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.Merchant;
@@ -15,6 +19,21 @@ import org.jetbrains.annotations.Nullable;
*/
public interface Villager extends Ageable, NPC, InventoryHolder, Merchant {
/**
* Get the nearest village of this villager in range .
*
* @return The nearest village. null if there is no village in range.
*/
public Village getNearestVillage(@NotNull double xRadius,@NotNull double yRadius,@NotNull double zRadius);
/**
* Get villages which are near by this villager in range.
*
* @return All the villages in range. Empty List if there is no village in range.
*/
public List<Village> getVillagesInRange(@NotNull double xRadius, @NotNull double yRadius, @NotNull double zRadius);
/**
* Gets the current profession of this villager.
*

View File

@@ -136,6 +136,11 @@
<artifactId>trove</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>16.0.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

View File

@@ -1,12 +1,15 @@
package org.bukkit.craftbukkit.entity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import net.minecraft.server.EntityVillager;
import net.minecraft.server.*;
import org.apache.commons.lang.Validate;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.inventory.CraftInventory;
import org.bukkit.craftbukkit.inventory.CraftMerchant;
import org.bukkit.entity.EntityType;
@@ -15,6 +18,7 @@ import org.bukkit.entity.Villager;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.MerchantRecipe;
import org.jetbrains.annotations.NotNull;
public class CraftVillager extends CraftAgeable implements Villager, InventoryHolder {
@@ -143,6 +147,54 @@ public class CraftVillager extends CraftAgeable implements Villager, InventoryHo
return null;
}
@Override
public Village getNearestVillage(@NotNull double xRadius, @NotNull double yRadius, @NotNull double zRadius) {
WorldServer nmsWorld = ((CraftWorld) this.getWorld()).getHandle();
PersistentVillage allVillage = nmsWorld.af();
List<Village> villageList = allVillage.getVillages();
Village nearestVillage = null;
double nearestRange = Double.NaN;
for (Village x : villageList) {
BlockPosition p = x.a();
double xRange = Math.abs(p.getX()-this.getLocation().getX());
double yRange = Math.abs(p.getY()-this.getLocation().getY());
double zRange = Math.abs(p.getZ()-this.getLocation().getZ());
if(xRange>xRadius||yRange>yRadius||zRange>zRadius){
continue;
}else {
if(nearestVillage==null){
nearestVillage = x;
nearestRange = Math.sqrt(Math.pow(xRange,2)+Math.pow(yRange,2)+Math.pow(zRange,2));
}else{
double range = Math.sqrt(Math.pow(xRange,2)+Math.pow(yRange,2)+Math.pow(zRange,2));
if(range<nearestRange) {
nearestVillage = x;
nearestRange = range;
}
}
}
}
return nearestVillage;
}
@Override
public List<Village> getVillagesInRange(@NotNull double xRadius, @NotNull double yRadius, @NotNull double zRadius) {
WorldServer nmsWorld = ((CraftWorld) this.getWorld()).getHandle();
PersistentVillage allVillage = nmsWorld.af();
List<Village> villageList = allVillage.getVillages();
List<Village> villagesInRange = new ArrayList<>();
for(Village x:villageList){
BlockPosition p = x.a();
double xRange = Math.abs(p.getX()-this.getLocation().getX());
double yRange = Math.abs(p.getY()-this.getLocation().getY());
double zRange = Math.abs(p.getZ()-this.getLocation().getZ());
if(xRange<xRadius&&yRange<yRadius&&zRange<zRadius){
villagesInRange.add(x);
}
}
return villagesInRange;
}
private static int getCareerID(Career career) {
return careerIDMap.getOrDefault(career, 0);
}