Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 5413a7a

Browse files
Implement IPlantable and hooks surrounding it (#102)
* Implement IPlantable and hooks surrounding it * We're not a patch file * Whitespace
1 parent 929bf8d commit 5413a7a

File tree

16 files changed

+2541
-1920
lines changed

16 files changed

+2541
-1920
lines changed

patchwork-enum-hacks/src/main/java/net/patchworkmc/api/enumhacks/EnumHacks.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private static void clearCachedValues(Class<? extends Enum<?>> clazz) {
135135
}
136136
}
137137

138-
private static <T extends Enum<T>> T constructAndAdd(Class<T> clazz, IntFunction<? extends T> constructor) {
138+
public static <T extends Enum<T>> T constructAndAdd(Class<T> clazz, IntFunction<? extends T> constructor) {
139139
T[] values = clazz.getEnumConstants();
140140
T instance = constructor.apply(values.length);
141141
addToValues(values, instance);
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
archivesBaseName = "patchwork-extensions-block"
2-
version = getSubprojectVersion(project, "0.1.0")
1+
archivesBaseName = "patchwork-extensions-block"
2+
version = getSubprojectVersion(project, "0.1.0")
3+
4+
dependencies {
5+
compile project(path: ':patchwork-enum-hacks', configuration: 'dev')
6+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.common;
21+
22+
import net.minecraft.block.BlockState;
23+
import net.minecraft.block.Blocks;
24+
import net.minecraft.block.CropBlock;
25+
import net.minecraft.block.FlowerBlock;
26+
import net.minecraft.block.SaplingBlock;
27+
import net.minecraft.util.math.BlockPos;
28+
import net.minecraft.world.BlockView;
29+
30+
public interface IPlantable {
31+
default PlantType getPlantType(BlockView world, BlockPos pos) {
32+
if (this instanceof CropBlock) return PlantType.Crop;
33+
if (this instanceof SaplingBlock) return PlantType.Plains;
34+
if (this instanceof FlowerBlock) return PlantType.Plains;
35+
if (this == Blocks.CACTUS) return PlantType.Desert;
36+
if (this == Blocks.LILY_PAD) return PlantType.Water;
37+
if (this == Blocks.RED_MUSHROOM) return PlantType.Cave;
38+
if (this == Blocks.BROWN_MUSHROOM) return PlantType.Cave;
39+
if (this == Blocks.NETHER_WART) return PlantType.Nether;
40+
if (this == Blocks.TALL_GRASS) return PlantType.Plains;
41+
return net.minecraftforge.common.PlantType.Plains;
42+
}
43+
44+
BlockState getPlant(BlockView world, BlockPos pos);
45+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.common;
21+
22+
import net.minecraft.block.BlockState;
23+
import net.minecraft.util.math.BlockPos;
24+
import net.minecraft.util.math.Direction;
25+
import net.minecraft.world.BlockView;
26+
27+
import net.patchworkmc.api.enumhacks.EnumHacks;
28+
import net.patchworkmc.mixin.extensions.block.PlantTypeAccessor;
29+
30+
public enum PlantType {
31+
Plains,
32+
Desert,
33+
Beach,
34+
Cave,
35+
Water,
36+
Nether,
37+
Crop;
38+
39+
/**
40+
* Getting a custom {@link PlantType}, or an existing one if it has the same name as that one. Your plant should implement {@link IPlantable}
41+
* and return this custom type in {@link IPlantable#getPlantType(net.minecraft.world.BlockView, BlockPos)}.
42+
*
43+
* <p>If your new plant grows on blocks like any one of them above, never create a new {@link PlantType}.
44+
* This enumeration is only functioning in
45+
* {@link net.minecraftforge.common.extensions.IForgeBlock#canSustainPlant(BlockState, BlockView, BlockPos, Direction, IPlantable)}
46+
* which you are supposed to override this function in your new block and create a new plant type to grow on that block.
47+
*
48+
* <p>You can create an instance of your plant type in your API and let your/others mods access it. It will be faster than calling this method.
49+
*
50+
* @param name the name of the type of plant, you had better follow the style above
51+
* @return the acquired {@link PlantType}, a new one if not found.
52+
*/
53+
public static PlantType create(String name) {
54+
return EnumHacks.constructAndAdd(PlantType.class, ordinal -> PlantTypeAccessor.invokeConstructor(name, ordinal));
55+
}
56+
}

0 commit comments

Comments
 (0)