Skip to content

Commit 393b352

Browse files
committed
PositionSongPlayer, NoteBlockSongPlayer edits (1.1.5)
added function isPlayerInRange added event PlayerRangeStateChangeEvent
1 parent dd6f63a commit 393b352

File tree

6 files changed

+100
-16
lines changed

6 files changed

+100
-16
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.xxmicloxx</groupId>
88
<artifactId>NoteBlockAPI</artifactId>
9-
<version>1.1.4</version>
9+
<version>1.1.5</version>
1010
<repositories>
1111
<repository>
1212
<id>spigot-repo</id>

src/main/java/com/xxmicloxx/NoteBlockAPI/NoteBlockSongPlayer.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
package com.xxmicloxx.NoteBlockAPI;
22

3+
import org.bukkit.Bukkit;
34
import org.bukkit.Material;
45
import org.bukkit.block.Block;
56
import org.bukkit.entity.Player;
67

7-
/**
8-
* Created with IntelliJ IDEA.
9-
* User: ml
10-
* Date: 07.12.13
11-
* Time: 12:56
12-
*/
138
public class NoteBlockSongPlayer extends SongPlayer {
149
private Block noteBlock;
1510
private int distance = 16;
@@ -48,6 +43,19 @@ public void playTick(Player p, int tick) {
4843
Instrument.getInstrument(note.getInstrument()),
4944
((l.getVolume() * (int) volume * (int) playerVolume) / 1000000f) * ((1f/16f) * distance),
5045
NotePitch.getPitch(note.getKey() - 33));
46+
if (isPlayerInRange(p)){
47+
if (!this.playerList.get(p.getName())){
48+
playerList.put(p.getName(), true);
49+
PlayerRangeStateChangeEvent event = new PlayerRangeStateChangeEvent(this, p, true);
50+
Bukkit.getPluginManager().callEvent(event);
51+
}
52+
} else {
53+
if (this.playerList.get(p.getName())){
54+
playerList.put(p.getName(), false);
55+
PlayerRangeStateChangeEvent event = new PlayerRangeStateChangeEvent(this, p, false);
56+
Bukkit.getPluginManager().callEvent(event);
57+
}
58+
}
5159
}
5260
}
5361

@@ -62,4 +70,12 @@ public void setDistance(int distance){
6270
public int getDistance(){
6371
return distance;
6472
}
73+
74+
public boolean isPlayerInRange(Player p){
75+
if (p.getLocation().distance(noteBlock.getLocation()) > distance){
76+
return false;
77+
} else {
78+
return true;
79+
}
80+
}
6581
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.xxmicloxx.NoteBlockAPI;
2+
3+
import org.bukkit.entity.Player;
4+
import org.bukkit.event.Event;
5+
import org.bukkit.event.HandlerList;
6+
7+
public class PlayerRangeStateChangeEvent extends Event{
8+
9+
private static final HandlerList handlers = new HandlerList();
10+
private SongPlayer song;
11+
private Player p;
12+
private Boolean state;
13+
14+
public PlayerRangeStateChangeEvent(SongPlayer song, Player p, Boolean state) {
15+
this.song = song;
16+
this.p = p;
17+
this.state = state;
18+
}
19+
20+
public static HandlerList getHandlerList() {
21+
return handlers;
22+
}
23+
24+
public SongPlayer getSongPlayer() {
25+
return song;
26+
}
27+
28+
@Override
29+
public HandlerList getHandlers() {
30+
return handlers;
31+
}
32+
33+
public Player getPlayer() {
34+
return p;
35+
}
36+
37+
public Boolean isInRange() {
38+
return state;
39+
}
40+
}

src/main/java/com/xxmicloxx/NoteBlockAPI/PositionSongPlayer.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.xxmicloxx.NoteBlockAPI;
22

3+
import org.bukkit.Bukkit;
34
import org.bukkit.Location;
45
import org.bukkit.entity.Player;
56

@@ -37,6 +38,19 @@ public void playTick(Player p, int tick) {
3738
Instrument.getInstrument(note.getInstrument()),
3839
((l.getVolume() * (int) volume * (int) playerVolume) / 1000000f) * ((1f/16f) * distance),
3940
NotePitch.getPitch(note.getKey() - 33));
41+
if (isPlayerInRange(p)){
42+
if (!this.playerList.get(p.getName())){
43+
playerList.put(p.getName(), true);
44+
PlayerRangeStateChangeEvent event = new PlayerRangeStateChangeEvent(this, p, true);
45+
Bukkit.getPluginManager().callEvent(event);
46+
}
47+
} else {
48+
if (this.playerList.get(p.getName())){
49+
playerList.put(p.getName(), false);
50+
PlayerRangeStateChangeEvent event = new PlayerRangeStateChangeEvent(this, p, false);
51+
Bukkit.getPluginManager().callEvent(event);
52+
}
53+
}
4054
}
4155
}
4256

@@ -51,4 +65,12 @@ public void setDistance(int distance){
5165
public int getDistance(){
5266
return distance;
5367
}
68+
69+
public boolean isPlayerInRange(Player p){
70+
if (p.getLocation().distance(targetLocation) > distance){
71+
return false;
72+
} else {
73+
return true;
74+
}
75+
}
5476
}

src/main/java/com/xxmicloxx/NoteBlockAPI/SongPlayer.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package com.xxmicloxx.NoteBlockAPI;
22

3-
import org.bukkit.Bukkit;
4-
import org.bukkit.entity.Player;
5-
63
import java.util.ArrayList;
74
import java.util.Collections;
5+
import java.util.HashMap;
86
import java.util.List;
97
import java.util.concurrent.Callable;
108

9+
import org.bukkit.Bukkit;
10+
import org.bukkit.entity.Player;
11+
1112
public abstract class SongPlayer {
1213

1314
protected Song song;
1415
protected boolean playing = false;
1516
protected short tick = -1;
16-
protected ArrayList<String> playerList = new ArrayList<String>();
17+
//protected ArrayList<String> playerList = new ArrayList<String>();
18+
protected HashMap<String, Boolean> playerList = new HashMap<String, Boolean>();
1719
protected boolean autoDestroy = false;
1820
protected boolean destroyed = false;
1921
protected Thread playerThread;
@@ -130,7 +132,7 @@ public Boolean call() {
130132
return false;
131133
}
132134
}
133-
for (String s : playerList) {
135+
for (String s : playerList.keySet()) {
134136
@SuppressWarnings("deprecation")
135137
Player p = Bukkit.getPlayerExact(s);
136138
if (p == null) {
@@ -169,13 +171,15 @@ public Boolean call() {
169171
}
170172

171173
public List<String> getPlayerList() {
172-
return Collections.unmodifiableList(playerList);
174+
List<String> list = new ArrayList<String>();
175+
list.addAll(playerList.keySet());
176+
return Collections.unmodifiableList(list);
173177
}
174178

175179
public void addPlayer(Player p) {
176180
synchronized (this) {
177-
if (!playerList.contains(p.getName())) {
178-
playerList.add(p.getName());
181+
if (!playerList.containsKey(p.getName())) {
182+
playerList.put(p.getName(), false);
179183
ArrayList<SongPlayer> songs = NoteBlockPlayerMain.plugin.playingSongs
180184
.get(p.getName());
181185
if (songs == null) {
@@ -187,6 +191,8 @@ public void addPlayer(Player p) {
187191
}
188192
}
189193

194+
195+
190196
public boolean getAutoDestroy() {
191197
synchronized (this) {
192198
return autoDestroy;

src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: NoteBlockAPI
22

33
main: com.xxmicloxx.NoteBlockAPI.NoteBlockPlayerMain
4-
version: 1.1.4
4+
version: 1.1.5
55

66
description: a developer interface to play nbs-files ingame
77
authors: [xxmicloxx, michidk, koca2000]

0 commit comments

Comments
 (0)