Skip to content

Commit c1256dd

Browse files
author
TrueMB
committed
Fix: double Shop/Hotel loading and Villager NPCs
1 parent 89c8e6e commit c1256dd

File tree

8 files changed

+115
-106
lines changed

8 files changed

+115
-106
lines changed

src/main/java/me/truemb/rentit/commands/ShopCOMMAND.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public boolean execute(CommandSender sender, String label, String[] args) {
188188
//SHOP IS OWNED
189189
if(rentHandler.isAdmin() || ownerUUID != null) {
190190
if(this.instance.getVillagerUtils() != null) {
191-
this.instance.getVillagerUtils().spawnVillager(shopId, prefix, ownerName);
191+
this.instance.getVillagerUtils().createVillager(shopId, prefix, ownerName);
192192
}else {
193193
this.instance.getNpcUtils().spawnAndEditNPC(shopId, prefix, ownerName);
194194
}
@@ -203,7 +203,7 @@ public boolean execute(CommandSender sender, String label, String[] args) {
203203

204204
//SHOP IS OWNED
205205
if(rentHandler.isAdmin() || ownerUUID != null) {
206-
this.instance.getVillagerUtils().spawnVillager(shopId, prefix, ownerName);
206+
this.instance.getVillagerUtils().createVillager(shopId, prefix, ownerName);
207207
}
208208
}
209209
}
@@ -1207,7 +1207,7 @@ public void accept(ResultSet rs) {
12071207
if(this.instance.manageFile().getBoolean("Options.useNPCs")) {
12081208
this.instance.getNpcUtils().spawnAndEditNPC(shopId, prefix, owner);
12091209
}else {
1210-
this.instance.getVillagerUtils().spawnVillager(shopId, prefix, owner);
1210+
this.instance.getVillagerUtils().createVillager(shopId, prefix, owner);
12111211
}
12121212
}
12131213

src/main/java/me/truemb/rentit/database/CategoriesSQL.java

+36-38
Original file line numberDiff line numberDiff line change
@@ -119,51 +119,49 @@ public void delete(RentTypes type, int catId){
119119
sql.queryUpdate("DELETE FROM " + (type.equals(RentTypes.SHOP) ? sql.t_shop_categories : sql.t_hotel_categories) + " WHERE catID=?;", String.valueOf(catId));
120120
}
121121

122-
public void setupCategories(Consumer<Boolean> c) {
122+
public void setupCategory(RentTypes type, Consumer<Boolean> c) {
123123
AsyncSQL sql = this.instance.getAsyncSQL();
124124

125-
for(RentTypes type : RentTypes.values()) {
126-
sql.prepareStatement("SELECT * FROM " + (type.equals(RentTypes.SHOP) ? sql.t_shop_categories : sql.t_hotel_categories) + ";", (rs) -> {
127-
try {
128-
int catAmount = 0;
129-
130-
while (rs.next()) {
131-
catAmount++;
125+
sql.prepareStatement("SELECT * FROM " + (type.equals(RentTypes.SHOP) ? sql.t_shop_categories : sql.t_hotel_categories) + ";", (rs) -> {
126+
try {
127+
int catAmount = 0;
128+
129+
while (rs.next()) {
130+
catAmount++;
132131

133-
int catID = rs.getInt("catID");
134-
String alias = rs.getString("alias") != null && !rs.getString("alias").equalsIgnoreCase("null") ? rs.getString("alias") : null;
135-
double costs = rs.getDouble("costs");
136-
String time = rs.getString("time");
132+
int catID = rs.getInt("catID");
133+
String alias = rs.getString("alias") != null && !rs.getString("alias").equalsIgnoreCase("null") ? rs.getString("alias") : null;
134+
double costs = rs.getDouble("costs");
135+
String time = rs.getString("time");
137136

138-
CategoryHandler handler = new CategoryHandler(catID, costs, time);
139-
handler.setAlias(alias);
140-
141-
if(type.equals(RentTypes.SHOP)) {
142-
int size = rs.getInt("size");
143-
handler.setSize(size);
144-
145-
int maxSite = rs.getInt("maxSite");
146-
if(maxSite <= 0)
147-
maxSite = 1;
148-
handler.setMaxSite(maxSite);
149-
}
137+
CategoryHandler handler = new CategoryHandler(catID, costs, time);
138+
handler.setAlias(alias);
150139

151-
HashMap<Integer, CategoryHandler> hash = new HashMap<>();
152-
if(this.instance.catHandlers.containsKey(type))
153-
hash = this.instance.catHandlers.get(type);
140+
if(type.equals(RentTypes.SHOP)) {
141+
int size = rs.getInt("size");
142+
handler.setSize(size);
154143

155-
hash.put(catID, handler);
156-
this.instance.catHandlers.put(type, hash);
144+
int maxSite = rs.getInt("maxSite");
145+
if(maxSite <= 0)
146+
maxSite = 1;
147+
handler.setMaxSite(maxSite);
157148
}
158149

159-
this.instance.getLogger().info(String.valueOf(catAmount) + " " + type.toString() + "-Categories are loaded.");
160-
c.accept(true);
161-
} catch (SQLException e) {
162-
e.printStackTrace();
163-
c.accept(false);
164-
return;
165-
}
166-
});
167-
}
150+
HashMap<Integer, CategoryHandler> hash = new HashMap<>();
151+
if(this.instance.catHandlers.containsKey(type))
152+
hash = this.instance.catHandlers.get(type);
153+
154+
hash.put(catID, handler);
155+
this.instance.catHandlers.put(type, hash);
156+
}
157+
158+
this.instance.getLogger().info(String.valueOf(catAmount) + " " + type.toString() + "-Categories are loaded.");
159+
c.accept(true);
160+
} catch (SQLException e) {
161+
e.printStackTrace();
162+
c.accept(false);
163+
return;
164+
}
165+
});
168166
}
169167
}

src/main/java/me/truemb/rentit/database/ShopInventorySQL.java

+25-24
Original file line numberDiff line numberDiff line change
@@ -148,35 +148,36 @@ public void accept(ResultSet rs) {
148148
ItemStack[] buyContents = buyInvS != null && !buyInvS.equalsIgnoreCase("null") ? InventoryUtils.itemStackArrayFromBase64(buyInvS) : null;
149149

150150
//Add Items one site before, if still space
151-
if(catHandler != null) {
152-
//Sell Inventory
153-
if(sellInv != null && sellContents != null) {
154-
ItemStack[] sellContentsClone = sellContents.clone();
155-
skipSellInv = true; //Items getting checked, if there is still space in the inventory before
151+
if(catHandler == null)
152+
continue;
153+
154+
//Sell Inventory
155+
if(sellInv != null && sellContents != null) {
156+
ItemStack[] sellContentsClone = sellContents.clone();
157+
skipSellInv = true; //Items getting checked, if there is still space in the inventory before
156158

157-
outer: for(int i = 0; i < sellContentsClone.length; i++) {
158-
ItemStack item = sellContentsClone[i];
159-
if(item != null && item.getType() != Material.AIR && !item.getItemMeta().getPersistentDataContainer().has(instance.guiItem, PersistentDataType.STRING)) {
159+
outer: for(int i = 0; i < sellContentsClone.length; i++) {
160+
ItemStack item = sellContentsClone[i];
161+
if(item != null && item.getType() != Material.AIR && !item.getItemMeta().getPersistentDataContainer().has(instance.guiItem, PersistentDataType.STRING)) {
160162

161-
boolean foundFreeSlot = false;
162-
for(int slot = 0; slot < (catHandler.getMaxSite() > 1 ? sellInv.getSize() - 9 : sellInv.getSize()); slot++) {
163-
ItemStack temp = sellInv.getItem(slot);
163+
boolean foundFreeSlot = false;
164+
for(int slot = 0; slot < (catHandler.getMaxSite() > 1 ? sellInv.getSize() - 9 : sellInv.getSize()); slot++) {
165+
ItemStack temp = sellInv.getItem(slot);
164166

165-
if(temp == null || temp.getType() == Material.AIR) {
166-
sellInv.setItem(slot, item);
167-
sellContents[i] = null; //Removes the Items from the next Site
168-
foundFreeSlot = true;
169-
break; //Move to next Item
170-
}
167+
if(temp == null || temp.getType() == Material.AIR) {
168+
sellInv.setItem(slot, item);
169+
sellContents[i] = null; //Removes the Items from the next Site
170+
foundFreeSlot = true;
171+
break; //Move to next Item
171172
}
173+
}
172174

173-
if(!foundFreeSlot) {
174-
skipSellInv = false;
175-
break outer;
176-
}
177-
}else
178-
sellContents[i] = null; //Could be guiItem -> remove
179-
}
175+
if(!foundFreeSlot) {
176+
skipSellInv = false;
177+
break outer;
178+
}
179+
}else
180+
sellContents[i] = null; //Could be guiItem -> remove
180181
}
181182

182183
//Buy Inventory

src/main/java/me/truemb/rentit/database/ShopsSQL.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public void setupShops() {
217217

218218
if(!this.instance.manageFile().getBoolean("Options.disableNPC")) {
219219
if(this.instance.getVillagerUtils() != null) {
220-
this.instance.getVillagerUtils().spawnVillager(id, prefix, handler.isAdmin() ? this.instance.translateHexColorCodes(this.instance.manageFile().getString("Options.adminShopName")) : ownerName);
220+
//this.instance.getVillagerUtils().spawnVillager(id, prefix, handler.isAdmin() ? this.instance.translateHexColorCodes(this.instance.manageFile().getString("Options.adminShopName")) : ownerName);
221221
}else {
222222
this.instance.getNpcUtils().spawnAndEditNPC(id, prefix, handler.isAdmin() ? this.instance.translateHexColorCodes(this.instance.manageFile().getString("Options.adminShopName")) : ownerName);
223223
}

src/main/java/me/truemb/rentit/filemanager/NPCFileManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public NPCFileManager(Main plugin) {
3636
}
3737
}
3838

39-
private YamlConfiguration getConfig() {
39+
public YamlConfiguration getConfig() {
4040
return YamlConfiguration.loadConfiguration(this.file);
4141
}
4242

src/main/java/me/truemb/rentit/listener/GUI_ConfirmationListener.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public void onConfirmClick(InventoryClickEvent e) {
152152
if(this.instance.getNpcUtils() != null) {
153153
this.instance.getNpcUtils().spawnAndEditNPC(id, prefix, rentHandler.getOwnerName());
154154
}else {
155-
this.instance.getVillagerUtils().spawnVillager(id, prefix, rentHandler.getOwnerName());
155+
this.instance.getVillagerUtils().createVillager(id, prefix, rentHandler.getOwnerName());
156156
}
157157
}
158158
}

src/main/java/me/truemb/rentit/main/Main.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ public void onEnable() {
211211
this.setupCitizens(); //CITIZENS NPC
212212
}else {
213213
this.vilUtils = new VillagerUtils(this); //VILLAGER NPC
214+
this.getVillagerUtils().loadVillagers();
214215
new VillagerShopListener(this);
215216
}
216217
}
@@ -274,7 +275,8 @@ public void onEnable() {
274275
new Metrics(this, BSTATS_PLUGIN_ID);
275276

276277
//UPDATE CHECKER
277-
this.checkForUpdate();
278+
if(!this.isFoliaServer)
279+
this.checkForUpdate();
278280

279281
//PAYMENT SCHEDULER
280282
this.getThreadHandler().runTaskTimerAsync(new PaymentRunnable(this), 20 * 10, 20 * 60);
@@ -309,9 +311,6 @@ public void onDisable() {
309311
}
310312
}
311313

312-
if(this.getVillagerUtils() != null)
313-
this.getVillagerUtils().disableVillagers();
314-
315314
if(this.getAsyncSQL() != null && this.getAsyncSQL().getDatabaseConnector() != null && this.getAsyncSQL().getDatabaseConnector().getConnection() != null)
316315
this.getAsyncSQL().getDatabaseConnector().closeConnection();
317316
}
@@ -320,10 +319,6 @@ public void initRestart(CommandSender sender) {
320319

321320
//------ CLOSE EVERTHING ------
322321
this.getThreadHandler().runTaskAsync((t) -> {
323-
324-
//DISABLING VILLAGERS
325-
if(this.getVillagerUtils() != null)
326-
this.getVillagerUtils().disableVillagers();
327322

328323
//CLOSE SQL CONNECTION
329324
if(this.getAsyncSQL() != null && this.getAsyncSQL().getDatabaseConnector() != null && this.getAsyncSQL().getDatabaseConnector().getConnection() != null)
@@ -361,6 +356,7 @@ public void initRestart(CommandSender sender) {
361356
this.setupCitizens(); //CITIZENS NPC
362357
}else {
363358
this.vilUtils = new VillagerUtils(this); //VILLAGER NPC
359+
this.getVillagerUtils().loadVillagers();
364360
new VillagerShopListener(this);
365361
}
366362
}
@@ -393,8 +389,11 @@ public void initRestart(CommandSender sender) {
393389

394390
private void initHandlers() {
395391
//First load the categories. They are needed for the Shops/Hotelrooms
396-
this.getCategorySQL().setupCategories(b -> {
392+
this.getCategorySQL().setupCategory(RentTypes.SHOP, b -> {
397393
this.getShopsSQL().setupShops();
394+
});
395+
396+
this.getCategorySQL().setupCategory(RentTypes.HOTEL, b -> {
398397
this.getHotelsSQL().setupHotels();
399398
});
400399
}
@@ -562,9 +561,9 @@ private void setupPlaceholderAPI() {
562561
//PLUGIN WAS FOUND
563562
if(Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null){
564563
new PlaceholderAPI(this).register();
565-
this.getLogger().info("PlacerHolderAPI was found and registered!");
564+
this.getLogger().info("PlaceholderAPI was found and registered!");
566565
}else {
567-
this.getLogger().info("PlacerHolderAPI was not found. (Is not needed, but supported)");
566+
this.getLogger().info("PlaceholderAPI was not found. (Is not needed, but supported)");
568567
}
569568

570569
}

src/main/java/me/truemb/rentit/utils/VillagerUtils.java

+38-27
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.HashMap;
44

55
import org.bukkit.Location;
6+
import org.bukkit.configuration.file.YamlConfiguration;
7+
import org.bukkit.entity.Entity;
68
import org.bukkit.entity.EntityType;
79
import org.bukkit.entity.Villager;
810
import org.bukkit.metadata.FixedMetadataValue;
@@ -19,13 +21,25 @@ public VillagerUtils(Main plugin) {
1921
this.instance = plugin;
2022
}
2123

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+
2943
}
3044
}
3145

@@ -50,33 +64,30 @@ public void moveVillager(int shopId, Location loc) {
5064
vil.teleport(loc);
5165
}
5266

53-
public void spawnVillager(int shopId, String prefix, String playerName) {
67+
public void createVillager(int shopId, String prefix, String playerName) {
5468

5569
Location loc = this.instance.getNPCFileManager().getNPCLocForShop(shopId);
5670
if (loc == null)
5771
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);
6375

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
6981

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+
);
7587

76-
v.setCustomName(customName);
77-
v.setCustomNameVisible(true);
88+
v.setCustomName(customName);
89+
v.setCustomNameVisible(true);
7890

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
8192
}
8293
}

0 commit comments

Comments
 (0)