Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/me/profelements/dynatech/DynaTech.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import me.profelements.dynatech.listeners.PicnicBasketListener;
import me.profelements.dynatech.setup.DynaTechItemsSetup;
import me.profelements.dynatech.tasks.ItemBandTask;
import me.profelements.dynatech.tasks.AntigravityBubbleTask;
import org.apache.commons.lang.Validate;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -68,6 +69,12 @@ public void onEnable() {
getLogger().log(Level.WARNING, " DynaTech will be switching to JAVA 11 ");
getLogger().log(Level.WARNING, " Please Update to JAVA 11 ");
}

Bukkit.getScheduler().scheduleSyncRepeatingTask(DynaTech.getInstance(), new Runnable() {
public void run() {
AntigravityBubbleTask.run();
}
}, 0, 60);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker;
import me.profelements.dynatech.items.electric.abstracts.AMachine;
import org.bukkit.Bukkit;
import me.profelements.dynatech.tasks.AntigravityBubbleTask;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
Expand All @@ -19,16 +20,11 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.UUID;

public class AntigravityBubble extends AMachine {

private final Set<UUID> enabledPlayers = new HashSet<>();

private static Location bubbleLocation;
private static final int[] BORDER = new int[] { 1, 2, 6, 7, 9, 10, 11, 15, 16, 17, 19, 20, 24, 25 };
private static final int[] BORDER_IN = new int[] { 3, 4, 5, 12, 14, 21, 22, 23 };
private static final int[] BORDER_OUT = new int[] { 0, 8, 18, 26 };
Expand Down Expand Up @@ -57,50 +53,29 @@ public boolean isSynchronized() {

@Override
public void tick(Block b) {
Collection<Entity> bubbledEntities = b.getWorld().getNearbyEntities(b.getLocation(), 25, 25, 25);
bubbleLocation = b.getLocation();
AntigravityBubbleTask.addBubble(bubbleLocation);

Collection<Entity> bubbledEntities = b.getWorld().getNearbyEntities(b.getLocation(), 25, 25, 25);

for (Entity entity : bubbledEntities) {
if (entity instanceof Player) {
Player p = (Player) entity;

if (!p.getAllowFlight()) {
enabledPlayers.add(p.getUniqueId());
p.setAllowFlight(true);
removeCharge(b.getLocation(), getEnergyConsumption());
}
}
}

final Iterator<UUID> playerIterator = enabledPlayers.iterator();
while (playerIterator.hasNext()) {
final UUID uuid = playerIterator.next();
Player p = Bukkit.getPlayer(uuid);

if (p != null && !bubbledEntities.contains(p)) {
p.setAllowFlight(false);
p.setFlying(false);
p.setFallDistance(0.0f);
playerIterator.remove();
}
}
}

private ItemHandler onBlockBreak() {
AntigravityBubbleTask.removeBubble(bubbleLocation);
return new BlockBreakHandler(false, false) {

@Override
public void onPlayerBreak(BlockBreakEvent e, ItemStack tool, List<ItemStack> drops) {
final Iterator<UUID> playerIterator = enabledPlayers.iterator();
while (playerIterator.hasNext()) {
final UUID uuid = playerIterator.next();
Player p = Bukkit.getPlayer(uuid);
if (p != null) {
p.setAllowFlight(false);
p.setFlying(false);
p.setFallDistance(0.0F);
playerIterator.remove();
}
}

AntigravityBubbleTask.removeBubble(e.getBlock().getLocation());
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package me.profelements.dynatech.tasks;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;


import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;


/**
* @author Fhoz https://github.com/Fhoz
*/
public class AntigravityBubbleTask {
static Set<Location> bubbleLocations = new HashSet<>();
static Set<UUID> bubbledPlayers = new HashSet<>();
static Set<UUID> playerLog = new HashSet<>();
public static void run() {
for (Location bubble : bubbleLocations) {
Collection<Entity> bubbledEntities = bubble.getWorld().getNearbyEntities(bubble, 25, 25, 25);
for (Entity entity : bubbledEntities) {
if (entity instanceof Player) {
Player p = (Player) entity;
UUID uuid = p.getUniqueId();
playerLog.add(uuid);
if (!p.getAllowFlight()) {
bubbledPlayers.add(uuid);
p.setAllowFlight(true);
}
}
}
}
if (bubbledPlayers != null) {
for (UUID uuid : bubbledPlayers) {
Player p = Bukkit.getPlayer(uuid);
if (p != null && !playerLog.contains(uuid)) {
p.setAllowFlight(false);
p.setFlying(false);
p.setFallDistance(0.0f);
bubbledPlayers.remove(uuid);
}
}
}

playerLog.clear();
}


public static void addBubble(Location blockLocation) {
bubbleLocations.add(blockLocation);
}
public static void removeBubble(Location blockLocation) {
bubbleLocations.remove(blockLocation);
}
}