[go: up one dir, main page]

Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Commit

Permalink
removed virtual class VOFFLINEPLAYER because it was very unreliable a…
Browse files Browse the repository at this point in the history
…nd expensive
  • Loading branch information
velnias75 committed Aug 12, 2021
1 parent 064b965 commit 078f3a4
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 78 deletions.
4 changes: 2 additions & 2 deletions src/main/java/de/rangun/luegenpresse/CommandLuegenpresse.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public boolean onCommand(CommandSender sender, Command command, String label, St

try {

final Spew spew = Spew.getInstance(plugin.getHeadline(), plugin.getOfflineDefnStringProvider(),
plugin.getOnlineDefnStringProvider(), null);
final Spew spew = Spew.getInstance(plugin.getHeadline(), plugin.getOnlineDefnStringProvider(),
null);

spew.reload();

Expand Down
26 changes: 0 additions & 26 deletions src/main/java/de/rangun/luegenpresse/LuegenpressePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -74,27 +73,6 @@ public final class LuegenpressePlugin extends JavaPlugin {

private File headline;

private final DefnStringProvider offline_dsp = new DefnStringProvider() {

@Override
public List<Byte> getString(int rnd) {

final OfflinePlayer[] op = Bukkit.getOfflinePlayers();
final List<Byte> l;

if (op.length > 0) {
l = Lists.newArrayList(ArrayUtils.toObject(
op[Math.min(op.length - 1, rnd % op.length)].getName().getBytes(StandardCharsets.UTF_8)));
} else {
l = Lists.newArrayList(ArrayUtils.toObject("FakeOfflinePlayer".getBytes(StandardCharsets.UTF_8)));
}

l.add((byte) '\0');

return l;
}
};

private final DefnStringProvider online_dsp = new DefnStringProvider() {

@Override
Expand Down Expand Up @@ -173,10 +151,6 @@ public File getHeadline() {
return headline;
}

public DefnStringProvider getOfflineDefnStringProvider() {
return offline_dsp;
}

public DefnStringProvider getOnlineDefnStringProvider() {
return online_dsp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public void onPlayerInteract(final PlayerInteractEvent event) {
int attempto2 = 0;

final Spew spew = Spew.getInstance(plugin.getHeadline(),
plugin.getOfflineDefnStringProvider(), plugin.getOnlineDefnStringProvider(),
null);
plugin.getOnlineDefnStringProvider(), null);
final Set<String> lieheadlines = new HashSet<>(100);

do {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/de/rangun/luegenpresse/TellLie.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ protected TellLie(final LuegenpressePlugin plugin) {

protected String getLie() throws IOException, SpewException {

final Spew spew = Spew.getInstance(plugin.getHeadline(), plugin.getOfflineDefnStringProvider(),
plugin.getOnlineDefnStringProvider(), null);
final Spew spew = Spew.getInstance(plugin.getHeadline(), plugin.getOnlineDefnStringProvider(), null);
String lie = spew.getHeadline();

if (lie.endsWith("\n")) {
Expand Down
51 changes: 6 additions & 45 deletions src/main/java/de/rangun/luegenpresse/spew/Spew.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,6 @@ public final class Spew {
private final static byte SLASH = '/';
private final static byte BSLASH = '\\';

private final static ArrayList<Byte> VOFFLINE = new ArrayList<Byte>(16) {
private static final long serialVersionUID = -5742816479367105985L;
{
add((byte) '%');
add((byte) 'V');
add((byte) 'O');
add((byte) 'F');
add((byte) 'F');
add((byte) 'L');
add((byte) 'I');
add((byte) 'N');
add((byte) 'E');
add((byte) 'P');
add((byte) 'L');
add((byte) 'A');
add((byte) 'Y');
add((byte) 'E');
add((byte) 'R');
add((byte) '\0');
}
};

private final static ArrayList<Byte> VONLINE = new ArrayList<Byte>(15) {
private static final long serialVersionUID = 3295129968331809597L;
{
Expand Down Expand Up @@ -116,19 +94,15 @@ public final class Spew {
private ArrayList<Class> Class = new ArrayList<>();
private ArrayList<Byte> InLine = new ArrayList<>();

@Nonnull
private final DefnStringProvider offline_dsp;

@Nonnull
private final DefnStringProvider online_dsp;

private static Spew instance = null;

private Spew(@Nonnull final File in, @Nonnull DefnStringProvider offline_dsp,
@Nonnull DefnStringProvider online_dsp, @Nullable final Long seed) throws IOException, SpewException {
private Spew(@Nonnull final File in, @Nonnull DefnStringProvider online_dsp, @Nullable final Long seed)
throws IOException, SpewException {

this.in = in;
this.offline_dsp = offline_dsp;
this.online_dsp = online_dsp;

if (seed != null)
Expand All @@ -137,11 +111,11 @@ private Spew(@Nonnull final File in, @Nonnull DefnStringProvider offline_dsp,
reload();
}

public static Spew getInstance(@Nonnull final File in, @Nonnull DefnStringProvider offline_dsp,
@Nonnull DefnStringProvider online_dsp, @Nullable final Long seed) throws IOException, SpewException {
public static Spew getInstance(@Nonnull final File in, @Nonnull DefnStringProvider online_dsp,
@Nullable final Long seed) throws IOException, SpewException {

if (instance == null) {
instance = new Spew(in, offline_dsp, online_dsp, seed);
instance = new Spew(in, online_dsp, seed);
}

return instance;
Expand Down Expand Up @@ -219,18 +193,6 @@ private void readtext() throws IOException, SpewException {

checkForReserved = false;

// inject virtual class for offline players
InLine = VOFFLINE;

cp = processClass(cp, () -> {
InLine = Lists.newArrayList(Byte.valueOf((byte) '\0'));
}, () -> {
InLine = Lists.newArrayList(Byte.valueOf((byte) '%'), Byte.valueOf((byte) '%'));
return false;
}, () -> {
return new vdefn(offline_dsp);
});

// inject virtual class for online players
InLine = VONLINE;

Expand Down Expand Up @@ -379,8 +341,7 @@ private void setup(Class cp) throws SpewException {

--p;

if (checkForReserved && (VONLINE.subList(1, VONLINE.size()).equals(cp.name)
|| VOFFLINE.subList(1, VOFFLINE.size()).equals(cp.name)))
if (checkForReserved && (VONLINE.subList(1, VONLINE.size()).equals(cp.name)))
throw new SpewException("Class is reserved by plugin: ", cp.name);

for (;;) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/de/rangun/luegenpresse/test/SpewTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void setUp() throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("headline").getFile());

spew = Spew.getInstance(new File(file.getAbsolutePath()), dsp, dsp, Long.valueOf(1L));
spew = Spew.getInstance(new File(file.getAbsolutePath()), dsp, Long.valueOf(1L));
}

@Test
Expand Down

0 comments on commit 078f3a4

Please sign in to comment.