Skip to content

Instantly share code, notes, and snippets.

@kBULOSU
Created June 11, 2022 17:51
Show Gist options
  • Save kBULOSU/89dccee65880e4832d0a8cdb64d69508 to your computer and use it in GitHub Desktop.
Save kBULOSU/89dccee65880e4832d0a8cdb64d69508 to your computer and use it in GitHub Desktop.
Forma simples de resetar lentamente uma área de blocos.
package com.drazyh.mines;
import com.drazyh.common.spigot.SpigotConstants;
import com.drazyh.common.spigot.location.SerializedLocation;
import com.drazyh.common.spigot.misc.utils.ProbabilityCollection;
import com.drazyh.common.spigot.misc.utils.WorldCuboid;
import com.drazyh.mines.api.MineOre;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayDeque;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;
@RequiredArgsConstructor
@Getter
@EqualsAndHashCode(of = "id")
public class Mine {
private final int id;
private final String displayName;
@Setter
private SerializedLocation joinLocation;
@Setter
private WorldCuboid cuboid;
@Setter
private long delayInMills;
private Long lastReseted;
@Setter
private ProbabilityCollection<MineOre> ores;
public boolean canReset() {
if (lastReseted == null) {
return true;
}
long diff = System.currentTimeMillis() - lastReseted;
return diff >= delayInMills;
}
public void reset() {
if (cuboid == null) {
System.out.println("Cuboid nulo. ID: " + this.id);
MineConstants.CURRENT_RESETING_MINE_ID.set(0);
return;
}
if (this.ores.isEmpty()) {
System.out.println("Sem ores. ID: " + this.id);
MineConstants.CURRENT_RESETING_MINE_ID.set(0);
return;
}
List<Entity> entities = Bukkit.getWorld(cuboid.getWorldName()).getEntities().stream()
.filter(entity -> entity.getType() == EntityType.PLAYER)
.filter(entity -> cuboid.contains(entity.getLocation(), true))
.collect(Collectors.toList());
if (!entities.isEmpty()) {
Bukkit.getScheduler().runTask(MinesPlugin.INSTANCE, () -> teleportEntities(entities));
}
int blocks0 = cuboid.getBlocks0();
final Queue<Block> blocks = new ArrayDeque<>(blocks0);
computeBlocks(blocks);
processBlocks(blocks, () -> {
System.out.println("Finalizei de resetar mina '" + getDisplayName() + "'.");
lastReseted = System.currentTimeMillis();
MineConstants.CURRENT_RESETING_MINE_ID.set(0);
blocks.clear();
});
}
private void computeBlocks(Queue<Block> blocks) {
boolean onlyAir = lastReseted != null;
if (onlyAir) {
System.out.println("Resetando onlyAir na mina " + getId());
}
cuboid.getTopToBottomBlocks(blocks::add, onlyAir);
}
private void processBlocks(Queue<Block> blocks, Runnable notifyRunnable) {
new BukkitRunnable() {
@Override
public void run() {
for (int i = 0; i < 180; i++) {
Block poll = blocks.poll();
if (poll == null) {
this.cancel();
notifyRunnable.run();
break;
}
poll.setType(ores.get().getMaterial());
}
}
}.runTaskTimer(MinesPlugin.INSTANCE, 1L, 10L);
}
private void teleportEntities(List<Entity> entities) {
Location parser = getJoinLocation().parser(SpigotConstants.LOCATION_PARSER);
for (Entity entity : entities) {
if (entity.getType() != EntityType.PLAYER) {
continue;
}
entity.teleport(parser, PlayerTeleportEvent.TeleportCause.UNKNOWN);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment