use of dev.rosewood.rosestacker.stack.StackedBlock in project RoseStacker by Rosewood-Development.
the class DataUtils method readStackedBlocks.
public static List<StackedBlock> readStackedBlocks(Chunk chunk) {
PersistentDataContainer pdc = chunk.getPersistentDataContainer();
List<StackedBlock> stackedBlocks = new ArrayList<>();
byte[] data = pdc.get(CHUNK_BLOCKS_KEY, PersistentDataType.BYTE_ARRAY);
if (data == null)
return stackedBlocks;
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
ObjectInputStream dataInput = new ObjectInputStream(inputStream)) {
int dataVersion = dataInput.readInt();
if (dataVersion == 1) {
int length = dataInput.readInt();
for (int i = 0; i < length; i++) {
int stackSize = dataInput.readInt();
int x = dataInput.readInt();
int y = dataInput.readInt();
int z = dataInput.readInt();
stackedBlocks.add(new StackedBlock(stackSize, chunk.getBlock(x, y, z)));
}
}
} catch (Exception e) {
e.printStackTrace();
pdc.remove(CHUNK_BLOCKS_KEY);
}
return stackedBlocks;
}
use of dev.rosewood.rosestacker.stack.StackedBlock 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);
}
use of dev.rosewood.rosestacker.stack.StackedBlock in project RoseStacker by Rosewood-Development.
the class BlockListener method onBlockClicked.
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockClicked(PlayerInteractEvent event) {
Block block = event.getClickedBlock();
if (block == null)
return;
StackManager stackManager = this.rosePlugin.getManager(StackManager.class);
if (stackManager.isWorldDisabled(event.getPlayer().getWorld()))
return;
// Check for igniting stacked TNT
ItemStack item = event.getItem();
if (item != null && stackManager.isBlockStackingEnabled() && block.getType() == Material.TNT && stackManager.isBlockStacked(block) && (item.getType() == Material.FLINT_AND_STEEL || item.getType() == Material.FIRE_CHARGE)) {
event.setUseInteractedBlock(Event.Result.DENY);
return;
}
if (event.getPlayer().isSneaking() && event.getAction() == Action.RIGHT_CLICK_BLOCK) {
if (stackManager.isBlockStackingEnabled() && Setting.BLOCK_GUI_ENABLED.getBoolean()) {
StackedBlock stackedBlock = stackManager.getStackedBlock(block);
if (stackedBlock != null) {
stackedBlock.openGui(event.getPlayer());
event.setCancelled(true);
return;
}
}
if (stackManager.isSpawnerStackingEnabled() && block.getType() == Material.SPAWNER && Setting.SPAWNER_GUI_ENABLED.getBoolean()) {
StackedSpawner stackedSpawner = stackManager.getStackedSpawner(block);
if (stackedSpawner == null)
// Doesn't exist, need it to in order to open the GUI
stackManager.createSpawnerStack(block, 1, false);
if (stackedSpawner != null) {
stackedSpawner.openGui(event.getPlayer());
event.setCancelled(true);
}
}
}
}
use of dev.rosewood.rosestacker.stack.StackedBlock in project RoseStacker by Rosewood-Development.
the class StackToolListener method onInteract.
/**
* Handles Shift Left Click for StackedItems
*
* @param event The PlayerInteractEvent
*/
@EventHandler(priority = EventPriority.LOWEST)
public void onInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
ItemStack tool = player.getInventory().getItemInMainHand();
if (event.getAction() == Action.PHYSICAL || !ItemUtils.isStackingTool(tool))
return;
event.setCancelled(true);
if (!player.isSneaking() || event.getHand() != EquipmentSlot.HAND)
return;
if (!player.hasPermission("rosestacker.stacktool")) {
this.localeManager.sendMessage(player, "command-stacktool-no-permission");
return;
}
if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) {
for (Entity entity : player.getNearbyEntities(3, 3, 3)) {
if (entity.getType() != EntityType.DROPPED_ITEM)
continue;
Item item = (Item) entity;
if (!EntityUtils.isLookingAtItem(player, item))
continue;
StackedItem stackedItem = this.stackManager.getStackedItem(item);
if (stackedItem == null)
continue;
ItemMeta itemMeta = item.getItemStack().getItemMeta();
this.localeManager.sendMessage(player, "command-stacktool-info");
this.localeManager.sendSimpleMessage(player, "command-stacktool-info-uuid", StringPlaceholders.single("uuid", item.getUniqueId().toString()));
this.localeManager.sendSimpleMessage(player, "command-stacktool-info-entity-id", StringPlaceholders.single("id", item.getEntityId()));
this.localeManager.sendSimpleMessage(player, "command-stacktool-info-item-type", StringPlaceholders.single("type", item.getItemStack().getType().name()));
this.localeManager.sendSimpleMessage(player, "command-stacktool-info-stack-size", StringPlaceholders.single("amount", stackedItem.getStackSize()));
if (itemMeta != null && itemMeta.hasDisplayName())
this.localeManager.sendSimpleMessage(player, "command-stacktool-info-custom-name", StringPlaceholders.single("name", itemMeta.getDisplayName()));
this.localeManager.sendSimpleMessage(player, "command-stacktool-info-location", StringPlaceholders.builder("x", item.getLocation().getBlockX()).addPlaceholder("y", item.getLocation().getBlockY()).addPlaceholder("z", item.getLocation().getBlockZ()).addPlaceholder("world", item.getWorld().getName()).build());
this.localeManager.sendSimpleMessage(player, "command-stacktool-info-chunk", StringPlaceholders.builder("x", item.getLocation().getChunk().getX()).addPlaceholder("z", item.getLocation().getChunk().getZ()).build());
return;
}
}
if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
Block clickedBlock = event.getClickedBlock();
if (clickedBlock == null)
return;
if (clickedBlock.getType() != Material.SPAWNER) {
StackedBlock stackedBlock = this.stackManager.getStackedBlock(clickedBlock);
if (stackedBlock == null)
return;
this.localeManager.sendMessage(player, "command-stacktool-info");
this.localeManager.sendSimpleMessage(player, "command-stacktool-info-block-type", StringPlaceholders.single("type", clickedBlock.getType().name()));
this.localeManager.sendSimpleMessage(player, "command-stacktool-info-stack-size", StringPlaceholders.single("amount", stackedBlock.getStackSize()));
this.localeManager.sendSimpleMessage(player, "command-stacktool-info-location", StringPlaceholders.builder("x", clickedBlock.getX()).addPlaceholder("y", clickedBlock.getY()).addPlaceholder("z", clickedBlock.getZ()).addPlaceholder("world", clickedBlock.getWorld().getName()).build());
this.localeManager.sendSimpleMessage(player, "command-stacktool-info-chunk", StringPlaceholders.builder("x", clickedBlock.getChunk().getX()).addPlaceholder("z", clickedBlock.getChunk().getZ()).build());
} else {
StackedSpawner stackedSpawner = this.stackManager.getStackedSpawner(clickedBlock);
if (stackedSpawner == null)
return;
this.localeManager.sendMessage(player, "command-stacktool-info");
this.localeManager.sendSimpleMessage(player, "command-stacktool-info-spawner-type", StringPlaceholders.single("type", stackedSpawner.getSpawnerTile().getSpawnedType().name()));
this.localeManager.sendSimpleMessage(player, "command-stacktool-info-stack-size", StringPlaceholders.single("amount", stackedSpawner.getStackSize()));
this.localeManager.sendSimpleMessage(player, "command-stacktool-info-location", StringPlaceholders.builder("x", clickedBlock.getX()).addPlaceholder("y", clickedBlock.getY()).addPlaceholder("z", clickedBlock.getZ()).addPlaceholder("world", clickedBlock.getWorld().getName()).build());
this.localeManager.sendSimpleMessage(player, "command-stacktool-info-chunk", StringPlaceholders.builder("x", clickedBlock.getChunk().getX()).addPlaceholder("z", clickedBlock.getChunk().getZ()).build());
}
}
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
Block clickedBlock = event.getClickedBlock();
if (clickedBlock == null || clickedBlock.getType() != Material.SPAWNER)
return;
StackedSpawner stackedSpawner = this.stackManager.getStackedSpawner(clickedBlock);
if (stackedSpawner == null) {
CreatureSpawner creatureSpawner = (CreatureSpawner) clickedBlock.getState();
creatureSpawner.setDelay(5);
creatureSpawner.update();
} else {
stackedSpawner.getSpawnerTile().setDelay(5);
}
int points = 50;
for (int i = 0; i < points; i++) {
double dx = Math.cos(Math.PI * 2 * ((double) i / points)) * 0.25;
double dy = 0.5;
double dz = Math.sin(Math.PI * 2 * ((double) i / points)) * 0.25;
double angle = Math.atan2(dz, dx);
double xAng = Math.cos(angle);
double zAng = Math.sin(angle);
clickedBlock.getWorld().spawnParticle(Particle.END_ROD, clickedBlock.getLocation().add(0.5 + dx, dy, 0.5 + dz), 0, xAng, 0, zAng, 0.15);
}
}
// Make the item bob a little bit
player.updateInventory();
}
use of dev.rosewood.rosestacker.stack.StackedBlock in project RoseStacker by Rosewood-Development.
the class DataUtils method writeStackedBlocks.
public static void writeStackedBlocks(Collection<StackedBlock> stackedBlocks, Chunk chunk) {
PersistentDataContainer pdc = chunk.getPersistentDataContainer();
byte[] data = null;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream dataOutput = new ObjectOutputStream(outputStream)) {
dataOutput.writeInt(BLOCK_DATA_VERSION);
dataOutput.writeInt(stackedBlocks.size());
for (StackedBlock stackedBlock : stackedBlocks) {
dataOutput.writeInt(stackedBlock.getStackSize());
dataOutput.writeInt(stackedBlock.getLocation().getBlockX() & 0xF);
dataOutput.writeInt(stackedBlock.getLocation().getBlockY());
dataOutput.writeInt(stackedBlock.getLocation().getBlockZ() & 0xF);
}
// Serialize that array
dataOutput.close();
data = outputStream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
if (data != null)
pdc.set(CHUNK_BLOCKS_KEY, PersistentDataType.BYTE_ARRAY, data);
}
Aggregations