use of me.botsko.prism.api.actions.Handler in project Prism-Bukkit by prism.
the class PrismMiscEvents method onPrismBlocksDrainEvent.
/**
* PrismDrainEvent.
*
* @param event PrismDrainEvent
*/
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPrismBlocksDrainEvent(final PrismDrainEvent event) {
// Get all block changes for this event
final ArrayList<BlockStateChange> blockStateChanges = event.getBlockStateChanges();
if (!blockStateChanges.isEmpty()) {
// Create an entry for the rollback as a whole
final Handler primaryAction = ActionFactory.createPrismProcess("prism-process", PrismProcessType.DRAIN, event.onBehalfOf(), "" + event.getRadius());
final long id = RecordingTask.insertActionIntoDatabase(primaryAction);
if (id == 0) {
return;
}
for (final BlockStateChange stateChange : blockStateChanges) {
final BlockState orig = stateChange.getOriginalBlock();
final BlockState newBlock = stateChange.getNewBlock();
// Build the action
RecordingQueue.addToQueue(ActionFactory.createPrismRollback("prism-drain", orig, newBlock, event.onBehalfOf(), id));
}
// ActionQueue.save();
}
}
use of me.botsko.prism.api.actions.Handler in project Prism-Bukkit by prism.
the class ActionFactory method createBlock.
/**
* Create Handler.
*
* @param actionType type
* @param block block
* @param nonPlayer not a player
* @return Handler
*/
public static Handler createBlock(String actionType, Block block, String nonPlayer) {
final Handler a = createBlock(actionType, block, (OfflinePlayer) null);
a.setSourceName(nonPlayer);
return a;
}
use of me.botsko.prism.api.actions.Handler in project Prism-Bukkit by prism.
the class ActionFactory method createGrow.
/**
* GrowHandler.
*
* @param actionType type
* @param blockstate state
* @param nonPlayer nonplayer
* @return Handler
*/
public static Handler createGrow(String actionType, BlockState blockstate, String nonPlayer) {
final Handler a = createGrow(actionType, blockstate, (OfflinePlayer) null);
a.setSourceName(nonPlayer);
return a;
}
use of me.botsko.prism.api.actions.Handler in project Prism-Bukkit by prism.
the class SqlSelectQueryBuilder method executeSelect.
@Override
public QueryResult executeSelect(TimeTaken eventTimer) {
final List<Handler> actions = new ArrayList<>();
// Build conditions based off final args
final String query = getQuery(parameters, shouldGroup);
eventTimer.recordTimedEvent("query started");
try (Connection conn = Prism.getPrismDataSource().getDataSource().getConnection();
PreparedStatement s = conn.prepareStatement(query);
ResultSet rs = s.executeQuery()) {
RecordingManager.failedDbConnectionCount = 0;
eventTimer.recordTimedEvent("query returned, building results");
Map<Integer, String> worldsInverse = new HashMap<>();
for (final Entry<String, Integer> entry : Prism.prismWorlds.entrySet()) {
worldsInverse.put(entry.getValue(), entry.getKey());
}
while (rs.next()) {
if (rs.getString(3) == null) {
continue;
}
// Convert action ID to name
// Performance-wise this is a lot faster than table joins
// and the cache data should always be available
int actionId = rs.getInt(3);
String actionName = "";
for (final Entry<String, Integer> entry : Prism.prismActions.entrySet()) {
if (entry.getValue() == actionId) {
actionName = entry.getKey();
}
}
if (actionName.isEmpty()) {
Prism.warn("Record contains action ID that doesn't exist in cache: " + actionId + ", cacheSize=" + Prism.prismActions.size());
continue;
}
// Get the action handler
final ActionTypeImpl actionType = Prism.getActionRegistry().getAction(actionName);
if (actionType == null) {
continue;
}
long rowId = 0;
try {
final Handler baseHandler = Prism.getHandlerRegistry().create(actionType.getHandler());
// Convert world ID to name
// Performance-wise this is typically a lot faster than
// table joins
rowId = rs.getLong(1);
// Set all shared values
baseHandler.setActionType(actionType);
baseHandler.setId(rowId);
baseHandler.setUnixEpoch(rs.getLong(2));
String worldName = worldsInverse.getOrDefault(rs.getInt(5), "");
baseHandler.setWorld(Bukkit.getWorld(worldName));
baseHandler.setX(rs.getInt(6));
baseHandler.setY(rs.getInt(7));
baseHandler.setZ(rs.getInt(8));
int blockId = rs.getInt(9);
int blockSubId = rs.getInt(10);
int oldBlockId = rs.getInt(11);
int oldBlockSubId = rs.getInt(12);
String extraData = rs.getString(13);
boolean validBlockId = false;
boolean validOldBlockId = false;
MaterialState current = Prism.getItems().idsToMaterial(blockId, blockSubId, false);
if (current != null) {
ItemStack item = current.asItem();
BlockData block = current.asBlockData();
if (block != null) {
validBlockId = true;
baseHandler.setMaterial(block.getMaterial());
baseHandler.setBlockData(block);
baseHandler.setDurability((short) 0);
} else if (item != null) {
validBlockId = true;
baseHandler.setMaterial(item.getType());
BlockData newData;
try {
newData = Bukkit.createBlockData(item.getType());
} catch (IllegalArgumentException e) {
// This exception occurs, for example, with "ItemStack{DIAMOND_LEGGINGS x 1}"
Prism.debug("IllegalArgumentException for record #" + rowId + " calling createBlockData for " + item.toString());
newData = null;
}
baseHandler.setBlockData(newData);
baseHandler.setDurability((short) ItemUtils.getItemDamage(item));
}
}
MaterialState old = Prism.getItems().idsToMaterial(oldBlockId, oldBlockSubId, false);
if (old != null) {
ItemStack oldItem = old.asItem();
BlockData oldBlock = old.asBlockData();
validOldBlockId = true;
if (oldBlock != null) {
baseHandler.setOldMaterial(oldBlock.getMaterial());
baseHandler.setOldBlockData(oldBlock);
baseHandler.setOldDurability((short) 0);
} else {
baseHandler.setOldMaterial(oldItem.getType());
baseHandler.setOldBlockData(Bukkit.createBlockData(oldItem.getType()));
baseHandler.setOldDurability((short) ItemUtils.getItemDamage(oldItem));
}
}
if (!validBlockId && !validOldBlockId) {
// Entry could not be converted to a block or an item
boolean logWarning;
// The current item is likely a spawn or death event for an entity, for example, a cow or horse
logWarning = blockId != 0 || oldBlockId != 0 || extraData == null || !extraData.contains("entity_name");
if (logWarning) {
String itemMetadataDesc;
if (extraData == null) {
itemMetadataDesc = "";
} else {
itemMetadataDesc = ", metadata=" + extraData;
}
if (blockId > 0) {
Prism.warn("Unable to convert record #" + rowId + " to material: " + "block_id=" + blockId + ", block_subid=" + blockSubId + itemMetadataDesc);
} else if (oldBlockId > 0) {
Prism.warn("Unable to convert record #" + rowId + " to material: " + "old_block_id=" + oldBlockId + ", old_block_subid=" + oldBlockSubId + itemMetadataDesc);
} else {
Prism.warn("Unable to convert record #" + rowId + " to material: " + "block_id=0, old_block_id=0" + itemMetadataDesc);
}
}
}
// data
try {
baseHandler.deserialize(extraData);
} catch (JsonSyntaxException e) {
if (Prism.isDebug()) {
Prism.warn("Deserialization Error: " + e.getLocalizedMessage(), e);
}
}
// player
baseHandler.setSourceName(rs.getString(4));
// player_uuid
try {
// Calls UUID.fromString, must handle potential exceptions
OfflinePlayer offline = Bukkit.getOfflinePlayer(SqlPlayerIdentificationHelper.uuidFromDbString(rs.getString(14)));
// Fake player
if (offline.hasPlayedBefore()) {
baseHandler.setUuid(offline.getUniqueId());
}
} catch (IllegalArgumentException | NullPointerException e) {
// Not a valid uuid
}
// Set aggregate counts if a lookup
int aggregated = 0;
if (shouldGroup) {
aggregated = rs.getInt(15);
}
baseHandler.setAggregateCount(aggregated);
actions.add(baseHandler);
} catch (final SQLException e) {
Prism.warn("Ignoring data from record #" + rowId + " because it caused an error:", e);
}
}
} catch (NullPointerException e) {
if (RecordingManager.failedDbConnectionCount == 0) {
Prism.log("Prism database error. Connection missing. Leaving actions to log in queue.");
Prism.debug(e.getMessage());
}
RecordingManager.failedDbConnectionCount++;
return new QueryResult(actions, parameters);
} catch (SQLException e) {
Prism.getPrismDataSource().handleDataSourceException(e);
}
return new QueryResult(actions, parameters);
}
use of me.botsko.prism.api.actions.Handler in project Prism-Bukkit by prism.
the class UndoCommand method handle.
@Override
public void handle(CallInfo call) {
final Audience audience = Prism.getAudiences().player(call.getPlayer());
if (call.getArgs().length > 1) {
final ActionsQuery aq = new ActionsQuery(plugin);
long recordId = 0;
if (TypeUtils.isNumeric(call.getArg(1))) {
recordId = Long.parseLong(call.getArg(1));
if (recordId <= 0) {
Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError("Record ID must be greater than zero."));
return;
}
} else {
if (call.getArg(1).equals("last")) {
recordId = aq.getUsersLastPrismProcessId(call.getPlayer().getName());
}
}
// Invalid id
if (recordId == 0) {
Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError("Either you have no last process or an invalid ID."));
return;
}
final PrismProcessAction process = aq.getPrismProcessRecord(recordId);
if (process == null) {
Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError("A process does not exists with that value."));
return;
}
// We only support this for drains
if (!process.getProcessChildActionType().equals("prism-drain")) {
Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError("You can't currently undo anything other than a drain process."));
return;
}
// Pull the actual block change data for this undo event
final QueryParameters parameters = new QueryParameters();
parameters.setWorld(call.getPlayer().getWorld().getName());
parameters.addActionType(process.getProcessChildActionType());
parameters.addPlayerName(call.getPlayer().getName());
parameters.setParentId(recordId);
parameters.setProcessType(PrismProcessType.UNDO);
// make sure the distance isn't too far away
final QueryResult results = aq.lookup(parameters, call.getPlayer());
if (!results.getActionResults().isEmpty()) {
Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerHeaderMsg(Il8nHelper.getMessage("command-undo-complete")));
final Previewable rb = new Undo(plugin, call.getPlayer(), results.getActionResults(), parameters, new PrismApplierCallback());
rb.apply();
} else {
Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError("Nothing found to undo. Must be a problem with Prism."));
}
} else {
// Show the list
// Process and validate all of the arguments
final QueryParameters parameters = new QueryParameters();
parameters.setAllowNoRadius(true);
parameters.addActionType("prism-process");
parameters.addPlayerName(call.getPlayer().getName());
// @todo config this, and move the logic
parameters.setLimit(5);
// to queryparams
final ActionsQuery aq = new ActionsQuery(plugin);
final QueryResult results = aq.lookup(parameters, call.getPlayer());
if (!results.getActionResults().isEmpty()) {
Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerHeaderMsg(Il8nHelper.formatMessage("lookup-header-message", results.getTotalResults(), 1, results.getTotalPages())));
Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerSubduedHeaderMsg(Il8nHelper.getMessage("command-undo-help")));
final List<Handler> paginated = results.getPaginatedActionResults();
if (paginated != null) {
for (final Handler a : paginated) {
final ActionMessage am = new ActionMessage(a);
if (parameters.hasFlag(Flag.EXTENDED) || plugin.getConfig().getBoolean("prism.messenger.always-show-extended")) {
am.showExtended();
}
Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerMsg(am.getMessage()));
}
} else {
Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError("Pagination can't find anything. Do you have the right page number?"));
}
} else {
Prism.messenger.sendMessage(call.getPlayer(), Prism.messenger.playerError("Nothing found." + ChatColor.GRAY + " Either you're missing something, or we are."));
}
}
}
Aggregations