|
| 1 | +package valandur.webapi.integration.villagershops; |
| 2 | + |
| 3 | +import com.flowpowered.math.vector.Vector3d; |
| 4 | +import de.dosmike.sponge.vshop.*; |
| 5 | +import org.spongepowered.api.entity.Entity; |
| 6 | +import org.spongepowered.api.entity.EntityType; |
| 7 | +import org.spongepowered.api.text.serializer.TextSerializers; |
| 8 | +import org.spongepowered.api.world.Location; |
| 9 | +import valandur.webapi.integration.villagershops.wrapper.CachedStockItem; |
| 10 | +import valandur.webapi.integration.villagershops.wrapper.CachedVShop; |
| 11 | + |
| 12 | +import javax.ws.rs.BadRequestException; |
| 13 | +import javax.ws.rs.NotFoundException; |
| 14 | +import java.util.Optional; |
| 15 | + |
| 16 | +class VShopCompareUtils { |
| 17 | + |
| 18 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 19 | + static void applyDiv(CachedVShop update, NPCguard shop) { |
| 20 | + Entity rep = shop.getLe(); |
| 21 | + boolean respawnShop = false; //if the entity changed a new entity / mob has to be spawned |
| 22 | + if (update.getEntityType() != null && !update.getEntityType().getId().equalsIgnoreCase(shop.getNpcType().toString())) { |
| 23 | + EntityType et = update.getEntityType().getLive(EntityType.class).orElse(null); |
| 24 | + if (et == null) |
| 25 | + throw new BadRequestException("EntityType " + update.getEntityType() + " is unknown"); |
| 26 | + respawnShop = true; |
| 27 | + shop.setNpcType(et); |
| 28 | + } |
| 29 | + if (update.getEntityVariant() != null && !update.getEntityVariant().equalsIgnoreCase(shop.getVariantName())) { |
| 30 | + respawnShop = true; |
| 31 | + shop.setVariant(update.getEntityVariant()); |
| 32 | + } |
| 33 | + if (update.getLocation() != null) { |
| 34 | + Optional<Location> ol = update.getLocation().getLive(); |
| 35 | + if (!ol.isPresent()) |
| 36 | + throw new BadRequestException("Could not get Live version of Location"); |
| 37 | + shop.move(ol.get()); |
| 38 | + } |
| 39 | + if (update.getName() != null && !update.getName().equals(TextSerializers.FORMATTING_CODE.serialize(shop.getDisplayName()))) { |
| 40 | + shop.setDisplayName(TextSerializers.FORMATTING_CODE.deserialize(update.getName())); |
| 41 | + } |
| 42 | + if (update.getOwner() != null && !update.getOwner().equals(shop.getShopOwner().orElse(null))) { |
| 43 | +// if (update.isPlayerShop() == null) |
| 44 | +// throw new BadRequestException("Missing flag playershop"); |
| 45 | +// if (!update.isPlayerShop()) { |
| 46 | +// throw new BadRequestException("Can't set owner of non player-shops"); |
| 47 | +// } else { |
| 48 | + shop.setPlayerShop(update.getOwner()); |
| 49 | +// } |
| 50 | +// } else if (!update.isPlayerShop()) { |
| 51 | + } else if (update.getOwner() == null && shop.getShopOwner().isPresent()) { |
| 52 | + shop.setPlayerShop(null); |
| 53 | +// } else { |
| 54 | +// throw new BadRequestException("Missing owner UUID for player-shop"); |
| 55 | + } |
| 56 | + if (update.getRotation() != null && update.getRotation() != shop.getRot().getY()) { |
| 57 | + shop.setRot(new Vector3d(0.0, update.getRotation(), 0.0)); |
| 58 | + } |
| 59 | + if (update.getStockContainer() == null && shop.getStockContainer().isPresent()) { |
| 60 | + API.playershop(shop, null, null); |
| 61 | + } else if (update.getStockContainer() != null) { |
| 62 | + Optional<Location> ol = update.getLocation().getLive(); |
| 63 | + if (!ol.isPresent()) |
| 64 | + throw new BadRequestException("Could not get Live version of Stock Location"); |
| 65 | + if (!ol.get().equals(shop.getStockContainer().orElse(null))) { |
| 66 | + API.playershop(shop, shop.getShopOwner().get(), ol.get()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (update.getStockItems() != null) { |
| 71 | + VillagerShops.closeShopInventories(shop.getIdentifier()); |
| 72 | + Optional<NPCguard> npc = VillagerShops.getNPCfromShopUUID(shop.getIdentifier()); |
| 73 | + if (!npc.isPresent()) { |
| 74 | + throw new NotFoundException("Shop with id " + shop.getIdentifier() + " not found"); |
| 75 | + } |
| 76 | + InvPrep inv = npc.get().getPreparator(); |
| 77 | + int s = inv.size(); |
| 78 | + int newS = update.getStockItems().size(); |
| 79 | + for (int i = 0; i < Math.max(s, newS); i++) { |
| 80 | + if (i >= s) { |
| 81 | + CachedStockItem item = update.getStockItems().get(i); |
| 82 | + inv.addItem(new StockItem( |
| 83 | + item.getItem().createStack(), |
| 84 | + item.getSellPrice(), |
| 85 | + item.getBuyPrice(), |
| 86 | + VillagerShops.getInstance().CurrencyByName(item.getCurrency().getId()), |
| 87 | + item.getMaxStock())); |
| 88 | + } else if (i >= newS) { |
| 89 | + inv.removeIndex(i); |
| 90 | + } else { |
| 91 | + CachedStockItem item = update.getStockItems().get(i); |
| 92 | + inv.setItem(i, new StockItem( |
| 93 | + item.getItem().createStack(), |
| 94 | + item.getSellPrice(), |
| 95 | + item.getBuyPrice(), |
| 96 | + VillagerShops.getInstance().CurrencyByName(item.getCurrency().getId()), |
| 97 | + item.getMaxStock())); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (respawnShop && rep != null && !rep.isRemoved()) { |
| 103 | + rep.remove(); |
| 104 | + //tick will respawn the new entity |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments