Improve immutability & prevent NPE

This commit is contained in:
TheMode 2021-04-10 06:08:08 +02:00
parent 50ceeb33e4
commit d2efb43625
4 changed files with 19 additions and 14 deletions

View File

@ -61,7 +61,6 @@ public abstract class ItemMetaBuilder {
@Contract("_ -> this")
public @NotNull ItemMetaBuilder displayName(@Nullable Component displayName) {
this.displayName = displayName;
handleCompound("display", nbtCompound -> {
if (displayName != null) {
final String name = AdventureSerializer.serialize(displayName);
@ -70,14 +69,12 @@ public abstract class ItemMetaBuilder {
nbtCompound.removeTag("Name");
}
});
return this;
}
@Contract("_ -> this")
public @NotNull ItemMetaBuilder lore(@NotNull List<@NotNull Component> lore) {
this.lore = lore;
handleCompound("display", nbtCompound -> {
final NBTList<NBTString> loreNBT = new NBTList<>(NBTTypes.TAG_String);
for (Component line : lore) {
@ -85,7 +82,6 @@ public abstract class ItemMetaBuilder {
}
nbtCompound.set("Lore", loreNBT);
});
return this;
}

View File

@ -8,6 +8,8 @@ import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTList;
import org.jglrxavpok.hephaistos.nbt.NBTTypes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Supplier;
@ -20,12 +22,12 @@ public class FireworkMeta extends ItemMeta implements ItemMetaBuilder.Provider<F
protected FireworkMeta(@NotNull ItemMetaBuilder metaBuilder, List<FireworkEffect> effects,
byte flightDuration) {
super(metaBuilder);
this.effects = effects;
this.effects = new ArrayList<>(effects);
this.flightDuration = flightDuration;
}
public List<FireworkEffect> getEffects() {
return effects;
return Collections.unmodifiableList(effects);
}
public byte getFlightDuration() {

View File

@ -11,6 +11,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTList;
import org.jglrxavpok.hephaistos.nbt.NBTString;
import org.jglrxavpok.hephaistos.nbt.NBTTypes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
@ -27,7 +28,7 @@ public class WritableBookMeta extends ItemMeta implements ItemMetaBuilder.Provid
super(metaBuilder);
this.author = author;
this.title = title;
this.pages = Collections.unmodifiableList(pages);
this.pages = new ArrayList<>(pages);
}
public String getAuthor() {
@ -38,15 +39,15 @@ public class WritableBookMeta extends ItemMeta implements ItemMetaBuilder.Provid
return title;
}
public List<Component> getPages() {
return pages;
public @NotNull List<@NotNull Component> getPages() {
return Collections.unmodifiableList(pages);
}
public static class Builder extends ItemMetaBuilder {
private String author;
private String title;
private List<Component> pages;
private List<Component> pages = new ArrayList<>();
public Builder author(String author) {
this.author = author;

View File

@ -13,6 +13,8 @@ import org.jglrxavpok.hephaistos.nbt.NBTList;
import org.jglrxavpok.hephaistos.nbt.NBTString;
import org.jglrxavpok.hephaistos.nbt.NBTTypes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
@ -33,7 +35,7 @@ public class WrittenBookMeta extends ItemMeta implements ItemMetaBuilder.Provide
this.generation = generation;
this.author = author;
this.title = title;
this.pages = Collections.unmodifiableList(pages);
this.pages = new ArrayList<>(pages);
}
public boolean isResolved() {
@ -52,8 +54,8 @@ public class WrittenBookMeta extends ItemMeta implements ItemMetaBuilder.Provide
return title;
}
public List<Component> getPages() {
return pages;
public @NotNull List<@NotNull Component> getPages() {
return Collections.unmodifiableList(pages);
}
public enum WrittenBookGeneration {
@ -84,7 +86,7 @@ public class WrittenBookMeta extends ItemMeta implements ItemMetaBuilder.Provide
private WrittenBookGeneration generation;
private String author;
private String title;
private List<Component> pages;
private List<Component> pages = new ArrayList<>();
public Builder resolved(boolean resolved) {
this.resolved = resolved;
@ -123,6 +125,10 @@ public class WrittenBookMeta extends ItemMeta implements ItemMetaBuilder.Provide
return this;
}
public Builder pages(Component... pages) {
return pages(Arrays.asList(pages));
}
@Override
public @NotNull WrittenBookMeta build() {
return new WrittenBookMeta(this, resolved, generation, author, title, pages);