Search in sources :

Example 1 with StackSettingManager

use of dev.rosewood.rosestacker.manager.StackSettingManager in project RoseStacker by Rosewood-Development.

the class BlockListener method onBlockPlace.

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
    StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
    if (stackManager.isWorldDisabled(event.getPlayer().getWorld()))
        return;
    Player player = event.getPlayer();
    Block block = event.getBlock();
    if (block.getType() == Material.SPAWNER) {
        if (!stackManager.isSpawnerStackingEnabled() || !stackManager.isSpawnerTypeStackable(((CreatureSpawner) block.getState()).getSpawnedType()))
            return;
    } else {
        if (!stackManager.isBlockStackingEnabled() || !stackManager.isBlockTypeStackable(block))
            return;
    }
    Block against = event.getBlockAgainst();
    if (against.equals(block))
        against = against.getRelative(BlockFace.DOWN);
    // Get the block in the player's hand that's being placed
    ItemStack placedItem = player.getInventory().getItemInMainHand();
    boolean isOffHand = false;
    if (placedItem.getType() == Material.AIR || !placedItem.getType().isBlock()) {
        placedItem = player.getInventory().getItemInOffHand();
        isOffHand = true;
    }
    // Will be true if we are adding to an existing stack (including a stack of 1), or false if we are creating a new one from an itemstack with a stack value
    boolean isDistanceStack = false;
    boolean isAdditiveStack = against.getType() == block.getType();
    EntityType entityType = placedItem.getType() == Material.SPAWNER ? ItemUtils.getStackedItemEntityType(placedItem) : null;
    int stackAmount = ItemUtils.getStackedItemStackAmount(placedItem);
    // See if we can stack the spawner (if applicable) into one nearby
    int autoStackRange = Setting.SPAWNER_AUTO_STACK_RANGE.getInt();
    boolean autoStackChunk = Setting.SPAWNER_AUTO_STACK_CHUNK.getBoolean();
    boolean useAutoStack = autoStackRange > 0 || autoStackChunk;
    if (useAutoStack && block.getType() == Material.SPAWNER) {
        StackedSpawner nearest = null;
        boolean anyNearby = false;
        List<StackedSpawner> spawners = new ArrayList<>(stackManager.getStackingThread(block.getWorld()).getStackedSpawners().values());
        if (!autoStackChunk) {
            double closestDistance = autoStackRange * autoStackRange;
            for (StackedSpawner spawner : spawners) {
                double distance = spawner.getLocation().distanceSquared(block.getLocation());
                if (distance < closestDistance) {
                    boolean sameType = spawner.getSpawnerTile().getSpawnedType() == entityType;
                    anyNearby = Setting.SPAWNER_AUTO_STACK_PREVENT_MULTIPLE_IN_RANGE.getBoolean() || sameType;
                    if (sameType && spawner.getStackSize() + stackAmount <= spawner.getStackSettings().getMaxStackSize()) {
                        closestDistance = distance;
                        nearest = spawner;
                    }
                }
            }
        } else {
            Chunk chunk = block.getChunk();
            for (StackedSpawner spawner : spawners) {
                if (chunk == spawner.getBlock().getChunk()) {
                    boolean sameType = spawner.getSpawnerTile().getSpawnedType() == entityType;
                    anyNearby = Setting.SPAWNER_AUTO_STACK_PREVENT_MULTIPLE_IN_RANGE.getBoolean() || sameType;
                    if (sameType && spawner.getStackSize() + stackAmount <= spawner.getStackSettings().getMaxStackSize()) {
                        nearest = spawner;
                        break;
                    }
                }
            }
        }
        if (nearest != null) {
            against = nearest.getBlock();
            isAdditiveStack = true;
            isDistanceStack = true;
        } else if (anyNearby) {
            event.setCancelled(true);
            return;
        }
    }
    // Don't allow placing if they don't have permission
    if (entityType != null && Setting.SPAWNER_ADVANCED_PERMISSIONS.getBoolean() && !player.hasPermission("rosestacker.spawnerplace." + entityType.name().toLowerCase())) {
        this.rosePlugin.getManager(LocaleManager.class).sendMessage(player, "spawner-advanced-place-no-permission");
        event.setCancelled(true);
        return;
    }
    if (isAdditiveStack && against.getType() == Material.SPAWNER)
        isAdditiveStack = ((CreatureSpawner) against.getState()).getSpawnedType() == entityType;
    if (isAdditiveStack && (!player.isSneaking() || isDistanceStack)) {
        if (block.getType() == Material.SPAWNER) {
            if (!stackManager.isSpawnerTypeStackable(entityType))
                return;
            // Handle spawner stacking
            StackedSpawner stackedSpawner = stackManager.getStackedSpawner(against);
            if (stackedSpawner != null && stackedSpawner.getStackSize() + stackAmount > stackedSpawner.getStackSettings().getMaxStackSize()) {
                event.setCancelled(true);
                return;
            }
            if (stackedSpawner != null) {
                SpawnerStackEvent spawnerStackEvent = new SpawnerStackEvent(player, stackedSpawner, stackAmount, false);
                Bukkit.getPluginManager().callEvent(spawnerStackEvent);
                if (spawnerStackEvent.isCancelled()) {
                    event.setCancelled(true);
                    return;
                }
                stackAmount = spawnerStackEvent.getIncreaseAmount();
            } else {
                stackedSpawner = stackManager.createSpawnerStack(against, 1, false);
                SpawnerStackEvent spawnerStackEvent = new SpawnerStackEvent(player, stackedSpawner, stackAmount, true);
                Bukkit.getPluginManager().callEvent(spawnerStackEvent);
                if (spawnerStackEvent.isCancelled()) {
                    event.setCancelled(true);
                    return;
                }
                if (stackedSpawner.getStackSize() + stackAmount > stackedSpawner.getStackSettings().getMaxStackSize()) {
                    event.setCancelled(true);
                    return;
                }
            }
            stackedSpawner.increaseStackSize(stackAmount);
            // Fling particles from the attempted place location to the actual place location
            if (isDistanceStack && Setting.SPAWNER_AUTO_STACK_PARTICLES.getBoolean()) {
                for (int i = 0; i < 50; i++) {
                    Vector offset = Vector.getRandom();
                    Location startLoc = block.getLocation().clone().add(offset);
                    Vector start = startLoc.toVector();
                    Vector end = against.getLocation().toVector().add(offset).add(new Vector(0.0, 0.1, 0.0));
                    Vector angle = end.clone().subtract(start);
                    double length = angle.length() * 0.09;
                    angle.normalize();
                    player.spawnParticle(Particle.END_ROD, startLoc, 0, angle.getX(), angle.getY(), angle.getZ(), length);
                }
            }
        } else {
            if (!stackManager.isBlockTypeStackable(against))
                return;
            // Handle normal block stacking
            StackedBlock stackedBlock = stackManager.getStackedBlock(against);
            if (stackedBlock != null) {
                if (stackedBlock.isLocked()) {
                    event.setCancelled(true);
                    return;
                }
                if (stackedBlock.getStackSize() + stackAmount > stackedBlock.getStackSettings().getMaxStackSize()) {
                    event.setCancelled(true);
                    return;
                }
                BlockStackEvent blockStackEvent = new BlockStackEvent(player, stackedBlock, stackAmount, false);
                Bukkit.getPluginManager().callEvent(blockStackEvent);
                if (blockStackEvent.isCancelled()) {
                    event.setCancelled(true);
                    return;
                }
                stackAmount = blockStackEvent.getIncreaseAmount();
            } else {
                stackedBlock = stackManager.createBlockStack(against, 1);
                BlockStackEvent blockStackEvent = new BlockStackEvent(player, stackedBlock, stackAmount, false);
                Bukkit.getPluginManager().callEvent(blockStackEvent);
                if (blockStackEvent.isCancelled()) {
                    event.setCancelled(true);
                    return;
                }
                if (stackedBlock.getStackSize() + stackAmount > stackedBlock.getStackSettings().getMaxStackSize()) {
                    event.setCancelled(true);
                    return;
                }
            }
            stackedBlock.increaseStackSize(stackAmount);
        }
        event.setCancelled(true);
        BlockLoggingHook.recordBlockPlace(player, against);
    } else {
        // Handle singular items that have a stack multiplier
        // Set the spawner type
        StackSettingManager stackSettingManager = this.rosePlugin.getManager(StackSettingManager.class);
        if (placedItem.getType() == Material.SPAWNER) {
            EntityType spawnedType = ItemUtils.getStackedItemEntityType(placedItem);
            if (spawnedType == null)
                return;
            SpawnerStackSettings spawnerStackSettings = stackSettingManager.getSpawnerStackSettings(spawnedType);
            if (spawnerStackSettings == null)
                return;
            if (stackAmount <= 0)
                return;
            if (stackAmount > spawnerStackSettings.getMaxStackSize()) {
                event.setCancelled(true);
                return;
            }
            StackedSpawner tempStackedSpawner = new StackedSpawner(0, block, true);
            SpawnerStackEvent spawnerStackEvent = new SpawnerStackEvent(player, tempStackedSpawner, stackAmount, true);
            Bukkit.getPluginManager().callEvent(spawnerStackEvent);
            if (spawnerStackEvent.isCancelled()) {
                tempStackedSpawner.setStackSize(0);
                event.setCancelled(true);
                return;
            }
            stackAmount = spawnerStackEvent.getIncreaseAmount();
            StackedSpawner stackedSpawner = stackManager.createSpawnerStack(block, stackAmount, true);
            if (stackedSpawner != null) {
                stackedSpawner.getSpawnerTile().setSpawnedType(spawnedType);
                stackedSpawner.updateSpawnerProperties(true);
            }
        } else {
            if (stackAmount <= 1)
                return;
            BlockStackSettings blockStackSettings = stackSettingManager.getBlockStackSettings(block);
            if (blockStackSettings == null)
                return;
            if (stackAmount > blockStackSettings.getMaxStackSize()) {
                event.setCancelled(true);
                return;
            }
            StackedBlock tempStackedBlock = new StackedBlock(0, block);
            BlockStackEvent blockStackEvent = new BlockStackEvent(player, tempStackedBlock, stackAmount, true);
            Bukkit.getPluginManager().callEvent(blockStackEvent);
            if (blockStackEvent.isCancelled()) {
                tempStackedBlock.setStackSize(0);
                event.setCancelled(true);
                return;
            }
            stackManager.createBlockStack(block, stackAmount);
        }
    }
    // Take an item from the player's hand
    ItemUtils.takeOneItem(player, isOffHand ? EquipmentSlot.OFF_HAND : EquipmentSlot.HAND);
}
Also used : SpawnerStackSettings(dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings) Player(org.bukkit.entity.Player) StackedSpawner(dev.rosewood.rosestacker.stack.StackedSpawner) StackManager(dev.rosewood.rosestacker.manager.StackManager) ArrayList(java.util.ArrayList) StackSettingManager(dev.rosewood.rosestacker.manager.StackSettingManager) Chunk(org.bukkit.Chunk) BlockStackSettings(dev.rosewood.rosestacker.stack.settings.BlockStackSettings) EntityType(org.bukkit.entity.EntityType) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) Block(org.bukkit.block.Block) StackedBlock(dev.rosewood.rosestacker.stack.StackedBlock) LocaleManager(dev.rosewood.rosestacker.manager.LocaleManager) SpawnerStackEvent(dev.rosewood.rosestacker.event.SpawnerStackEvent) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector) BlockStackEvent(dev.rosewood.rosestacker.event.BlockStackEvent) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Example 2 with StackSettingManager

use of dev.rosewood.rosestacker.manager.StackSettingManager in project RoseStacker by Rosewood-Development.

the class RoseCommand method onTranslate.

@Subcommand("translate")
@CommandPermission("rosestacker.translate")
@CommandCompletion("@translationLocales *")
public void onTranslate(CommandSender sender, String locale, @Optional String spawnerFormat) {
    LocaleManager localeManager = this.rosePlugin.getManager(LocaleManager.class);
    StackSettingManager stackSettingManager = this.rosePlugin.getManager(StackSettingManager.class);
    if (spawnerFormat == null) {
        spawnerFormat = "{}";
        localeManager.sendMessage(sender, "command-translate-spawner-format");
    }
    if (!spawnerFormat.contains("{}")) {
        localeManager.sendMessage(sender, "command-translate-spawner-format-invalid");
        return;
    }
    localeManager.sendMessage(sender, "command-translate-loading");
    String finalSpawnerFormat = spawnerFormat;
    localeManager.getMinecraftTranslationValues(locale, response -> {
        if (response.getResult() == Result.FAILURE) {
            localeManager.sendMessage(sender, "command-translate-failure");
            return;
        }
        if (response.getResult() == Result.INVALID_LOCALE) {
            localeManager.sendMessage(sender, "command-translate-invalid-locale");
            return;
        }
        CommentedFileConfiguration blockStackConfig = CommentedFileConfiguration.loadConfiguration(stackSettingManager.getBlockSettingsFile());
        CommentedFileConfiguration entityStackConfig = CommentedFileConfiguration.loadConfiguration(stackSettingManager.getEntitySettingsFile());
        CommentedFileConfiguration itemStackConfig = CommentedFileConfiguration.loadConfiguration(stackSettingManager.getItemSettingsFile());
        CommentedFileConfiguration spawnerStackConfig = CommentedFileConfiguration.loadConfiguration(stackSettingManager.getSpawnerSettingsFile());
        Map<Material, String> materialValues = response.getMaterialValues();
        Map<EntityType, String> entityValues = response.getEntityValues();
        for (Entry<Material, String> entry : materialValues.entrySet()) {
            Material material = entry.getKey();
            String value = entry.getValue();
            if (blockStackConfig.isConfigurationSection(material.name()))
                blockStackConfig.set(material.name() + ".display-name", value);
            if (itemStackConfig.isConfigurationSection(material.name()))
                itemStackConfig.set(material.name() + ".display-name", value);
        }
        for (Entry<EntityType, String> entry : entityValues.entrySet()) {
            EntityType entityType = entry.getKey();
            String value = entry.getValue();
            if (entityStackConfig.isConfigurationSection(entityType.name()))
                entityStackConfig.set(entityType.name() + ".display-name", value);
            if (spawnerStackConfig.isConfigurationSection(entityType.name())) {
                String name = finalSpawnerFormat.replaceAll(Pattern.quote("{}"), value);
                spawnerStackConfig.set(entityType.name() + ".display-name", name);
            }
        }
        blockStackConfig.save();
        entityStackConfig.save();
        itemStackConfig.save();
        spawnerStackConfig.save();
        Bukkit.getScheduler().runTask(this.rosePlugin, () -> {
            this.rosePlugin.reload();
            localeManager.sendMessage(sender, "command-translate-success");
        });
    });
}
Also used : EntityType(org.bukkit.entity.EntityType) CommentedFileConfiguration(dev.rosewood.rosegarden.config.CommentedFileConfiguration) StackSettingManager(dev.rosewood.rosestacker.manager.StackSettingManager) Material(org.bukkit.Material) LocaleManager(dev.rosewood.rosestacker.manager.LocaleManager) Subcommand(co.aikar.commands.annotation.Subcommand) CommandCompletion(co.aikar.commands.annotation.CommandCompletion) CommandPermission(co.aikar.commands.annotation.CommandPermission)

Aggregations

LocaleManager (dev.rosewood.rosestacker.manager.LocaleManager)2 StackSettingManager (dev.rosewood.rosestacker.manager.StackSettingManager)2 EntityType (org.bukkit.entity.EntityType)2 CommandCompletion (co.aikar.commands.annotation.CommandCompletion)1 CommandPermission (co.aikar.commands.annotation.CommandPermission)1 Subcommand (co.aikar.commands.annotation.Subcommand)1 CommentedFileConfiguration (dev.rosewood.rosegarden.config.CommentedFileConfiguration)1 BlockStackEvent (dev.rosewood.rosestacker.event.BlockStackEvent)1 SpawnerStackEvent (dev.rosewood.rosestacker.event.SpawnerStackEvent)1 StackManager (dev.rosewood.rosestacker.manager.StackManager)1 StackedBlock (dev.rosewood.rosestacker.stack.StackedBlock)1 StackedSpawner (dev.rosewood.rosestacker.stack.StackedSpawner)1 BlockStackSettings (dev.rosewood.rosestacker.stack.settings.BlockStackSettings)1 SpawnerStackSettings (dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings)1 ArrayList (java.util.ArrayList)1 Chunk (org.bukkit.Chunk)1 Location (org.bukkit.Location)1 Material (org.bukkit.Material)1 Block (org.bukkit.block.Block)1 Player (org.bukkit.entity.Player)1