Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
380 changes: 356 additions & 24 deletions eco-api/src/main/java/com/willfp/eco/core/blocks/Blocks.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.willfp.eco.core.blocks;

import org.bukkit.block.Block;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

/**
* A block and its hash.
*/
public final class HashedBlock {
/**
* The block.
*/
private final Block block;

/**
* The hash.
*/
private final int hash;

/**
* Create new hashed block.
*
* @param block The block.
* @param hash The hash.
*/
private HashedBlock(@NotNull final Block block,
final int hash) {
this.block = block;
this.hash = hash;
}

/**
* Get the block.
*
* @return The Block.
*/
public Block getBlock() {
return this.block;
}

/**
* Get the hash.
*
* @return The hash.
*/
public int getHash() {
return this.hash;
}

@Override
public int hashCode() {
return this.hash;
}

@Override
public boolean equals(@Nullable final Object other) {
if (!(other instanceof HashedBlock o)) {
return false;
}

return o.hash == this.hash;
}

/**
* Hashed block from a block.
*
* @param block The block.
* @return The hashed block.
*/
public static HashedBlock of(@NotNull final Block block) {
return new HashedBlock(block, Objects.hash(block.getWorld().getName(), block.getX(), block.getY(), block.getZ()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,15 @@ public interface TestableBlock extends Testable<Block> {
*/
@NotNull
Block place(@NotNull Location location);

/**
* If a block matching this test should be marked as a custom block.
* <p>
* This is true by default for backwards compatibility reasons.
*
* @return If the block should be marked as custom.
*/
default boolean shouldMarkAsCustom() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.willfp.eco.core.blocks.args;

import com.willfp.eco.core.entities.args.EntityArgParser;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;
import java.util.function.Predicate;

/**
* The result of an arg parses.
*
* @param test The test for the block.
* @param modifier The modifier to apply to the block.
* @see EntityArgParser
*/
public record BlockArgParseResult(@NotNull Predicate<Block> test,
@NotNull Consumer<Block> modifier) {
/**
* Kotlin destructuring support.
*
* @return The test.
*/
public Predicate<Block> component1() {
return test;
}

/**
* Kotlin destructuring support.
*
* @return The modifier.
*/
public Consumer<Block> component2() {
return modifier;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.willfp.eco.core.blocks.args;

import com.willfp.eco.core.blocks.TestableBlock;
import org.bukkit.Location;
import org.bukkit.block.data.BlockData;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* An argument parser that should generate
* a modified BlockData for {@link TestableBlock#place(Location)} .
*/
public interface BlockArgParser {
/**
* Parse the arguments.
*
* @param args The arguments.
* @param blockData The block data to be modified.
* @return The modified block data.
*/
@Nullable BlockArgParseResult parseArguments(@NotNull String[] args,
@NotNull BlockData blockData);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.willfp.eco.core.blocks.impl;

import com.willfp.eco.core.blocks.TestableBlock;
import org.apache.commons.lang.Validate;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.jetbrains.annotations.NotNull;
Expand All @@ -24,6 +25,8 @@ public boolean matches(@Nullable final Block other) {

@Override
public @NotNull Block place(@NotNull final Location location) {
Validate.notNull(location.getWorld());

return location.getBlock();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -16,10 +17,10 @@ public class MaterialTestableBlock implements TestableBlock {
/**
* The block type.
*/
private final Material material;
protected final Material material;

/**
* Create a new unrestricted material testable block.
* Create a new block data testable block.
*
* @param material The material.
*/
Expand All @@ -42,16 +43,16 @@ public boolean matches(@Nullable final Block block) {
public @NotNull Block place(@NotNull Location location) {
Validate.notNull(location.getWorld());

Block block = location.getWorld().getBlockAt(location);
Block block = location.getBlock();
block.setType(material);

return block;
}

/**
* Get the material.
* Get the block data.
*
* @return The material.
* @return The block data.
*/
public Material getMaterial() {
return this.material;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.willfp.eco.core.blocks.impl;

import com.willfp.eco.core.blocks.TestableBlock;
import org.apache.commons.lang.Validate;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.Function;
import java.util.function.Predicate;

/**
* Existing testable block with an extra function.
*
* @see com.willfp.eco.core.entities.CustomEntity
*/
public class ModifiedTestableBlock implements TestableBlock {
/**
* The block.
*/
private final TestableBlock handle;

/**
* The data.
*/
private final Predicate<Block> test;

/**
* The provider to place a block.
*/
private final Function<Location, Block> provider;

/**
* Create a new modified testable block.
*
* @param block The base block.
* @param test The test.
* @param provider The provider to place a block.
*/
public ModifiedTestableBlock(@NotNull final TestableBlock block,
@NotNull final Predicate<@NotNull Block> test,
@NotNull final Function<Location, Block> provider) {
this.handle = block;
this.test = test;
this.provider = provider;
}

@Override
public boolean matches(@Nullable final Block block) {
return block != null && handle.matches(block) && test.test(block);
}

@Override
public @NotNull Block place(@NotNull final Location location) {
Validate.notNull(location.getWorld());

return provider.apply(location);
}

/**
* Get the handle.
*
* @return The handle.
*/
public TestableBlock getHandle() {
return this.handle;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.willfp.eco.core.blocks.impl;

import com.willfp.eco.core.blocks.TestableBlock;
import org.apache.commons.lang.Validate;
import org.bukkit.Location;
import org.bukkit.Material;
Expand All @@ -11,19 +10,15 @@
/**
* A testable block for materials regardless of data.
*/
public class UnrestrictedMaterialTestableBlock implements TestableBlock {
/**
* The block type.
*/
private final Material material;
public class UnrestrictedMaterialTestableBlock extends MaterialTestableBlock {

/**
* Create a new unrestricted material testable block.
*
* @param material The material.
*/
public UnrestrictedMaterialTestableBlock(@NotNull final Material material) {
this.material = material;
super(material);
}

@Override
Expand All @@ -40,13 +35,4 @@ public boolean matches(@Nullable final Block other) {

return block;
}

/**
* Get the material.
*
* @return The material.
*/
public Material getMaterial() {
return this.material;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.willfp.eco.core.blocks.tag;

import com.willfp.eco.core.blocks.TestableBlock;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* A group of blocks that share a common trait.
*/
public interface BlockTag {
/**
* Get the identifier of the tag.
*
* @return The identifier.
*/
@NotNull
String getIdentifier();

/**
* Check if a block matches the tag.
*
* @param block The block to check.
* @return If the block matches the tag.
*/
boolean matches(@NotNull Block block);

/**
* Convert this tag to a testable block.
*
* @return The testable block.
*/
@NotNull
default TestableBlock toTestableBlock() {
return new TestableBlock() {
@Override
public boolean matches(@Nullable final Block block) {
return block != null && BlockTag.this.matches(block);
}

@Override
public @NotNull Block place(@NotNull final Location location) {
return location.getBlock();
}

@Override
public String toString() {
return "BlockTagTestableBlock{" +
"tag=" + BlockTag.this.getIdentifier() +
'}';
}
};
}
}
Loading
Loading