Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package timers;
- import java.util.ArrayList;
- import java.util.List;
- import org.bukkit.Chunk;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.World;
- import org.bukkit.block.Block;
- import org.bukkit.block.BlockFace;
- import org.bukkit.entity.Entity;
- import org.bukkit.entity.LivingEntity;
- import org.bukkit.entity.Monster;
- import org.bukkit.entity.Player;
- import blocks.BlockStatus;
- import enums.BlockData;
- import main.SevenDays;
- import main.SevenDaysListener;
- import main.sdData;
- import main.sdPlayer;
- public class TrapTimer implements Runnable {
- private SevenDays plugin;
- private Long delay = 0l;
- public TrapTimer(SevenDays sevenDays) {
- this.plugin = sevenDays;
- }
- @Override
- public void run() {
- sdData.manageInjuries(); //This timer Also handles player injuries, like broken legs.
- //We have ONE block we use only for barbed wire, this bit scans the entire world's loaded chunks for //barbed wire because I don't specifically save their locations, but it does them a few chunks at a time, //after a few passes its got all the loaded chunks scanned. You probably don't need this bit.
- if(System.currentTimeMillis() >= this.delay)
- {
- for(Chunk c:SevenDays.gameWorld.getLoadedChunks()) //iterate over all loaded chunks
- if(sdData.getCheckedChunks().contains(c))
- continue; //this chunk has already been cheked skip it.
- else if(sdData.getChunksToCheck().contains(c))
- continue; //this chunk hasn't been checked but is in the list.
- else
- sdData.getChunksToCheck().add(c); //a new chunk added to the list to check.
- int count = 0; //number of chunks cheked
- int count2 = 0; //number of traps found
- this.delay = System.currentTimeMillis() + 10000; //delay between scan passes, searching too many at once causes lag.
- int passLimit = 450; //number of chunks to check before taking a break.
- for(Chunk c:sdData.getChunksToCheck())
- {
- if(count >= passLimit)
- break;
- count2 = count2 + scanChunk(c);
- count++;
- sdData.getCheckedChunks().add(c);
- }
- //if(count > 0)
- //System.out.println("Scanned " + count + " of " + sdData.getChunksToCheck().size() + " loaded chunks for traps, found: " + count2 + " total chunks scanned: " + sdData.getCheckedChunks().size());
- sdData.getChunksToCheck().removeAll(sdData.getCheckedChunks());
- }
- //END CHUNK SCANNING
- //This bit of code checks the blocks where barbed wire *should be* like it was placed or picked up by
- //the scanner, it verifys that it is either a barbed wire block, or a barbed wire wrapped block.
- //if not it is removed from the list because a player/explosion etc broke it.
- if(sdData.getBarbedWire().size() > 0) {
- List<Block> removed = new ArrayList<Block>();
- //sData is just a class that holds all my lists/hash maps that need to be accessed from multiple classes
- // so they're in one place for easy access. You could use a hash map at the top of this class just as
- //easily.
- for(Block trap:sdData.getBarbedWire()) {
- if(trap.getType() != Material.WEB && (trap.getType() != Material.LOG_2
- && trap.getData()!= (byte) 0) && (trap.getType() != Material.LOG_2
- && trap.getData() != (byte) 1)) {
- removed.add(trap);
- continue;
- }
- double range = 0.5;
- if(trap.getType() == Material.LOG_2)
- {
- range = 1.25;
- }
- //This next part is what calculates the damage to the living entities, when theyre within range of the
- //barbed wire, you could just as easily trigger an explosion here. I apply the damage to each of the
- //entities that are within the distance requirement, you could check for team mates here, create just
- //an explosion effect (one that does zero damage) and manually apply the damage to the entities you
- //choose to.
- for(Entity e:trap.getWorld().getNearbyEntities(trap.getLocation(), range,range,range ))
- {
- if(!(e instanceof LivingEntity)) //barbed wire does not damage non living things.
- continue;
- LivingEntity le = (LivingEntity) e;
- //the timer itself has a 20 tick delay per run, so any living entity in range takes 1.5 damage per second //when in contact with the barbed wire.
- double damage = 1.5;
- le.damage(damage);
- //barbed wire damages monsters more than other living entities. so we up the damage.
- if(le instanceof Monster) {
- damage = 30d;
- damageBlock(trap,damage,le);
- }
- }
- if(!removed.isEmpty()) //if there were any invalid traps found, remove them from list.
- sdData.getBarbedWire().removeAll(removed);
- }
- }
- }
- public static int scanChunk(Chunk chunk) {
- //this block of code handles scanning the chunks for specific block types
- int count = 0;
- int bx = chunk.getX()<<4;
- int bz = chunk.getZ()<<4;
- World world = chunk.getWorld();
- if(world.getName().equalsIgnoreCase("7days"))
- {
- for(int xx = bx; xx < bx+16; xx++) {
- for(int zz = bz; zz < bz+16; zz++) {
- for(int yy = 0; yy < 128; yy++) {
- int typeId = world.getBlockTypeIdAt(xx, yy, zz);
- if(typeId == Material.WEB.getId() || typeId == Material.LOG_2.getId()) {
- if(!sdData.getBarbedWire().contains(world.getBlockAt(xx,yy,xx))) {
- sdData.getBarbedWire().add(world.getBlockAt(xx,yy,zz));
- count ++;
- }
- }
- }
- }
- }
- }
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement