!Separated BlockType#place and BlockType#regen

This commit is contained in:
Indyuce 2020-12-27 23:58:27 +01:00
parent 8aaa1b848b
commit 05a6d46a0d
4 changed files with 34 additions and 26 deletions

View File

@ -1,13 +1,20 @@
package net.Indyuce.mmocore.api.block;
import org.bukkit.Location;
import org.bukkit.block.Block;
import net.Indyuce.mmocore.api.block.BlockInfo.RegeneratingBlock;
public interface BlockType {
void place(Location loc, RegeneratingBlock regenerating);
void regen(Location loc, RegeneratingBlock regenerating);
/**
* Called when placing temporary blocks
*/
void place(RegeneratingBlock placed);
/**
* Called when regenerating an older block with block regen
*/
void regenerate(RegeneratingBlock regenerating);
/**
* Generates a key used to store the BlockInfo instance in the manager map,
@ -16,9 +23,9 @@ public interface BlockType {
String generateKey();
/**
* GenerateKey() determines if the block is handled by that block type,
* breakRestrictions(Block) applies some extra break restrictions; returns
* TRUE if the block can be broken
* Applies some extra break restrictions; returns TRUE if the block can be
* broken. This method is used to prevent non mature crops from being broken
* for example
*/
boolean breakRestrictions(Block block);
}

View File

@ -1,12 +1,13 @@
package net.Indyuce.mmocore.api.block;
import org.bukkit.Location;
import org.bukkit.block.Block;
import net.Indyuce.mmocore.api.block.BlockInfo.RegeneratingBlock;
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
import net.mmogroup.mmolib.MMOLib;
import net.mmogroup.mmolib.api.MMOLineConfig;
import net.mmogroup.mmolib.version.VersionMaterial;
import org.bukkit.Location;
import org.bukkit.block.Block;
public class SkullBlockType implements BlockType {
private final String value;
@ -26,7 +27,8 @@ public class SkullBlockType implements BlockType {
}
@Override
public void place(Location loc, RegeneratingBlock block) {
public void place(RegeneratingBlock block) {
Location loc = block.getLocation();
loc.getBlock().setType(VersionMaterial.PLAYER_HEAD.toMaterial());
// save skull orientation if replaced block is a player head
@ -37,14 +39,14 @@ public class SkullBlockType implements BlockType {
}
@Override
public void regen(Location loc, RegeneratingBlock block) {
// This makes sure that if a skull loses it's original rotation
public void regenerate(RegeneratingBlock block) {
Location loc = block.getLocation();
// This makes sure that if a skull loses its original rotation
// it can revert back to it when the base block is regenerated
loc.getBlock().setBlockData(block.getBlockData());
MMOLib.plugin.getVersion().getWrapper().setSkullValue(loc.getBlock(), value);
}
@Override
public String generateKey() {
return "vanilla-skull-" + value;

View File

@ -1,6 +1,5 @@
package net.Indyuce.mmocore.api.block;
import net.mmogroup.mmolib.MMOLib;
import org.apache.commons.lang.Validate;
import org.bukkit.Location;
import org.bukkit.Material;
@ -15,8 +14,8 @@ public class VanillaBlockType implements BlockType {
private final Material type;
/*
* allows to plant back crops with a custom age so that it does not always have
* to full grow again
* allows to plant back crops with a custom age so that it does not always
* have to full grow again
*/
private final int age;
@ -39,10 +38,11 @@ public class VanillaBlockType implements BlockType {
}
@Override
public void place(Location loc, RegeneratingBlock block) {
loc.getBlock().setType(type);
public void place(RegeneratingBlock block) {
Location loc = block.getLocation();
block.getLocation().getBlock().setType(type);
BlockData state = loc.getBlock().getBlockData();
BlockData state = block.getLocation().getBlock().getBlockData();
if (age > 0 && state instanceof Ageable) {
((Ageable) state).setAge(age);
loc.getBlock().setBlockData(state);
@ -50,7 +50,9 @@ public class VanillaBlockType implements BlockType {
}
@Override
public void regen(Location loc, RegeneratingBlock block) {
public void regenerate(RegeneratingBlock block) {
Location loc = block.getLocation();
loc.getBlock().setType(type);
// Sets the original blocks old data (only when regenerating)
loc.getBlock().setBlockData(block.getBlockData());
}
@ -62,7 +64,6 @@ public class VanillaBlockType implements BlockType {
@Override
public boolean breakRestrictions(Block block) {
return age == 0
|| (block.getBlockData() instanceof Ageable && ((Ageable) block.getBlockData()).getAge() >= age);
return age == 0 || (block.getBlockData() instanceof Ageable && ((Ageable) block.getBlockData()).getAge() >= age);
}
}

View File

@ -106,7 +106,7 @@ public class CustomBlockManager extends MMOManager {
}
if (info.getRegeneratingBlock().getRegenerationInfo().hasTemporaryBlock())
info.getRegeneratingBlock().getRegenerationInfo().getTemporaryBlock().place(info.getLocation(), info);
info.getRegeneratingBlock().getRegenerationInfo().getTemporaryBlock().place(info);
}
/**
@ -119,12 +119,10 @@ public class CustomBlockManager extends MMOManager {
* This prevents any issue when editing lists being iterated
*/
private void regen(RegeneratingBlock info, boolean shutdown) {
Location infoLocation = info.getLocation();
// Get the chunk and load it async if needed.
PaperLib.getChunkAtAsync(infoLocation).whenComplete((chunk, ex) -> {
info.getRegeneratingBlock().getBlock().place(infoLocation, info);
info.getRegeneratingBlock().getBlock().regen(infoLocation, info);
PaperLib.getChunkAtAsync(info.getLocation()).whenComplete((chunk, ex) -> {
info.getRegeneratingBlock().getBlock().regenerate(info);
info.getLocation().getBlock().getState().update();
if (!shutdown)
active.remove(info);