Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes & CustomBlock type for mob spawning #358

Merged
merged 3 commits into from
Nov 24, 2023
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
1 change: 1 addition & 0 deletions src/main/java/world/bentobox/aoneblock/AOneBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ public void onReload() {
blockListener.saveCache();
if (loadSettings()) {
log("Reloaded AOneBlock settings");
loadData();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.gson.annotations.Expose;

import world.bentobox.aoneblock.oneblocks.OneBlockObject;
import world.bentobox.aoneblock.oneblocks.customblock.MobCustomBlock;
import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.database.objects.Table;

Expand Down Expand Up @@ -43,6 +44,12 @@ public class OneBlockIslands implements DataObject {
@Expose
private String hologram = "";

/**
* Timestamp of last phase change
*/
@Expose
private long lastPhaseChangeTime = 0;

private Queue<OneBlockObject> queue = new LinkedList<>();

/**
Expand Down Expand Up @@ -135,7 +142,16 @@ public Queue<OneBlockObject> getQueue() {
* @return list of upcoming mobs
*/
public List<EntityType> getNearestMob(int i) {
return getQueue().stream().limit(i).filter(OneBlockObject::isEntity).map(OneBlockObject::getEntityType).toList();
return getQueue().stream().limit(i).filter(obo ->
// Include OneBlockObjects that are Entity, or custom block of type MobCustomBlock
obo.isEntity() || (obo.isCustomBlock() && obo.getCustomBlock() instanceof MobCustomBlock)
).map(obo -> {
if (obo.isCustomBlock() && obo.getCustomBlock() instanceof MobCustomBlock) {
return ((MobCustomBlock) obo.getCustomBlock()).getMob();
}

return obo.getEntityType();
}).toList();
}

/**
Expand Down Expand Up @@ -186,5 +202,17 @@ public void setLifetime(long lifetime) {
this.lifetime = lifetime;
}

/**
* @return Timestamp of last phase change
*/
public long getLastPhaseChangeTime() {
return this.lastPhaseChangeTime;
}

/**
* @param lastPhaseChangeTime Timestamp of last phase change
*/
public void setLastPhaseChangeTime(long lastPhaseChangeTime) {
this.lastPhaseChangeTime = lastPhaseChangeTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,15 +287,17 @@ private void process(@NonNull Cancellable e, @NonNull Island i, @Nullable Player
saveIsland(i);
}
// Check if requirements met
if (check.phaseRequirementsFail(player, i, phase, world)) {
if (check.phaseRequirementsFail(player, i, is, phase, world)) {
e.setCancelled(true);
return;
}
if (newPhase) {
is.clearQueue();
is.setLastPhaseChangeTime(System.currentTimeMillis());
}
// Get the block number in this phase
int blockNumber = is.getBlockNumber() - phase.getBlockNumberValue() + (int) is.getQueue().stream().filter(OneBlockObject::isMaterial).count();
int materialBlocksInQueue = (int) is.getQueue().stream().filter(obo -> obo.isMaterial() || obo.isCustomBlock()).count();
int blockNumber = is.getBlockNumber() - (phase.getBlockNumberValue() - 1) + materialBlocksInQueue;
// Get the block that is being broken
Block block = Objects.requireNonNull(i.getCenter()).toVector().toLocation(world).getBlock();
// Fill a 5 block queue
Expand Down Expand Up @@ -406,7 +408,7 @@ else if (player.getLocation().getBlock().equals(block.getRelative(BlockFace.UP))

private void spawnBlock(@NonNull OneBlockObject nextBlock, @NonNull Block block) {
if (nextBlock.isCustomBlock()) {
nextBlock.getCustomBlock().setBlock(block);
nextBlock.getCustomBlock().execute(addon, block);
} else if (nextBlock.isItemsAdderBlock()) {
//Get Custom Block from ItemsAdder and place it
CustomBlock cBlock = CustomBlock.getInstance(nextBlock.getItemsAdderBlock());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public CheckPhase(AOneBlock addon, BlockListener blockListener) {
* @param world - world
* @return true if the player cannot proceed to the next phase.
*/
protected boolean phaseRequirementsFail(@Nullable Player player, @NonNull Island i, OneBlockPhase phase, @NonNull World world) {
protected boolean phaseRequirementsFail(@Nullable Player player, @NonNull Island i, @NonNull OneBlockIslands is, OneBlockPhase phase, @NonNull World world) {
if (phase.getRequirements().isEmpty()) {
return false;
}
Expand Down Expand Up @@ -88,6 +88,14 @@ protected boolean phaseRequirementsFail(@Nullable Player player, @NonNull Island
}
yield false;
}
case COOLDOWN -> {
long remainingTime = r.getCooldown() - (System.currentTimeMillis() - is.getLastPhaseChangeTime()) / 1000;
if(remainingTime > 0){
User.getInstance(player).sendMessage("aoneblock.phase.cooldown", TextVariables.NUMBER, String.valueOf(remainingTime));
yield true;
}
yield false;
}
};
if (b) blocked = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public MakeSpace(AOneBlock addon) {
* @param entity Entity that is spawned.
* @param spawnLocation Location where entity is spawned.
*/
void makeSpace(@NonNull Entity entity, @NonNull Location spawnLocation)
public void makeSpace(@NonNull Entity entity, @NonNull Location spawnLocation)
{
World world = entity.getWorld();
List<Block> airBlocks = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package world.bentobox.aoneblock.oneblocks;

import org.bukkit.block.Block;
import world.bentobox.aoneblock.AOneBlock;

/**
* Represents a custom block
* Represents a custom block with custom executable
*
* @author HSGamer
*/
public interface OneBlockCustomBlock {
/**
* Set the block
* Executes the custom logic
*
* @param block the block
*/
void setBlock(Block block);
void execute(AOneBlock addon, Block block);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package world.bentobox.aoneblock.oneblocks;

import world.bentobox.aoneblock.oneblocks.customblock.BlockDataCustomBlock;
import world.bentobox.aoneblock.oneblocks.customblock.MobCustomBlock;

import java.util.*;
import java.util.function.Function;
Expand All @@ -16,6 +17,7 @@ public final class OneBlockCustomBlockCreator {

static {
register("block-data", BlockDataCustomBlock::fromMap);
register("mob", MobCustomBlock::fromMap);
register("short", map -> {
String type = Objects.toString(map.get("data"), null);
if (type == null) {
Expand Down
Loading