Skip to content

Commit

Permalink
feat: starting items and new level util method
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHillcox committed Mar 13, 2023
1 parent 19688e4 commit 53fe5e5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 41 deletions.
38 changes: 3 additions & 35 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,8 @@
# Kube Utils Changelog

## [1.0.1]

> This is features ported up for 0.1.4 (1.18.2 update)
## [1.0.2]

### Added

- A new `KuEvents.playerStarterItems` event that, once used and successfully gave items, will stop running
- You can define an item and equipment slot (For things like armor) too
```javascript
KuEvents.playerStarterItems(event => {
// Item.of is optional here
event.addItems("5x minecraft:gold_ingot", Item.of("2x minecraft:grass_block"))

// Valid options are part of the EquipmentSlot Enum
event.addEquipmentItem("chest", "minecraft:golden_chestplate")
event.addEquipmentItem("offhand", "minecraft:stone")
})
```
- A new `Ku.Player` class that brings some helpful methods
- `showActionBar(text: string, color?: Color = Color.WHITE, bold = false, italic = false)`
- Uses the built-in client action bar to display a message. This is already supported through the player class but this method allows for less boilerplate and stable code ports
- `showActionBarComponent(component: Component)`
- Mostly the same as the above but gives you access to use a JS Object as your component which might look a something like this
```javascript
const player = Ku.Player(event.player);
player.showActionBarComponent({
text: "Hello",
bold: true
})
```
- `clearStarterItemsFlag`
- This method simply reset the flag for the `ku.player.starter-items` meaning on the next login, the player will be given the items once again
- `isClientSide`
- Lets you know if the client being wrapped is client side. This was mostly a helper for my code but it could be helpful

### Changed

- Renamed the internal binding classes to be suffixed with Ku so they're visually different from vanilla and KubeJS
- Added a new Level method to allow for finding a single block within an area `findSingleBlockWithinRadius`
- Added a `triggeredBy` field to the `KuEvents.playerStarterItems` event as we now trigger the event for a player joining as well as a player changing dimension. If the event does not place and item in the inventory. We assume it failed and will retry on either a join or a dimension change
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ loom.platform=forge
minecraft_version=1.19.2
forge_version=1.19.2-43.2.3

mod_version=1.0.1
mod_version=1.0.2
maven_group=pro.mikey.mods
archives_base_name=kube-utils
mod_id=kubeutils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
KuEventsGroup.PLAYER_STARTING_ITEMS.post(new PlayerStarterItems(event.getEntity()));
}
}

@SubscribeEvent
void onPlayerChangeDimension(PlayerEvent.PlayerChangedDimensionEvent event) {
if (!event.getEntity().kjs$getPersistentData().getBoolean(PlayerStarterItems.STARTER_ITEMS_GIVEN_FLAG)) {
KuEventsGroup.PLAYER_STARTING_ITEMS.post(new PlayerStarterItems(event.getEntity(), "dimension_change"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,17 @@ public class PlayerStarterItems extends PlayerEventJS {
private final List<ItemStack> items = new ArrayList<>();
private final Map<EquipmentSlot, ItemStack> armorItems = new HashMap<>();

public String triggeredFrom = "join";

public PlayerStarterItems(Player player) {
this.player = player;
}

public PlayerStarterItems(Player player, String triggeredFrom) {
this.player = player;
this.triggeredFrom = triggeredFrom;
}

public void addItems(ItemStack... items) {
this.items.addAll(List.of(items));
}
Expand Down
36 changes: 31 additions & 5 deletions src/main/java/pro/mikey/kubeutils/kubejs/modules/LevelKu.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,47 @@ public List<LivingEntity> findEntitiesWithinRadius(ResourceLocation entityId, Bl
* @param range the range
* @param absolute to check for the state or the block
*
* @return the position of the found block or null when not found
* @return the position of the found blocks
*/
@Nullable
public List<BlockPos> findBlockWithinRadius(BlockState block, BlockPos start, int range, boolean absolute) {
Iterator<BlockPos> iterator = BlockPos.betweenClosedStream(new BoundingBox(start).inflatedBy(range)).iterator();

List<BlockPos> positions = new ArrayList<>();
while (iterator.hasNext()) {
var current = iterator.next();
if (!absolute && level.getBlockState(current).getBlock() == block.getBlock()) {
positions.add(current.immutable());
} else if (absolute && level.getBlockState(current) == block) {
if (blocksAreEqual(level.getBlockState(current), block, !absolute)) {
positions.add(current.immutable());
}
}

return positions;
}

/**
* Very much the same as {@link #findBlockWithinRadius(BlockState, BlockPos, int, boolean)} but instead will find
* the first block and return early with its position. If no block is found then we return null
*
* @param block the block state we're looking for (block is used in absolute mode)
* @param start the starting position
* @param range the radius to build from the starting position
* @param absolute when true, we check for state equality, when false, we check for block equality
*
* @return the position of the block found or null when nothing is found.
*/
@Nullable
public BlockPos findSingleBlockWithinRadius(BlockState block, BlockPos start, int range, boolean absolute) {
Iterator<BlockPos> iterator = BlockPos.betweenClosedStream(new BoundingBox(start).inflatedBy(range)).iterator();

while (iterator.hasNext()) {
var current = iterator.next();
if (blocksAreEqual(level.getBlockState(current), block, !absolute)) {
return current.immutable();
}
}

return null;
}

/**
* Generate a random location as a {@link BlockPos} at within two given bounds.
*
Expand Down Expand Up @@ -202,4 +224,8 @@ public Set<Structure> getStructuresAtLocation(BlockPos pos) {
public List<ResourceLocation> getStructureIdsAtLocation(BlockPos pos) {
return getStructuresAtLocation(pos).stream().map(e -> Registry.STRUCTURE_TYPES.getKey(e.type())).toList();
}

private static boolean blocksAreEqual(BlockState state, BlockState state2, boolean ignoreState) {
return ignoreState ? state == state2 : state.getBlock() == state2.getBlock();
}
}

0 comments on commit 53fe5e5

Please sign in to comment.