use of com.codehusky.huskycrates.crate.VirtualCrate in project HuskyCrates-Sponge by codeHusky.
the class DBReader method loadHuskyData.
public static void loadHuskyData() throws SQLException {
connectDB();
HashMap<Integer, World> worldIDtoWorld = new HashMap<>();
ResultSet worldInfo = dbConnection.prepareStatement("SELECT * FROM WORLDINFO").executeQuery();
// HuskyCrates.instance.logger.info("isClosed: " + worldInfo.isClosed());
while (worldInfo.next()) {
// HuskyCrates.instance.logger.info("worldInfo thing!");
String worldUUID = worldInfo.getString("uuid");
String worldName = worldInfo.getString("name");
int id = worldInfo.getInt("ID");
Optional<World> preWorld = Sponge.getServer().getWorld(UUID.fromString(worldUUID));
if (!preWorld.isPresent()) {
HuskyCrates.instance.logger.warn("Invalid World UUID (BUG SPONGE DEVS)");
preWorld = Sponge.getServer().getWorld(worldName);
}
if (preWorld.isPresent()) {
worldIDtoWorld.put(id, preWorld.get());
HuskyCrates.instance.logger.info("Loaded world \"" + worldName + "\" successfully.");
} else {
HuskyCrates.instance.logger.warn("WorldInfo #" + id + " provides invalid world info. Removing from table.");
Statement removal = dbConnection.createStatement();
removal.executeQuery("SELECT * FROM WORLDINFO WHERE ID=" + id);
removal.executeUpdate("DELETE FROM WORLDINFO");
removal.close();
}
}
ResultSet cratePositions = dbConnection.prepareStatement("SELECT * FROM CRATELOCATIONS").executeQuery();
HuskyCrates.instance.crateUtilities.physicalCrates = new HashMap<>();
// HuskyCrates.instance.logger.info("isClosed: " + cratePositions.isClosed());
while (cratePositions.next()) {
// HuskyCrates.instance.logger.info("cratePositions thing!");
int id = cratePositions.getInt("ID");
double x = cratePositions.getDouble("X");
double y = cratePositions.getDouble("Y");
double z = cratePositions.getDouble("Z");
int worldID = cratePositions.getInt("worldID");
boolean entityCrate = cratePositions.getBoolean("isEntityCrate");
String crateID = cratePositions.getString("crateID");
if (worldIDtoWorld.containsKey(worldID)) {
// VALID WORLD
World world = worldIDtoWorld.get(worldID);
Location<World> loco = new Location<>(world, x, y, z);
HuskyCrates.instance.crateUtilities.physicalCrates.put(loco, new PhysicalCrate(loco, crateID, HuskyCrates.instance, entityCrate));
HuskyCrates.instance.logger.info("Loaded " + crateID + " @ " + x + "," + y + "," + z + ((entityCrate) ? " (ENTITY CRATE)" : ""));
} else {
HuskyCrates.instance.logger.warn("CrateLocation #" + id + " provides an invalid world ID. Removing from table.");
Statement removal = dbConnection.createStatement();
removal.executeQuery("SELECT * FROM CRATELOCATIONS WHERE ID=" + id);
removal.executeUpdate("DELETE FROM CRATELOCATIONS");
removal.close();
}
}
ResultSet crateKeyUUIDs = dbConnection.prepareStatement("SELECT * FROM VALIDKEYS").executeQuery();
// HuskyCrates.instance.logger.info("isClosed: " + crateKeyUUIDs.isClosed());
for (VirtualCrate vc : HuskyCrates.instance.crateUtilities.crateTypes.values()) {
vc.pendingKeys = new HashMap<>();
vc.virtualBalances = new HashMap<>();
PreparedStatement statement = dbConnection.prepareStatement("SELECT * FROM LASTUSED WHERE crateID = ?");
statement.setString(1, vc.id);
ResultSet results = statement.executeQuery();
while (results.next()) {
vc.lastUsed.put(UUID.fromString(results.getString(1)), LocalDateTime.ofInstant(Instant.ofEpochMilli(results.getLong(3)), ZoneOffset.systemDefault()));
}
}
while (crateKeyUUIDs.next()) {
// HuskyCrates.instance.logger.info("crateKeyUUIDs thing!");
UUID keyUUID = UUID.fromString(crateKeyUUIDs.getString("keyUUID"));
String crateID = crateKeyUUIDs.getString("crateID");
int amount = crateKeyUUIDs.getInt("amount");
if (HuskyCrates.instance.crateUtilities.crateTypes.containsKey(crateID)) {
VirtualCrate vc = HuskyCrates.instance.crateUtilities.crateTypes.get(crateID);
vc.pendingKeys.put(keyUUID.toString(), amount);
} else {
HuskyCrates.instance.logger.warn("ValidKeys " + keyUUID + " provides an invalid crate ID. Removing from table.");
Statement removal = dbConnection.createStatement();
removal.executeQuery("SELECT * FROM CRATELOCATIONS WHERE KEYUUID=" + keyUUID.toString());
removal.executeUpdate("DELETE FROM CRATELOCATIONS");
removal.close();
}
}
ResultSet keyBalances = dbConnection.prepareStatement("SELECT * FROM KEYBALANCES").executeQuery();
// HuskyCrates.instance.logger.info("isClosed: " + keyBalances.isClosed());
while (keyBalances.next()) {
// HuskyCrates.instance.logger.info("keyBalances thing!");
UUID userUUID = UUID.fromString(keyBalances.getString("userUUID"));
String crateID = keyBalances.getString("crateID");
int amount = keyBalances.getInt("amount");
if (HuskyCrates.instance.crateUtilities.getCrateTypes().contains(crateID)) {
HuskyCrates.instance.crateUtilities.getVirtualCrate(crateID).virtualBalances.put(userUUID.toString(), amount);
} else {
HuskyCrates.instance.logger.warn("KeyBalances for UUID " + userUUID + " provides an invalid crate ID. Removing from table.");
Statement removal = dbConnection.createStatement();
removal.executeQuery("SELECT * FROM KEYBALANCES WHERE USERUUID=" + userUUID.toString());
removal.executeUpdate("DELETE FROM KEYBALANCES");
removal.close();
}
}
/*
CRATELOCATIONS (ID INTEGER NOT NULL AUTO_INCREMENT, X DOUBLE, Y DOUBLE, Z DOUBLE, worldID INTEGER, isEntityCrate BOOLEAN, crateID CHARACTER, PRIMARY KEY(ID))
VALIDKEYS (keyUUID CHARACTER, crateID CHARACTER, amount INTEGER )
KEYBALANCES (userUUID CHARACTER, crateID CHARACTER, amount INTEGER)
WORLDINFO (ID INTEGER NOT NULL AUTO_INCREMENT,uuid CHARACTER, name CHARACTER, PRIMARY KEY(ID))
*/
}
use of com.codehusky.huskycrates.crate.VirtualCrate in project HuskyCrates-Sponge by codeHusky.
the class HuskyCrates method crateInteract.
@Listener
public void crateInteract(InteractBlockEvent.Secondary.MainHand event) {
if (forceStop) {
return;
}
// pp.getInventory().offer(ItemStack.builder().fromContainer(ss.toContainer().set(DataQuery.of("UnsafeDamage"),3)).build());*/
if (!event.getTargetBlock().getLocation().isPresent())
return;
Location<World> blk = event.getTargetBlock().getLocation().get();
// System.out.println(blk.getBlock().getType());
if (validCrateBlocks.contains(blk.getBlockType())) {
Player plr = (Player) event.getCause().root();
if (crateUtilities.physicalCrates.containsKey(blk)) {
String crateType = crateUtilities.physicalCrates.get(blk).vc.id;
VirtualCrate vc = crateUtilities.getVirtualCrate(crateType);
crateUtilities.physicalCrates.get(blk).createHologram();
if (vc.crateBlockType == blk.getBlockType()) {
event.setCancelled(true);
} else {
return;
}
keyHandler(plr, crateUtilities.isAcceptedKey(crateUtilities.physicalCrates.get(blk), plr.getItemInHand(HandTypes.MAIN_HAND), plr), vc, blk, crateType);
}
}
}
use of com.codehusky.huskycrates.crate.VirtualCrate in project HuskyCrates-Sponge by codeHusky.
the class VirtualKey method execute.
@Override
public CommandResult execute(CommandSource commandSource, CommandContext commandContext) throws CommandException {
if (commandContext.getOne("operation").isPresent()) {
if (commandContext.getOne("type").isPresent()) {
String type = commandContext.<String>getOne("type").get();
Optional<User> player = commandContext.getOne("player");
VirtualCrate virtualCrate = HuskyCrates.instance.getCrateUtilities().getVirtualCrate(type);
int quantity = commandContext.getOne("quantity").isPresent() ? commandContext.<Integer>getOne("quantity").get() : 1;
if (virtualCrate == null) {
commandSource.sendMessage(Text.of("Invalid crate id: " + type + ". Please check your config."));
return CommandResult.empty();
}
if (!player.isPresent()) {
commandSource.sendMessage(Text.of("You need to be in game or specify a player for this command to work."));
return CommandResult.empty();
}
Integer oldBal = virtualCrate.getVirtualKeyBalance(player.get());
String operation = commandContext.<String>getOne("operation").get();
if (operation.equalsIgnoreCase("add")) {
virtualCrate.giveVirtualKeys(player.get(), quantity);
commandSource.sendMessage(Text.of("Gave " + player.get().getName() + " " + quantity + " vkeys."));
if (commandSource != player.get() && player.get() instanceof Player) {
((Player) player.get()).sendMessage(Text.of(TextColors.GREEN, "You received " + quantity + " virtual keys for a ", TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.displayName), "."));
}
} else if (operation.equalsIgnoreCase("set")) {
virtualCrate.setVirtualKeys(player.get(), quantity);
commandSource.sendMessage(Text.of("Set " + player.get().getName() + " " + quantity + " vkeys."));
if (commandSource != player.get() && player.get() instanceof Player) {
((Player) player.get()).sendMessage(Text.of(TextColors.GREEN, "Your virtual key balance was set to " + quantity + " for a ", TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.displayName), "."));
}
} else if (operation.equalsIgnoreCase("remove")) {
virtualCrate.takeVirtualKeys(player.get(), quantity);
commandSource.sendMessage(Text.of("Took " + player.get().getName() + " " + quantity + " vkeys."));
if (commandSource != player.get() && player.get() instanceof Player) {
((Player) player.get()).sendMessage(Text.of(TextColors.GREEN, "You lost " + quantity + " virtual keys for a ", TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.displayName), "."));
}
} else {
// invalid operation
}
fireEvent(player.get(), virtualCrate.id, oldBal, virtualCrate.getVirtualKeyBalance(player.get()));
} else {
printUsage(commandSource);
}
} else {
printUsage(commandSource);
}
return CommandResult.success();
}
use of com.codehusky.huskycrates.crate.VirtualCrate in project HuskyCrates-Sponge by codeHusky.
the class HuskyAPI method getKeyBals.
public HashMap<String, Integer> getKeyBals(User user) {
HashMap<String, Integer> bals = new HashMap<>();
for (String type : huskyCrates.crateUtilities.getCrateTypes()) {
VirtualCrate vc = huskyCrates.crateUtilities.getVirtualCrate(type);
bals.put(type, vc.getVirtualKeyBalance(user));
}
return bals;
}
use of com.codehusky.huskycrates.crate.VirtualCrate in project HuskyCrates-Sponge by codeHusky.
the class HuskyCrates method entityInteract.
@Listener
public void entityInteract(InteractEntityEvent.Secondary.MainHand event) {
if (forceStop) {
return;
}
// System.out.println(event.getTargetEntity().toContainer().get(DataQuery.of("UnsafeData","crateID")));
if (event.getCause().root() instanceof Player) {
Player plr = (Player) event.getCause().root();
if (plr.getItemInHand(HandTypes.MAIN_HAND).isPresent() && plr.hasPermission("huskycrates.wand")) {
ItemStack hand = plr.getItemInHand(HandTypes.MAIN_HAND).get();
if (hand.getType() == ItemTypes.BLAZE_ROD) {
if (hand.toContainer().get(DataQuery.of("UnsafeData", "crateID")).isPresent()) {
if (!crateUtilities.physicalCrates.containsKey(event.getTargetEntity().getLocation())) {
// System.out.println(event.getTargetEntity().getLocation().getBlockPosition());
event.getTargetEntity().offer(Keys.AI_ENABLED, false);
event.getTargetEntity().offer(Keys.IS_SILENT, true);
crateUtilities.physicalCrates.put(event.getTargetEntity().getLocation(), new PhysicalCrate(event.getTargetEntity().getLocation(), hand.toContainer().get(DataQuery.of("UnsafeData", "crateID")).get().toString(), this, true));
crateUtilities.physicalCrates.get(event.getTargetEntity().getLocation()).createHologram();
try {
DBReader.saveHuskyData();
} catch (SQLException e) {
e.printStackTrace();
}
} else {
event.getTargetEntity().offer(Keys.AI_ENABLED, true);
event.getTargetEntity().offer(Keys.IS_SILENT, false);
event.getTargetEntity().offer(Keys.CUSTOM_NAME_VISIBLE, false);
event.getTargetEntity().offer(Keys.DISPLAY_NAME, Text.of());
crateUtilities.physicalCrates.remove(event.getTargetEntity().getLocation());
crateUtilities.brokenCrates.add(event.getTargetEntity().getLocation());
try {
DBReader.saveHuskyData();
} catch (SQLException e) {
e.printStackTrace();
}
}
event.setCancelled(true);
return;
}
}
}
if (crateUtilities.physicalCrates.containsKey(event.getTargetEntity().getLocation())) {
String crateType = crateUtilities.physicalCrates.get(event.getTargetEntity().getLocation()).vc.id;
VirtualCrate vc = crateUtilities.getVirtualCrate(crateType);
crateUtilities.physicalCrates.get(event.getTargetEntity().getLocation()).createHologram();
// crateUtilities.recognizeChest(te.getLocation());
event.setCancelled(true);
int keyResult = crateUtilities.isAcceptedKey(crateUtilities.physicalCrates.get(event.getTargetEntity().getLocation()), plr.getItemInHand(HandTypes.MAIN_HAND), plr);
keyHandler(plr, keyResult, vc, event.getTargetEntity().getLocation(), crateType);
}
}
}
Aggregations