-
-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Block Extension QSL #100
Draft
Azagwen
wants to merge
15
commits into
QuiltMC:1.18
Choose a base branch
from
Azagwen:1.18
base: 1.18
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Block Extension QSL #100
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9903756
First QuiltBlock impl, multi-block property Proxying
Azagwen 1144ed2
Help I'm stupid
Azagwen c23f5b1
Merge branch 'QuiltMC:1.18' into 1.18
Azagwen 4c109c8
QuiltBlock Impl changes, I also hate my lfie because Blockstates
Azagwen e7679ac
move comments around
Azagwen 0adaeb5
added a way to copy default states from proxies + a way to safely get…
Azagwen dd6c73d
Merge branch 'QuiltMC:1.18' into 1.18
Azagwen d1b3757
removed obsolete TODOs
Azagwen cd547e7
Make createProxy use generic types for Settings
Azagwen c1ccef9
Don't use stuff reminiscent from the before diamond generic Java vers…
Azagwen 10a9f36
Attempt to generify "getProxyDefaultState", renamed to "mergeStates"
Azagwen cc44490
Merge remote-tracking branch 'origin/1.18' into 1.18
Azagwen ced6af5
Ran "applyLicenses", help why is my commit containing 31 changed files-
Azagwen 5ece6c5
update mergeStates javadoc comment
Azagwen 1586665
Merge branch 'QuiltMC:1.18' into 1.18
Azagwen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
...block/block_extensions/src/main/java/org/quiltmc/qsl/block/extensions/api/QuiltBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright 2022 QuiltMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.quiltmc.qsl.block.extensions.api; | ||
|
||
import net.minecraft.block.AbstractBlock; | ||
import net.minecraft.block.Block; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.state.property.Property; | ||
import org.quiltmc.qsl.block.extensions.impl.BlockExtension; | ||
import org.quiltmc.qsl.block.extensions.impl.QuiltBlockImpl; | ||
import org.quiltmc.qsl.block.extensions.mixin.BlockMixin; | ||
|
||
import java.util.function.Function; | ||
|
||
public class QuiltBlock { | ||
|
||
/** | ||
* Creates a proxy from one or multiple blocks (will be temporarily stocked in a {@link ThreadLocal} and used in {@link BlockMixin}). | ||
* @param settings Block settings of the block you want to add Proxies for (dummy usage, to be able to insert logic at {@code super()} call. | ||
* @param proxies Array of proxy blocks to be "merged" together as one (property merging). | ||
* @return Same Block settings as above. | ||
*/ | ||
public static <S extends AbstractBlock.Settings> S createProxy(S settings, Block... proxies) { | ||
QuiltBlockImpl.PROXY_BLOCKS_TEMP_CONTAINER.set(proxies); | ||
return settings; | ||
} | ||
|
||
/** | ||
* Creates a default state {@link BlockState} for blocks that have Proxies. | ||
* @param ownerBlock The block that "owns" the proxies ot create the state from. | ||
* @param proxyState A {@link Function} taking a Block as its input, and outputting a Blockstate, this is used to tell each proxy what state to give this method. | ||
* @return A {@link BlockState} usable as a default state for our block. | ||
*/ | ||
public static BlockState mergeStates(Block ownerBlock, Function<Block ,BlockState> proxyState) { | ||
var proxies = ((BlockExtension) ownerBlock).getProxies(); | ||
var ownerState = ownerBlock.getDefaultState(); | ||
for (var proxy : proxies) { | ||
var appliedProxyState = proxyState.apply(proxy); | ||
for (Property<?> property : appliedProxyState.getProperties()) { | ||
ownerState = QuiltBlock.copyProperty(appliedProxyState, ownerState, property); | ||
} | ||
} | ||
return ownerState; | ||
} | ||
|
||
/** | ||
* Copies a {@code property} from a {@code source} to a {@code target} {@link BlockState}. | ||
* Shamelessly copied from {@link Block} because it just works. | ||
* @param source The source {@link BlockState} | ||
* @param target The target {@link BlockState} | ||
* @param property The {@link Property} to copy from {@code source} to {@code target} | ||
* @return A {@link BlockState} where all properties have been transferred from {@code source} to {@code target} | ||
* @param <T> The {@link Property}'s type | ||
*/ | ||
public static <T extends Comparable<T>> BlockState copyProperty(BlockState source, BlockState target, Property<T> property) { | ||
if (target.contains(property)) { | ||
return target.with(property, source.get(property)); | ||
} | ||
else { | ||
QuiltBlockImpl.LOGGER.warn("Failed to copy properties from {}, {} does not exist in {}", source, property, target); | ||
return target; | ||
} | ||
} | ||
|
||
/** | ||
* Safely get a property's value from our {@code Block}'s current {@code state} | ||
* @param state The state we want to get the value from. | ||
* @param property The property we're looking for. | ||
* @return The value of the targeted property, OR a typed null if the peroperty doesn't exist in this block. | ||
* @param <T> The type of the target property. | ||
*/ | ||
public static <T extends Comparable<T>> T getProperty(BlockState state, Property<T> property) { | ||
if (state.contains(property)) { | ||
return state.get(property); | ||
} else { | ||
QuiltBlockImpl.LOGGER.warn("Cannot get property {} as it does not exist in {}, falling back on null", property, state.getBlock()); | ||
return (T) null; | ||
} | ||
} | ||
|
||
/** | ||
* Checks if this block has been built using {@code blockClass} as a Proxy | ||
* @param blockClass The Block class to check this block against | ||
* @return boolean telling us weither this block has {@code blockClass} as a proxy or not | ||
*/ | ||
public boolean isInstanceOfProxy(Class<? extends Block> blockClass) { | ||
return ((BlockExtension) this).isInstanceOf(blockClass); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
.../block_extensions/src/main/java/org/quiltmc/qsl/block/extensions/impl/BlockExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2022 QuiltMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.quiltmc.qsl.block.extensions.impl; | ||
|
||
import net.minecraft.block.Block; | ||
|
||
public interface BlockExtension { | ||
Block getProxyOfType(Class<? extends Block> type); | ||
boolean isInstanceOf(Class<? extends Block> type); | ||
Block[] getProxies(); | ||
} |
28 changes: 28 additions & 0 deletions
28
.../block_extensions/src/main/java/org/quiltmc/qsl/block/extensions/impl/QuiltBlockImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2022 QuiltMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.quiltmc.qsl.block.extensions.impl; | ||
|
||
import net.minecraft.block.Block; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
//TODO: add a way to make any item place-able | ||
//TODO: access widen stairs and other privated blocks by default | ||
public class QuiltBlockImpl { | ||
public static final Logger LOGGER = LoggerFactory.getLogger("Quilt Block Impl"); | ||
public static final ThreadLocal<Block[]> PROXY_BLOCKS_TEMP_CONTAINER = new ThreadLocal<>(); | ||
} |
104 changes: 104 additions & 0 deletions
104
...ock/block_extensions/src/main/java/org/quiltmc/qsl/block/extensions/mixin/BlockMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright 2022 QuiltMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.quiltmc.qsl.block.extensions.mixin; | ||
|
||
import net.minecraft.block.AbstractBlock; | ||
import net.minecraft.block.Block; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.state.StateManager; | ||
import net.minecraft.state.property.Property; | ||
import org.apache.commons.compress.utils.Lists; | ||
import org.quiltmc.qsl.block.extensions.impl.BlockExtension; | ||
import org.quiltmc.qsl.block.extensions.impl.QuiltBlockImpl; | ||
import org.slf4j.Logger; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture; | ||
|
||
import java.util.LinkedList; | ||
|
||
@Mixin(Block.class) | ||
public abstract class BlockMixin implements BlockExtension { | ||
|
||
@Shadow protected abstract Block asBlock(); | ||
@Shadow @Final private static Logger LOGGER; | ||
private final @Unique LinkedList<Block> proxies = new LinkedList<>(); | ||
|
||
@Inject(method = "<init>", | ||
at = @At(value = "INVOKE", target = "Lnet/minecraft/state/StateManager$Builder;build(Ljava/util/function/Function;Lnet/minecraft/state/StateManager$Factory;)Lnet/minecraft/state/StateManager;"), | ||
locals = LocalCapture.CAPTURE_FAILHARD) | ||
private void injectProxyProperties(AbstractBlock.Settings settings, CallbackInfo ci, StateManager.Builder<Block, BlockState> builder) { | ||
if (QuiltBlockImpl.PROXY_BLOCKS_TEMP_CONTAINER.get() != null && QuiltBlockImpl.PROXY_BLOCKS_TEMP_CONTAINER.get().length > 0){ | ||
var properties = new ArrayList<Property>(); | ||
for (var proxyBlock : QuiltBlockImpl.PROXY_BLOCKS_TEMP_CONTAINER.get()) { | ||
for (var property : proxyBlock.getStateManager().getProperties()) { | ||
if (!properties.contains(property)) { | ||
properties.add(property); | ||
} | ||
} | ||
this.proxies.add(proxyBlock); | ||
} | ||
builder.add(properties.toArray(Property[]::new)); | ||
QuiltBlockImpl.PROXY_BLOCKS_TEMP_CONTAINER.remove(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isInstanceOf(Class<? extends Block> type) { | ||
if (!this.proxies.isEmpty()){ | ||
for (var proxy : this.proxies) { | ||
if (proxy.getClass().isAssignableFrom(type)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@SuppressWarnings("ConstantConditions") | ||
@Override | ||
public Block getProxyOfType(Class<? extends Block> type) { | ||
var result = (Block) null; | ||
if (!this.proxies.isEmpty()){ | ||
for (var proxy : this.proxies) { | ||
if (proxy.getClass().isAssignableFrom(type)) { | ||
result = proxy; | ||
} | ||
} | ||
} | ||
if (result != null) { | ||
return result; | ||
} else { | ||
LOGGER.warn(String.format("%s has no proxy of type %s! Returning self to avoid issues", this.asBlock().toString(), type.getName())); | ||
return (Block) (Object) this; | ||
} | ||
} | ||
|
||
@Override | ||
public Block[] getProxies() { | ||
var array = new Block[proxies.size()]; | ||
for (int i = 0; i < proxies.size(); i++) { | ||
array[i] = proxies.get(i); | ||
} | ||
return array; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...ck/block_extensions/src/testmod/java/org/quiltmc/qsl/block/extensions/test/TestBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2022 QuiltMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.quiltmc.qsl.block.extensions.test; | ||
|
||
import net.minecraft.block.Block; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.StairsBlock; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.text.LiteralText; | ||
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.world.World; | ||
import org.quiltmc.qsl.block.extensions.api.QuiltBlock; | ||
|
||
public class TestBlock extends Block { | ||
|
||
public TestBlock(Settings settings, Block... proxies) { | ||
super(QuiltBlock.createProxy(settings, proxies)); | ||
this.setDefaultState(QuiltBlock.mergeStates(this, Block::getDefaultState)); | ||
} | ||
|
||
@Override | ||
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { | ||
var prop = QuiltBlock.getProperty(state, StairsBlock.HALF); | ||
if (prop != null) { | ||
player.sendMessage(new LiteralText(prop.asString()), false); | ||
return ActionResult.success(world.isClient); | ||
} | ||
return super.onUse(state, world, pos, player, hand, hit); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...tachments/src/main/java/org/quiltmc/qsl/registry/attachment/api/DefaultValueProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...try_attachments/src/main/java/org/quiltmc/qsl/registry/attachment/api/DispatchedType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...hments/src/main/java/org/quiltmc/qsl/registry/attachment/api/RegistryEntryAttachment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...attachments/src/main/java/org/quiltmc/qsl/registry/attachment/api/RegistryExtensions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This usage of getProperty doesn't sound useful?
If the stuff that was merged were merged properly, it should already include the property.