use of me.botsko.prism.appliers.ChangeResult in project Prism-Bukkit by prism.
the class BlockAction method placeBlock.
/**
* Place a block unless something other than air occupies the spot, or if we
* detect a falling block now sits there. This resolves the issue of falling
* blocks taking up the space, preventing this rollback. However, it also
* means that a rollback *could* interfere with a player-placed block.
*/
protected ChangeResult placeBlock(Player player, QueryParameters parameters, boolean is_preview, Block block, boolean is_deferred) {
final Material m = Material.getMaterial(getBlockId());
BlockStateChange stateChange;
// (essentially liquid/air).
if (!getType().requiresHandler("BlockChangeAction") && !getType().requiresHandler("PrismRollbackAction")) {
if (!me.botsko.elixr.BlockUtils.isAcceptableForBlockPlace(block.getType()) && !parameters.hasFlag(Flag.OVERWRITE)) {
// System.out.print("Block skipped due to being unaccaptable for block place.");
return new ChangeResult(ChangeResultType.SKIPPED, null);
}
}
// On the blacklist (except an undo)
if (Prism.getIllegalBlocks().contains(getBlockId()) && !parameters.getProcessType().equals(PrismProcessType.UNDO)) {
// System.out.print("Block skipped because it's not allowed to be placed.");
return new ChangeResult(ChangeResultType.SKIPPED, null);
}
// If we're not in a preview, actually apply this block
if (!is_preview) {
// Capture the block before we change it
final BlockState originalBlock = block.getState();
// it's set to stationary water so the lilypad will sit
if (getBlockId() == 111) {
final Block below = block.getRelative(BlockFace.DOWN);
if (below.getType().equals(Material.WATER) || below.getType().equals(Material.AIR) || below.getType().equals(Material.STATIONARY_WATER)) {
below.setType(Material.STATIONARY_WATER);
} else {
// Prism.debug("Lilypad skipped because no water exists below.");
return new ChangeResult(ChangeResultType.SKIPPED, null);
}
}
// If portal, we need to light the portal. seems to be the only way.
if (getBlockId() == 90) {
final Block obsidian = me.botsko.elixr.BlockUtils.getFirstBlockOfMaterialBelow(Material.OBSIDIAN, block.getLocation());
if (obsidian != null) {
final Block above = obsidian.getRelative(BlockFace.UP);
if (!(above.getType() == Material.PORTAL)) {
above.setType(Material.FIRE);
return new ChangeResult(ChangeResultType.APPLIED, null);
}
}
}
// it becomes unplayable
if (getBlockId() == 84) {
block_subid = 0;
}
// Set the material
block.setTypeId(getBlockId());
block.setData((byte) getBlockSubId());
/**
* Skulls
*/
if ((getBlockId() == 144 || getBlockId() == 397) && getActionData() instanceof SkullActionData) {
final SkullActionData s = (SkullActionData) getActionData();
// Set skull data
final Skull skull = (Skull) block.getState();
skull.setRotation(s.getRotation());
skull.setSkullType(s.getSkullType());
if (!s.owner.isEmpty()) {
skull.setOwner(s.owner);
}
skull.update();
}
/**
* Spawner
*/
if (getBlockId() == 52) {
final SpawnerActionData s = (SpawnerActionData) getActionData();
// Set spawner data
final CreatureSpawner spawner = (CreatureSpawner) block.getState();
spawner.setDelay(s.getDelay());
spawner.setSpawnedType(s.getEntityType());
spawner.update();
}
/**
* Restoring command block
*/
if (getBlockId() == 137) {
final CommandBlock cmdblock = (CommandBlock) block.getState();
cmdblock.setCommand(data);
cmdblock.update();
}
/**
* Signs
*/
if (parameters.getProcessType().equals(PrismProcessType.ROLLBACK) && (getBlockId() == 63 || getBlockId() == 68) && getActionData() instanceof SignActionData) {
final SignActionData s = (SignActionData) getActionData();
// https://snowy-evening.com/botsko/prism/455/
if (block.getState() instanceof Sign) {
// Set sign data
final Sign sign = (Sign) block.getState();
int i = 0;
if (s.lines != null && s.lines.length > 0) {
for (final String line : s.lines) {
sign.setLine(i, line);
i++;
}
}
sign.update();
}
}
// logic to use materials.
if (me.botsko.elixr.BlockUtils.materialRequiresSoil(block.getType())) {
final Block below = block.getRelative(BlockFace.DOWN);
if (below.getType().equals(Material.DIRT) || below.getType().equals(Material.AIR) || below.getType().equals(Material.GRASS)) {
below.setType(Material.SOIL);
} else {
// System.out.print("Block skipped because there's no soil below.");
return new ChangeResult(ChangeResultType.SKIPPED, null);
}
}
// Capture the new state
final BlockState newBlock = block.getState();
// Store the state change
stateChange = new BlockStateChange(originalBlock, newBlock);
// If we're rolling back a door, we need to set it properly
if (BlockUtils.isDoor(m)) {
BlockUtils.properlySetDoor(block, getBlockId(), (byte) getBlockSubId());
} else // Or a bed
if (m.equals(Material.BED_BLOCK)) {
BlockUtils.properlySetBed(block, getBlockId(), (byte) getBlockSubId());
} else // Or double plants
if (m.equals(Material.DOUBLE_PLANT)) {
BlockUtils.properlySetDoublePlant(block, getBlockId(), (byte) getBlockSubId());
}
} else {
// Otherwise, save the state so we can cancel if needed
final BlockState originalBlock = block.getState();
// Note: we save the original state as both old/new so we can re-use
// blockStateChanges
stateChange = new BlockStateChange(originalBlock, originalBlock);
// Preview it
player.sendBlockChange(block.getLocation(), getBlockId(), (byte) getBlockSubId());
// Send preview to shared players
for (final CommandSender sharedPlayer : parameters.getSharedPlayers()) {
if (sharedPlayer instanceof Player) {
((Player) sharedPlayer).sendBlockChange(block.getLocation(), getBlockId(), (byte) getBlockSubId());
}
}
}
return new ChangeResult(ChangeResultType.APPLIED, stateChange);
}
use of me.botsko.prism.appliers.ChangeResult in project Prism-Bukkit by prism.
the class BlockAction method removeBlock.
/**
*
*/
protected ChangeResult removeBlock(Player player, QueryParameters parameters, boolean is_preview, Block block) {
BlockStateChange stateChange;
if (!block.getType().equals(Material.AIR)) {
// Ensure it's acceptable to remove the current block
if (!me.botsko.elixr.BlockUtils.isAcceptableForBlockPlace(block.getType()) && !me.botsko.elixr.BlockUtils.areBlockIdsSameCoreItem(block.getTypeId(), getBlockId()) && !parameters.hasFlag(Flag.OVERWRITE)) {
return new ChangeResult(ChangeResultType.SKIPPED, null);
}
if (!is_preview) {
// Capture the block before we change it
final BlockState originalBlock = block.getState();
// Set
block.setType(Material.AIR);
// Capture the new state
final BlockState newBlock = block.getState();
// Store the state change
stateChange = new BlockStateChange(originalBlock, newBlock);
} else {
// Otherwise, save the state so we can cancel if needed
final BlockState originalBlock = block.getState();
// Note: we save the original state as both old/new so we can
// re-use blockStateChanges
stateChange = new BlockStateChange(originalBlock, originalBlock);
// Preview it
player.sendBlockChange(block.getLocation(), Material.AIR, (byte) 0);
// Send preview to shared players
for (final CommandSender sharedPlayer : parameters.getSharedPlayers()) {
if (sharedPlayer instanceof Player) {
((Player) sharedPlayer).sendBlockChange(block.getLocation(), getBlockId(), (byte) getBlockSubId());
}
}
}
return new ChangeResult(ChangeResultType.APPLIED, stateChange);
}
return new ChangeResult(ChangeResultType.SKIPPED, null);
}
use of me.botsko.prism.appliers.ChangeResult in project Prism-Bukkit by prism.
the class BlockChangeAction method placeBlock.
/**
*
* @param type
* @param old_id
* @param old_subid
* @param new_id
* @param new_subid
* @param block
* @param is_deferred
* @return
*/
protected ChangeResult placeBlock(Player player, QueryParameters parameters, boolean is_preview, String type, int old_id, int old_subid, int new_id, int new_subid, Block block, boolean is_deferred) {
final BlockAction b = new BlockAction();
b.setActionType(type);
b.setPlugin(plugin);
b.setWorldName(getWorldName());
b.setX(getX());
b.setY(getY());
b.setZ(getZ());
if (parameters.getProcessType().equals(PrismProcessType.ROLLBACK)) {
// and https://snowy-evening.com/botsko/prism/258/
if (me.botsko.elixr.BlockUtils.isAcceptableForBlockPlace(block.getType()) || me.botsko.elixr.BlockUtils.areBlockIdsSameCoreItem(block.getTypeId(), new_id) || is_preview || parameters.hasFlag(Flag.OVERWRITE)) {
b.setBlockId(old_id);
b.setBlockSubId(old_subid);
return b.placeBlock(player, parameters, is_preview, block, is_deferred);
} else {
// + block.getTypeId() + " vs " + new_id);
return new ChangeResult(ChangeResultType.SKIPPED, null);
}
} else if (parameters.getProcessType().equals(PrismProcessType.RESTORE)) {
// and https://snowy-evening.com/botsko/prism/258/
if (me.botsko.elixr.BlockUtils.isAcceptableForBlockPlace(block.getType()) || me.botsko.elixr.BlockUtils.areBlockIdsSameCoreItem(block.getTypeId(), old_id) || is_preview || parameters.hasFlag(Flag.OVERWRITE)) {
b.setBlockId(new_id);
b.setBlockSubId(new_subid);
return b.placeBlock(player, parameters, is_preview, block, is_deferred);
} else {
// + block.getTypeId() + " vs " + old_id);
return new ChangeResult(ChangeResultType.SKIPPED, null);
}
}
if (parameters.getProcessType().equals(PrismProcessType.UNDO)) {
b.setBlockId(old_id);
b.setBlockSubId(old_subid);
return b.placeBlock(player, parameters, is_preview, block, is_deferred);
}
return new ChangeResult(ChangeResultType.SKIPPED, null);
}
use of me.botsko.prism.appliers.ChangeResult in project Prism-Bukkit by prism.
the class EntityAction method applyRollback.
/**
*
*/
@Override
public ChangeResult applyRollback(Player player, QueryParameters parameters, boolean is_preview) {
if (getEntityType() == null) {
return new ChangeResult(ChangeResultType.SKIPPED, null);
}
if (Prism.getIllegalEntities().contains(getEntityType().name().toLowerCase())) {
return new ChangeResult(ChangeResultType.SKIPPED, null);
}
if (!is_preview) {
final Location loc = getLoc();
loc.setX(loc.getX() + 0.5);
loc.setZ(loc.getZ() + 0.5);
final Entity entity = loc.getWorld().spawnEntity(loc, getEntityType());
// Get custom name
if (entity instanceof LivingEntity && getCustomName() != null) {
final LivingEntity namedEntity = (LivingEntity) entity;
namedEntity.setCustomName(getCustomName());
}
// Get animal age
if (entity instanceof Ageable) {
final Ageable age = (Ageable) entity;
if (!isAdult()) {
age.setBaby();
} else {
age.setAdult();
}
}
// Set sheep color
if (entity.getType().equals(EntityType.SHEEP) && getColor() != null) {
final Sheep sheep = ((Sheep) entity);
sheep.setColor(getColor());
}
// Set villager profession
if (entity instanceof Villager && getProfession() != null) {
final Villager v = (Villager) entity;
v.setProfession(getProfession());
}
// Set wolf details
if (entity instanceof Wolf) {
// Owner
final Wolf wolf = (Wolf) entity;
final String tamingOwner = getTamingOwner();
if (tamingOwner != null) {
Player owner = plugin.getServer().getPlayer(tamingOwner);
if (owner == null) {
final OfflinePlayer offlinePlayer = plugin.getServer().getOfflinePlayer(tamingOwner);
if (offlinePlayer.hasPlayedBefore()) {
owner = offlinePlayer.getPlayer();
}
}
if (owner != null)
wolf.setOwner(owner);
}
// Collar color
if (getColor() != null) {
wolf.setCollarColor(getColor());
}
if (isSitting()) {
wolf.setSitting(true);
}
}
// Set ocelot details
if (entity instanceof Ocelot) {
// Owner
final Ocelot ocelot = (Ocelot) entity;
final String tamingOwner = getTamingOwner();
if (tamingOwner != null) {
Player owner = plugin.getServer().getPlayer(tamingOwner);
if (owner == null) {
final OfflinePlayer offlinePlayer = plugin.getServer().getOfflinePlayer(tamingOwner);
if (offlinePlayer.hasPlayedBefore()) {
owner = offlinePlayer.getPlayer();
}
}
if (owner != null) {
ocelot.setOwner(owner);
}
}
// Cat type
if (getCatType() != null) {
ocelot.setCatType(getCatType());
}
// Sitting
if (isSitting()) {
ocelot.setSitting(true);
}
}
// Set horse details
if (entity instanceof Horse) {
final Horse h = (Horse) entity;
if (getVariant() != null) {
h.setVariant(getVariant());
}
if (getHorseColor() != null) {
h.setColor(getHorseColor());
}
if (getStyle() != null) {
h.setStyle(getStyle());
}
h.setCarryingChest(this.actionData.chest);
h.setDomestication(this.actionData.dom);
h.setMaxDomestication(this.actionData.maxDom);
h.setJumpStrength(this.actionData.jump);
h.setMaxHealth(this.actionData.maxHealth);
// Stuff
h.getInventory().setSaddle(getSaddle());
h.getInventory().setArmor(getArmor());
// Owner
final String tamingOwner = getTamingOwner();
if (tamingOwner != null) {
Player owner = plugin.getServer().getPlayer(tamingOwner);
if (owner == null) {
final OfflinePlayer offlinePlayer = plugin.getServer().getOfflinePlayer(tamingOwner);
if (offlinePlayer.hasPlayedBefore()) {
owner = offlinePlayer.getPlayer();
}
}
if (owner != null)
h.setOwner(owner);
}
}
return new ChangeResult(ChangeResultType.APPLIED, null);
}
return new ChangeResult(ChangeResultType.PLANNED, null);
}
use of me.botsko.prism.appliers.ChangeResult in project Prism-Bukkit by prism.
the class SignAction method applyRestore.
/**
*
*/
@Override
public ChangeResult applyRestore(Player player, QueryParameters parameters, boolean is_preview) {
final Block block = getWorld().getBlockAt(getLoc());
// Ensure a sign exists there (and no other block)
if (block.getType().equals(Material.AIR) || block.getType().equals(Material.SIGN_POST) || block.getType().equals(Material.SIGN) || block.getType().equals(Material.WALL_SIGN)) {
if (block.getType().equals(Material.AIR)) {
block.setType(getSignType());
}
// Set the facing direction
if (block.getState().getData() instanceof org.bukkit.material.Sign) {
final org.bukkit.material.Sign s = (org.bukkit.material.Sign) block.getState().getData();
s.setFacingDirection(getFacing());
}
// Set the content
if (block.getState() instanceof org.bukkit.block.Sign) {
// Set sign data
final String[] lines = getLines();
final org.bukkit.block.Sign sign = (org.bukkit.block.Sign) block.getState();
int i = 0;
if (lines != null && lines.length > 0) {
for (final String line : lines) {
sign.setLine(i, line);
i++;
}
}
sign.update();
return new ChangeResult(ChangeResultType.APPLIED, null);
}
}
return new ChangeResult(ChangeResultType.SKIPPED, null);
}
Aggregations