Skip to content

Commit

Permalink
Remade my shitty use of libgui to the Loot Fabricator screen. Closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanPB committed Aug 1, 2020
1 parent 9eb4668 commit d7199b4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
25 changes: 5 additions & 20 deletions src/main/kotlin/dev/nathanpb/dml/block/BlockLootFabricator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@
package dev.nathanpb.dml.block

import dev.nathanpb.dml.blockEntity.BlockEntityLootFabricator
import dev.nathanpb.dml.screen.handler.HANDLER_LOOT_FABRICATOR
import io.netty.buffer.Unpooled
import dev.nathanpb.dml.screen.handler.LootFabricatorHandler
import dev.nathanpb.dml.screen.handler.LootFabricatorScreenHandlerFactory
import net.fabricmc.fabric.api.`object`.builder.v1.block.FabricBlockSettings
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory
import net.minecraft.block.*
import net.minecraft.block.entity.BlockEntity
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.entity.player.PlayerInventory
import net.minecraft.inventory.SidedInventory
import net.minecraft.item.ItemPlacementContext
import net.minecraft.item.ItemStack
import net.minecraft.network.PacketByteBuf
import net.minecraft.screen.ScreenHandler
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.state.StateManager
import net.minecraft.state.property.Properties
import net.minecraft.util.ActionResult
Expand All @@ -47,21 +43,10 @@ class BlockLootFabricator : HorizontalFacingBlock (
.with(Properties.HORIZONTAL_FACING, Direction.NORTH)
}

override fun onUse(state: BlockState?, world: World?, pos: BlockPos?, player: PlayerEntity?, hand: Hand?, hit: BlockHitResult?): ActionResult {
override fun onUse(state: BlockState?, world: World?, pos: BlockPos, player: PlayerEntity?, hand: Hand?, hit: BlockHitResult?): ActionResult {
if (world?.isClient == false && pos != null) {
player?.openHandledScreen(object: ExtendedScreenHandlerFactory {
override fun getDisplayName() = name

override fun createMenu(syncId: Int, inv: PlayerInventory?, player: PlayerEntity?): ScreenHandler? {
val buf = PacketByteBuf(Unpooled.buffer())
writeScreenOpeningData(player as? ServerPlayerEntity, buf)
return HANDLER_LOOT_FABRICATOR.create(syncId, inv, buf)
}

override fun writeScreenOpeningData(p0: ServerPlayerEntity?, buf: PacketByteBuf?) {
buf?.writeBlockPos(pos)
}

player?.openHandledScreen(LootFabricatorScreenHandlerFactory(pos) { syncId, inventory, context ->
LootFabricatorHandler(syncId, inventory, context)
})
}
return ActionResult.SUCCESS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2020 Nathan P. Bombana, IterationFunk
*
* 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 dev.nathanpb.dml.screen.handler

import dev.nathanpb.dml.block.BLOCK_LOOT_FABRICATOR
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.entity.player.PlayerInventory
import net.minecraft.network.PacketByteBuf
import net.minecraft.screen.ScreenHandler
import net.minecraft.screen.ScreenHandlerContext
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.text.TranslatableText
import net.minecraft.util.math.BlockPos

class LootFabricatorScreenHandlerFactory(
private val pos: BlockPos,
private val handlerFactory: (Int, PlayerInventory, ScreenHandlerContext)->ScreenHandler
) : ExtendedScreenHandlerFactory {

override fun getDisplayName() = TranslatableText(BLOCK_LOOT_FABRICATOR.translationKey)//TranslatableText("block.dml-refabricated.loot_fabricator")

override fun createMenu(syncId: Int, inv: PlayerInventory, player: PlayerEntity?): ScreenHandler? {
return handlerFactory(syncId, inv, ScreenHandlerContext.create(inv.player.world, pos))
}

override fun writeScreenOpeningData(player: ServerPlayerEntity?, buf: PacketByteBuf?) {
buf?.writeBlockPos(pos)
}

}

0 comments on commit d7199b4

Please sign in to comment.