Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Commit

Permalink
Added new target function
Browse files Browse the repository at this point in the history
  • Loading branch information
RypoFalem committed Nov 28, 2016
1 parent c72858c commit 6e28c81
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 24 deletions.
5 changes: 4 additions & 1 deletion resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ requireToolLore: false
toolLore: Let's get dangerous

#Name of the language file you wish to use
lang: en_US.yml
lang: en_US.yml

#Don't set to true unless you want players to see random messages or other undesirable behavior
debug: false
7 changes: 4 additions & 3 deletions resources/lang/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ setmode:
baseplate: Toggle BasePlate
placement: Placement
rotate: Rotate
target: Target
copy: Copy
paste: Paste
reset: Reset Pose
Expand All @@ -54,6 +53,10 @@ copied:
msg: ArmorStand state copied to slot <x>.
pasted:
msg: ArmorStand state pasted from slot <x>.
target:
msg: ArmorStand target locked.
notarget:
msg: ArmorStand target unlocked.

#warn
cantedit:
Expand Down Expand Up @@ -153,8 +156,6 @@ rotate:
msg: Rotate
description:
msg: Rotate the entire armorstand
target:
msg: Target
description:
msg: Coming soon!

This comment has been minimized.

Copy link
@AmauryCarrade

AmauryCarrade Nov 28, 2016

Contributor

These two lines were forgotten? I suppose they should be removed...

copy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void onEnable(){
if(requireToolData) editToolData = getConfig().getInt("toolData");
requireToolLore = getConfig().getBoolean("requireToolLore");
if(requireToolLore) editToolLore= getConfig().getString("toolLore");
debug = getConfig().getBoolean("debug");

editorManager = new PlayerEditorManager(this);
execute = new CommandEx(this);
Expand Down
64 changes: 51 additions & 13 deletions src/io/github/rypofalem/armorstandeditor/PlayerEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import io.github.rypofalem.armorstandeditor.modes.CopySlots;
import io.github.rypofalem.armorstandeditor.modes.EditMode;

import java.util.ArrayList;
import java.util.UUID;

import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.util.EulerAngle;

public class PlayerEditor {
Expand All @@ -28,6 +31,8 @@ public class PlayerEditor {
double movChange;
Menu chestMenu;
ArmorStand target;
ArrayList<ArmorStand> targetList = null;
int targetIndex = 0;
EquipmentMenu equipMenu;
long lastCancelled = 0;

Expand Down Expand Up @@ -73,6 +78,7 @@ public void setCopySlot(byte slot){
}

public void editArmorStand(ArmorStand armorStand) {
armorStand = attemptTarget(armorStand);
switch(eMode){
case LEFTARM: armorStand.setLeftArmPose(subEulerAngle(armorStand.getLeftArmPose()));
break;
Expand Down Expand Up @@ -129,6 +135,7 @@ private void openEquipment(ArmorStand armorStand) {
}

public void reverseEditArmorStand(ArmorStand armorStand){
armorStand = attemptTarget(armorStand);
switch(eMode){
case LEFTARM: armorStand.setLeftArmPose(addEulerAngle(armorStand.getLeftArmPose()));
break;
Expand Down Expand Up @@ -285,15 +292,57 @@ private EulerAngle subEulerAngle(EulerAngle angle) {
return angle;
}

public void setTarget(ArmorStand armorstand){
this.target = armorstand;
public void setTarget(ArrayList<ArmorStand> armorStands){
if(armorStands == null || armorStands.isEmpty()){
target = null;
targetList = null;
sendMessage("notarget", null);
return;
}

if(targetList == null){
targetList = armorStands;
targetIndex = 0;
sendMessage("target", null);
} else{
boolean same = targetList.size() == armorStands.size();
if(same) for(ArmorStand as : armorStands){
same = targetList.contains(as);
if(!same) break;
}

if(same){
targetIndex = ++targetIndex % targetList.size();
}else{
targetList = armorStands;
targetIndex = 0;
sendMessage("target", null);
}
}
plugin.print(targetIndex + "");
target = targetList.get(targetIndex);
glow(target);
}

ArmorStand attemptTarget(ArmorStand armorStand){
if(target == null) return armorStand;
if(target.getWorld() != getPlayer().getWorld()) return armorStand;
if(target.getLocation().distanceSquared(getPlayer().getLocation()) > 100) return armorStand;
armorStand = target;
glow(armorStand);
return armorStand;
}

void sendMessage(String path, String option){
String message = plugin.getLang().getMessage(path, "info", option);
plugin.getServer().getPlayer(getUUID()).sendMessage(message);
}

private void glow(ArmorStand armorStand){
armorStand.removePotionEffect(PotionEffectType.GLOWING);
armorStand.addPotionEffect(new PotionEffect(PotionEffectType.GLOWING, 15, 1, false, false));
}

public PlayerEditorManager getManager(){
return plugin.editorManager;
}
Expand All @@ -306,17 +355,6 @@ public UUID getUUID() {
return uuid;
}

// boolean canBuild(ArmorStand armorstand) {
// for(ASEProtection prot : plugin.getProtections()){
// if(!prot.canEdit(getPlayer(), armorstand)) return false;
// }
// return true;
// }

// private void cannotBuildMessage(){
// getPlayer().sendMessage(plugin.getLang().getMessage("cantedit", "warn"));
// }

public void openMenu() {
if(!isMenuCancelled()){
plugin.getServer().getScheduler().runTaskLater(plugin, new OpenMenuTask(), 1).getTaskId();
Expand Down
53 changes: 48 additions & 5 deletions src/io/github/rypofalem/armorstandeditor/PlayerEditorManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import net.md_5.bungee.api.ChatColor;

import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
Expand All @@ -24,13 +28,10 @@
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.*;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;

//Manages PlayerEditors and Player Events related to editing armorstands
public class PlayerEditorManager implements Listener{
Expand Down Expand Up @@ -114,6 +115,48 @@ void onArmorStandInteract(PlayerInteractAtEntityEvent event){
}// end rename
}

@EventHandler (priority = EventPriority.LOW, ignoreCancelled=true)
public void onSwitchHands(PlayerSwapHandItemsEvent event){
if(!plugin.isEditTool(event.getOffHandItem())) return; //event assumes they are already switched
event.setCancelled(true);
Player player = event.getPlayer();
getPlayerEditor(event.getPlayer().getUniqueId()).setTarget(getTargets(player));
}

ArrayList<ArmorStand> getTargets(Player player){
Location eyeLaser = player.getEyeLocation();
Vector direction = player.getLocation().getDirection();
ArrayList<ArmorStand> armorStands = new ArrayList<>();

final double STEPSIZE = .5;
final Vector STEP = direction.multiply(STEPSIZE);
final double RANGE = 10;
final double LASERRADIUS = .3;
List<Entity> nearbyEntities = player.getNearbyEntities(RANGE, RANGE, RANGE);
if(nearbyEntities == null || nearbyEntities.isEmpty()) return null;

for(double i = 0; i<RANGE; i+= STEPSIZE){
List<Entity> nearby = (List<Entity>) player.getWorld().getNearbyEntities(eyeLaser, LASERRADIUS, LASERRADIUS, LASERRADIUS);
if(!nearby.isEmpty()){
boolean endLoop = false;
for(Entity e : nearby){
if(e instanceof ArmorStand){
if(canEdit(player, (ArmorStand)e)){
armorStands.add((ArmorStand)e);
endLoop = true;
}
}
}
if(endLoop) break;

}
if(eyeLaser.getBlock().getType().isSolid()) break;
eyeLaser.add(STEP);
}

return armorStands;
}

boolean canEdit(Player player, ArmorStand as){
ignoreNextInteract = true;
ArrayList<Event> events = new ArrayList<Event>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class Language {
ArmorStandEditorPlugin plugin;

public Language(String langFileName, ArmorStandEditorPlugin plugin) {
if (langFileName == null) langFileName = DEFAULTLANG;
this.plugin = plugin;
reloadLang(langFileName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/io/github/rypofalem/armorstandeditor/menu/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private void fillInventory() {
}
ItemStack[] items =
{xAxis, yAxis, zAxis, null, coarseAdj, fineAdj, null, rotate, place,
null, headPos, null, null, null, null, null, null,
null, headPos, null, null, null, null, null, null, null,
rightArmPos, bodyPos, leftArmPos, reset, null, null, showArms, visibility, size,
rightLegPos, equipment, leftLegPos, null, null, null, null, gravity, plate,
null, null, null, null, copy, paste, null, null, null,
Expand Down

0 comments on commit 6e28c81

Please sign in to comment.