Skip to content

Commit

Permalink
変更
Browse files Browse the repository at this point in the history
  • Loading branch information
PTOM76 committed Jan 15, 2023
1 parent 1d52ed4 commit 30776c1
Show file tree
Hide file tree
Showing 29 changed files with 626 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.github.ptom76.tutorialfabricmod.entity.Entities;
import com.github.ptom76.tutorialfabricmod.item.ItemGroups;
import com.github.ptom76.tutorialfabricmod.item.Items;
import com.github.ptom76.tutorialfabricmod.screenhandler.ScreenHandlers;
import com.github.ptom76.tutorialfabricmod.tile.Tiles;
import net.fabricmc.api.ModInitializer;
import net.minecraft.util.Identifier;

Expand All @@ -16,9 +18,15 @@ public void onInitialize() {
// エンティティの登録
Entities.init();

// コンテナの登録
ScreenHandlers.init();

// ブロックの登録
Blocks.init();

// タイルの登録
Tiles.init();

// アイテムの登録
Items.init();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@

public class Blocks {
public static Block TUTORIAL_BLOCK = new TutorialBlock(FabricBlockSettings.of(Material.STONE).strength(1.5f, 3.0f).mapColor(MapColor.CYAN));
public static Block CLICK_COUNT_BLOCK = new ClickCountBlock(FabricBlockSettings.of(Material.WOOD).strength(1.0f, 3.0f).mapColor(MapColor.GOLD));
public static Block BLUE_APPLE_GENERATOR = new BlueAppleGenerator(FabricBlockSettings.of(Material.STONE).strength(1.5f, 3.0f).mapColor(MapColor.BLUE));

public static void init() {
// ブロックを登録
Registry.register(Registries.BLOCK, id("tutorial_block"), TUTORIAL_BLOCK);
Registry.register(Registries.BLOCK, id("click_count_block"), CLICK_COUNT_BLOCK);
Registry.register(Registries.BLOCK, id("blue_apple_generator"), BLUE_APPLE_GENERATOR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.github.ptom76.tutorialfabricmod.block;

import com.github.ptom76.tutorialfabricmod.tile.BlueAppleGeneratorTile;
import net.minecraft.block.Block;
import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityTicker;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.DirectionProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.ItemScatterer;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

public class BlueAppleGenerator extends Block implements BlockEntityProvider {

// 東西南北の方向性
public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING;

public BlueAppleGenerator(Settings settings) {
super(settings);
setDefaultState(getDefaultState().with(FACING, Direction.NORTH));
}

@Override
public void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(FACING);
super.appendProperties(builder);
}

@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return super.getPlacementState(ctx).with(FACING, ctx.getPlayer().getHorizontalFacing().getOpposite());
}

@Nullable
@Override
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new BlueAppleGeneratorTile(pos, state);
}

@SuppressWarnings("deprecation")
@Override
// ブロックを右クリックした時に呼び出される関数
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (world.isClient) return ActionResult.SUCCESS;
BlockEntity tile = world.getBlockEntity(pos);
if (tile instanceof BlueAppleGeneratorTile) {
BlueAppleGeneratorTile blueAppleGeneratorTile = (BlueAppleGeneratorTile) tile;

// GUIを開く
player.openHandledScreen(blueAppleGeneratorTile);

return ActionResult.SUCCESS;
}

return super.onUse(state, world, pos, player, hand, hit);
}

@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) {
return (world1, pos, state1, tile) -> {
if (tile instanceof BlueAppleGeneratorTile) {
BlueAppleGeneratorTile blueAppleGeneratorTile = (BlueAppleGeneratorTile) tile;
blueAppleGeneratorTile.tick(world1, pos, state1, blueAppleGeneratorTile);
}
};
}

@SuppressWarnings("deprecation")
@Override
public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
if (state.isOf(newState.getBlock())) {
return;
}
BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity instanceof Inventory) {
Inventory inventory = (Inventory) blockEntity;

// アイテムをドロップさせる
ItemScatterer.spawn(world, pos, inventory);

// コンパレータの更新
world.updateComparators(pos, this);
}
super.onStateReplaced(state, world, pos, newState, moved);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.github.ptom76.tutorialfabricmod.block;

import com.github.ptom76.tutorialfabricmod.tile.ClickCountBlockTile;
import net.minecraft.block.Block;
import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.DirectionProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

public class ClickCountBlock extends Block implements BlockEntityProvider {

// 東西南北の方向性
public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING;

public ClickCountBlock(Settings settings) {
super(settings);
setDefaultState(getDefaultState().with(FACING, Direction.NORTH));
}

@Override
public void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(FACING);
super.appendProperties(builder);
}

@Nullable
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return super.getPlacementState(ctx).with(FACING, ctx.getPlayer().getHorizontalFacing().getOpposite());
}

@Nullable
@Override
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new ClickCountBlockTile(pos, state);
}

@SuppressWarnings("deprecation")
@Override
// ブロックを右クリックした時に呼び出される関数
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
BlockEntity tile = world.getBlockEntity(pos);
if (tile instanceof ClickCountBlockTile) {
ClickCountBlockTile clickCountBlockTile = (ClickCountBlockTile) tile;

if (player.isSneaking()) {
// プレイヤーにメッセージを送る
if (!world.isClient)
player.sendMessage(Text.literal("クリックした回数: " + clickCountBlockTile.getClickCount()));
return ActionResult.SUCCESS;
}

// クリック回数を増やす
clickCountBlockTile.increaseClickCount();
return ActionResult.SUCCESS;
}

return super.onUse(state, world, pos, player, hand, hit);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.ptom76.tutorialfabricmod.client;

import com.github.ptom76.tutorialfabricmod.client.render.entity.EntityRenderers;
import com.github.ptom76.tutorialfabricmod.client.screen.Screens;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand All @@ -12,5 +13,8 @@ public class TutorialFabricClientMod implements ClientModInitializer {
public void onInitializeClient() {
// EntityのRenderer関係の登録
EntityRenderers.init();

// スクリーン(クライアント側のGUI)の登録
Screens.init();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.ptom76.tutorialfabricmod.client.screen;

import com.github.ptom76.tutorialfabricmod.TutorialFabricMod;
import com.github.ptom76.tutorialfabricmod.screenhandler.BlueAppleGeneratorScreenHandler;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public class BlueAppleGeneratorScreen extends HandledScreen<BlueAppleGeneratorScreenHandler> {
// assets/tutorialfabricmod/textures/gui/container/blue_apple_generator.png
private static final Identifier TEXTURE = TutorialFabricMod.id("textures/gui/container/blue_apple_generator.png");

public BlueAppleGeneratorScreen(BlueAppleGeneratorScreenHandler handler, PlayerInventory inventory, Text title) {
super(handler, inventory, title);
}

@Override
public void drawBackground(MatrixStack matrices, float delta, int mouseX, int mouseY) {
//RenderSystem.setShader(GameRenderer::getPositionTexShader); 1.19.2以前
RenderSystem.setShader(GameRenderer::getPositionTexProgram);
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);
RenderSystem.setShaderTexture(0, TEXTURE);
int x = (width - backgroundWidth) / 2;
int y = (height - backgroundHeight) / 2;
drawTexture(matrices, x, y, 0, 0, backgroundWidth, backgroundHeight);
}

@Override
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
renderBackground(matrices);
super.render(matrices, mouseX, mouseY, delta);
drawMouseoverTooltip(matrices, mouseX, mouseY);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.ptom76.tutorialfabricmod.client.screen;

import com.github.ptom76.tutorialfabricmod.screenhandler.ScreenHandlers;
import net.minecraft.client.gui.screen.ingame.HandledScreens;

public class Screens {

public static void init() {
// スクリーンの登録
HandledScreens.register(ScreenHandlers.BLUE_APPLE_GENERATOR_SCREEN_HANDLER, BlueAppleGeneratorScreen::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.github.ptom76.tutorialfabricmod.inventory;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventories;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.collection.DefaultedList;

public interface IInventory extends Inventory {

DefaultedList<ItemStack> getItems();

static IInventory of(DefaultedList<ItemStack> items) {
return () -> items;
}

static IInventory ofSize(int size) {
return of(DefaultedList.ofSize(size, ItemStack.EMPTY));
}

@Override
default int size() {
return getItems().size();
}

@Override
default boolean isEmpty() {
for (int i = 0; i < size(); i++) {
ItemStack stack = getStack(i);
if (!stack.isEmpty()) {
return false;
}
}
return true;
}

@Override
default ItemStack getStack(int slot) {
return getItems().get(slot);
}

@Override
default ItemStack removeStack(int slot, int count) {
ItemStack result = Inventories.splitStack(getItems(), slot, count);
if (!result.isEmpty()) {
markDirty();
}
return result;
}

@Override
default ItemStack removeStack(int slot) {
return Inventories.removeStack(getItems(), slot);
}

@Override
default void setStack(int slot, ItemStack stack) {
getItems().set(slot, stack);
if (stack.getCount() > getMaxCountPerStack()) {
stack.setCount(getMaxCountPerStack());
}
}

@Override
default void clear() {
getItems().clear();
}

@Override
default void markDirty() {
}

@Override
default boolean canPlayerUse(PlayerEntity player) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public static void addItems() {
// クリエイティブタブへ追加
ItemGroupEvents.modifyEntriesEvent(TUTORIAL).register((entries -> entries.add(TUTORIAL_ITEM)));
ItemGroupEvents.modifyEntriesEvent(TUTORIAL).register((entries -> entries.add(TUTORIAL_BLOCK)));
ItemGroupEvents.modifyEntriesEvent(TUTORIAL).register((entries -> entries.add(CLICK_COUNT_BLOCK)));
ItemGroupEvents.modifyEntriesEvent(TUTORIAL).register((entries -> entries.add(BLUE_APPLE_GENERATOR)));
ItemGroupEvents.modifyEntriesEvent(TUTORIAL).register((entries -> entries.add(TUTORIAL_SWORD)));
ItemGroupEvents.modifyEntriesEvent(TUTORIAL).register((entries -> entries.add(TUTORIAL_AXE)));
ItemGroupEvents.modifyEntriesEvent(TUTORIAL).register((entries -> entries.add(TUTORIAL_PICKAXE)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Items {

// ブロック
public static Item TUTORIAL_BLOCK = new BlockItem(Blocks.TUTORIAL_BLOCK, new Item.Settings());
public static Item CLICK_COUNT_BLOCK = new BlockItem(Blocks.CLICK_COUNT_BLOCK, new Item.Settings());
public static Item BLUE_APPLE_GENERATOR = new BlockItem(Blocks.BLUE_APPLE_GENERATOR, new Item.Settings());

// Material
public static TutorialToolMaterial TUTORIAL_TOOL_MATERIAL = new TutorialToolMaterial();
Expand All @@ -39,6 +41,8 @@ public static void init() {
// アイテムを登録
Registry.register(Registries.ITEM, id("tutorial_item"), TUTORIAL_ITEM);
Registry.register(Registries.ITEM, id("tutorial_block"), TUTORIAL_BLOCK);
Registry.register(Registries.ITEM, id("click_count_block"), CLICK_COUNT_BLOCK);
Registry.register(Registries.ITEM, id("blue_apple_generator"), BLUE_APPLE_GENERATOR);

Registry.register(Registries.ITEM, id("tutorial_sword"), TUTORIAL_SWORD);
Registry.register(Registries.ITEM, id("tutorial_axe"), TUTORIAL_AXE);
Expand Down
Loading

0 comments on commit 30776c1

Please sign in to comment.