Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ public static void renderOverlays(Matrix4f matrixArg) {
.forEach(Renderer::renderPlacementHelper);
}

if (getMod().snitchFieldToPreview != null) {
renderSnitchFieldPreview(getMod().snitchFieldToPreview);
}
getMod().snitchFieldToPreview.ifPresent(Renderer::renderSnitchFieldPreview);

RenderSystem.enableDepthTest();
RenderSystem.depthMask(true);
Expand Down Expand Up @@ -145,7 +143,7 @@ enum SnitchLiveliness {
}

long now = System.currentTimeMillis();
long snitchTimer = snitch.getType() != null ? snitch.getType().timer : Snitch.Type.NOTEBLOCK.timer;
long snitchTimer = snitch.getType().isPresent() ? snitch.getType().get().timer : Snitch.Type.NOTEBLOCK.timer;
SnitchLiveliness snitchLiveliness = SnitchLiveliness.DORMANT_EVENTUALLY;
if (snitch.wasBroken()) {
snitchLiveliness = SnitchLiveliness.BROKEN;
Expand Down Expand Up @@ -216,8 +214,8 @@ enum SnitchLiveliness {
final AABB blockBox = new AABB(snitch.pos).inflate(.01);
Color boxOutlineColor = snitchLiveliness.color;
if (
getMod().snitchFieldToPreview != null
&& getMod().snitchFieldToPreview.source().equals(snitch)
getMod().snitchFieldToPreview.isPresent()
&& getMod().snitchFieldToPreview.get().source().equals(snitch)
) {
boxOutlineColor = PINK;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,9 @@ public abstract class SnitchMod {

public boolean rangeOverlayVisible = false;
public boolean placementHelperVisible = false;
@Nullable
public SnitchFieldPreview snitchFieldToPreview = null;
@Nullable
public Snitch lastBrokenSnitch = null;

@Nullable
private SnitchesStore store;
public Optional<SnitchFieldPreview> snitchFieldToPreview = Optional.empty();
public Optional<Snitch> lastBrokenSnitch = Optional.empty();
private Optional<SnitchesStore> store = Optional.empty();

public static SnitchMod getMod() {
return INSTANCE;
Expand Down Expand Up @@ -114,14 +110,14 @@ public UUID getClientUuid() {
return mc.player.getUUID();
}

public @Nullable SnitchesStore getStore() {
public Optional<SnitchesStore> getStore() {
String server = getCurrentServer();
if (store != null && !store.server.equals(server)) {
store.close();
store = null;
if (store.isPresent() && !store.get().server.equals(server)) {
store.get().close();
store = Optional.empty();
}
if (store == null && server != null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

store == null -> store.isPresent()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey,
In the below line(119) I have done this, changed store == null to store.isPresent() and created a new SnitchesStore object and put that in optional, is there anything which I am missing here, please let me know.
Thanks

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it not be store.isEmpty()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are correct, my bad, I have corrected it and pushed the code.

store = new SnitchesStore(server);
if (store.isEmpty() && server != null) {
store = Optional.of(new SnitchesStore(server));
}
return store;
}
Expand All @@ -131,15 +127,15 @@ public void handleConnectedToServer() {
}

public void handleDisconnectedFromServer() {
if (store != null) store.close();
store = null;
store.ifPresent(SnitchesStore::close);
store = Optional.empty();
}

public void handleTick() {
while (openGuiKey.consumeClick()) {
// TODO open gui, and rename keybind
store.close();
store = null;
store.ifPresent(SnitchesStore::close);
store = Optional.empty();
getStore();
logToChat(Component.literal("Reloaded the database"));
}
Expand Down Expand Up @@ -169,10 +165,10 @@ public void handleTick() {
snitch.getPos().getZ()
);
if (snitch.isGone()) {
store.updateSnitchNoLongerGone(snitch.pos);
store.get().updateSnitchNoLongerGone(snitch.pos);
logToChat(Component.literal(String.format("Marked snitch %s as alive", formattedSnitch)));
} else {
store.updateSnitchGone(snitch.pos);
store.get().updateSnitchGone(snitch.pos);
logToChat(Component.literal(String.format("Marked snitch %s as gone", formattedSnitch)));
}
}
Expand All @@ -188,7 +184,7 @@ public void handleTick() {
placementHelperVisible = !placementHelperVisible;

if (placementHelperVisible) {
snitchFieldToPreview = null;
snitchFieldToPreview = Optional.empty();
}

logToChat(
Expand All @@ -201,7 +197,7 @@ public void handleTick() {
.filter(Snitch::isAlive)
.findFirst();
if (optNearestSnitch.isEmpty()) {
snitchFieldToPreview = null;
snitchFieldToPreview = Optional.empty();
logToChat(Component.literal("No nearby alive snitches to base a field preview on"));
break;
}
Expand All @@ -214,15 +210,15 @@ public void handleTick() {
SnitchFieldPreview newSnitchFieldToPreview = new SnitchFieldPreview(
nearestSnitch, Direction.ofPlayer(mc.player));
if (
snitchFieldToPreview != null
&& newSnitchFieldToPreview.equals(snitchFieldToPreview)
snitchFieldToPreview.isPresent()
&& newSnitchFieldToPreview.equals(snitchFieldToPreview.get())
) {
logToChat(Component.literal("Turning off the snitch field preview"));
snitchFieldToPreview = null;
break;
}

snitchFieldToPreview = newSnitchFieldToPreview;
snitchFieldToPreview = Optional.of(newSnitchFieldToPreview);
logToChat(Component.literal("Showing a snitch field preview in the direction you're looking"));
}
// TODO if block pos changed -> if pos inside snitch range not in before -> send jainfo -> mark refreshed
Expand All @@ -233,24 +229,24 @@ public void handleTick() {
*/
public boolean handleChat(Component message) {
getStore();
if (store == null) return false;
if (store.isEmpty()) return false;

SnitchAlert snitchAlert = SnitchAlert.fromChat(message, store.server, getCurrentWorld());
SnitchAlert snitchAlert = SnitchAlert.fromChat(message, store.get().server, getCurrentWorld());
if (snitchAlert != null) {
store.updateSnitchFromAlert(snitchAlert);
store.get().updateSnitchFromAlert(snitchAlert);
return false;
}

SnitchRename snitchRename = SnitchRename.fromChat(message, store.server, getCurrentWorld(), getClientUuid());
SnitchRename snitchRename = SnitchRename.fromChat(message, store.get().server, getCurrentWorld(), getClientUuid());
if (snitchRename != null) {
store.updateSnitchFromRename(snitchRename);
store.get().updateSnitchFromRename(snitchRename);
return false;
}

Snitch snitchCreated = SnitchCreatedChatParser.fromChat(message, store.server, getCurrentWorld(), getClientUuid());
Snitch snitchCreated = SnitchCreatedChatParser.fromChat(message, store.get().server, getCurrentWorld(), getClientUuid());
if (snitchCreated != null) {
Snitch alreadyExistingSnitch = store.getSnitch(snitchCreated.pos);
store.updateSnitchFromCreation(snitchCreated);
Snitch alreadyExistingSnitch = store.get().getSnitch(snitchCreated.pos);
store.get().updateSnitchFromCreation(snitchCreated);
if (
alreadyExistingSnitch != null
&& alreadyExistingSnitch.getName() != null
Expand All @@ -269,15 +265,15 @@ public boolean handleChat(Component message) {
}

if (
snitchFieldToPreview != null
&& snitchFieldToPreview.field().pos.equals(snitchCreated.pos)
snitchFieldToPreview.isPresent()
&& snitchFieldToPreview.get().field().pos.equals(snitchCreated.pos)
) {
if (placementHelperVisible) {
placementHelperVisible = false;
}

snitchFieldToPreview = new SnitchFieldPreview(
snitchCreated, snitchFieldToPreview.direction());
snitchFieldToPreview = Optional.of(new SnitchFieldPreview(
snitchCreated, snitchFieldToPreview.get().direction()));

logToChat(Component.literal("Showing an inferred snitch field preview"));
}
Expand All @@ -292,12 +288,12 @@ public boolean handleChat(Component message) {

public void handleWindowItems(List<ItemStack> stacks) {
getStore();
if (store == null) return;
if (store.isEmpty()) return;
List<JalistEntry> jalistEntries = new ArrayList<JalistEntry>(stacks.size());
for (int i = 0; i < stacks.size(); i++) {
ItemStack stack = stacks.get(i);
try {
JalistEntry jalistEntry = JalistEntry.fromStack(stack, store.server);
JalistEntry jalistEntry = JalistEntry.fromStack(stack, store.get().server);
if (jalistEntry != null) {
jalistEntries.add(jalistEntry);
}
Expand All @@ -307,7 +303,7 @@ public void handleWindowItems(List<ItemStack> stacks) {
logToChat(Component.literal("Failed reading snitch " + i + " on JAList page"));
}
}
store.updateSnitchesFromJalist(jalistEntries);
store.get().updateSnitchesFromJalist(jalistEntries);
if (jalistEntries.size() > 0) {
logToChat(Component.literal("Found " + jalistEntries.size() + " snitches on JAList page"));
}
Expand All @@ -319,8 +315,8 @@ public void handleRenderBlockOverlay(Matrix4f matrix) {

public Stream<Snitch> streamNearbySnitches(Vec3 playerPos, int distance) {
getStore();
if (store == null) return Stream.empty();
return store.getAllSnitches().stream()
if (store.isEmpty()) return Stream.empty();
return store.get().getAllSnitches().stream()
.filter(s -> s.getPos().getCenter().distanceTo(playerPos) < distance)
.sorted(Comparator.comparing(s -> s.getPos().getCenter().distanceTo(playerPos)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public void upsertSnitches(List<Snitch> snitches) {

for (Snitch snitch : snitches) {
String type = null;
if (snitch.getType() != null) {
type = snitch.getType().dbRepresentation;
if (snitch.getType().isPresent()) {
type = snitch.getType().get().dbRepresentation;
}
try {
int i = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ public String getGroup() {
return group;
}

@Nullable
public Type getType() {
return type;
@NotNull
public Optional<Type> getType() {
return Optional.ofNullable(type);
}

@Nullable
Expand Down