-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
143 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/java/online/umbcraft/libraries/dupes/PistonDupe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package online.umbcraft.libraries.dupes; | ||
|
||
import online.umbcraft.libraries.GoldenDupes; | ||
import online.umbcraft.libraries.config.ConfigPath; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.Location; | ||
import org.bukkit.World; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.entity.ItemFrame; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.EventPriority; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.block.BlockPistonRetractEvent; | ||
import org.bukkit.event.entity.EntityDamageByEntityEvent; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.util.Vector; | ||
|
||
import java.util.*; | ||
|
||
public class PistonDupe extends Dupe implements Listener { | ||
|
||
// set of all item frames which qualify to have their items duped | ||
final private Set<UUID> dupableFrames = new HashSet<>(); | ||
|
||
public PistonDupe(final GoldenDupes plugin) { | ||
super(plugin); | ||
} | ||
|
||
// marks an item frame as dupable if it was recently exposed to the back of a retracting piston | ||
@EventHandler(priority = EventPriority.NORMAL) | ||
public void onPistonRetract(final BlockPistonRetractEvent e) { | ||
World world = e.getBlock().getWorld(); | ||
Vector pistonLoc = e.getBlock().getLocation().toVector(); | ||
|
||
int xoff = -1 * e.getDirection().getModX(); | ||
int yoff = -1 * e.getDirection().getModY(); | ||
int zoff = -1 * e.getDirection().getModZ(); | ||
|
||
// amount to offset the regular piston coords by | ||
Vector buttOffset = new Vector(0.5*xoff + 0.5,0.5*yoff + 0.5,0.5*zoff + 0.5); | ||
|
||
// center of the face of the butt of the piston | ||
Location pistonButtInWorld = pistonLoc.add(buttOffset).toLocation(world); | ||
|
||
// all entities pressed against the butt of the piston | ||
Collection<Entity> nearbyEntities = e.getBlock().getWorld().getNearbyEntities(pistonButtInWorld,0.05,0.05,0.05); | ||
|
||
// mark the itemframe as valid for duping for the next few ticks, specified by tickdelay in the config | ||
for(Entity frame : nearbyEntities) { | ||
if (!(frame instanceof ItemFrame)) | ||
continue; | ||
|
||
dupableFrames.add(frame.getUniqueId()); | ||
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> { | ||
dupableFrames.remove(frame.getUniqueId()); | ||
}, plugin.getConfig().getLong(ConfigPath.PISTON_TICKDELAY.path())); | ||
return; | ||
} | ||
} | ||
|
||
|
||
// dupes the item in an item frame as it is being broken (if it was marked as dupable) | ||
@EventHandler(priority = EventPriority.HIGH) | ||
public void onBreakFrame(EntityDamageByEntityEvent e) { | ||
|
||
// only continue if an itemframe is damaged | ||
if(!(e.getEntity() instanceof ItemFrame)) | ||
return; | ||
|
||
ItemFrame frame = (ItemFrame) e.getEntity(); | ||
|
||
// ignore frames that are not being duped from | ||
if(!dupableFrames.contains(frame.getUniqueId())) | ||
return; | ||
|
||
// only players can trigger the dupe if non-players is set to false | ||
if(!plugin.getConfig().getBoolean(ConfigPath.PISTON_NONPLAYER.path()) && !(e.getDamager() instanceof Player)) | ||
return; | ||
|
||
// dupe the item and spawn it along with the original item | ||
ItemStack duped = this.dupe(frame.getItem(),1); | ||
frame.getWorld().dropItemNaturally(frame.getLocation(), duped); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters