use of dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings in project RoseStacker by Rosewood-Development.
the class ItemUtils method getSpawnerAsStackedItemStack.
public static ItemStack getSpawnerAsStackedItemStack(EntityType entityType, int amount) {
ItemStack itemStack = new ItemStack(Material.SPAWNER);
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta == null)
return itemStack;
SpawnerStackSettings stackSettings = RoseStacker.getInstance().getManager(StackSettingManager.class).getSpawnerStackSettings(entityType);
StringPlaceholders placeholders = StringPlaceholders.builder("amount", amount).addPlaceholder("name", stackSettings.getDisplayName()).build();
String displayString;
if (amount == 1) {
displayString = RoseStacker.getInstance().getManager(LocaleManager.class).getLocaleMessage("spawner-stack-display-single", placeholders);
} else {
displayString = RoseStacker.getInstance().getManager(LocaleManager.class).getLocaleMessage("spawner-stack-display", placeholders);
}
itemMeta.setDisplayName(displayString);
// Set the lore, if defined
List<String> lore = RoseStacker.getInstance().getManager(LocaleManager.class).getLocaleMessages("stack-item-lore-spawner", placeholders);
if (!lore.isEmpty())
itemMeta.setLore(lore);
// Set the spawned type directly onto the spawner item for hopeful compatibility with other plugins
BlockStateMeta blockStateMeta = (BlockStateMeta) itemMeta;
CreatureSpawner creatureSpawner = (CreatureSpawner) blockStateMeta.getBlockState();
creatureSpawner.setSpawnedType(entityType);
blockStateMeta.setBlockState(creatureSpawner);
itemStack.setItemMeta(itemMeta);
// Set stack size and spawned entity type
NMSHandler nmsHandler = NMSAdapter.getHandler();
itemStack = nmsHandler.setItemStackNBT(itemStack, "StackSize", amount);
itemStack = nmsHandler.setItemStackNBT(itemStack, "EntityType", entityType.name());
return itemStack;
}
use of dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings in project RoseStacker by Rosewood-Development.
the class StackedSpawnerTileImpl method c.
@Override
public void c() {
World level = this.a();
if (level == null)
return;
// Only tick the spawner if a player is nearby
this.playersTimeSinceLastCheck = (this.playersTimeSinceLastCheck + 1) % Setting.SPAWNER_PLAYER_CHECK_FREQUENCY.getInt();
if (this.playersTimeSinceLastCheck == 0)
this.playersNearby = this.isNearPlayer(level, this.blockPos);
if (!this.playersNearby)
return;
SpawnerStackSettings stackSettings = this.stackedSpawner.getStackSettings();
// Handle redstone deactivation if enabled
if (Setting.SPAWNER_DEACTIVATE_WHEN_POWERED.getBoolean()) {
if (this.redstoneTimeSinceLastCheck == 0) {
boolean hasSignal = level.isBlockIndirectlyPowered(this.blockPos);
if (this.redstoneDeactivated && !hasSignal) {
this.redstoneDeactivated = false;
this.requiredPlayerRange = stackSettings.getPlayerActivationRange();
this.updateTile();
} else if (!this.redstoneDeactivated && hasSignal) {
this.redstoneDeactivated = true;
this.requiredPlayerRange = 0;
this.updateTile();
}
if (this.redstoneDeactivated)
return;
}
this.redstoneTimeSinceLastCheck = (this.redstoneTimeSinceLastCheck + 1) % Setting.SPAWNER_POWERED_CHECK_FREQUENCY.getInt();
}
// Count down spawn timer unless we are ready to spawn
if (this.spawnDelay > 0) {
this.spawnDelay--;
return;
}
// Reset spawn delay
this.spawnDelay = level.getRandom().nextInt(this.maxSpawnDelay - this.minSpawnDelay + 1) + this.minSpawnDelay;
this.updateTile();
// Execute spawning method
if (this.spawnData != null) {
MinecraftKey resourceLocation = MinecraftKey.a(this.spawnData.getEntity().getString("id"));
if (resourceLocation != null) {
NamespacedKey namespacedKey = CraftNamespacedKey.fromMinecraft(resourceLocation);
EntityType entityType = this.fromKey(namespacedKey);
if (entityType != null)
new MobSpawningMethod(entityType).spawn(this.stackedSpawner);
}
}
// Randomize spawn potentials
if (!this.mobs.isEmpty())
this.setSpawnData(WeightedRandom.a(this.a().random, this.mobs));
}
use of dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings in project RoseStacker by Rosewood-Development.
the class StackedSpawnerTileImpl method c.
@Override
public void c() {
World level = this.a();
if (level == null)
return;
// Only tick the spawner if a player is nearby
this.playersTimeSinceLastCheck = (this.playersTimeSinceLastCheck + 1) % Setting.SPAWNER_PLAYER_CHECK_FREQUENCY.getInt();
if (this.playersTimeSinceLastCheck == 0)
this.playersNearby = this.isNearPlayer(level, this.blockPos);
if (!this.playersNearby)
return;
SpawnerStackSettings stackSettings = this.stackedSpawner.getStackSettings();
// Handle redstone deactivation if enabled
if (Setting.SPAWNER_DEACTIVATE_WHEN_POWERED.getBoolean()) {
if (this.redstoneTimeSinceLastCheck == 0) {
boolean hasSignal = level.isBlockIndirectlyPowered(this.blockPos);
if (this.redstoneDeactivated && !hasSignal) {
this.redstoneDeactivated = false;
this.requiredPlayerRange = stackSettings.getPlayerActivationRange();
this.updateTile();
} else if (!this.redstoneDeactivated && hasSignal) {
this.redstoneDeactivated = true;
this.requiredPlayerRange = 0;
this.updateTile();
}
if (this.redstoneDeactivated)
return;
}
this.redstoneTimeSinceLastCheck = (this.redstoneTimeSinceLastCheck + 1) % Setting.SPAWNER_POWERED_CHECK_FREQUENCY.getInt();
}
// Count down spawn timer unless we are ready to spawn
if (this.spawnDelay > 0) {
this.spawnDelay--;
return;
}
// Reset spawn delay
this.spawnDelay = level.getRandom().nextInt(this.maxSpawnDelay - this.minSpawnDelay + 1) + this.minSpawnDelay;
this.updateTile();
// Execute spawning method
if (this.spawnData != null) {
MinecraftKey resourceLocation = MinecraftKey.a(this.spawnData.getEntity().getString("id"));
if (resourceLocation != null) {
NamespacedKey namespacedKey = CraftNamespacedKey.fromMinecraft(resourceLocation);
EntityType entityType = this.fromKey(namespacedKey);
if (entityType != null)
new MobSpawningMethod(entityType).spawn(this.stackedSpawner);
}
}
// Randomize spawn potentials
if (!this.mobs.isEmpty())
this.setSpawnData(WeightedRandom.a(this.a().random, this.mobs));
}
use of dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings in project RoseStacker by Rosewood-Development.
the class StackedSpawnerTileImpl method serverTick.
@Override
public void serverTick(ServerLevel level, BlockPos blockPos) {
// Only tick the spawner if a player is nearby
this.playersTimeSinceLastCheck = (this.playersTimeSinceLastCheck + 1) % Setting.SPAWNER_PLAYER_CHECK_FREQUENCY.getInt();
if (this.playersTimeSinceLastCheck == 0)
this.playersNearby = this.isNearPlayer(level, blockPos);
if (!this.playersNearby)
return;
SpawnerStackSettings stackSettings = this.stackedSpawner.getStackSettings();
// Handle redstone deactivation if enabled
if (Setting.SPAWNER_DEACTIVATE_WHEN_POWERED.getBoolean()) {
if (this.redstoneTimeSinceLastCheck == 0) {
boolean hasSignal = level.hasNeighborSignal(this.blockPos);
if (this.redstoneDeactivated && !hasSignal) {
this.redstoneDeactivated = false;
this.requiredPlayerRange = stackSettings.getPlayerActivationRange();
this.updateTile();
} else if (!this.redstoneDeactivated && hasSignal) {
this.redstoneDeactivated = true;
this.requiredPlayerRange = 0;
this.updateTile();
}
if (this.redstoneDeactivated)
return;
}
this.redstoneTimeSinceLastCheck = (this.redstoneTimeSinceLastCheck + 1) % Setting.SPAWNER_POWERED_CHECK_FREQUENCY.getInt();
}
// Count down spawn timer unless we are ready to spawn
if (this.spawnDelay > 0) {
this.spawnDelay--;
return;
}
// Reset spawn delay
this.spawnDelay = level.getRandom().nextInt(this.maxSpawnDelay - this.minSpawnDelay + 1) + this.minSpawnDelay;
this.updateTile();
// Execute spawning method
if (this.nextSpawnData != null) {
ResourceLocation resourceLocation = ResourceLocation.tryParse(this.nextSpawnData.getEntityToSpawn().getString("id"));
if (resourceLocation != null) {
NamespacedKey namespacedKey = CraftNamespacedKey.fromMinecraft(resourceLocation);
EntityType entityType = this.fromKey(namespacedKey);
if (entityType != null)
new MobSpawningMethod(entityType).spawn(this.stackedSpawner);
}
}
// Randomize spawn potentials
this.spawnPotentials.getRandom(level.getRandom()).ifPresent(x -> this.nextSpawnData = x.getData());
}
use of dev.rosewood.rosestacker.stack.settings.SpawnerStackSettings 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);
}
Aggregations