Skip to content

Commit

Permalink
Fix NullPointerException
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Nov 25, 2024
1 parent f4b3b80 commit 9924869
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private void applyIfMatches(EquipmentSlot equipmentSlot) {
var previousItem = lastInEquipment.get(equipmentSlot);
boolean hasChanged;
if (previousItem != null) {
if (item == null || previousItem.type() != item.type()) {
if (item.isEmpty() || previousItem.type() != item.get().type()) {
// Item before, but we don't have one now, or it's different
hasChanged = true;

Expand All @@ -181,13 +181,13 @@ private void applyIfMatches(EquipmentSlot equipmentSlot) {
}
} else {
// No item before, but we have one now
hasChanged = item != null;
hasChanged = item.isPresent();
}

if (hasChanged && item != null) {
connection.dataManager().localPlayer().attributeState().putItemModifiers(item, equipmentSlot);
if (hasChanged && item.isPresent()) {
connection.dataManager().localPlayer().attributeState().putItemModifiers(item.get(), equipmentSlot);
}

lastInEquipment.put(equipmentSlot, item);
lastInEquipment.put(equipmentSlot, item.orElse(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public PlayerInventoryContainer(InventoryManager inventoryManager) {
this.inventoryManager = inventoryManager;
}

public SFItemStack getEquipmentSlotItem(EquipmentSlot slot) {
return getEquipmentSlot(slot).map(ContainerSlot::item).orElse(null);
public Optional<SFItemStack> getEquipmentSlotItem(EquipmentSlot slot) {
return getEquipmentSlot(slot).map(ContainerSlot::item);
}

public Optional<ContainerSlot> getEquipmentSlot(EquipmentSlot slot) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,9 @@ public boolean canEntityWalkOnPowderSnow(Entity entity) {
if (this.level().tagsState().is(entity.entityType(), EntityTypeTags.POWDER_SNOW_WALKABLE_MOBS)) {
return true;
} else {
return entity instanceof LivingEntity le && le.getItemBySlot(EquipmentSlot.FEET).type() == ItemType.LEATHER_BOOTS;
return entity instanceof LivingEntity le && le.getItemBySlot(EquipmentSlot.FEET)
.map(item -> item.type() == ItemType.LEATHER_BOOTS)
.orElse(false);
}
}

Expand Down Expand Up @@ -650,8 +652,9 @@ public boolean isUsingItem() {

protected boolean canGlide() {
if (!this.onGround() && !this.effectState().hasEffect(EffectType.LEVITATION)) {
for (var lv : EquipmentSlot.values()) {
if (canGlideUsing(this.getItemBySlot(lv), lv)) {
for (var slot : EquipmentSlot.values()) {
var item = this.getItemBySlot(slot);
if (item.isPresent() && canGlideUsing(item.get(), slot)) {
return true;
}
}
Expand All @@ -660,7 +663,7 @@ protected boolean canGlide() {
return false;
}

public abstract SFItemStack getItemBySlot(EquipmentSlot slot);
public abstract Optional<SFItemStack> getItemBySlot(EquipmentSlot slot);

protected float getFlyingSpeed() {
return 0.02F;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.PlayerState;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.*;

import java.util.Optional;

/**
* Represents the bot itself as an entity.
*/
Expand Down Expand Up @@ -447,7 +449,7 @@ protected boolean isImmobile() {
}

@Override
public SFItemStack getItemBySlot(EquipmentSlot slot) {
public Optional<SFItemStack> getItemBySlot(EquipmentSlot slot) {
return this.connection.inventoryManager().playerInventory().getEquipmentSlotItem(slot);
}

Expand Down

0 comments on commit 9924869

Please sign in to comment.