3
3
import java .util .HashMap ;
4
4
5
5
import org .bukkit .Location ;
6
+ import org .bukkit .configuration .file .YamlConfiguration ;
7
+ import org .bukkit .entity .Entity ;
6
8
import org .bukkit .entity .EntityType ;
7
9
import org .bukkit .entity .Villager ;
8
10
import org .bukkit .metadata .FixedMetadataValue ;
@@ -19,13 +21,25 @@ public VillagerUtils(Main plugin) {
19
21
this .instance = plugin ;
20
22
}
21
23
22
- public void disableVillagers () {
23
-
24
- //Remove VILLAGERS
25
- for (Villager vil : this .shop_villagers .values ()) {
26
- this .instance .getThreadHandler ().runTaskSync (vil , (t ) -> {
27
- vil .remove ();
28
- });
24
+ public void loadVillagers () {
25
+ YamlConfiguration config = this .instance .getNPCFileManager ().getConfig ();
26
+ for (String shopIdS : config .getConfigurationSection ("" ).getKeys (false )) {
27
+ int shopId = Integer .parseInt (shopIdS );
28
+
29
+ Location loc = this .instance .getNPCFileManager ().getNPCLocForShop (shopId );
30
+ this .instance .getThreadHandler ().runTaskLaterSync (loc , (t ) -> {
31
+ for (Entity entities : loc .getNearbyEntities (1 , 1 , 1 )) {
32
+ if (entities instanceof Villager ) {
33
+ Villager v = (Villager ) entities ;
34
+ //Replace Meta since it gets removed
35
+ v .setMetadata ("shopid" , new FixedMetadataValue (this .instance , String .valueOf (shopId ))); // PUTTING THE SHOP AS ENTITY META
36
+
37
+ this .shop_villagers .put (shopId , v );
38
+ break ;
39
+ }
40
+ }
41
+ }, 30 );
42
+
29
43
}
30
44
}
31
45
@@ -50,33 +64,30 @@ public void moveVillager(int shopId, Location loc) {
50
64
vil .teleport (loc );
51
65
}
52
66
53
- public void spawnVillager (int shopId , String prefix , String playerName ) {
67
+ public void createVillager (int shopId , String prefix , String playerName ) {
54
68
55
69
Location loc = this .instance .getNPCFileManager ().getNPCLocForShop (shopId );
56
70
if (loc == null )
57
71
return ;
58
-
59
- this .instance .getThreadHandler ().runTaskSync (loc , (t ) -> {
60
-
61
- Villager v = (Villager ) loc .getWorld ().spawnEntity (loc , EntityType .VILLAGER ); //SHOP VILLAGER
62
- this .shop_villagers .put (shopId , v );
72
+
73
+ Villager v = (Villager ) loc .getWorld ().spawnEntity (loc , EntityType .VILLAGER ); //SHOP VILLAGER
74
+ this .shop_villagers .put (shopId , v );
63
75
64
- v .setAI (false ); //CANT MOVE
65
- v .setAware (false ); //CANT GET PUSHED
66
- v .setCollidable (false ); // "
67
- v .setGravity (false ); //CANT FALL
68
- v .setSilent (true ); // NO SOUNDS
76
+ v .setAI (false ); //CANT MOVE
77
+ v .setAware (false ); //CANT GET PUSHED
78
+ v .setCollidable (false ); // "
79
+ v .setGravity (false ); //CANT FALL
80
+ v .setSilent (true ); // NO SOUNDS
69
81
70
- String displayname = prefix + playerName ;
71
- String customName = this .instance .translateHexColorCodes (this .instance .manageFile ().getString ("Options.userShopName" )
72
- .replaceAll ("(?i)%" + "displayname" + "%" , displayname != null ? displayname : "" )
73
- .replaceAll ("(?i)%" + "username" + "%" , playerName != null ? playerName : "" )
74
- );
82
+ String displayname = prefix + playerName ;
83
+ String customName = this .instance .translateHexColorCodes (this .instance .manageFile ().getString ("Options.userShopName" )
84
+ .replaceAll ("(?i)%" + "displayname" + "%" , displayname != null ? displayname : "" )
85
+ .replaceAll ("(?i)%" + "username" + "%" , playerName != null ? playerName : "" )
86
+ );
75
87
76
- v .setCustomName (customName );
77
- v .setCustomNameVisible (true );
88
+ v .setCustomName (customName );
89
+ v .setCustomNameVisible (true );
78
90
79
- v .setMetadata ("shopid" , new FixedMetadataValue (this .instance , String .valueOf (shopId ))); // PUTTING THE SHOP AS ENTITY META
80
- });
91
+ v .setMetadata ("shopid" , new FixedMetadataValue (this .instance , String .valueOf (shopId ))); // PUTTING THE SHOP AS ENTITY META
81
92
}
82
93
}
0 commit comments