!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; package net.Indyuce.mmocore.api.block;
import org.bukkit.Location;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import net.Indyuce.mmocore.api.block.BlockInfo.RegeneratingBlock; import net.Indyuce.mmocore.api.block.BlockInfo.RegeneratingBlock;
public interface BlockType { 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, * Generates a key used to store the BlockInfo instance in the manager map,
@ -16,9 +23,9 @@ public interface BlockType {
String generateKey(); String generateKey();
/** /**
* GenerateKey() determines if the block is handled by that block type, * Applies some extra break restrictions; returns TRUE if the block can be
* breakRestrictions(Block) applies some extra break restrictions; returns * broken. This method is used to prevent non mature crops from being broken
* TRUE if the block can be broken * for example
*/ */
boolean breakRestrictions(Block block); boolean breakRestrictions(Block block);
} }

View File

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

View File

@ -1,6 +1,5 @@
package net.Indyuce.mmocore.api.block; package net.Indyuce.mmocore.api.block;
import net.mmogroup.mmolib.MMOLib;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
@ -15,8 +14,8 @@ public class VanillaBlockType implements BlockType {
private final Material type; private final Material type;
/* /*
* allows to plant back crops with a custom age so that it does not always have * allows to plant back crops with a custom age so that it does not always
* to full grow again * have to full grow again
*/ */
private final int age; private final int age;
@ -39,10 +38,11 @@ public class VanillaBlockType implements BlockType {
} }
@Override @Override
public void place(Location loc, RegeneratingBlock block) { public void place(RegeneratingBlock block) {
loc.getBlock().setType(type); 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) { if (age > 0 && state instanceof Ageable) {
((Ageable) state).setAge(age); ((Ageable) state).setAge(age);
loc.getBlock().setBlockData(state); loc.getBlock().setBlockData(state);
@ -50,7 +50,9 @@ public class VanillaBlockType implements BlockType {
} }
@Override @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) // Sets the original blocks old data (only when regenerating)
loc.getBlock().setBlockData(block.getBlockData()); loc.getBlock().setBlockData(block.getBlockData());
} }
@ -62,7 +64,6 @@ public class VanillaBlockType implements BlockType {
@Override @Override
public boolean breakRestrictions(Block block) { public boolean breakRestrictions(Block block) {
return age == 0 return age == 0 || (block.getBlockData() instanceof Ageable && ((Ageable) block.getBlockData()).getAge() >= age);
|| (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()) 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 * This prevents any issue when editing lists being iterated
*/ */
private void regen(RegeneratingBlock info, boolean shutdown) { private void regen(RegeneratingBlock info, boolean shutdown) {
Location infoLocation = info.getLocation();
// Get the chunk and load it async if needed. // Get the chunk and load it async if needed.
PaperLib.getChunkAtAsync(infoLocation).whenComplete((chunk, ex) -> { PaperLib.getChunkAtAsync(info.getLocation()).whenComplete((chunk, ex) -> {
info.getRegeneratingBlock().getBlock().place(infoLocation, info); info.getRegeneratingBlock().getBlock().regenerate(info);
info.getRegeneratingBlock().getBlock().regen(infoLocation, info);
info.getLocation().getBlock().getState().update(); info.getLocation().getBlock().getState().update();
if (!shutdown) if (!shutdown)
active.remove(info); active.remove(info);