Skip to content

Commit

Permalink
Implement aiStep and rewrite input code
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Nov 22, 2024
1 parent 1ecaf99 commit 8e341b9
Show file tree
Hide file tree
Showing 20 changed files with 847 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
import lombok.SneakyThrows;
import net.minecraft.Util;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.FallingBlock;
import net.minecraft.world.level.block.FenceGateBlock;
import net.minecraft.world.level.block.IceBlock;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.state.BlockBehaviour;

public class BlocksJsonGenerator implements IDataGenerator {
Expand Down Expand Up @@ -58,6 +55,9 @@ public static JsonObject generateBlock(Block block) {
if (block instanceof FenceGateBlock) {
blockDesc.addProperty("fenceGateBlock", true);
}
if (block instanceof TrapDoorBlock) {
blockDesc.addProperty("trapDoorBlock", true);
}
if (defaultState.canBeReplaced()) {
blockDesc.addProperty("replaceable", true);
}
Expand Down
1 change: 1 addition & 0 deletions data-generator/src/main/resources/templates/BlockType.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public record BlockType(
boolean fallingBlock,
boolean iceBlock,
boolean fenceGateBlock,
boolean trapDoorBlock,
boolean replaceable,
boolean requiresCorrectToolForDrops,
List<LootPoolEntry> lootTableData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public record BlockType(
boolean fallingBlock,
boolean iceBlock,
boolean fenceGateBlock,
boolean trapDoorBlock,
boolean replaceable,
boolean requiresCorrectToolForDrops,
List<LootPoolEntry> lootTableData,
Expand Down
56 changes: 50 additions & 6 deletions server/src/main/java/com/soulfiremc/server/data/EquipmentSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,55 @@
*/
package com.soulfiremc.server.data;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public enum EquipmentSlot {
MAINHAND,
OFFHAND,
FEET,
LEGS,
CHEST,
HEAD
MAINHAND(Type.HAND),
OFFHAND(Type.HAND),
FEET(Type.HUMANOID_ARMOR),
LEGS(Type.HUMANOID_ARMOR),
CHEST(Type.HUMANOID_ARMOR),
HEAD(Type.HUMANOID_ARMOR),
BODY(Type.ANIMAL_ARMOR);

private final Type type;

public static EquipmentSlot fromMCPl(org.geysermc.mcprotocollib.protocol.data.game.entity.EquipmentSlot slot) {
return switch (slot) {
case MAIN_HAND -> MAINHAND;
case OFF_HAND -> OFFHAND;
case BOOTS -> FEET;
case LEGGINGS -> LEGS;
case CHESTPLATE -> CHEST;
case HELMET -> HEAD;
case BODY -> BODY;
};
}

public boolean isArmor() {
return type == Type.HUMANOID_ARMOR || type == Type.ANIMAL_ARMOR;
}

public boolean isHand() {
return type == Type.HAND;
}

public org.geysermc.mcprotocollib.protocol.data.game.entity.EquipmentSlot toMCPl() {
return switch (this) {
case MAINHAND -> org.geysermc.mcprotocollib.protocol.data.game.entity.EquipmentSlot.MAIN_HAND;
case OFFHAND -> org.geysermc.mcprotocollib.protocol.data.game.entity.EquipmentSlot.OFF_HAND;
case FEET -> org.geysermc.mcprotocollib.protocol.data.game.entity.EquipmentSlot.BOOTS;
case LEGS -> org.geysermc.mcprotocollib.protocol.data.game.entity.EquipmentSlot.LEGGINGS;
case CHEST -> org.geysermc.mcprotocollib.protocol.data.game.entity.EquipmentSlot.CHESTPLATE;
case HEAD -> org.geysermc.mcprotocollib.protocol.data.game.entity.EquipmentSlot.HELMET;
case BODY -> org.geysermc.mcprotocollib.protocol.data.game.entity.EquipmentSlot.BODY;
};
}

public enum Type {
HAND,
HUMANOID_ARMOR,
ANIMAL_ARMOR
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private static void putOn(
return;
}

var equipmentSlot = inventory.getEquipmentSlot(armorType.toEquipmentSlot());
var equipmentSlot = inventory.getEquipmentSlot(armorType.toEquipmentSlot()).orElseThrow();
var equipmentSlotItem = equipmentSlot.item();
if (equipmentSlotItem != null) {
var targetIndex = armorType.itemTypes().indexOf(equipmentSlotItem.type());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void applyItemAttributes() {
}

private void applyIfMatches(EquipmentSlot equipmentSlot) {
var item = playerInventory.getEquipmentSlot(equipmentSlot).item();
var item = playerInventory.getEquipmentSlotItem(equipmentSlot);
var previousItem = lastInEquipment.get(equipmentSlot);
boolean hasChanged;
if (previousItem != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ public PlayerInventoryContainer(InventoryManager inventoryManager) {
this.inventoryManager = inventoryManager;
}

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

public Optional<ContainerSlot> getEquipmentSlot(EquipmentSlot slot) {
return switch (slot) {
case MAINHAND -> getHeldItem();
case OFFHAND -> getOffhand();
case HEAD -> getHelmet();
case CHEST -> getChestplate();
case LEGS -> getLeggings();
case FEET -> getBoots();
case MAINHAND -> Optional.of(getHeldItem());
case OFFHAND -> Optional.of(getOffhand());
case HEAD -> Optional.of(getHelmet());
case CHEST -> Optional.of(getChestplate());
case LEGS -> Optional.of(getLeggings());
case FEET -> Optional.of(getBoots());
case BODY -> Optional.empty();
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.soulfiremc.server.protocol.bot.container;

import com.soulfiremc.server.data.ItemType;
import com.soulfiremc.server.util.MathHelper;
import lombok.Getter;
import org.geysermc.mcprotocollib.protocol.data.game.item.ItemStack;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponent;
Expand Down Expand Up @@ -91,4 +92,48 @@ public boolean canStackWith(SFItemStack other) {

return this.type == other.type;
}

public boolean has(DataComponentType<?> component) {
return components().getOptional(component).isPresent();
}

public <T> T get(DataComponentType<T> component) {
return components().get(component);
}

public <T> T getOrDefault(DataComponentType<T> component, T defaultValue) {
return components().getOptional(component).orElse(defaultValue);
}

public int getMaxStackSize() {
return this.getOrDefault(DataComponentType.MAX_STACK_SIZE, 1);
}

public boolean isStackable() {
return this.getMaxStackSize() > 1 && (!this.isDamageableItem() || !this.isDamaged());
}

public boolean isDamageableItem() {
return this.has(DataComponentType.MAX_DAMAGE) && !this.has(DataComponentType.UNBREAKABLE) && this.has(DataComponentType.DAMAGE);
}

public boolean isDamaged() {
return this.isDamageableItem() && this.getDamageValue() > 0;
}

public int getDamageValue() {
return MathHelper.clamp(this.getOrDefault(DataComponentType.DAMAGE, 0), 0, this.getMaxDamage());
}

public int getMaxDamage() {
return this.getOrDefault(DataComponentType.MAX_DAMAGE, 0);
}

public boolean isBroken() {
return this.isDamageableItem() && this.getDamageValue() >= this.getMaxDamage();
}

public boolean nextDamageWillBreak() {
return this.isDamageableItem() && this.getDamageValue() >= this.getMaxDamage() - 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.soulfiremc.server.protocol.bot.model;

import lombok.*;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.player.ServerboundPlayerAbilitiesPacket;

@Setter
@Getter
Expand All @@ -36,4 +37,8 @@ public final class AbilitiesData {
public AbilitiesData() {
this(false, false, false, false, true, 0.05F, 0.1F);
}

public ServerboundPlayerAbilitiesPacket toPacket() {
return new ServerboundPlayerAbilitiesPacket(flying);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ public void addModifier(Attribute.Modifier modifier) {
public void removeModifier(Key id) {
modifiers.remove(id);
}

public Attribute.Modifier getModifier(Key id) {
return modifiers.get(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.level.ServerboundPlayerInputPacket;

@Setter
@Getter
Expand All @@ -31,9 +30,9 @@ public class ControlState {
private boolean backward;
private boolean left;
private boolean right;
private boolean sprinting;
private boolean jumping;
private boolean sneaking;
private boolean sprinting;
private boolean flying;

public void incrementActivelyControlling() {
Expand Down Expand Up @@ -63,7 +62,7 @@ public void resetAll() {
flying = false;
}

public ServerboundPlayerInputPacket toServerboundPlayerInputPacket() {
return new ServerboundPlayerInputPacket(forward, backward, left, right, jumping, sneaking, sprinting);
public KeyPresses toKeyPresses() {
return new KeyPresses(forward, backward, left, right, jumping, sneaking, sprinting);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ private static boolean isNotPartOf(ItemAttributeModifiers.EquipmentSlotGroup ite
case ANY -> true;
case MAIN_HAND -> comparedTo == EquipmentSlot.MAINHAND;
case OFF_HAND -> comparedTo == EquipmentSlot.OFFHAND;
case HAND -> comparedTo == EquipmentSlot.MAINHAND || comparedTo == EquipmentSlot.OFFHAND;
case HAND -> comparedTo.isHand();
case FEET -> comparedTo == EquipmentSlot.FEET;
case LEGS -> comparedTo == EquipmentSlot.LEGS;
case CHEST -> comparedTo == EquipmentSlot.CHEST;
case HEAD -> comparedTo == EquipmentSlot.HEAD;
case ARMOR -> comparedTo == EquipmentSlot.CHEST || comparedTo == EquipmentSlot.LEGS || comparedTo == EquipmentSlot.FEET;
case BODY -> comparedTo == EquipmentSlot.HEAD || comparedTo == EquipmentSlot.CHEST || comparedTo == EquipmentSlot.LEGS || comparedTo == EquipmentSlot.FEET;
case ARMOR -> comparedTo.isArmor();
case BODY -> comparedTo == EquipmentSlot.BODY;
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* SoulFire
* Copyright (C) 2024 AlexProgrammerDE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.soulfiremc.server.protocol.bot.state;

import lombok.RequiredArgsConstructor;
import org.cloudburstmc.math.vector.Vector2d;

@RequiredArgsConstructor
public class InputState {
private final ControlState state;
public KeyPresses keyPresses = KeyPresses.EMPTY;
public float leftImpulse;
public float forwardImpulse;

private static float calculateImpulse(boolean input, boolean otherInput) {
if (input == otherInput) {
return 0.0F;
} else {
return input ? 1.0F : -1.0F;
}
}

public Vector2d getMoveVector() {
return Vector2d.from(this.leftImpulse, this.forwardImpulse);
}

public boolean hasForwardImpulse() {
return this.forwardImpulse > 1.0E-5F;
}

public void makeJump() {
this.keyPresses = new KeyPresses(
this.keyPresses.forward(),
this.keyPresses.backward(),
this.keyPresses.left(),
this.keyPresses.right(),
true,
this.keyPresses.shift(),
this.keyPresses.sprint()
);
}

public void tick(boolean movingSlowly, float amount) {
this.keyPresses = state.toKeyPresses();
this.forwardImpulse = calculateImpulse(this.keyPresses.forward(), this.keyPresses.backward());
this.leftImpulse = calculateImpulse(this.keyPresses.left(), this.keyPresses.right());
if (movingSlowly) {
this.leftImpulse *= amount;
this.forwardImpulse *= amount;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* SoulFire
* Copyright (C) 2024 AlexProgrammerDE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.soulfiremc.server.protocol.bot.state;

import org.geysermc.mcprotocollib.protocol.packet.ingame.serverbound.level.ServerboundPlayerInputPacket;

public record KeyPresses(
boolean forward,
boolean backward,
boolean left,
boolean right,
boolean jump,
boolean shift,
boolean sprint
) {
public static final KeyPresses EMPTY = new KeyPresses(false, false, false, false, false, false, false);

public ServerboundPlayerInputPacket toServerboundPlayerInputPacket() {
return new ServerboundPlayerInputPacket(forward, backward, left, right, jump, shift, sprint);
}
}
Loading

0 comments on commit 8e341b9

Please sign in to comment.