Clearlag
This script removes all items from world every 30 minutes. Only works in 1.12.
// Create item whitelist filter that won't be deleted with clearlag
var whitelist = ingredient.Ingredient.matchAny([
'minecraft:diamond', // Adds diamond to whitelist
'minecraft:gold_ingot',
'@tinkersconstruct', // Adds all items from tinkerscontruct to whitelist
'minecraft:emerald'
])
// Create variable for last clearlag result
var lastClearLagResult = utils.Utils.newList()
// Create variable for total number of items
var lastTotalClearLagResult = utils.Utils.newCountingMap()
// Create new function that clears lag
functionvar clearLag (server)= server => {
// Get a list of all entities on server with filter that only returns items
var itemList = server.getEntities('@e[type=item]')
// Create new local map for item counters
var lastResult = utils.Utils.newCountingMap()
// Clear old result lists
lastClearLagResult.clear()
lastTotalClearLagResult.clear()
// Iterate over each entity in itemList and add item counters
itemList.forEach(functionentity (entity)=> {
if (!whitelist.test(entity.item)) {
// Get the name of item
var key = entity.item.name
// Add to entity count
lastResult.add(key, 1)
// Add to total item count
lastTotalClearLagResult.add(key, entity.item.count)
// Kill the item entity
entity.kill()
}
})
// Update and sort last result list
lastClearLagResult.addAll(lastResult.entries)
lastClearLagResult.sort(null)
// Tell everyone how many items will be removed
server.tell([
text.Text.lightPurple('[ClearLag]'),
' Removed ',
lastTotalClearLagResult.totalCount,
' items. ',
text.Text.yellow('Click here').click('command:/clearlagresults'),
' for results.'
])
}
// Listen for server load event
events.listen('server.load', functionevent (event)=> {
// Log message in console
event.server.tell([ text.Text.lightPurple('[ClearLag]'), ' Timer started, clearing lag in 30 minutes!' ])
// Schedule new task in 30 minutes
event.server.schedule(MINUTE * 30, event.server, functioncallback (callback)=> {
// Tell everyone on server that items will be removed
callback.data.tell([ text.Text.lightPurple('[ClearLag]'), ' Removing all items on ground in one minute!' ])
// Schedule a subtask that will clear items in one minute
callback.data.schedule(MINUTE, callback.data, functioncallback2 (callback2)=> {
clearLag(callback2.data)
})
// Re-schedule this task for another 30 minutes (endless loop)
callback.reschedule()
})
})
// Doesnt work in 1.16+!
// Register commands
events.listen('command.registry', functionevent (event)=> {
// Register new OP command /clearlag, that instantly runs clearlag
event
.create('clearlag')
.op()
.execute(function (sender, args) {
clearLag(sender.server)
})
.add()
// Register new non-OP command /clearlagresults, that displays stats of all removed items from previous /clearlag
event
.create('clearlagresults')
.execute(function (sender, args) => {
sender.tell([ text.Text.lightPurple('[ClearLag]'), ' Last clearlag results:' ])
lastClearLagResult.forEach(functionentry (entry)=> {
var total = lastTotalClearLagResult.get(entry.key)
if (entry.value == total) {
sender.tell([ text.Text.gold(entry.key), ': ', text.Text.red(entry.value) ])
} else {
sender.tell([ text.Text.gold(entry.key), ': ', text.Text.red(entry.value), ' entities, ', text.Text.red(total), ' total' ])
}
})
})
.add()
})