use of com.codehusky.huskycrates.crate.VirtualCrate in project HuskyCrates-Sponge by codeHusky.
the class Wand method execute.
@Override
public CommandResult execute(CommandSource commandSource, CommandContext commandContext) throws CommandException {
if (commandContext.getOne("type").isPresent() && commandSource instanceof Player) {
String type = commandContext.<String>getOne("type").get();
Player player = (Player) commandSource;
VirtualCrate virtualCrate = HuskyCrates.instance.getCrateUtilities().getVirtualCrate(type);
if (virtualCrate == null) {
commandSource.sendMessage(Text.of("Invalid crate id: " + type + ". Please check your config."));
return CommandResult.empty();
}
ItemStack keyItemStack = virtualCrate.getCrateWand();
InventoryTransactionResult.Type mainInventory = player.getInventory().offer(keyItemStack.copy()).getType();
if (!mainInventory.equals(InventoryTransactionResult.Type.SUCCESS)) {
InventoryTransactionResult.Type enderInventory = player.getEnderChestInventory().offer(keyItemStack.copy()).getType();
if (!enderInventory.equals(InventoryTransactionResult.Type.SUCCESS)) {
commandSource.sendMessage(Text.of("Couldn't give wand to " + player.getName() + " because of a full inventory and enderchest"));
HuskyCrates.instance.logger.info("Couldn't give wand to " + player.getName() + " because of a full inventory and enderchest");
} else {
player.sendMessage(Text.of("You have been given a ", TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.displayName), " wand, but it has been placed in your Ender Chest."));
}
}
} else {
commandSource.sendMessage(Text.of("Usage: /crate wand <id>"));
}
return CommandResult.success();
}
use of com.codehusky.huskycrates.crate.VirtualCrate in project HuskyCrates-Sponge by codeHusky.
the class Chest method execute.
@Override
public CommandResult execute(CommandSource commandSource, CommandContext commandContext) throws CommandException {
if (commandContext.getOne("type").isPresent()) {
String type = commandContext.<String>getOne("type").get();
Optional<Player> 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 either specify a player or be in game"));
return CommandResult.empty();
}
ItemStack chestItemStack = virtualCrate.getCrateItem(quantity);
InventoryTransactionResult.Type mainInventory = player.get().getInventory().offer(chestItemStack.copy()).getType();
if (!mainInventory.equals(InventoryTransactionResult.Type.SUCCESS)) {
InventoryTransactionResult.Type enderInventory = player.get().getEnderChestInventory().offer(chestItemStack.copy()).getType();
if (!enderInventory.equals(InventoryTransactionResult.Type.SUCCESS)) {
commandSource.sendMessage(Text.of("Couldn't give chest to " + player.get().getName() + " because of a full inventory and enderchest"));
HuskyCrates.instance.logger.info("Couldn't give chest to " + player.get().getName() + " because of a full inventory and enderchest");
} else {
if (player.isPresent()) {
player.get().sendMessage(Text.of("You have been given 1 or more ", TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.displayName), " crate(s), but some have been placed in your Ender Chest."));
} else {
commandSource.sendMessage(Text.of("You have been given 1 or more ", TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.displayName), " crate(s), but some have been placed in your Ender Chest."));
}
}
}
} else {
commandSource.sendMessage(Text.of("Usage: /crate chest <id> [player]"));
}
return CommandResult.success();
}
use of com.codehusky.huskycrates.crate.VirtualCrate in project HuskyCrates-Sponge by codeHusky.
the class HC method getCrateKey.
public ItemStack getCrateKey(String id, int quantity) {
VirtualCrate vc = plugin.crateUtilities.getVirtualCrate(id);
if (vc != null) {
ItemStack key = ItemStack.builder().itemType(ItemTypes.NETHER_STAR).quantity(quantity).add(Keys.DISPLAY_NAME, TextSerializers.LEGACY_FORMATTING_CODE.deserialize(vc.displayName + " Key")).build();
ArrayList<Text> bb = new ArrayList<>();
bb.add(Text.of(TextColors.WHITE, "A key for a ", TextSerializers.LEGACY_FORMATTING_CODE.deserialize(vc.displayName), TextColors.WHITE, "."));
bb.add(Text.of(TextColors.WHITE, "crate_" + id));
key.offer(Keys.ITEM_LORE, bb);
return key;
}
return null;
}
use of com.codehusky.huskycrates.crate.VirtualCrate in project HuskyCrates-Sponge by codeHusky.
the class KeyAll method execute.
@Override
public CommandResult execute(CommandSource commandSource, CommandContext commandContext) throws CommandException {
if (commandContext.getOne("type").isPresent()) {
String type = commandContext.<String>getOne("type").get();
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();
}
for (Player player : Sponge.getServer().getOnlinePlayers()) {
ItemStack keyItemStack = virtualCrate.getCrateKey(quantity);
InventoryTransactionResult.Type mainInventory = player.getInventory().offer(keyItemStack.copy()).getType();
if (!mainInventory.equals(InventoryTransactionResult.Type.SUCCESS)) {
InventoryTransactionResult.Type enderInventory = player.getEnderChestInventory().offer(keyItemStack.copy()).getType();
if (!enderInventory.equals(InventoryTransactionResult.Type.SUCCESS)) {
commandSource.sendMessage(Text.of("Couldn't give key to " + player.getName() + " because of a full inventory and enderchest"));
HuskyCrates.instance.logger.info("Couldn't give key to " + player.getName() + " because of a full inventory and enderchest");
} else {
player.sendMessage(Text.of("You have been given 1 or more ", TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.displayName), " key(s), but some have been placed in your Ender Chest."));
}
}
}
} else {
commandSource.sendMessage(Text.of("Usage: /crate keyall <id> [count]"));
}
return CommandResult.success();
}
use of com.codehusky.huskycrates.crate.VirtualCrate in project HuskyCrates-Sponge by codeHusky.
the class HuskyCrates method postGameStart.
@Listener(order = Order.POST)
public void postGameStart(GameStartedServerEvent event) {
checkVersion();
if (forceStop) {
// logger.error("Since a blacklisted mod is loaded, HuskyCrates will not start. Please check higher in your logs for the reasoning.");
return;
}
Sponge.getScheduler().createTaskBuilder().execute(new Consumer<Task>() {
@Override
public void accept(Task task) {
logger.info("Deleting existing armor stands...");
removeArmorstands();
logger.info("Initalizing config...");
if (!crateUtilities.hasInitalizedVirtualCrates) {
crateUtilities.generateVirtualCrates(crateConfig);
}
// doublecheck
crateUtilities.hasInitalizedVirtualCrates = true;
logger.info("Done initalizing config.");
logger.info("Attempting legacy Physical Crates method (this will be removed in a later version)");
CommentedConfigurationNode root = null;
boolean convertFired = false;
try {
root = crateConfig.load();
double max = root.getNode("positions").getChildrenList().size();
double count = 0;
for (VirtualCrate vc : crateUtilities.crateTypes.values()) {
if (vc.pendingKeys.size() > 0) {
logger.warn("legacy keys loaded! warn warn warn.");
if (!root.getNode("keys").isVirtual()) {
root.removeChild("keys");
}
convertFired = true;
}
}
if (!root.getNode("positions").isVirtual()) {
convertFired = true;
logger.warn("Legacy position data detected. Will convert.");
for (CommentedConfigurationNode node : root.getNode("positions").getChildrenList()) {
count++;
Location<World> ee;
try {
ee = node.getNode("location").getValue(TypeToken.of(Location.class));
} catch (InvalidDataException err2) {
logger.warn("Bug sponge developers about world UUIDs!");
ee = new Location<World>(Sponge.getServer().getWorld(node.getNode("location", "WorldName").getString()).get(), node.getNode("location", "X").getDouble(), node.getNode("location", "Y").getDouble(), node.getNode("location", "Z").getDouble());
}
if (!crateUtilities.physicalCrates.containsKey(ee))
crateUtilities.physicalCrates.put(ee, new PhysicalCrate(ee, node.getNode("crateID").getString(), HuskyCrates.instance, node.getNode("location", "BlockType").getString().equals("minecraft:air")));
logger.info("(LEGACY) PROGRESS: " + Math.round((count / max) * 100) + "%");
}
root.removeChild("positions");
}
if (!root.getNode("users").isVirtual()) {
for (Object uuidPre : root.getNode("users").getChildrenMap().keySet()) {
for (Object crateIDPre : root.getNode("users", uuidPre, "keys").getChildrenMap().keySet()) {
String uuid = uuidPre.toString();
String crateID = crateIDPre.toString();
int amount = root.getNode("users", uuid, "keys", crateID).getInt(0);
HuskyCrates.instance.crateUtilities.crateTypes.get(crateID).virtualBalances.put(uuid, amount);
}
}
root.removeChild("users");
}
crateConfig.save(root);
} catch (Exception e) {
crateUtilities.exceptionHandler(e);
if (event.getCause().root() instanceof Player) {
CommandSource cs = (CommandSource) event.getCause().root();
cs.sendMessage(Text.of(TextColors.GOLD, "HuskyCrates", TextColors.WHITE, ":", TextColors.RED, " An error has occured. Please check the console for more information."));
}
return;
}
logger.info("Done with legacy loading technique");
logger.info("Running DB routine.");
try {
DBReader.dbInitCheck();
if (convertFired) {
logger.info("Saving data.");
DBReader.saveHuskyData();
logger.info("Done saving data.");
} else {
logger.info("Loading data.");
DBReader.loadHuskyData();
logger.info("Done loading data.");
}
} catch (SQLException e) {
e.printStackTrace();
}
logger.info("DB Data routine finished.");
crateUtilities.startParticleEffects();
logger.info("Initalization complete.");
Sponge.getScheduler().createTaskBuilder().execute(() -> {
try {
DBReader.dbInitCheck();
DBReader.saveHuskyData();
logger.info("Updated Database.");
} catch (SQLException e) {
e.printStackTrace();
}
}).interval(15, TimeUnit.MINUTES).delay(15, TimeUnit.MINUTES).async().submit(HuskyCrates.instance);
}
}).delayTicks(1).submit(this);
}
Aggregations