-
Notifications
You must be signed in to change notification settings - Fork 104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added actions #198
base: main
Are you sure you want to change the base?
Added actions #198
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,25 @@ | |
|
||
import eu.decentsoftware.holograms.api.DecentHolograms; | ||
import eu.decentsoftware.holograms.api.Lang; | ||
import eu.decentsoftware.holograms.api.actions.ClickType; | ||
import eu.decentsoftware.holograms.api.holograms.Hologram; | ||
import eu.decentsoftware.holograms.api.utils.scheduler.S; | ||
import eu.decentsoftware.holograms.event.DecentHologramsRegisterEvent; | ||
import eu.decentsoftware.holograms.event.HologramClickEvent; | ||
import lombok.Data; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.Chunk; | ||
import org.bukkit.Location; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.EventPriority; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.PlayerJoinEvent; | ||
import org.bukkit.event.player.PlayerQuitEvent; | ||
import org.bukkit.event.player.PlayerRespawnEvent; | ||
import org.bukkit.event.player.PlayerTeleportEvent; | ||
import org.bukkit.event.player.*; | ||
import org.bukkit.util.Vector; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.stream.Collectors; | ||
|
||
@SuppressWarnings("unused") | ||
public class PlayerListener implements Listener { | ||
|
@@ -19,6 +29,7 @@ public class PlayerListener implements Listener { | |
|
||
public PlayerListener(DecentHolograms decentHolograms) { | ||
this.decentHolograms = decentHolograms; | ||
this.ref(); | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR) | ||
|
@@ -53,6 +64,89 @@ public void onRespawn(PlayerRespawnEvent e) { | |
S.async(() -> decentHolograms.getHologramManager().hideAll(player)); | ||
} | ||
|
||
|
||
// 视角对着的全息 | ||
meteorOSS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private Map<Player,String> viewHolograms = new ConcurrentHashMap<>(); | ||
|
||
|
||
private Map<Player,Long> tickLog = new ConcurrentHashMap<>(); | ||
|
||
public Map<Chunk,List<Hologram>> chunkListMap = new ConcurrentHashMap<>(); | ||
|
||
private void ref(){ | ||
chunkListMap.clear(); | ||
decentHolograms.getHologramManager().getHolograms().stream().forEach(hologram -> { | ||
Chunk chunk = hologram.getLocation().getChunk(); | ||
chunkListMap.putIfAbsent(chunk,new ArrayList<>()); | ||
chunkListMap.get(chunk).add(hologram); | ||
}); | ||
} | ||
|
||
|
||
@EventHandler | ||
public void onRegisterHd(DecentHologramsRegisterEvent registerEvent){ | ||
this.ref(); | ||
} | ||
|
||
@EventHandler | ||
public void onPlayerMove(PlayerMoveEvent playerMoveEvent){ | ||
Player player = playerMoveEvent.getPlayer(); | ||
Long orDefault = tickLog.getOrDefault(player, 0L); | ||
|
||
if(chunkListMap.isEmpty()) ref(); | ||
|
||
if((System.currentTimeMillis()-orDefault) >= 200L){ | ||
Optional.ofNullable(chunkListMap.get(player.getLocation().getChunk())).ifPresent(list->{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the use of Optionals is needed here. A simple getting and later null check does the same. |
||
List<Hologram> holograms = list.stream().collect(Collectors.toList()); | ||
meteorOSS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Vector playerDirection = player.getEyeLocation().getDirection(); | ||
double threshold = 0.2; // 0.2弧度 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As with other comment, use english. |
||
|
||
tickLog.put(player,System.currentTimeMillis()); | ||
|
||
holograms.removeIf(hologram -> { | ||
Location location = hologram.getLocation(); | ||
Vector toHologram = location.toVector().subtract(player.getEyeLocation().toVector()); | ||
double angle = playerDirection.angle(toHologram); | ||
return angle>=threshold; | ||
}); | ||
|
||
String currentViewHd = viewHolograms.get(player); | ||
|
||
|
||
for (Hologram hologram : holograms) { | ||
if(currentViewHd==null) viewHolograms.put(player,hologram.getName()); | ||
|
||
currentViewHd = viewHolograms.get(player); | ||
|
||
// 如果从一个全息移动到了另一个全息上 | ||
meteorOSS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if(!currentViewHd.equalsIgnoreCase(hologram.getName())){ | ||
Hologram fromHologram = decentHolograms.getHologramManager().getHologram(currentViewHd); | ||
List<Integer> clickableEntityIds = fromHologram.getPage(player).getClickableEntityIds(); | ||
if(clickableEntityIds.size()>0){ | ||
fromHologram.onClick(player,clickableEntityIds.get(0),ClickType.HOVER_LEAVE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to have dedicated onHover methods. |
||
viewHolograms.put(player,hologram.getName()); | ||
} | ||
} | ||
|
||
List<Integer> clickableEntityIds = hologram.getPage(player).getClickableEntityIds(); | ||
if(!clickableEntityIds.isEmpty()){ | ||
hologram.onClick(player,clickableEntityIds.get(0), ClickType.HOVER); | ||
meteorOSS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return; | ||
} | ||
|
||
if(currentViewHd!=null){ | ||
Hologram fromHologram = decentHolograms.getHologramManager().getHologram(currentViewHd); | ||
List<Integer> clickableEntityIds = fromHologram.getPage(player).getClickableEntityIds(); | ||
if(clickableEntityIds.size()>0){ | ||
fromHologram.onClick(player,clickableEntityIds.get(0),ClickType.HOVER_LEAVE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See other comment before. |
||
viewHolograms.remove(player); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
@EventHandler | ||
public void onTeleport(PlayerTeleportEvent e) { | ||
Player player = e.getPlayer(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package eu.decentsoftware.holograms.event; | ||
|
||
import org.bukkit.event.Event; | ||
import org.bukkit.event.HandlerList; | ||
|
||
/** | ||
* This event is called after the DecentHolograms plugin is reloaded. | ||
* | ||
* @author d0by | ||
* @since 2.7.8 | ||
*/ | ||
meteorOSS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public class DecentHologramsRegisterEvent extends Event { | ||
|
||
private static final HandlerList HANDLERS = new HandlerList(); | ||
|
||
public DecentHologramsRegisterEvent() { | ||
meteorOSS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public HandlerList getHandlers() { | ||
return HANDLERS; | ||
} | ||
|
||
public static HandlerList getHandlerList() { | ||
return HANDLERS; | ||
} | ||
|
||
public static boolean isRegistered() { | ||
return HANDLERS.getRegisteredListeners().length > 0; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of a non-click action (just looking at a page/line) being in a enum specifically for click types.
Given this would only be looking and not looking at a hologram can this be just some lists of actions or similar in the Hologram itself, rather than dedicate an entire type to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your reply,But I think hovering the mouse over the hologram should also be considered a ClickType. If we put them into actions, it would lose its meaning, as it would be triggered by LEFT_CLICK or RIGHT_CLICK instead of when the mouse hovers over the hologram.
While I acknowledge that my programming skills may have room for improvement, I am open to further discussion and finding a solution that best aligns with the project's needs. What are your thoughts on this approach?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm mixed feelings on this to be honest.
On one hand is it nice that you want to add hover support, but on the other hand do I feel like this may not be as good in terms of performance...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, it does indeed have performance issues; perhaps it's an immature submission. I hope this PR can remain open for others to see, maybe there are better approaches from others.